Exemplo n.º 1
0
class OperationsWidget(QWidget):
    operations_changed = QtCore.pyqtSignal()
    operation_changed = QtCore.pyqtSignal(dict, int)
    operation_added = QtCore.pyqtSignal(dict, int)

    def __init__(self, parent=None):
        QWidget.__init__(self, parent)

        self.widget_layout = QVBoxLayout()

        title_layout = QHBoxLayout()
        title_layout.addStretch()
        style = "<span style=\'color: #444444\'><b>%s</b></span>"
        title = QLabel(style % "Operations")
        title_layout.addWidget(title)
        title_layout.addStretch()
        self.widget_layout.addLayout(title_layout)

        # Create ListWidget and add 10 items to move around.
        self.list_widget = QListWidget()

        # self.list_widget.setDragDropMode(QAbstractItemView.InternalMove)
        self.list_widget.setSelectionMode(QAbstractItemView.ExtendedSelection)
        self.list_widget.setSortingEnabled(False)
        self.list_widget.currentItemChanged.connect(self._populate_settings_update)
        self.widget_layout.addWidget(self.list_widget)

        otitle_layout = QHBoxLayout()
        otitle_layout.addStretch()
        otitle = QLabel(style % "Operation settings")
        otitle_layout.addWidget(otitle)
        otitle_layout.addStretch()
        self.widget_layout.addLayout(otitle_layout)

        self.operations_combo = QComboBox()
        self.operations_combo.currentIndexChanged.connect(self._populate_settings_add)
        self.widget_layout.addWidget(self.operations_combo)

        self.operation_settings = GenericOperationWidget()
        self.widget_layout.addWidget(self.operation_settings)

        self.toolbar = QToolBar()
        self.toolbar.addAction(get_icon('apply.png'), "Apply/Replace",
                               self._change_operation)
        self.toolbar.addAction(get_icon('editors/edit_add.png'), "Add after",
                               self._add_operation)
        self.toolbar.addAction(get_icon('trash.png'), "Remove",
                               self._remove_operation)


        self.widget_layout.addWidget(self.toolbar)
        self.setLayout(self.widget_layout)

    def populate_available_operations(self, dict):
        """
        Populate combobox with available operation names
        """
        self.operations_combo.addItems(dict)

    def set_operations(self, operations_dict):
        """
        Populate operations list with given dict of operations
        """
        self.list_widget.clear()
        for op in operations_dict:
            self.list_widget.addItem(Operation(op))

    def get_operations(self):
        """
        Return list of operations.
        """
        operations = []
        for i in range(self.list_widget.count()):
            op = self.list_widget.item(i)
            operations.append(op._op)

        return operations

    def _remove_operation(self):
        self.list_widget.takeItem(self.list_widget.currentRow())
        self.operations_changed.emit()

    def _add_operation(self):
        """
        Add operation currently in self.operation_settings to the operation
        list.

        Signals:
        ========
        Emits self.opeartion_added and self.operations_changed on success
        """
        op = self.operation_settings.get_operation()
        current_row = self.list_widget.currentRow()
        self.list_widget.insertItem(current_row + 1, Operation(op))
        index = self.list_widget.model().index(current_row + 1, 0)
        self.list_widget.setCurrentIndex(index)
        self.operation_added.emit(op, current_row + 1)
        self.operations_changed.emit()

    def _change_operation(self):
        """
        Replace currently selected operation with operation in
        self.operation_settings (apply changed settings or replace operation).

        Signals:
        ========
        Emits self.operation_changed and self.operations_changed on success
        """
        op = self.operation_settings.get_operation()
        current_row = self.list_widget.currentRow()
        self.list_widget.takeItem(self.list_widget.currentRow())
        self.list_widget.insertItem(current_row, Operation(op))
        index = self.list_widget.model().index(current_row, 0)
        self.list_widget.setCurrentIndex(index)

        self.operation_changed.emit(op, current_row)
        self.operations_changed.emit()


    def _populate_settings_update(self, item):
        """
        Fill self.operation_settings with details of currently selected
        operation.
        """
        try:
            idx = self.operations_combo.findText(item._op["module"])
            if idx >= 0:
                self.operations_combo.setCurrentIndex(idx)
            self.operation_settings.set_operation(item._op)
        except AttributeError:
            pass

    def _populate_settings_add(self, index):
        self.operation_settings.set_operation({"module": self.operations_combo.currentText()})