class Randomize(BaseEditor): RandomizeClasses, RandomizeAttributes, RandomizeMetas = _Randomize.Type def __init__(self, parent=None, **kwargs): super().__init__(parent, **kwargs) self.setLayout(QVBoxLayout()) form = QFormLayout() self.__rand_type_cb = QComboBox() self.__rand_type_cb.addItems(["Classes", "Features", "Meta data"]) self.__rand_type_cb.currentIndexChanged.connect(self.changed) self.__rand_type_cb.activated.connect(self.edited) self.__rand_seed_ch = QCheckBox() self.__rand_seed_ch.clicked.connect(self.edited) form.addRow("Randomize:", self.__rand_type_cb) form.addRow("Replicable shuffling:", self.__rand_seed_ch) self.layout().addLayout(form) def setParameters(self, params): rand_type = params.get("rand_type", Randomize.RandomizeClasses) self.__rand_type_cb.setCurrentIndex( _enum_to_index(_Randomize.Type, rand_type)) self.__rand_seed_ch.setChecked(params.get("rand_seed", 1) or 0) def parameters(self): return { "rand_type": _index_to_enum(_Randomize.Type, self.__rand_type_cb.currentIndex()), "rand_seed": 1 if self.__rand_seed_ch.isChecked() else None, } @staticmethod def createinstance(params): rand_type = params.get("rand_type", Randomize.RandomizeClasses) rand_seed = params.get("rand_seed", 1) return _Randomize(rand_type=rand_type, rand_seed=rand_seed) def __repr__(self): return "{}, {}".format( self.__rand_type_cb.currentText(), "Replicable" if self.__rand_seed_ch.isChecked() else "Not replicable", )
class UpdateOptionsWidget(QWidget): """ A Widget with download/update/remove options. """ #: Install/update button was clicked installClicked = Signal() #: Remove button was clicked. removeClicked = Signal() def __init__(self, state=AVAILABLE, parent=None): QWidget.__init__(self, parent) layout = QHBoxLayout() layout.setSpacing(1) layout.setContentsMargins(1, 1, 1, 1) self.checkButton = QCheckBox() layout.addWidget(self.checkButton) self.setLayout(layout) self.setMinimumHeight(20) self.setMaximumHeight(20) self.state = -1 self.setState(state) def setState(self, state): """ Set the current update state for the widget (AVAILABLE, CURRENT, OUTDATED or DEPRECTED). """ self.state = state self._update() def _update(self): if self.state == AVAILABLE: self.checkButton.setChecked(False) elif self.state == CURRENT: self.checkButton.setChecked(True) elif self.state == OUTDATED: self.checkButton.setChecked(True) elif self.state == DEPRECATED: self.checkButton.setChecked(True) else: raise ValueError("Invalid state %r" % self._state) try: self.checkButton.clicked.disconnect( ) # Remove old signals if they exist except Exception: pass if not self.checkButton.isChecked( ): # Switch signals if the file is present or not self.checkButton.clicked.connect(self.installClicked) else: self.checkButton.clicked.connect(self.removeClicked)
class AddTracUI(QWidget): def __init__(self, parent): super(AddTracUI, self).__init__() self.title = 'Create TracControl Element' self.setLayout(QVBoxLayout(self)) self.parent = parent self.trac = Trac(name='', outputPin=0, enabled=False, icon=Config.icon('tracControl', 'rearDiff'), momentary=False) self._nameControl = LineEdit('Name', self) self._nameControl.kb.connect(self.showOSK) self._outputPinControlLabel = QLabel('Output Pin', self) self._outputPinControl = QComboBox(self) for _pin in self.parent.availablePins(): self._outputPinControl.addItem(str(_pin)) self._outputPinControl.setCurrentIndex(self._outputPinControl.findText(str(self.trac.outputPin))) self._enabledControl = QCheckBox('Enabled', self) self._iconControlLabel = QLabel('Icon', self) self._iconControl = QComboBox(self) for _key in Config.icons['tracControl'].keys(): icon = Config.icon('tracControl', _key) self._iconControl.addItem(icon['name'], _key) self._iconControl.setItemIcon(self._iconControl.count() - 1, QIcon(icon['path'])) self._addTracBtn = QPushButton('Add TracControl Element', self) self._addTracBtn.clicked.connect(self.__createTracBtnAction) self._cancelBtn = QPushButton('Cancel', self) self._cancelBtn.clicked.connect(self.__cancel) _layout = [ ['_nameControl'], ['_outputPinControlLabel', '_outputPinControl'], ['_enabledControl'], ['_iconControlLabel', '_iconControl'], ['_addTracBtn', '_cancelBtn'] ] for _list in _layout: _panel = QWidget(self) _panel.setLayout(QHBoxLayout(_panel)) _panel.layout().setAlignment(Qt.AlignCenter) for _control in _list: _panel.layout().addWidget(eval('self.%s' % _control)) self.layout().addWidget(_panel) def __createTracBtnAction(self): self.trac.name = self._nameControl.text() self.trac.outputPin = int(self._outputPinControl.currentText()) self.trac.enabled = self._enabledControl.isChecked() self.trac.icon = self._iconControl.currentData() self.parent.tracs.addTrac(self.trac) self.parent.tracs.save() self.parent.loadUI('config_trac') self.parent.enableConfigButtons() def __cancel(self): self.parent.loadUI('config_trac') self.parent.enableConfigButtons() def showOSK(self): self.window().dock.show() self.window().osk.rWidget = self._nameControl
def _add_row(self, parameter: Optional[Parameter] = None): row_id = len(self.__controls) if parameter is None: parameter = Parameter(f"p{row_id + 1}") edit = QLineEdit(text=parameter.name) edit.setFixedWidth(60) edit.textChanged.connect(self.__on_text_changed) button = gui.button(None, self, "×", callback=self.__on_remove_button_clicked, autoDefault=False, width=34, sizePolicy=(QSizePolicy.Maximum, QSizePolicy.Maximum)) kwargs = {"minimum": -2147483647, "maximum": 2147483647} init_spin = QDoubleSpinBox(decimals=4, **kwargs) lower_spin = QDoubleSpinBox(**kwargs) upper_spin = QDoubleSpinBox(**kwargs) init_spin.setValue(parameter.initial) lower_spin.setValue(parameter.lower) upper_spin.setValue(parameter.upper) lower_check = QCheckBox(checked=bool(parameter.use_lower)) upper_check = QCheckBox(checked=bool(parameter.use_upper)) lower_spin.setEnabled(lower_check.isChecked()) upper_spin.setEnabled(upper_check.isChecked()) init_spin.valueChanged.connect(self.__on_init_spin_changed) lower_spin.valueChanged.connect(self.__on_lower_spin_changed) upper_spin.valueChanged.connect(self.__on_upper_spin_changed) lower_check.stateChanged.connect(self.__on_lower_check_changed) upper_check.stateChanged.connect(self.__on_upper_check_changed) controls = (button, edit, init_spin, lower_check, lower_spin, upper_check, upper_spin) n_rows = self.__layout.rowCount() for i, control in enumerate(controls): self.__layout.addWidget(control, n_rows, i) self.__data.append(parameter) self.__controls.append(controls) self._set_labels_visible(True)
class Randomize(BaseEditor): RandomizeClasses, RandomizeAttributes, RandomizeMetas = _Randomize.Type def __init__(self, parent=None, **kwargs): super().__init__(parent, **kwargs) self.setLayout(QVBoxLayout()) form = QFormLayout() self.__rand_type_cb = QComboBox() self.__rand_type_cb.addItems(["Classes", "Features", "Meta data"]) self.__rand_type_cb.currentIndexChanged.connect(self.changed) self.__rand_type_cb.activated.connect(self.edited) self.__rand_seed_ch = QCheckBox() self.__rand_seed_ch.clicked.connect(self.edited) form.addRow("Randomize:", self.__rand_type_cb) form.addRow("Replicable shuffling:", self.__rand_seed_ch) self.layout().addLayout(form) def setParameters(self, params): rand_type = params.get("rand_type", Randomize.RandomizeClasses) self.__rand_type_cb.setCurrentIndex( enum_to_index(_Randomize.Type, rand_type)) self.__rand_seed_ch.setChecked(params.get("rand_seed", 1) or 0) def parameters(self): return {"rand_type": index_to_enum(_Randomize.Type, self.__rand_type_cb.currentIndex()), "rand_seed": 1 if self.__rand_seed_ch.isChecked() else None} @staticmethod def createinstance(params): rand_type = params.get("rand_type", Randomize.RandomizeClasses) rand_seed = params.get("rand_seed", 1) return _Randomize(rand_type=rand_type, rand_seed=rand_seed) def __repr__(self): return "{}, {}".format(self.__rand_type_cb.currentText(), "Replicable" if self.__rand_seed_ch.isChecked() else "Not replicable")
class UpdateOptionsWidget(QWidget): """ A Widget with download/update/remove options. """ #: Install/update button was clicked installClicked = Signal() #: Remove button was clicked. removeClicked = Signal() def __init__(self, state=AVAILABLE, parent=None): QWidget.__init__(self, parent) layout = QHBoxLayout() layout.setSpacing(1) layout.setContentsMargins(1, 1, 1, 1) self.checkButton = QCheckBox() layout.addWidget(self.checkButton) self.setLayout(layout) self.setMinimumHeight(20) self.setMaximumHeight(20) self.state = -1 self.setState(state) def setState(self, state): """ Set the current update state for the widget (AVAILABLE, CURRENT, OUTDATED or DEPRECTED). """ self.state = state self._update() def _update(self): if self.state == AVAILABLE: self.checkButton.setChecked(False) elif self.state == CURRENT: self.checkButton.setChecked(True) elif self.state == OUTDATED: self.checkButton.setChecked(True) elif self.state == DEPRECATED: self.checkButton.setChecked(True) else: raise ValueError("Invalid state %r" % self._state) try: self.checkButton.clicked.disconnect() # Remove old signals if they exist except Exception: pass if not self.checkButton.isChecked(): # Switch signals if the file is present or not self.checkButton.clicked.connect(self.installClicked) else: self.checkButton.clicked.connect(self.removeClicked)
class NormalizationModule(SingleMethodModule): attribute = 'normalizer' title = 'Normalization' toggle_enabled = True enabled = settings.Setting(False) methods = [ preprocess.PorterStemmer, preprocess.SnowballStemmer, preprocess.WordNetLemmatizer, preprocess.UDPipeLemmatizer, ] SNOWBALL = 1 UDPIPE = 3 snowball_language = settings.Setting('English') udpipe_language = settings.Setting('English') udpipe_tokenizer = settings.Setting(False) def __init__(self, master): super().__init__(master) label = gui.label(self, self, 'Language:') label.setAlignment(Qt.AlignRight) self.method_layout.addWidget(label, self.SNOWBALL, 1) snowball_box = widgets.ComboBox(self, 'snowball_language', items=preprocess.SnowballStemmer.supported_languages) snowball_box.currentIndexChanged.connect(self.change_language) self.method_layout.addWidget(snowball_box, self.SNOWBALL, 2) self.methods[self.SNOWBALL].language = self.snowball_language self.udpipe_tokenizer_box = QCheckBox("UDPipe tokenizer", self, checked=self.udpipe_tokenizer) self.udpipe_tokenizer_box.stateChanged.connect(self.change_tokenizer) self.method_layout.addWidget(self.udpipe_tokenizer_box, self.UDPIPE, 1) self.udpipe_label = gui.label(self, self, 'Language:') self.udpipe_label.setAlignment(Qt.AlignRight) self.method_layout.addWidget(self.udpipe_label, self.UDPIPE, 2) self.udpipe_models = UDPipeModels() self.create_udpipe_box() self.udpipe_online = self.udpipe_models.online self.on_off_button.stateChanged.connect(self.check_udpipe_online) self.check_udpipe_online() self.methods[self.UDPIPE].language = self.udpipe_language self.methods[self.UDPIPE].use_tokenizer = self.udpipe_tokenizer def create_udpipe_box(self): if not self.udpipe_models.supported_languages: self.group.button(self.UDPIPE).setEnabled(False) self.udpipe_tokenizer_box.setEnabled(False) self.udpipe_label.setEnabled(False) self.udpipe_box = widgets.ComboBox(self, 'udpipe_language', items=['']) self.udpipe_box.setEnabled(False) else: self.group.button(self.UDPIPE).setEnabled(True) self.udpipe_tokenizer_box.setEnabled(True) self.udpipe_label.setEnabled(True) self.udpipe_box = widgets.ComboBox(self, 'udpipe_language', items=self.udpipe_models.supported_languages) self.udpipe_box.currentIndexChanged.connect(self.change_language) self.method_layout.addWidget(self.udpipe_box, self.UDPIPE, 3) def check_udpipe_online(self): current_state = self.udpipe_models.online if self.udpipe_online != current_state: self.create_udpipe_box() self.udpipe_online = current_state self.master.Warning.udpipe_offline.clear() self.master.Warning.udpipe_offline_no_models.clear() if not current_state and self.enabled: if self.udpipe_models.supported_languages: self.master.Warning.udpipe_offline() else: self.master.Warning.udpipe_offline_no_models() def change_language(self): if self.methods[self.SNOWBALL].language != self.snowball_language: self.methods[self.SNOWBALL].language = self.snowball_language if self.method_index == self.SNOWBALL: self.change_signal.emit() if self.methods[self.UDPIPE].language != self.udpipe_language: self.methods[self.UDPIPE].language = self.udpipe_language if self.method_index == self.UDPIPE: self.change_signal.emit() def change_tokenizer(self): self.udpipe_tokenizer = self.udpipe_tokenizer_box.isChecked() if self.methods[self.UDPIPE].use_tokenizer != self.udpipe_tokenizer: self.methods[self.UDPIPE].use_tokenizer = self.udpipe_tokenizer if self.method_index == self.UDPIPE: self.change_signal.emit()
class EditLightUI(QWidget): def __init__(self, light, parent): super(EditLightUI, self).__init__() self.title = 'Edit Lighting Element' self.parent = parent self.light = light self.setLayout(QVBoxLayout(self)) self.layout().setAlignment(Qt.AlignCenter) # Init Name text control self._nameControl = LineEdit('Name', self) self._nameControl.setText(self.light.name) self._nameControl.kb.connect(self.showOSK) # Init Output Pin dropdown control self._outputPinControlLabel = QLabel('Output Pin', self) self._outputPinControl = QComboBox(self) for x in self.parent.availablePins(): self._outputPinControl.addItem(str(x)) for i in range(self._outputPinControl.count()): if self._outputPinControl.itemText(i) == str(self.light.outputPin): self._outputPinControl.setCurrentIndex(i) break del x, i # Init Enabled checkbox control and set value self._enabledControl = QCheckBox('Enabled', self) self._enabledControl.setChecked(self.light.enabled) # Init Icon dropdown control self._iconControlLabel = QLabel('Icon Path', self) self._iconControl = QComboBox(self) for key in Config.icons['lights'].keys(): icon = Config.icon('lights', key) self._iconControl.addItem(icon['name'], key) self._iconControl.setItemIcon(self._iconControl.count() - 1, QIcon(icon['path'])) for i in range(self._iconControl.count()): if self.light.icon is not None and self._iconControl.itemData( i) == self.light.icon: self._iconControl.setCurrentIndex(i) break del key, i # Init Strobe checkbox control and set value self._strobeControl = QCheckBox('Strobe', self) self._strobeControl.setChecked(self.light.strobe) # Init Save button self._saveBtn = QPushButton('Save', self) self._saveBtn.clicked.connect(self.__saveBtnAction) # Init cancel button self._cancelBtn = QPushButton('Cancel', self) self._cancelBtn.clicked.connect(self.__cancel) # Assign control layout layoutList = [['_nameControl'], ['_outputPinControlLabel', '_outputPinControl'], ['_enabledControl', '_strobeControl'], ['_iconControlLabel', '_iconControl'], ['_saveBtn', '_cancelBtn']] for l in layoutList: panel = QWidget(self) panel.setLayout(QHBoxLayout(panel)) panel.layout().setAlignment(Qt.AlignCenter) for ctrl in l: panel.layout().addWidget(eval('self.%s' % ctrl)) self.layout().addWidget(panel) # Destroy local variables del l, ctrl def __saveBtnAction(self): self.light.name = self._nameControl.text() self.light.outputPin = int(self._outputPinControl.currentText()) self.light.enabled = self._enabledControl.isChecked() self.light.icon = self._iconControl.currentData() self.light.strobe = self._strobeControl.isChecked() self.parent.lights.save() self.parent.loadUI("config_light") def __cancel(self): self.parent.loadUI("config_light") def showOSK(self): self.window().dock.show() self.window().osk.rWidget = self._nameControl
class EditTracUI(QWidget): def __init__(self, trac, parent): super(EditTracUI, self).__init__() self.title = 'Edit TracControl Element' self.setLayout(QVBoxLayout(self)) self.layout().setAlignment(Qt.AlignCenter) self.parent = parent self.trac = trac # Init controls self._nameControl = LineEdit('Name', self) self._nameControl.setText(self.trac.name) self._nameControl.kb.connect(self.showOSK) self._outputPinControlLabel = QLabel('Output Pin', self) self._outputPinControl = QComboBox(self) for _pins in self.parent.availablePins(): self._outputPinControl.addItem(str(_pins)) for _i in range(self._outputPinControl.count()): if self._outputPinControl.itemText(_i) == str(self.trac.outputPin): self._outputPinControl.setCurrentIndex(_i) break self._enabledControl = QCheckBox('Enabled', self) self._enabledControl.setChecked(self.trac.enabled) self._iconControlLabel = QLabel('Icon Path', self) self._iconControl = QComboBox(self) for _key in Config.icons['tracControl'].keys(): icon = Config.icon('tracControl', _key) self._iconControl.addItem(icon['name'], _key) self._iconControl.setItemIcon(self._iconControl.count() - 1, QIcon(icon['path'])) # Set combobox selection to icon variable for iconIdx in range(self._iconControl.count()): if self.trac.icon is not None and self._iconControl.itemData( iconIdx) == self.trac.icon: self._iconControl.setCurrentIndex(iconIdx) break self._saveBtn = QPushButton('Save', self) self._saveBtn.clicked.connect(self.__saveBtnAction) self._cancelBtn = QPushButton('Cancel', self) self._cancelBtn.clicked.connect(self.__cancel) _layout = [['_nameControl'], ['_outputPinControlLabel', '_outputPinControl'], ['_enabledControl'], ['_iconControlLabel', '_iconControl'], ['_saveBtn', '_cancelBtn']] for _list in _layout: _panel = QWidget(self) _panel.setLayout(QHBoxLayout(_panel)) _panel.layout().setAlignment(Qt.AlignCenter) for _ctrl in _list: _panel.layout().addWidget(eval('self.%s' % _ctrl)) self.layout().addWidget(_panel) def __saveBtnAction(self): self.trac.name = self._nameControl.text() self.trac.outputPin = int(self._outputPinControl.currentText()) self.trac.enabled = self._enabledControl.isChecked() self.trac.icon = self._iconControl.currentData() self.parent.tracs.save() self.parent.loadUI('config_trac') def __cancel(self): self.parent.loadUI('config_trac') def showOSK(self): self.window().dock.show() self.window().osk.rWidget = self._nameControl
class DiscreteVariableEditor(VariableEditor): """An editor widget for editing a discrete variable. Extends the :class:`VariableEditor` to enable editing of variables values. """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) form = self.layout().itemAt(0) assert isinstance(form, QFormLayout) self.ordered_cb = QCheckBox("Ordered", self, toolTip="Is this an ordered categorical.") self.ordered_cb.toggled.connect(self._set_ordered) #: A list model of discrete variable's values. self.values_model = itemmodels.PyListModel( flags=Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsEditable) vlayout = QVBoxLayout(spacing=1, margin=0) self.values_edit = QListView(editTriggers=QListView.DoubleClicked | QListView.EditKeyPressed) self.values_edit.setItemDelegate(CategoriesEditDelegate(self)) self.values_edit.setModel(self.values_model) self.values_model.dataChanged.connect(self.on_values_changed) self.values_edit.selectionModel().selectionChanged.connect( self.on_value_selection_changed) self.values_model.layoutChanged.connect( self.on_value_selection_changed) self.values_model.rowsMoved.connect(self.on_value_selection_changed) vlayout.addWidget(self.values_edit) hlayout = QHBoxLayout(spacing=1, margin=0) self.categories_action_group = group = QActionGroup( self, objectName="action-group-categories", enabled=False) self.move_value_up = QAction( "\N{UPWARDS ARROW}", group, toolTip="Move the selected item up.", shortcut=QKeySequence(Qt.ControlModifier | Qt.AltModifier | Qt.Key_BracketLeft), shortcutContext=Qt.WidgetShortcut, ) self.move_value_up.triggered.connect(self.move_up) self.move_value_down = QAction( "\N{DOWNWARDS ARROW}", group, toolTip="Move the selected item down.", shortcut=QKeySequence(Qt.ControlModifier | Qt.AltModifier | Qt.Key_BracketRight), shortcutContext=Qt.WidgetShortcut, ) self.move_value_down.triggered.connect(self.move_down) self.add_new_item = QAction( "+", group, objectName="action-add-item", toolTip="Append a new item.", shortcut=QKeySequence(QKeySequence.New), shortcutContext=Qt.WidgetShortcut, ) self.remove_item = QAction( "\N{MINUS SIGN}", group, objectName="action-remove-item", toolTip="Delete the selected item.", shortcut=QKeySequence(QKeySequence.Delete), shortcutContext=Qt.WidgetShortcut, ) self.add_new_item.triggered.connect(self._add_category) self.remove_item.triggered.connect(self._remove_category) button1 = FixedSizeButton(self, defaultAction=self.move_value_up, accessibleName="Move up") button2 = FixedSizeButton(self, defaultAction=self.move_value_down, accessibleName="Move down") button3 = FixedSizeButton(self, defaultAction=self.add_new_item, accessibleName="Add") button4 = FixedSizeButton(self, defaultAction=self.remove_item, accessibleName="Remove") self.values_edit.addActions([ self.move_value_up, self.move_value_down, self.add_new_item, self.remove_item ]) hlayout.addWidget(button1) hlayout.addWidget(button2) hlayout.addSpacing(3) hlayout.addWidget(button3) hlayout.addWidget(button4) hlayout.addStretch(10) vlayout.addLayout(hlayout) form.insertRow(1, "", self.ordered_cb) form.insertRow(2, "Values:", vlayout) QWidget.setTabOrder(self.name_edit, self.ordered_cb) QWidget.setTabOrder(self.ordered_cb, self.values_edit) QWidget.setTabOrder(self.values_edit, button1) QWidget.setTabOrder(button1, button2) QWidget.setTabOrder(button2, button3) QWidget.setTabOrder(button3, button4) def set_data(self, var, transform=()): # type: (Optional[Categorical], Sequence[Transform]) -> None """ Set the variable to edit. """ # pylint: disable=too-many-branches super().set_data(var, transform) tr = None # type: Optional[CategoriesMapping] ordered = None # type: Optional[ChangeOrdered] for tr_ in transform: if isinstance(tr_, CategoriesMapping): tr = tr_ if isinstance(tr_, ChangeOrdered): ordered = tr_ items = [] if tr is not None: ci_index = {c: i for i, c in enumerate(var.categories)} for ci, cj in tr.mapping: if ci is None and cj is not None: # level added item = { Qt.EditRole: cj, EditStateRole: ItemEditState.Added, SourcePosRole: None } elif ci is not None and cj is None: # ci level dropped item = { Qt.EditRole: ci, EditStateRole: ItemEditState.Dropped, SourcePosRole: ci_index[ci], SourceNameRole: ci } elif ci is not None and cj is not None: # rename or reorder item = { Qt.EditRole: cj, EditStateRole: ItemEditState.NoState, SourcePosRole: ci_index[ci], SourceNameRole: ci } else: assert False, "invalid mapping: {!r}".format(tr.mapping) items.append(item) elif var is not None: items = [{ Qt.EditRole: c, EditStateRole: ItemEditState.NoState, SourcePosRole: i, SourceNameRole: c } for i, c in enumerate(var.categories)] else: items = [] with disconnected(self.values_model.dataChanged, self.on_values_changed): self.values_model.clear() self.values_model.insertRows(0, len(items)) for i, item in enumerate(items): self.values_model.setItemData(self.values_model.index(i, 0), item) if ordered is not None: self.ordered_cb.setChecked(ordered.ordered) elif var is not None: self.ordered_cb.setChecked(isinstance(var, Ordered)) self.add_new_item.actionGroup().setEnabled(var is not None) def __categories_mapping(self): # type: () -> CategoriesMappingType model = self.values_model source = self.var.categories res = [] for i in range(model.rowCount()): midx = model.index(i, 0) category = midx.data(Qt.EditRole) source_pos = midx.data(SourcePosRole) # type: Optional[int] if source_pos is not None: source_name = source[source_pos] else: source_name = None state = midx.data(EditStateRole) if state == ItemEditState.Dropped: res.append((source_name, None)) elif state == ItemEditState.Added: res.append((None, category)) else: res.append((source_name, category)) return res def get_data(self): """Retrieve the modified variable """ var, tr = super().get_data() if var is None: return var, tr mapping = self.__categories_mapping() if any(_1 != _2 or _2 != _3 for (_1, _2), _3 in zip_longest(mapping, var.categories)): tr.append(CategoriesMapping(mapping)) ordered = self.ordered_cb.isChecked() if ordered != isinstance(var, Ordered): tr.append(ChangeOrdered(ordered)) return var, tr def clear(self): """Clear the model state. """ super().clear() self.values_model.clear() def move_rows(self, rows, offset): if not rows: return assert len(rows) == 1 i = rows[0].row() if offset > 0: offset += 1 self.values_model.moveRows(QModelIndex(), i, 1, QModelIndex(), i + offset) self.variable_changed.emit() def move_up(self): rows = self.values_edit.selectionModel().selectedRows() self.move_rows(rows, -1) def move_down(self): rows = self.values_edit.selectionModel().selectedRows() self.move_rows(rows, 1) @Slot() def on_values_changed(self): self.variable_changed.emit() @Slot() def on_value_selection_changed(self): rows = self.values_edit.selectionModel().selectedRows() if rows: i = rows[0].row() self.move_value_up.setEnabled(i) self.move_value_down.setEnabled( i != self.values_model.rowCount() - 1) else: self.move_value_up.setEnabled(False) self.move_value_down.setEnabled(False) def _remove_category(self): """ Remove the current selected category. If the item is an existing category present in the source variable it is marked as removed in the view. But if it was added in the set transformation it is removed entirely from the model and view. """ view = self.values_edit rows = view.selectionModel().selectedRows(0) if not rows: return assert len(rows) == 1 index = rows[0] # type: QModelIndex model = index.model() state = index.data(EditStateRole) pos = index.data(Qt.UserRole) if pos is not None and pos >= 0: # existing level -> only mark/toggle its dropped state model.setData( index, ItemEditState.Dropped if state != ItemEditState.Dropped else ItemEditState.NoState, EditStateRole) elif state == ItemEditState.Added: # new level -> remove it model.removeRow(index.row()) else: assert False, "invalid state '{}' for {}" \ .format(state, index.row()) def _add_category(self): """ Add a new category """ view = self.values_edit model = view.model() with disconnected(model.dataChanged, self.on_values_changed, Qt.UniqueConnection): row = model.rowCount() if not model.insertRow(model.rowCount()): return index = model.index(row, 0) model.setItemData( index, { Qt.EditRole: "", SourcePosRole: None, EditStateRole: ItemEditState.Added }) view.setCurrentIndex(index) view.edit(index) self.on_values_changed() def _set_ordered(self, ordered): self.ordered_cb.setChecked(ordered) self.variable_changed.emit()
class SchemeInfoDialog(QDialog): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.scheme = None self.__autoCommit = True self.__setupUi() def __setupUi(self): layout = QVBoxLayout() layout.setContentsMargins(0, 0, 0, 0) layout.setSpacing(0) self.editor = SchemeInfoEdit(self) self.editor.layout().setContentsMargins(20, 20, 20, 20) self.editor.layout().setSpacing(15) self.editor.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.MinimumExpanding) heading = self.tr("Workflow Info") heading = "<h3>{0}</h3>".format(heading) self.heading = QLabel(heading, self, objectName="heading") # Insert heading self.editor.layout().insertRow(0, self.heading) self.buttonbox = QDialogButtonBox( QDialogButtonBox.Ok | QDialogButtonBox.Cancel, Qt.Horizontal, self ) # Insert button box self.editor.layout().addRow(self.buttonbox) widget = StyledWidget(self, objectName="auto-show-container") check_layout = QHBoxLayout() check_layout.setContentsMargins(20, 10, 20, 10) self.__showAtNewSchemeCheck = \ QCheckBox(self.tr("Show when I make a New Workflow."), self, objectName="auto-show-check", checked=False, ) check_layout.addWidget(self.__showAtNewSchemeCheck) check_layout.addWidget( QLabel(self.tr("You can also edit Workflow Info later " "(File -> Workflow Info)."), self, objectName="auto-show-info"), alignment=Qt.AlignRight) widget.setLayout(check_layout) widget.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Fixed) if self.__autoCommit: self.buttonbox.accepted.connect(self.editor.commit) self.buttonbox.accepted.connect(self.accept) self.buttonbox.rejected.connect(self.reject) layout.addWidget(self.editor, stretch=10) layout.addWidget(widget) self.setLayout(layout) def setShowAtNewScheme(self, checked): """ Set the 'Show at new scheme' check state. """ self.__showAtNewSchemeCheck.setChecked(checked) def showAtNewScheme(self): """ Return the check state of the 'Show at new scheme' check box. """ return self.__showAtNewSchemeCheck.isChecked() def setAutoCommit(self, auto): if self.__autoCommit != auto: self.__autoCommit = auto if auto: self.buttonbox.accepted.connect(self.editor.commit) else: self.buttonbox.accepted.disconnect(self.editor.commit) def setScheme(self, scheme): """Set the scheme to display/edit. """ self.scheme = scheme self.editor.setScheme(scheme)
class NormalizationModule(SingleMethodModule): attribute = 'normalizer' title = 'Normalization' toggle_enabled = True enabled = settings.Setting(False) methods = [ preprocess.PorterStemmer, preprocess.SnowballStemmer, preprocess.WordNetLemmatizer, preprocess.UDPipeLemmatizer, ] SNOWBALL = 1 UDPIPE = 3 snowball_language = settings.Setting('English') udpipe_language = settings.Setting('English') udpipe_tokenizer = settings.Setting(False) def __init__(self, master): super().__init__(master) label = gui.label(self, self, 'Language:') label.setAlignment(Qt.AlignRight) self.method_layout.addWidget(label, self.SNOWBALL, 1) snowball_box = widgets.ComboBox( self, 'snowball_language', items=preprocess.SnowballStemmer.supported_languages) snowball_box.currentIndexChanged.connect(self.change_language) self.method_layout.addWidget(snowball_box, self.SNOWBALL, 2) self.methods[self.SNOWBALL].language = self.snowball_language self.udpipe_tokenizer_box = QCheckBox("UDPipe tokenizer", self, checked=self.udpipe_tokenizer) self.udpipe_tokenizer_box.stateChanged.connect(self.change_tokenizer) self.method_layout.addWidget(self.udpipe_tokenizer_box, self.UDPIPE, 1) label = gui.label(self, self, 'Language:') label.setAlignment(Qt.AlignRight) self.method_layout.addWidget(label, self.UDPIPE, 2) udpipe_box = widgets.ComboBox( self, 'udpipe_language', items=preprocess.UDPipeLemmatizer.supported_languages) udpipe_box.currentIndexChanged.connect(self.change_language) self.method_layout.addWidget(udpipe_box, self.UDPIPE, 3) self.methods[self.UDPIPE].language = self.udpipe_language self.methods[self.UDPIPE].use_tokenizer = self.udpipe_tokenizer def change_language(self): if self.methods[self.SNOWBALL].language != self.snowball_language: self.methods[self.SNOWBALL].language = self.snowball_language if self.method_index == self.SNOWBALL: self.change_signal.emit() if self.methods[self.UDPIPE].language != self.udpipe_language: self.methods[self.UDPIPE].language = self.udpipe_language if self.method_index == self.UDPIPE: self.change_signal.emit() def change_tokenizer(self): self.udpipe_tokenizer = self.udpipe_tokenizer_box.isChecked() if self.methods[self.UDPIPE].use_tokenizer != self.udpipe_tokenizer: self.methods[self.UDPIPE].use_tokenizer = self.udpipe_tokenizer if self.method_index == self.UDPIPE: self.change_signal.emit()
class FilterCollectionWidget(CollectionWidget): def __init__(self, tree, dataset, master, parent=None): CollectionWidget.__init__(self, tree, dataset, master, parent) self.setLayout(QGridLayout()) self.layout().setContentsMargins(5, 5, 5, 5) self.layout().setColumnStretch(0, 10) self.layout().setColumnStretch(1, 10) self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred) self.setCollection(tree, dataset) def setCollection(self, collection, dataset, flags=0): CollectionWidget.setCollection(self, collection, dataset, flags) filters = [f for f in collection.elements("FilterDescription") if not is_hidden(f)] + \ [f for f in collection.elements("AttributeDescription") if not is_hidden(f)] # in case of pointers to filters (upstream/downstream flank) self.enableCB = QCheckBox(getattr(collection, "displayName", ""), self) if hasattr(collection, "description"): self.setToolTip(collection.description) if len(filters) == 1 and (not hasattr(filters[0], "displayName") or \ getattr(filters[0], "displayName", "") == getattr(collection, "displayName", "")): flags = flags | self.SINGLE_FILTER_FLAG i = 0 if not flags & self.SINGLE_FILTER_FLAG: self.layout().addWidget(self.enableCB, 0, 0) i += 1 for filter in filters: fType = getattr(filter, "type", None) if filter.is_pointer(): try: dataset, newfilter = filter.get_pointed() except ValueError as ex: newfilter = None if not newfilter: continue filter = newfilter fType = getattr(filter, "type", None) if fType is None else fType label, filter_widget = self.buildFilter(filter, flags, fTypeHint=fType) if isinstance(label, six.string_types): label = QLabel(label) self.layout().addWidget(label, i, 0) self.layout().addWidget(filter_widget, i, 1) i += 1 self.addSubControl(filter, filter_widget) if self.layout().count() == 0: self.layout().addWidget(self.enableCB, 0, 0) def buildFilter(self, filter, flags, fTypeHint=None): if flags & self.SINGLE_FILTER_FLAG: label = self.enableCB else: label = getattr(filter, "displayName", "") fType = filterType(filter) if fTypeHint == "drop_down_basic_filter": fType = ("list", "menu", "") filter_widget = None if fType == ("text", "", ""): filter_widget = TextFieldFilter(filter, self.dataset, self.master, self) elif fType == ("text", "", "1") or fType == ("text", "", "true"): filter_widget = IdListFilter(filter, self.dataset, self.master, self) elif fType == ("list", "radio", ""): filter_widget = RadioBooleanFilter(filter, self.dataset, self.master, self) elif fType == ("list", "menu", ""): filter_widget = DropDownFilter(filter, self.dataset, self.master, self) elif fType == ("list", "menu", "1") or fType == ("list", "menu", "true"): filter_widget = MultiSelectListFilter(filter, self.dataset, self.master, self) elif fType == ("container", "", ""): fType = set(map(filterType, filter.elements_top("Option"))) if len(fType) != 1: warnings.warn("Multiple filter types in a single container!" + str(fType)) fType = fType.pop() if fType[0] == "text": # ("text", "", "1"): filter_widget = DropDownIdListFilter(filter, self.dataset, self.master, self) elif fType == ("list", "radio", ""): filter_widget = DropDownRadioBooleanFilter(filter, self.dataset, self.master, self) if filter_widget is None: warnings.warn("Unknown filter type '%s' %s'" % (repr(fType), repr(filter))) filter_widget = UnknownFilter(filter, self.dataset, self.master, self) filter_widget.setMaximumWidth(400) return label, filter_widget def query(self): if self.enableCB.isChecked(): return CollectionWidget.query(self) else: return []
class DownloadOption(QWidget): """ A Widget with download/update/remove options. """ download_clicked = Signal() remove_clicked = Signal() def __init__(self, state=AVAILABLE, parent=None): QWidget.__init__(self, parent) layout = QHBoxLayout() layout.setSpacing(1) layout.setContentsMargins(1, 1, 1, 1) self.checkButton = QCheckBox() layout.addWidget(self.checkButton) self.setLayout(layout) self.setMinimumHeight(20) self.setMaximumHeight(20) self._state = state self._update() @property def state(self): return self._state @state.setter def state(self, state): self._state = state self._update() def _update(self): self.checkButton.setDisabled(False) if self.state == AVAILABLE: self.checkButton.setChecked(False) elif self.state == CURRENT: self.checkButton.setChecked(True) elif self.state == OUTDATED: self.checkButton.setChecked(True) elif self.state == DEPRECATED: self.checkButton.setChecked(True) elif self.state == USER_FILE: self.checkButton.setChecked(False) self.checkButton.setDisabled(True) else: raise ValueError("Invalid state %r" % self.state) try: self.checkButton.clicked.disconnect( ) # Remove old signals if they exist except Exception: pass if not self.checkButton.isChecked( ): # Switch signals if the file is present or not self.checkButton.clicked.connect(self.download_clicked) else: self.checkButton.clicked.connect(self.remove_clicked)
class AddonManagerWidget(QWidget): statechanged = Signal() def __init__(self, parent=None, **kwargs): super(AddonManagerWidget, self).__init__(parent, **kwargs) self.__items = [] self.setLayout(QVBoxLayout()) self.__header = QLabel( wordWrap=True, textFormat=Qt.RichText ) self.__search = QLineEdit( placeholderText=self.tr("Filter") ) self.__only_trusted = QCheckBox( self.tr("Show only trusted add-ons"), ) topline = QHBoxLayout() topline.addWidget(self.__search) topline.addWidget(self.__only_trusted) self.layout().addLayout(topline) self.__only_trusted.setChecked(True) self.show_only_trusted = True self.__only_trusted.stateChanged.connect(self._show_only_trusted_changed) self.__view = view = QTreeView( rootIsDecorated=False, editTriggers=QTreeView.NoEditTriggers, selectionMode=QTreeView.SingleSelection, alternatingRowColors=True ) self.__view.setItemDelegateForColumn(0, TristateCheckItemDelegate()) self.layout().addWidget(view) self.__model = model = QStandardItemModel() model.setHorizontalHeaderLabels(["", "Name", "Version", "Action"]) model.dataChanged.connect(self.__data_changed) self.__proxy = proxy = SortFilterProxyTrusted( filterKeyColumn=1, filterCaseSensitivity=Qt.CaseInsensitive ) proxy.setSourceModel(model) self.__search.textChanged.connect(proxy.setFilterFixedString) view.setModel(proxy) view.selectionModel().selectionChanged.connect( self.__update_details ) header = self.__view.header() header.setSectionResizeMode(0, QHeaderView.Fixed) header.setSectionResizeMode(2, QHeaderView.ResizeToContents) self.__details = QTextBrowser( frameShape=QTextBrowser.NoFrame, readOnly=True, lineWrapMode=QTextBrowser.WidgetWidth, openExternalLinks=True, ) self.__details.setWordWrapMode(QTextOption.WordWrap) palette = QPalette(self.palette()) palette.setColor(QPalette.Base, Qt.transparent) self.__details.setPalette(palette) self.layout().addWidget(self.__details) def _show_only_trusted_changed(self): self.__proxy.set_show_only_trusted(self.__only_trusted.isChecked()) def set_items(self, items): self.__items = items model = self.__model model.clear() model.setHorizontalHeaderLabels(["", "Name", "Version", "Action"]) for item in items: if isinstance(item, Installed): installed = True ins, dist = item name = dist.project_name summary = get_dist_meta(dist).get("Summary", "") version = ins.version if ins is not None else dist.version else: installed = False (ins,) = item dist = None name = ins.name summary = ins.summary version = ins.version updatable = is_updatable(item) item1 = QStandardItem() item1.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsUserCheckable | (Qt.ItemIsTristate if updatable else 0)) if installed and updatable: item1.setCheckState(Qt.PartiallyChecked) elif installed: item1.setCheckState(Qt.Checked) else: item1.setCheckState(Qt.Unchecked) item2 = QStandardItem(cleanup(name)) item2.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable) item2.setToolTip(summary) item2.setData(item, Qt.UserRole) if updatable: version = "{} < {}".format(dist.version, ins.version) item3 = QStandardItem(version) item3.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable) item4 = QStandardItem() item4.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable) model.appendRow([item1, item2, item3, item4]) self.__view.resizeColumnToContents(0) self.__view.setColumnWidth( 1, max(150, self.__view.sizeHintForColumn(1))) self.__view.setColumnWidth( 2, max(150, self.__view.sizeHintForColumn(2))) if self.__items: self.__view.selectionModel().select( self.__view.model().index(0, 0), QItemSelectionModel.Select | QItemSelectionModel.Rows ) def item_state(self): steps = [] for i, item in enumerate(self.__items): modelitem = self.__model.item(i, 0) state = modelitem.checkState() if modelitem.flags() & Qt.ItemIsTristate and state == Qt.Checked: steps.append((Upgrade, item)) elif isinstance(item, Available) and state == Qt.Checked: steps.append((Install, item)) elif isinstance(item, Installed) and state == Qt.Unchecked: steps.append((Uninstall, item)) return steps def __selected_row(self): indices = self.__view.selectedIndexes() if indices: proxy = self.__view.model() indices = [proxy.mapToSource(index) for index in indices] return indices[0].row() else: return -1 def set_install_projects(self, names): """Mark for installation the add-ons that match any of names""" model = self.__model for row in range(model.rowCount()): item = model.item(row, 1) if item.text() in names: model.item(row, 0).setCheckState(Qt.Checked) def __data_changed(self, topleft, bottomright): rows = range(topleft.row(), bottomright.row() + 1) for i in rows: modelitem = self.__model.item(i, 0) actionitem = self.__model.item(i, 3) item = self.__items[i] state = modelitem.checkState() flags = modelitem.flags() if flags & Qt.ItemIsTristate and state == Qt.Checked: actionitem.setText("Update") elif isinstance(item, Available) and state == Qt.Checked: actionitem.setText("Install") elif isinstance(item, Installed) and state == Qt.Unchecked: actionitem.setText("Uninstall") else: actionitem.setText("") self.statechanged.emit() def __update_details(self): index = self.__selected_row() if index == -1: self.__details.setText("") else: item = self.__model.item(index, 1) item = item.data(Qt.UserRole) assert isinstance(item, (Installed, Available)) text = self._detailed_text(item) self.__details.setText(text) def _detailed_text(self, item): if isinstance(item, Installed): remote, dist = item if remote is None: meta = get_dist_meta(dist) description = meta.get("Description") or meta.get('Summary') else: description = remote.description else: description = item[0].description if docutils is not None: try: html = docutils.core.publish_string( trim(description), writer_name="html", settings_overrides={ "output-encoding": "utf-8", # "embed-stylesheet": False, # "stylesheet": [], # "stylesheet_path": [] } ).decode("utf-8") except docutils.utils.SystemMessage: html = "<pre>{}<pre>".format(escape(description)) except Exception: html = "<pre>{}<pre>".format(escape(description)) else: html = "<pre>{}<pre>".format(escape(description)) return html def sizeHint(self): return QSize(480, 420)
class WelcomeDialog(QDialog): """ A welcome widget shown at startup presenting a series of buttons (actions) for a beginner to choose from. """ triggered = Signal(QAction) def __init__(self, *args, **kwargs): showAtStartup = kwargs.pop("showAtStartup", True) feedbackUrl = kwargs.pop("feedbackUrl", "") super().__init__(*args, **kwargs) self.__triggeredAction = None # type: Optional[QAction] self.__showAtStartupCheck = None self.__mainLayout = None self.__feedbackUrl = None self.__feedbackLabel = None self.setupUi() self.setFeedbackUrl(feedbackUrl) self.setShowAtStartup(showAtStartup) def setupUi(self): self.setLayout(QVBoxLayout()) self.layout().setContentsMargins(0, 0, 0, 0) self.layout().setSpacing(0) self.__mainLayout = QVBoxLayout() self.__mainLayout.setContentsMargins(0, 40, 0, 40) self.__mainLayout.setSpacing(65) self.layout().addLayout(self.__mainLayout) self.setStyleSheet(WELCOME_WIDGET_BUTTON_STYLE) bottom_bar = QWidget(objectName="bottom-bar") bottom_bar_layout = QHBoxLayout() bottom_bar_layout.setContentsMargins(20, 10, 20, 10) bottom_bar.setLayout(bottom_bar_layout) bottom_bar.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Maximum) self.__showAtStartupCheck = QCheckBox( self.tr("Show at startup"), bottom_bar, checked=False ) self.__feedbackLabel = QLabel( textInteractionFlags=Qt.TextBrowserInteraction, openExternalLinks=True, visible=False, ) bottom_bar_layout.addWidget( self.__showAtStartupCheck, alignment=Qt.AlignVCenter | Qt.AlignLeft ) bottom_bar_layout.addWidget( self.__feedbackLabel, alignment=Qt.AlignVCenter | Qt.AlignRight ) self.layout().addWidget(bottom_bar, alignment=Qt.AlignBottom, stretch=1) self.setSizeGripEnabled(False) self.setFixedSize(620, 390) def setShowAtStartup(self, show): # type: (bool) -> None """ Set the 'Show at startup' check box state. """ if self.__showAtStartupCheck.isChecked() != show: self.__showAtStartupCheck.setChecked(show) def showAtStartup(self): # type: () -> bool """ Return the 'Show at startup' check box state. """ return self.__showAtStartupCheck.isChecked() def setFeedbackUrl(self, url): # type: (str) -> None """ Set an 'feedback' url. When set a link is displayed in the bottom row. """ self.__feedbackUrl = url if url: text = self.tr("Help us improve!") self.__feedbackLabel.setText( '<a href="{url}">{text}</a>'.format(url=url, text=escape(text)) ) else: self.__feedbackLabel.setText("") self.__feedbackLabel.setVisible(bool(url)) def addRow(self, actions, background="light-orange"): """Add a row with `actions`. """ count = self.__mainLayout.count() self.insertRow(count, actions, background) def insertRow(self, index, actions, background="light-orange"): # type: (int, Iterable[QAction], Union[QColor, str]) -> None """Insert a row with `actions` at `index`. """ widget = QWidget(objectName="icon-row") layout = QHBoxLayout() layout.setContentsMargins(40, 0, 40, 0) layout.setSpacing(65) widget.setLayout(layout) self.__mainLayout.insertWidget(index, widget, stretch=10, alignment=Qt.AlignCenter) for i, action in enumerate(actions): self.insertAction(index, i, action, background) def insertAction(self, row, index, action, background="light-orange"): """Insert `action` in `row` in position `index`. """ button = self.createButton(action, background) self.insertButton(row, index, button) def insertButton(self, row, index, button): # type: (int, int, QToolButton) -> None """Insert `button` in `row` in position `index`. """ item = self.__mainLayout.itemAt(row) layout = item.widget().layout() layout.insertWidget(index, button) button.triggered.connect(self.__on_actionTriggered) def createButton(self, action, background="light-orange"): # type: (QAction, Union[QColor, str]) -> QToolButton """Create a tool button for action. """ button = WelcomeActionButton(self) button.setDefaultAction(action) button.setText(action.iconText()) button.setIcon(decorate_welcome_icon(action.icon(), background)) button.setToolTip(action.toolTip()) button.setFixedSize(100, 100) button.setToolButtonStyle(Qt.ToolButtonTextUnderIcon) button.setVisible(action.isVisible()) font = QFont(button.font()) font.setPointSize(13) button.setFont(font) return button def buttonAt(self, i, j): # type: (int, int) -> QToolButton """Return the button at i-t row and j-th column. """ item = self.__mainLayout.itemAt(i) row = item.widget() item = row.layout().itemAt(j) return item.widget() def triggeredAction(self): # type: () -> Optional[QAction] """Return the action that was triggered by the user. """ return self.__triggeredAction def showEvent(self, event): # Clear the triggered action before show. self.__triggeredAction = None super().showEvent(event) def __on_actionTriggered(self, action): # type: (QAction) -> None """Called when the button action is triggered. """ self.triggered.emit(action) self.__triggeredAction = action
class DiscreteVariableEditor(VariableEditor): """An editor widget for editing a discrete variable. Extends the :class:`VariableEditor` to enable editing of variables values. """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) form = self.layout().itemAt(0) assert isinstance(form, QFormLayout) self.ordered_cb = QCheckBox( "Ordered", self, toolTip="Is this an ordered categorical." ) self.ordered_cb.toggled.connect(self._set_ordered) #: A list model of discrete variable's values. self.values_model = itemmodels.PyListModel( flags=Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsEditable ) vlayout = QVBoxLayout(spacing=1, margin=0) self.values_edit = QListView( editTriggers=QListView.DoubleClicked | QListView.EditKeyPressed ) self.values_edit.setItemDelegate(CategoriesEditDelegate(self)) self.values_edit.setModel(self.values_model) self.values_model.dataChanged.connect(self.on_values_changed) self.values_edit.selectionModel().selectionChanged.connect( self.on_value_selection_changed) self.values_model.layoutChanged.connect(self.on_value_selection_changed) self.values_model.rowsMoved.connect(self.on_value_selection_changed) vlayout.addWidget(self.values_edit) hlayout = QHBoxLayout(spacing=1, margin=0) self.categories_action_group = group = QActionGroup( self, objectName="action-group-categories", enabled=False ) self.move_value_up = QAction( "\N{UPWARDS ARROW}", group, toolTip="Move the selected item up.", shortcut=QKeySequence(Qt.ControlModifier | Qt.AltModifier | Qt.Key_BracketLeft), shortcutContext=Qt.WidgetShortcut, ) self.move_value_up.triggered.connect(self.move_up) self.move_value_down = QAction( "\N{DOWNWARDS ARROW}", group, toolTip="Move the selected item down.", shortcut=QKeySequence(Qt.ControlModifier | Qt.AltModifier | Qt.Key_BracketRight), shortcutContext=Qt.WidgetShortcut, ) self.move_value_down.triggered.connect(self.move_down) self.add_new_item = QAction( "+", group, objectName="action-add-item", toolTip="Append a new item.", shortcut=QKeySequence(QKeySequence.New), shortcutContext=Qt.WidgetShortcut, ) self.remove_item = QAction( "\N{MINUS SIGN}", group, objectName="action-remove-item", toolTip="Delete the selected item.", shortcut=QKeySequence(QKeySequence.Delete), shortcutContext=Qt.WidgetShortcut, ) self.add_new_item.triggered.connect(self._add_category) self.remove_item.triggered.connect(self._remove_category) button1 = FixedSizeButton( self, defaultAction=self.move_value_up, accessibleName="Move up" ) button2 = FixedSizeButton( self, defaultAction=self.move_value_down, accessibleName="Move down" ) button3 = FixedSizeButton( self, defaultAction=self.add_new_item, accessibleName="Add" ) button4 = FixedSizeButton( self, defaultAction=self.remove_item, accessibleName="Remove" ) self.values_edit.addActions([self.move_value_up, self.move_value_down, self.add_new_item, self.remove_item]) hlayout.addWidget(button1) hlayout.addWidget(button2) hlayout.addSpacing(3) hlayout.addWidget(button3) hlayout.addWidget(button4) hlayout.addStretch(10) vlayout.addLayout(hlayout) form.insertRow(1, "", self.ordered_cb) form.insertRow(2, "Values:", vlayout) QWidget.setTabOrder(self.name_edit, self.ordered_cb) QWidget.setTabOrder(self.ordered_cb, self.values_edit) QWidget.setTabOrder(self.values_edit, button1) QWidget.setTabOrder(button1, button2) QWidget.setTabOrder(button2, button3) QWidget.setTabOrder(button3, button4) def set_data(self, var, transform=()): # type: (Optional[Categorical], Sequence[Transform]) -> None """ Set the variable to edit. """ # pylint: disable=too-many-branches super().set_data(var, transform) tr = None # type: Optional[CategoriesMapping] ordered = None # type: Optional[ChangeOrdered] for tr_ in transform: if isinstance(tr_, CategoriesMapping): tr = tr_ if isinstance(tr_, ChangeOrdered): ordered = tr_ items = [] if tr is not None: ci_index = {c: i for i, c in enumerate(var.categories)} for ci, cj in tr.mapping: if ci is None and cj is not None: # level added item = { Qt.EditRole: cj, EditStateRole: ItemEditState.Added, SourcePosRole: None } elif ci is not None and cj is None: # ci level dropped item = { Qt.EditRole: ci, EditStateRole: ItemEditState.Dropped, SourcePosRole: ci_index[ci], SourceNameRole: ci } elif ci is not None and cj is not None: # rename or reorder item = { Qt.EditRole: cj, EditStateRole: ItemEditState.NoState, SourcePosRole: ci_index[ci], SourceNameRole: ci } else: assert False, "invalid mapping: {!r}".format(tr.mapping) items.append(item) elif var is not None: items = [ {Qt.EditRole: c, EditStateRole: ItemEditState.NoState, SourcePosRole: i, SourceNameRole: c} for i, c in enumerate(var.categories) ] else: items = [] with disconnected(self.values_model.dataChanged, self.on_values_changed): self.values_model.clear() self.values_model.insertRows(0, len(items)) for i, item in enumerate(items): self.values_model.setItemData( self.values_model.index(i, 0), item ) if ordered is not None: self.ordered_cb.setChecked(ordered.ordered) elif var is not None: self.ordered_cb.setChecked(isinstance(var, Ordered)) self.add_new_item.actionGroup().setEnabled(var is not None) def __categories_mapping(self): # type: () -> CategoriesMappingType model = self.values_model source = self.var.categories res = [] for i in range(model.rowCount()): midx = model.index(i, 0) category = midx.data(Qt.EditRole) source_pos = midx.data(SourcePosRole) # type: Optional[int] if source_pos is not None: source_name = source[source_pos] else: source_name = None state = midx.data(EditStateRole) if state == ItemEditState.Dropped: res.append((source_name, None)) elif state == ItemEditState.Added: res.append((None, category)) else: res.append((source_name, category)) return res def get_data(self): """Retrieve the modified variable """ var, tr = super().get_data() if var is None: return var, tr mapping = self.__categories_mapping() if any(_1 != _2 or _2 != _3 for (_1, _2), _3 in zip_longest(mapping, var.categories)): tr.append(CategoriesMapping(mapping)) ordered = self.ordered_cb.isChecked() if ordered != isinstance(var, Ordered): tr.append(ChangeOrdered(ordered)) return var, tr def clear(self): """Clear the model state. """ super().clear() self.values_model.clear() def move_rows(self, rows, offset): if not rows: return assert len(rows) == 1 i = rows[0].row() if offset > 0: offset += 1 self.values_model.moveRows(QModelIndex(), i, 1, QModelIndex(), i + offset) self.variable_changed.emit() def move_up(self): rows = self.values_edit.selectionModel().selectedRows() self.move_rows(rows, -1) def move_down(self): rows = self.values_edit.selectionModel().selectedRows() self.move_rows(rows, 1) @Slot() def on_values_changed(self): self.variable_changed.emit() @Slot() def on_value_selection_changed(self): rows = self.values_edit.selectionModel().selectedRows() if rows: i = rows[0].row() self.move_value_up.setEnabled(i) self.move_value_down.setEnabled(i != self.values_model.rowCount() - 1) else: self.move_value_up.setEnabled(False) self.move_value_down.setEnabled(False) def _remove_category(self): """ Remove the current selected category. If the item is an existing category present in the source variable it is marked as removed in the view. But if it was added in the set transformation it is removed entirely from the model and view. """ view = self.values_edit rows = view.selectionModel().selectedRows(0) if not rows: return assert len(rows) == 1 index = rows[0] # type: QModelIndex model = index.model() state = index.data(EditStateRole) pos = index.data(Qt.UserRole) if pos is not None and pos >= 0: # existing level -> only mark/toggle its dropped state model.setData( index, ItemEditState.Dropped if state != ItemEditState.Dropped else ItemEditState.NoState, EditStateRole) elif state == ItemEditState.Added: # new level -> remove it model.removeRow(index.row()) else: assert False, "invalid state '{}' for {}" \ .format(state, index.row()) def _add_category(self): """ Add a new category """ view = self.values_edit model = view.model() with disconnected(model.dataChanged, self.on_values_changed, Qt.UniqueConnection): row = model.rowCount() if not model.insertRow(model.rowCount()): return index = model.index(row, 0) model.setItemData( index, { Qt.EditRole: "", SourcePosRole: None, EditStateRole: ItemEditState.Added } ) view.setCurrentIndex(index) view.edit(index) self.on_values_changed() def _set_ordered(self, ordered): self.ordered_cb.setChecked(ordered) self.variable_changed.emit()
class SchemeInfoDialog(QDialog): def __init__(self, *args, **kwargs): QDialog.__init__(self, *args, **kwargs) self.scheme = None self.__autoCommit = True self.__setupUi() def __setupUi(self): layout = QVBoxLayout() layout.setContentsMargins(0, 0, 0, 0) layout.setSpacing(0) self.editor = SchemeInfoEdit(self) self.editor.layout().setContentsMargins(20, 20, 20, 20) self.editor.layout().setSpacing(15) self.editor.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.MinimumExpanding) heading = self.tr("数据挖掘流程属性信息") heading = "<h3>{0}</h3>".format(heading) self.heading = QLabel(heading, self, objectName="heading") # Insert heading self.editor.layout().insertRow(0, self.heading) self.buttonbox = QDialogButtonBox( QDialogButtonBox.Ok | QDialogButtonBox.Cancel, Qt.Horizontal, self ) #self.buttonBox = QDialogButtonBox(self) #self.buttonBox.setOrientation(Qt.Horizontal) self.buttonbox.button(QDialogButtonBox.Ok).setText("确定") self.buttonbox.button(QDialogButtonBox.Cancel).setText("取消") # Insert button box self.editor.layout().addRow(self.buttonbox) widget = StyledWidget(self, objectName="auto-show-container") check_layout = QHBoxLayout() check_layout.setContentsMargins(20, 10, 20, 10) self.__showAtNewSchemeCheck = \ QCheckBox(self.tr("创建新的数据挖掘流程时显示."), self, objectName="auto-show-check", checked=False, ) check_layout.addWidget(self.__showAtNewSchemeCheck) check_layout.addWidget( QLabel(self.tr("编辑数据挖掘流程属性信息" "(文件 -> 属性)."), self, objectName="auto-show-info"), alignment=Qt.AlignRight) widget.setLayout(check_layout) widget.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Fixed) if self.__autoCommit: self.buttonbox.accepted.connect(self.editor.commit) self.buttonbox.accepted.connect(self.accept) self.buttonbox.rejected.connect(self.reject) layout.addWidget(self.editor, stretch=10) layout.addWidget(widget) self.setLayout(layout) def setShowAtNewScheme(self, checked): """ Set the 'Show at new scheme' check state. """ self.__showAtNewSchemeCheck.setChecked(checked) def showAtNewScheme(self): """ Return the check state of the 'Show at new scheme' check box. """ return self.__showAtNewSchemeCheck.isChecked() def setAutoCommit(self, auto): if self.__autoCommit != auto: self.__autoCommit = auto if auto: self.buttonbox.accepted.connect(self.editor.commit) else: self.buttonbox.accepted.disconnect(self.editor.commit) def setScheme(self, scheme): """Set the scheme to display/edit. """ self.scheme = scheme self.editor.setScheme(scheme)
class WelcomeDialog(QDialog): """ A welcome widget shown at startup presenting a series of buttons (actions) for a beginner to choose from. """ triggered = Signal(QAction) def __init__(self, *args, **kwargs): showAtStartup = kwargs.pop("showAtStartup", True) feedbackUrl = kwargs.pop("feedbackUrl", "") super().__init__(*args, **kwargs) self.__triggeredAction = None self.__showAtStartupCheck = None self.__mainLayout = None self.__feedbackUrl = None self.__feedbackLabel = None self.setupUi() self.setFeedbackUrl(feedbackUrl) self.setShowAtStartup(showAtStartup) def setupUi(self): self.setLayout(QVBoxLayout()) self.layout().setContentsMargins(0, 0, 0, 0) self.layout().setSpacing(0) self.__mainLayout = QVBoxLayout() self.__mainLayout.setContentsMargins(0, 40, 0, 40) self.__mainLayout.setSpacing(65) self.layout().addLayout(self.__mainLayout) self.setStyleSheet(WELCOME_WIDGET_BUTTON_STYLE) bottom_bar = QWidget(objectName="bottom-bar") bottom_bar_layout = QHBoxLayout() bottom_bar_layout.setContentsMargins(20, 10, 20, 10) bottom_bar.setLayout(bottom_bar_layout) bottom_bar.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Maximum) self.__showAtStartupCheck = QCheckBox( self.tr("Show at startup"), bottom_bar, checked=False ) self.__feedbackLabel = QLabel( textInteractionFlags=Qt.TextBrowserInteraction, openExternalLinks=True, visible=False, ) bottom_bar_layout.addWidget( self.__showAtStartupCheck, alignment=Qt.AlignVCenter | Qt.AlignLeft ) bottom_bar_layout.addWidget( self.__feedbackLabel, alignment=Qt.AlignVCenter | Qt.AlignRight ) self.layout().addWidget(bottom_bar, alignment=Qt.AlignBottom, stretch=1) self.setSizeGripEnabled(False) self.setFixedSize(620, 390) def setShowAtStartup(self, show): """ Set the 'Show at startup' check box state. """ if self.__showAtStartupCheck.isChecked() != show: self.__showAtStartupCheck.setChecked(show) def showAtStartup(self): """ Return the 'Show at startup' check box state. """ return self.__showAtStartupCheck.isChecked() def setFeedbackUrl(self, url): # type: (str) -> None """ Set an 'feedback' url. When set a link is displayed in the bottom row. """ self.__feedbackUrl = url if url: text = self.tr("Help us improve!") self.__feedbackLabel.setText( '<a href="{url}">{text}</a>'.format(url=url, text=escape(text)) ) else: self.__feedbackLabel.setText("") self.__feedbackLabel.setVisible(bool(url)) def addRow(self, actions, background="light-orange"): """Add a row with `actions`. """ count = self.__mainLayout.count() self.insertRow(count, actions, background) def insertRow(self, index, actions, background="light-orange"): """Insert a row with `actions` at `index`. """ widget = QWidget(objectName="icon-row") layout = QHBoxLayout() layout.setContentsMargins(40, 0, 40, 0) layout.setSpacing(65) widget.setLayout(layout) self.__mainLayout.insertWidget(index, widget, stretch=10, alignment=Qt.AlignCenter) for i, action in enumerate(actions): self.insertAction(index, i, action, background) def insertAction(self, row, index, action, background="light-orange"): """Insert `action` in `row` in position `index`. """ button = self.createButton(action, background) self.insertButton(row, index, button) def insertButton(self, row, index, button): """Insert `button` in `row` in position `index`. """ item = self.__mainLayout.itemAt(row) layout = item.widget().layout() layout.insertWidget(index, button) button.triggered.connect(self.__on_actionTriggered) def createButton(self, action, background="light-orange"): """Create a tool button for action. """ button = WelcomeActionButton(self) button.setDefaultAction(action) button.setText(action.iconText()) button.setIcon(decorate_welcome_icon(action.icon(), background)) button.setToolTip(action.toolTip()) button.setFixedSize(100, 100) button.setToolButtonStyle(Qt.ToolButtonTextUnderIcon) button.setVisible(action.isVisible()) font = QFont(button.font()) font.setPointSize(13) button.setFont(font) return button def buttonAt(self, i, j): """Return the button at i-t row and j-th column. """ item = self.__mainLayout.itemAt(i) row = item.widget() item = row.layout().itemAt(j) return item.widget() def triggeredAction(self): """Return the action that was triggered by the user. """ return self.__triggeredAction def showEvent(self, event): # Clear the triggered action before show. self.__triggeredAction = None super().showEvent(event) def __on_actionTriggered(self, action): """Called when the button action is triggered. """ self.triggered.emit(action) self.__triggeredAction = action
class NormalizeEditor(ScBaseEditor): DEFAULT_GROUP_BY = False DEFAULT_GROUP_VAR = None DEFAULT_METHOD = Normalize.CPM def __init__(self, parent=None, master=None, **kwargs): super().__init__(parent, **kwargs) self._group_var = self.DEFAULT_GROUP_VAR self._master = master self._master.input_data_changed.connect(self._set_model) self.setLayout(QVBoxLayout()) form = QFormLayout() cpm_b = QRadioButton("Counts per million", checked=True) med_b = QRadioButton("Median") self.group = QButtonGroup() self.group.buttonClicked.connect(self._on_button_clicked) for i, button in enumerate([cpm_b, med_b]): index = index_to_enum(Normalize.Method, i).value self.group.addButton(button, index - 1) form.addRow(button) self.group_by_check = QCheckBox("Cell Groups: ", enabled=self.DEFAULT_GROUP_BY) self.group_by_check.clicked.connect(self.edited) self.group_by_combo = QComboBox(enabled=self.DEFAULT_GROUP_BY) self.group_by_model = DomainModel(order=(DomainModel.METAS, DomainModel.CLASSES), valid_types=DiscreteVariable, alphabetical=True) self.group_by_combo.setModel(self.group_by_model) self.group_by_combo.currentIndexChanged.connect(self.changed) self.group_by_combo.activated.connect(self.edited) form.addRow(self.group_by_check, self.group_by_combo) self.layout().addLayout(form) self._set_model() def _set_model(self): data = self._master.data self.group_by_model.set_domain(data and data.domain) enable = bool(self.group_by_model) self.group_by_check.setChecked(False) self.group_by_check.setEnabled(enable) self.group_by_combo.setEnabled(enable) if self.group_by_model: self.group_by_combo.setCurrentIndex(0) if self._group_var and self._group_var in data.domain: index = self.group_by_model.indexOf(self._group_var) self.group_by_combo.setCurrentIndex(index) else: self.group_by_combo.setCurrentText(None) def _on_button_clicked(self): self.changed.emit() self.edited.emit() def setParameters(self, params): method = params.get("method", self.DEFAULT_METHOD) index = enum_to_index(Normalize.Method, method) self.group.buttons()[index].setChecked(True) self._group_var = params.get("group_var", self.DEFAULT_GROUP_VAR) group = bool(self._group_var and self.group_by_model) if group: index = self.group_by_model.indexOf(self._group_var) self.group_by_combo.setCurrentIndex(index) group_by = params.get("group_by", self.DEFAULT_GROUP_BY) self.group_by_check.setChecked(group_by and group) def parameters(self): index = self.group_by_combo.currentIndex() group_var = self.group_by_model[index] if index > -1 else None group_by = self.group_by_check.isChecked() method = index_to_enum(Normalize.Method, self.group.checkedId()) return {"group_var": group_var, "group_by": group_by, "method": method} @staticmethod def createinstance(params): group_var = params.get("group_var") group_by = params.get("group_by", NormalizeEditor.DEFAULT_GROUP_BY) method = params.get("method", NormalizeEditor.DEFAULT_METHOD) return NormalizeGroups(group_var, method) \ if group_by and group_var else NormalizeSamples(method) def __repr__(self): method = self.group.button(self.group.checkedId()).text() index = self.group_by_combo.currentIndex() group_var = self.group_by_model[index] if index > -1 else None group_by = self.group_by_check.isChecked() group_text = ", Grouped by: {}".format(group_var) if group_by else "" return "Method: {}".format(method) + group_text
class UserPrefUI(QWidget): def __init__(self, prefs, parent): super(UserPrefUI, self).__init__() self.title = 'User Preferences' self.setLayout(QVBoxLayout(self)) self.parent = parent self.prefs = prefs # Init logger self.logger = Logger('userPrefsUI', 'UI : User Preferences') # Set up container container = QWidget() container.setLayout(QVBoxLayout()) # Set up scroll area scrollArea = QScrollArea() scrollArea.setWidget(container) scrollArea.setWidgetResizable(True) self.layout().addWidget(scrollArea) # Permanent controls self._saveButton = QPushButton('Save', self) self._saveButton.clicked.connect(self.__save) self._startMaximized = QCheckBox('Start Maximized', self) self._allowDuplicatePins = QCheckBox('Allow Duplicate Output Pins', self) self._enableOBA = QCheckBox('OnBoard Air', self) self._enableLighting = QCheckBox('Lighting', self) self._enableTracControl = QCheckBox('Traction Control', self) self._enableCamViewer = QCheckBox('Camera Viewer', self) self._enableGyro = QCheckBox('Inclinometer', self) self._panelLabel = QLabel( '<html><center>Enable Modules:<br>\ <em>Restart TacOS for changes to take effect.</em></center></html>', self) self._i2cBus = QComboBox(self) self._i2cBus.addItems(Config.busList) self._i2cBusLabel = QLabel('I2C Bus', self) self._i2cAddress = QLineEdit('I2C Address', self) self._i2cLabel = QLabel( '<html><center>I2C Parameters:<br>\ <em>Restart TacOS for changes to take effect.</em></center></html>', self) self._i2cDebug = QCheckBox('I2C Debug Mode', self) self._i2cDebugLabel = QLabel( '<html><center>I2C Debug Mode:<br>\ <em>Disables all I2C comms.<br>\ Restart TacOS for changes to take effect.</em></center></html>', self) self._debugLogging = QCheckBox('Enable Debug Logs', self) self._restartButton = QPushButton('Restart TacOS', self) self._restartButton.clicked.connect(self.__restart) # Set initial values for control in [ 'startMaximized', 'allowDuplicatePins', 'enableOBA', 'enableLighting', 'enableTracControl', 'enableCamViewer', 'enableGyro', 'i2cDebug', 'debugLogging' ]: if control in prefs.keys(): exec('self._%s.setChecked(prefs["%s"])' % (control, control)) else: if control in [ 'enableOBA', 'enableLighting', 'enableTracControl', 'enableCamViewer', 'enableGyro' ]: exec('self._%s.setChecked(True)' % control) else: exec('self._%s.setChecked(False)' % control) if 'i2cAddress' in prefs.keys(): self._i2cAddress.setText(prefs['i2cAddress']) else: self._i2cAddress.setText('0x20') if 'i2cBus' in prefs.keys(): self._i2cBus.setCurrentText(str(prefs['i2cBus'])) else: self._i2cBus.setCurrentIndex(0) layoutList = [[ '_startMaximized', '_allowDuplicatePins', '_debugLogging' ], ['_panelLabel'], ['_enableOBA', '_enableLighting', '_enableTracControl'], ['_enableCamViewer', '_enableGyro'], ['_i2cLabel'], ['_i2cBusLabel', '_i2cBus', '_i2cAddress'], ['_i2cDebugLabel'], ['_i2cDebug'], ['_saveButton', '_restartButton']] for i in layoutList: panel = QWidget() panel.setLayout(QHBoxLayout(panel)) panel.layout().setSpacing(20) panel.layout().setAlignment(Qt.AlignCenter) for c in i: panel.layout().addWidget(eval('self.%s' % c)) container.layout().addWidget(panel) def refresh(self): self.__init__(self.window().prefs, self.parent) def __save(self): self.parent.prefs = self.__getPrefs() self.parent.savePrefs() def __restart(self): os.system("sudo sh /home/pi/TacOS/launcher.sh") sys.exit() def __getPrefs(self): _updates = { 'startMaximized': self._startMaximized.isChecked(), 'allowDuplicatePins': self._allowDuplicatePins.isChecked(), 'enableOBA': self._enableOBA.isChecked(), 'enableLighting': self._enableLighting.isChecked(), 'enableTracControl': self._enableTracControl.isChecked(), 'enableCamViewer': self._enableCamViewer.isChecked(), 'enableGyro': self._enableGyro.isChecked(), 'i2cBus': self._i2cBus.currentText(), 'i2cAddress': hex(int(self._i2cAddress.text(), 16)), 'i2cDebug': self._i2cDebug.isChecked(), 'debugLogging': self._debugLogging.isChecked() } for _key in _updates.keys(): self.prefs[_key] = _updates[_key] return self.prefs
class StandardizeEditor(ScBaseEditor): DEFAULT_LOWER_CLIP = False DEFAULT_UPPER_CLIP = False DEFAULT_LOWER_BOUND = -10 DEFAULT_UPPER_BOUND = 10 def __init__(self, parent=None, **kwargs): super().__init__(parent, **kwargs) self._lower_bound = self.DEFAULT_LOWER_BOUND self._upper_bound = self.DEFAULT_UPPER_BOUND self.setLayout(QVBoxLayout()) box = QGroupBox(title="Clipping", flat=True) form = QFormLayout() self.lower_check = QCheckBox("Lower Bound: ") self.lower_check.clicked.connect(self.edited) self.lower_spin = QSpinBox(minimum=-99, maximum=0, value=self._lower_bound) self.lower_spin.valueChanged[int].connect(self._set_lower_bound) self.lower_spin.editingFinished.connect(self.edited) self.upper_check = QCheckBox("Upper Bound: ") self.upper_check.clicked.connect(self.edited) self.upper_spin = QSpinBox(value=self._upper_bound) self.upper_spin.valueChanged[int].connect(self._set_upper_bound) self.upper_spin.editingFinished.connect(self.edited) form.addRow(self.lower_check, self.lower_spin) form.addRow(self.upper_check, self.upper_spin) box.setLayout(form) self.layout().addWidget(box) def _set_lower_bound(self, x): if self._lower_bound != x: self._lower_bound = x self.lower_spin.setValue(x) self.changed.emit() def _set_upper_bound(self, x): if self._upper_bound != x: self._upper_bound = x self.upper_spin.setValue(x) self.changed.emit() def setParameters(self, params): lower_clip = params.get("lower_clip", self.DEFAULT_LOWER_CLIP) self.lower_check.setChecked(lower_clip) self._set_lower_bound(params.get("lower", self.DEFAULT_LOWER_BOUND)) upper_clip = params.get("upper_clip", self.DEFAULT_UPPER_CLIP) self.upper_check.setChecked(upper_clip) self._set_upper_bound(params.get("upper", self.DEFAULT_UPPER_BOUND)) def parameters(self): return { "lower_clip": self.lower_check.isChecked(), "lower": self._lower_bound, "upper_clip": self.upper_check.isChecked(), "upper": self._upper_bound } @staticmethod def createinstance(params): lower, upper = None, None if params.get("lower_clip", StandardizeEditor.DEFAULT_LOWER_CLIP): lower = params.get("lower", StandardizeEditor.DEFAULT_LOWER_BOUND) if params.get("upper_clip", StandardizeEditor.DEFAULT_UPPER_CLIP): upper = params.get("upper", StandardizeEditor.DEFAULT_UPPER_BOUND) return Standardize(lower, upper) def __repr__(self): clips = [] if self.lower_check.isChecked(): clips.append("Lower Bound: {}".format(self.lower_spin.value())) if self.upper_check.isChecked(): clips.append("Upper Bound: {}".format(self.upper_spin.value())) return ", ".join(clips) if clips else "No Clipping"
class SelectGenesEditor(ScBaseEditor): DEFAULT_N_GENS = 1000 DEFAULT_METHOD = SelectMostVariableGenes.Dispersion DEFAULT_COMPUTE_STATS = True DEFAULT_N_GROUPS = 20 def __init__(self, parent=None, **kwargs): super().__init__(parent, **kwargs) self.setLayout(QVBoxLayout()) self._n_genes = self.DEFAULT_N_GENS self._n_groups = self.DEFAULT_N_GROUPS form = QFormLayout() self.n_genes_spin = QSpinBox(minimum=1, maximum=10**6, value=self._n_genes) self.n_genes_spin.valueChanged[int].connect(self._set_n_genes) self.n_genes_spin.editingFinished.connect(self.edited) form.addRow("Number of genes:", self.n_genes_spin) self.layout().addLayout(form) disp_b = QRadioButton("Dispersion", checked=True) vari_b = QRadioButton("Variance") mean_b = QRadioButton("Mean") self.group = QButtonGroup() self.group.buttonClicked.connect(self._on_button_clicked) for i, button in enumerate([disp_b, vari_b, mean_b]): index = index_to_enum(SelectMostVariableGenes.Method, i).value self.group.addButton(button, index - 1) form.addRow(button) self.stats_check = QCheckBox("Compute statistics for", checked=self.DEFAULT_COMPUTE_STATS) self.stats_check.clicked.connect(self.edited) self.n_groups_spin = QSpinBox(minimum=1, value=self._n_groups) self.n_groups_spin.valueChanged[int].connect(self._set_n_groups) self.n_groups_spin.editingFinished.connect(self.edited) box = QHBoxLayout() box.addWidget(self.stats_check) box.addWidget(self.n_groups_spin) box.addWidget(QLabel("gene groups.")) box.addStretch() self.layout().addLayout(box) def _set_n_genes(self, n): if self._n_genes != n: self._n_genes = n self.n_genes_spin.setValue(n) self.changed.emit() def _set_n_groups(self, n): if self._n_groups != n: self._n_groups = n self.n_groups_spin.setValue(n) self.changed.emit() def _on_button_clicked(self): self.changed.emit() self.edited.emit() def setParameters(self, params): self._set_n_genes(params.get("n_genes", self.DEFAULT_N_GENS)) method = params.get("method", self.DEFAULT_METHOD) index = enum_to_index(SelectMostVariableGenes.Method, method) self.group.buttons()[index].setChecked(True) compute_stats = params.get("compute_stats", self.DEFAULT_COMPUTE_STATS) self.stats_check.setChecked(compute_stats) self._set_n_groups(params.get("n_groups", self.DEFAULT_N_GROUPS)) def parameters(self): method = index_to_enum(SelectMostVariableGenes.Method, self.group.checkedId()) return { "n_genes": self._n_genes, "method": method, "compute_stats": self.stats_check.isChecked(), "n_groups": self._n_groups } @staticmethod def createinstance(params): method = params.get("method", SelectGenesEditor.DEFAULT_METHOD) n_genes = params.get("n_genes", SelectGenesEditor.DEFAULT_N_GENS) compute_stats = params.get("compute_stats", SelectGenesEditor.DEFAULT_COMPUTE_STATS) n_groups = params.get("n_groups", SelectGenesEditor.DEFAULT_N_GROUPS) \ if compute_stats else None return SelectMostVariableGenes(method, n_genes, n_groups) def __repr__(self): method = self.group.button(self.group.checkedId()).text() text = "Method: {}, Number of Genes: {}".format(method, self._n_genes) if self.stats_check.isChecked(): text += ", Number of Groups: {}".format(self._n_groups) return text
class SchemeInfoDialog(QDialog): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.scheme = None # type: Optional[Scheme] self.__autoCommit = True self.__setupUi() def __setupUi(self): layout = QVBoxLayout() layout.setContentsMargins(0, 0, 0, 0) layout.setSpacing(0) self.editor = SchemeInfoEdit(self) self.editor.layout().setContentsMargins(20, 20, 20, 20) self.editor.layout().setSpacing(15) self.editor.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.MinimumExpanding) heading = self.tr("Workflow Info") heading = "<h3>{0}</h3>".format(heading) self.heading = QLabel(heading, self, objectName="heading") # Insert heading self.editor.layout().insertRow(0, self.heading) self.buttonbox = QDialogButtonBox( QDialogButtonBox.Ok | QDialogButtonBox.Cancel, Qt.Horizontal, self ) # Insert button box self.editor.layout().addRow(self.buttonbox) widget = StyledWidget(self, objectName="auto-show-container") check_layout = QHBoxLayout() check_layout.setContentsMargins(20, 10, 20, 10) self.__showAtNewSchemeCheck = \ QCheckBox(self.tr("Show when I make a New Workflow."), self, objectName="auto-show-check", checked=False, ) check_layout.addWidget(self.__showAtNewSchemeCheck) check_layout.addWidget( QLabel(self.tr("You can also edit Workflow Info later " "(File -> Workflow Info)."), self, objectName="auto-show-info"), alignment=Qt.AlignRight) widget.setLayout(check_layout) widget.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Fixed) if self.__autoCommit: self.buttonbox.accepted.connect(self.editor.commit) self.buttonbox.accepted.connect(self.accept) self.buttonbox.rejected.connect(self.reject) layout.addWidget(self.editor, stretch=10) layout.addWidget(widget) self.setLayout(layout) def setShowAtNewScheme(self, checked): # type: (bool) -> None """ Set the 'Show at new scheme' check state. """ self.__showAtNewSchemeCheck.setChecked(checked) def showAtNewScheme(self): # type: () -> bool """ Return the check state of the 'Show at new scheme' check box. """ return self.__showAtNewSchemeCheck.isChecked() def setAutoCommit(self, auto): # type: (bool) -> None if self.__autoCommit != auto: self.__autoCommit = auto if auto: self.buttonbox.accepted.connect(self.editor.commit) else: self.buttonbox.accepted.disconnect(self.editor.commit) def setScheme(self, scheme): # type: (Scheme) -> None """Set the scheme to display/edit. """ self.scheme = scheme self.editor.setScheme(scheme)
class EditOBAUI(QWidget): def __init__(self, oba, parent): super(EditOBAUI, self).__init__() self.parent = parent self.oba = oba self.setLayout(QVBoxLayout(self)) self.layout().setAlignment(Qt.AlignCenter) # Init Name text control self._nameControl = LineEdit('Name', self) self._nameControl.setText(self.oba.name) self._nameControl.kb.connect(self.showOSK) # Init Output Pin dropdown control self._outputPinControlLabel = QLabel('Output Pin', self) self._outputPinControl = QComboBox(self) for _pin in self.parent.availablePins(): self._outputPinControl.addItem(str(_pin)) for _i in range(self._outputPinControl.count()): if self._outputPinControl.itemText(_i) == str(self.oba.outputPin): self._outputPinControl.setCurrentIndex(_i) break # Init Momentary checkbox control and set value self._momentaryControl = QCheckBox('Momentary', self) self._momentaryControl.setChecked(self.oba.momentary) # Init Enabled checkbox control and set value self._enabledControl = QCheckBox('Enabled', self) self._enabledControl.setChecked(self.oba.enabled) # Init Icon dropdown control self._iconControlLabel = QLabel('Icon Path', self) self._iconControl = QComboBox(self) for _key in Config.icons['oba'].keys(): icon = Config.icon('oba', _key) self._iconControl.addItem(icon['name'], _key) self._iconControl.setItemIcon(self._iconControl.count() - 1, QIcon(icon['path'])) for _i in range(self._iconControl.count()): # Set current index if matching icon attribute if self.oba.icon is not None and self._iconControl.itemData( _i) == self.oba.icon: self._iconControl.setCurrentIndex(_i) break # Init Save button self._saveBtn = QPushButton('Save', self) self._saveBtn.clicked.connect(self.__saveBtnAction) # Init cancel button self._cancelBtn = QPushButton('Cancel', self) self._cancelBtn.clicked.connect(self.__cancel) # Assign control layout _layout = [['_nameControl'], ['_outputPinControlLabel', '_outputPinControl'], ['_momentaryControl', '_enabledControl'], ['_iconControlLabel', '_iconControl'], ['_saveBtn', '_cancelBtn']] for _list in _layout: _panel = QWidget(self) _panel.setLayout(QHBoxLayout(_panel)) _panel.layout().setAlignment(Qt.AlignCenter) _panel.layout().setSpacing(20) for _control in _list: _panel.layout().addWidget(eval('self.%s' % _control)) self.layout().addWidget(_panel) def __saveBtnAction(self): self.oba.name = self._nameControl.text() self.oba.outputPin = int(self._outputPinControl.currentText()) self.oba.enabled = self._enabledControl.isChecked() self.oba.icon = self._iconControl.currentData() self.oba.momentary = self._momentaryControl.isChecked() self.parent.obas.save() self.parent.loadUI('config_oba') def __cancel(self): self.parent.loadUI('config_oba') def showOSK(self): self.window().dock.show() self.window().osk.rWidget = self._nameControl