Example #1
0
    def __init__(self, parent, model, checkbox, all_models):
        QWidget.__init__(self, parent)

        self._ui = Ui_BranchView()
        self._ui.setupUi(self)

        self._model = model
        self._all_models = all_models
        self._parent = parent

        self._table_view = QTableView(parent)

        self._name_widget = ButtonLineEdit(model, checkbox, all_models, self)
        self._ui.layout.addWidget(self._name_widget, 0, 0)
        self._ui.layout.addWidget(self._table_view, 1, 0)

        self._hidden_rows_from_models = []
        self.set_model(model)

        self.connect_signals()
Example #2
0
class BranchView(QWidget):
    """
        This is a widget containing the tableView and the ButtonLineEdit with
        the branch name.
    """

    def __init__(self, parent, model, checkbox, all_models):
        QWidget.__init__(self, parent)

        self._ui = Ui_BranchView()
        self._ui.setupUi(self)

        self._model = model
        self._all_models = all_models
        self._parent = parent

        self._table_view = QTableView(parent)

        self._name_widget = ButtonLineEdit(model, checkbox, all_models, self)
        self._ui.layout.addWidget(self._name_widget, 0, 0)
        self._ui.layout.addWidget(self._table_view, 1, 0)

        self._hidden_rows_from_models = []
        self.set_model(model)

        self.connect_signals()

    def connect_signals(self):
        """
            Connect the right signals to the right slots.
        """
        connect(self._table_view,
                SIGNAL("customContextMenuRequested(const QPoint&)"),
                self.context_menu)

        signals = "activated(const QModelIndex&)", "clicked(const QModelIndex&)"
        for signal in signals:
            connect(self._table_view, SIGNAL(signal), self.fwd_commit_clicked)

        connect(self._name_widget, SIGNAL("newHistAction"),
                self.fwd_new_hist_action)

        connect(self._name_widget, SIGNAL("hideDataFromModel"), self.hide_data)
        connect(self._name_widget, SIGNAL("unhideDataFromModel"),
                                                              self.unhide_data)

        connect(self._name_widget, SIGNAL("showColumn"), self.show_column)
        connect(self._name_widget, SIGNAL("hideColumn"), self.hide_column)

    def fwd_commit_clicked(self, index):
        """
            Simple signal forwarder to RebaseMainClass.
        """
        self.emit(SIGNAL("activated(const QModelIndex&)"), index)

    def fwd_new_hist_action(self, action):
        """
            Simple signal forwarder to RebaseMainClass.
        """
        self.emit(SIGNAL("newHistAction"), action)

    def unhide_data(self):
        """
            Unhide previously hidden data from another model.
        """
        # Un-forbid dropping on a reduced view of the model
        self._table_view.setDragDropMode(self._table_view.DragDrop)

        for row in self._hidden_rows_from_models:
            self._table_view.showRow(row)
        self._hidden_rows_from_models = []

    def hide_data(self, data):
        """
            Hide certain rows of the table view model based on the given data
            list.

            :param data:
                The list of commits used to hide rows of the table view. The
                list is build like:
                    [ [authored_timestamp, author_name, commit_message],
                      ... ]
        """
        self.unhide_data()

        # Forbid dropping on a reduced view of the model
        self._table_view.setDragDropMode(self._table_view.DragOnly)

        for row in xrange(self._model.rowCount()):
            if to_hide_subset(self._model, row) in data:
                self._table_view.hideRow(row)
                self._hidden_rows_from_models.append(row)

    def show_column(self, column):
        """
            Show the given column of the table view.
        """
        column_position = self._model.get_columns().index(column)
        self._table_view.showColumn(column_position)
        self.resize_table_view()

    def hide_column(self, column):
        """
            Hide the given column of the table view.
        """
        column_position = self._model.get_columns().index(column)
        self._table_view.hideColumn(column_position)
        self.resize_table_view()

    def context_menu(self, q_point):
        """
            Creates a menu with the actions:
                - copy
                - delete
                - paste after
                - paste before
        """
        menu = QMenu(self._parent)
        table_view = self._table_view

        indexes = table_view.selectedIndexes()
        selected_rows = set([index.row() for index in indexes])
        copy_data = self._parent.get_copy_data()

        copy_action = menu.addAction("Copy")
        delete_action = menu.addAction("Delete")
        paste_after_action = menu.addAction("Paste after")
        paste_after_action.setDisabled(copy_data == "")
        paste_before_action = menu.addAction("Paste before")
        paste_before_action.setDisabled(copy_data == "")
        create_branch_action = menu.addAction("Create branch from this commit")

        chosen_action = menu.exec_(table_view.viewport().mapToGlobal(q_point))

        if chosen_action == delete_action:
            self.remove_rows()

        elif chosen_action == copy_action:
            self.emit(SIGNAL("newCopiedData"),
                      table_view.model().mimeData(indexes))

        elif chosen_action == paste_after_action:
            drop_after = max(selected_rows) + 1
            table_view.model().dropMimeData(copy_data, Qt.CopyAction,
                                            drop_after, 0, self.parent())

        elif chosen_action == paste_before_action:
            drop_before = min(selected_rows)
            table_view.model().dropMimeData(copy_data, Qt.CopyAction,
                                            drop_before, 0, self.parent())

        elif chosen_action == create_branch_action:
            self.emit(SIGNAL("newBranchFromCommit"), indexes)

    def remove_rows(self, rows=False):
        """
            When <Del> is pressed, this method removes the selected rows of the
            table view.

            We delete the rows starting with the last one, in order to use the
            correct indexes.

            :param rows:
                This is intended for tests. It's a list of index rows.
        """
        remove_selected_rows(self._table_view, self._parent, rows)

    def model(self):
        """
            Return the QTableView model.
        """
        return self._model

    def set_model(self, model):
        """
            Sets the QTableView model and the label's name.
        """
        table_view = self._table_view

        table_view.setModel(model)
        self._model = model

        show_fields = ["hexsha", "message"]
        self._name_widget.inform_shown_columns(show_fields)
        for column, field in enumerate(model.get_columns()):
            if not field in show_fields:
                table_view.hideColumn(column)

        table_view.setSelectionMode(table_view.ExtendedSelection)
        table_view.setDragDropMode(table_view.DragDrop)
        table_view.setSelectionBehavior(table_view.SelectRows)
        table_view.setEditTriggers(table_view.NoEditTriggers)
        table_view.setContextMenuPolicy(Qt.CustomContextMenu)

        self.resize_table_view()

    def resize_table_view(self):
        """
            Resize the table view to its contents.
        """
        custom_resize_columns_to_contents(self._table_view)
        self._table_view.horizontalHeader().setStretchLastSection(True)

    def show_modifications(self):
        """
            Show modifications of the branch view and name.
        """
        self._table_view.setModel(self._model)
        self._name_widget.show_modifications()

    def hide_modifications(self):
        """
            Hide modifications of the branch view and name.
        """
        if hasattr(self._model, 'get_orig_q_git_model') and \
           self._model.get_orig_q_git_model():
            model = self._model.get_orig_q_git_model()
            self._table_view.setModel(model)
            self._name_widget.hide_modifications()

    def reset_displayed_name(self):
        self._name_widget.reset_displayed_name()

    def tableview_has_focus(self):
        return self._table_view.hasFocus()