class ReportDialog(QDialog): """Show a list of items.""" def __init__(self, items, parent=None): """Constructor.""" super().__init__(parent) self._items = items self._setup_ui() self.setWindowTitle('Report') def _setup_ui(self): self.layout = QVBoxLayout() self.setLayout(self.layout) if self._items: self.header = QLabel('Failed PVs', self) self.layout.addWidget(self.header) self.items_list = QListWidget(self) self.items_list.addItems(self._items) self.layout.addWidget(self.items_list) self.setMinimumSize(QSize(600, 300)) else: self.header = QLabel('Done', self) self.layout.addWidget(self.header) self.setMinimumSize(QSize(300, 100)) self.ok_btn = QPushButton('Ok', self) self.layout.addWidget(self.ok_btn) self.ok_btn.clicked.connect(self.accept)
class SearchableListWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) self.list_widget = QListWidget() self.filter_widget = QLineEdit() self.filter_widget.textChanged.connect(self.update_visible) layout = QVBoxLayout() layout.addWidget(self.filter_widget) layout.addWidget(self.list_widget) self.setLayout(layout) def __getattr__(self, item): if hasattr(self.list_widget, item): return getattr(self.list_widget, item) return super().__getattr__(item) def update_visible(self, text): items_text = [x.text() for x in self.list_widget.findItems(text, Qt.MatchContains)] for index in range(self.list_widget.count()): item = self.item(index) item.setHidden(item.text() not in items_text) def addItems(self, *args): self.list_widget.addItems(*args) self.update_visible(self.filter_widget.text()) def addItem(self, *args): self.list_widget.addItem(*args) self.update_visible(self.filter_widget.text())
def refresh_profiles(self, list_widget: QListWidget, new_values: typing.List[str]): index = self.get_index(list_widget.currentItem(), new_values) list_widget.clear() list_widget.addItems(new_values) if index != -1: list_widget.setCurrentRow(index)
def __init__(self, package, parent=None): QSplitter.__init__(self, parent) self.setWindowTitle(_("Tests - %s module") % package.__name__) self.setWindowIcon(get_icon("%s.svg" % package.__name__, "guidata.svg")) test_package_name = "%s.tests" % package.__name__ _temp = __import__(test_package_name) test_package = sys.modules[test_package_name] tests = get_tests(test_package) listwidget = QListWidget(self) listwidget.addItems([osp.basename(test.filename) for test in tests]) self.properties = TestPropertiesWidget(self) self.addWidget(listwidget) self.addWidget(self.properties) self.properties.run_button.clicked.connect( lambda: tests[listwidget.currentRow()].run()) self.properties.quit_button.clicked.connect(self.close) listwidget.currentRowChanged.connect( lambda row: self.properties.set_item(tests[row])) listwidget.itemActivated.connect( lambda: tests[listwidget.currentRow()].run()) listwidget.setCurrentRow(0) QShortcut(QKeySequence("Escape"), self, self.close) self.setSizes([150, 1]) self.setStretchFactor(1, 1) self.resize(QSize(950, 600)) self.properties.set_item(tests[0])
def import_pythonpath(self): """Import from PYTHONPATH environment variable""" env_pypath = os.environ.get('PYTHONPATH', '') if env_pypath: env_pypath = env_pypath.split(os.pathsep) dlg = QDialog(self) dlg.setWindowTitle(_("PYTHONPATH")) dlg.setWindowIcon(ima.icon('pythonpath')) dlg.setAttribute(Qt.WA_DeleteOnClose) dlg.setMinimumWidth(400) label = QLabel("The following paths from your PYTHONPATH " "environment variable will be imported.") listw = QListWidget(dlg) listw.addItems(env_pypath) bbox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) bbox.accepted.connect(dlg.accept) bbox.rejected.connect(dlg.reject) layout = QVBoxLayout() layout.addWidget(label) layout.addWidget(listw) layout.addWidget(bbox) dlg.setLayout(layout) if dlg.exec(): spy_pypath = self.get_path_dict() n = len(spy_pypath) for path in reversed(env_pypath): if (path in spy_pypath) or not self.check_path(path): continue item = self._create_item(path) self.listwidget.insertItem(n, item) self.refresh() else: QMessageBox.information( self, _("PYTHONPATH"), _("Your <tt>PYTHONPATH</tt> environment variable is empty, so " "there is nothing to import."), QMessageBox.Ok)
class RemoveLasersWindow(QWidget): def __init__(self, main_window): super().__init__() self.main_window = main_window lasers = self.main_window.lasers self.setWindowTitle("Remove laser") layout = QVBoxLayout() self.setLayout(layout) self.laser_list = QListWidget() self.laser_list.setSelectionMode(QAbstractItemView.ExtendedSelection) layout.addWidget(self.laser_list) names = [] for laser in lasers: settings = laser.get_settings() names.append(settings['name']) print(names) self.laser_list.addItems(names) self.removeButton = QPushButton("Remove") layout.addWidget(self.removeButton) self._createActions() self._connectActions() def _createActions(self): self.removeAction = QAction(self) self.removeAction.setText("Remove") def _connectActions(self): self.removeButton.clicked.connect(self.removeAction.trigger) self.removeAction.triggered.connect(self.remove_lasers) def remove_lasers(self): selected_rows = [x.row() for x in self.laser_list.selectedIndexes()] print(selected_rows) self.main_window.remove_lasers(selected_rows)
class RemoveRPWindow(QWidget): def __init__(self, main_window): super().__init__() self.main_window = main_window rps = self.main_window.rps self.setWindowTitle("Remove RedPitaya") layout = QVBoxLayout() self.setLayout(layout) self.laser_list = QListWidget() self.laser_list.setSelectionMode(QAbstractItemView.ExtendedSelection) layout.addWidget(self.laser_list) names = [] self.laser_list.addItems(rps) self.removeButton = QPushButton("Remove") layout.addWidget(self.removeButton) self._createActions() self._connectActions() def _createActions(self): self.removeAction = QAction(self) self.removeAction.setText("Remove") def _connectActions(self): self.removeButton.clicked.connect(self.removeAction.trigger) self.removeAction.triggered.connect(self.remove_lasers) def remove_lasers(self): selected_rows = [x.row() for x in self.laser_list.selectedIndexes()] print(selected_rows) self.main_window.remove_rps(selected_rows)
class MeasurementSettings(QWidget): """ :type settings: Settings """ def __init__(self, settings: PartSettings, parent=None): super().__init__(parent) self.chosen_element: Optional[MeasurementListWidgetItem] = None self.chosen_element_area: Optional[Tuple[AreaType, float]] = None self.settings = settings self.profile_list = QListWidget(self) self.profile_description = QTextEdit(self) self.profile_description.setReadOnly(True) self.profile_options = QListWidget() self.profile_options_chosen = QListWidget() self.measurement_area_choose = QEnumComboBox(enum_class=AreaType) self.per_component = QEnumComboBox(enum_class=PerComponent) self.power_num = QDoubleSpinBox() self.power_num.setDecimals(3) self.power_num.setRange(-100, 100) self.power_num.setValue(1) self.choose_butt = QPushButton("→", self) self.discard_butt = QPushButton("←", self) self.proportion_butt = QPushButton("Ratio", self) self.proportion_butt.setToolTip("Create proportion from two parameter") self.move_up = QPushButton("↑", self) self.move_down = QPushButton("↓", self) self.remove_button = QPushButton("Remove") self.save_butt = QPushButton("Save") self.save_butt.setToolTip( "Set name for set and choose at least one parameter") self.save_butt_with_name = QPushButton( "Save with custom parameters designation") self.save_butt_with_name.setToolTip( "Set name for set and choose at least one parameter") self.reset_butt = QPushButton("Clear") self.soft_reset_butt = QPushButton("Remove user parameters") self.profile_name = QLineEdit(self) self.delete_profile_butt = QPushButton("Delete ") self.export_profiles_butt = QPushButton("Export") self.import_profiles_butt = QPushButton("Import") self.edit_profile_butt = QPushButton("Edit") self.choose_butt.setDisabled(True) self.choose_butt.clicked.connect(self.choose_option) self.discard_butt.setDisabled(True) self.discard_butt.clicked.connect(self.discard_option) self.proportion_butt.setDisabled(True) self.proportion_butt.clicked.connect(self.proportion_action) self.save_butt.setDisabled(True) self.save_butt.clicked.connect(self.save_action) self.save_butt_with_name.setDisabled(True) self.save_butt_with_name.clicked.connect(self.named_save_action) self.profile_name.textChanged.connect(self.name_changed) self.move_down.setDisabled(True) self.move_down.clicked.connect(self.move_down_fun) self.move_up.setDisabled(True) self.move_up.clicked.connect(self.move_up_fun) self.remove_button.setDisabled(True) self.remove_button.clicked.connect(self.remove_element) self.reset_butt.clicked.connect(self.reset_action) self.soft_reset_butt.clicked.connect(self.soft_reset) self.delete_profile_butt.setDisabled(True) self.delete_profile_butt.clicked.connect(self.delete_profile) self.export_profiles_butt.clicked.connect( self.export_measurement_profiles) self.import_profiles_butt.clicked.connect( self.import_measurement_profiles) self.edit_profile_butt.clicked.connect(self.edit_profile) self.profile_list.itemSelectionChanged.connect(self.profile_chosen) self.profile_options.itemSelectionChanged.connect( self.create_selection_changed) self.profile_options_chosen.itemSelectionChanged.connect( self.create_selection_chosen_changed) self.settings.measurement_profiles_changed.connect( self._refresh_profiles) layout = QVBoxLayout() layout.addWidget(QLabel("Measurement set:")) profile_layout = QHBoxLayout() profile_layout.addWidget(self.profile_list) profile_layout.addWidget(self.profile_description) profile_buttons_layout = QHBoxLayout() profile_buttons_layout.addWidget(self.delete_profile_butt) profile_buttons_layout.addWidget(self.export_profiles_butt) profile_buttons_layout.addWidget(self.import_profiles_butt) profile_buttons_layout.addWidget(self.edit_profile_butt) profile_buttons_layout.addStretch() layout.addLayout(profile_layout) layout.addLayout(profile_buttons_layout) heading_layout = QHBoxLayout() # heading_layout.addWidget(QLabel("Create profile"), 1) heading_layout.addWidget(h_line(), 6) layout.addLayout(heading_layout) name_layout = QHBoxLayout() name_layout.addWidget(QLabel("Set name:")) name_layout.addWidget(self.profile_name) name_layout.addStretch() name_layout.addWidget(QLabel("Per component:")) name_layout.addWidget(self.per_component) name_layout.addWidget(QLabel("Area:")) name_layout.addWidget(self.measurement_area_choose) name_layout.addWidget(QLabel("to power:")) name_layout.addWidget(self.power_num) layout.addLayout(name_layout) create_layout = QHBoxLayout() create_layout.addWidget(self.profile_options) butt_op_layout = QVBoxLayout() butt_op_layout.addStretch() butt_op_layout.addWidget(self.choose_butt) butt_op_layout.addWidget(self.discard_butt) butt_op_layout.addWidget(self.proportion_butt) butt_op_layout.addWidget(self.reset_butt) butt_op_layout.addStretch() create_layout.addLayout(butt_op_layout) create_layout.addWidget(self.profile_options_chosen) butt_move_layout = QVBoxLayout() butt_move_layout.addStretch() butt_move_layout.addWidget(self.move_up) butt_move_layout.addWidget(self.move_down) butt_move_layout.addWidget(self.remove_button) butt_move_layout.addStretch() create_layout.addLayout(butt_move_layout) layout.addLayout(create_layout) save_butt_layout = QHBoxLayout() save_butt_layout.addWidget(self.soft_reset_butt) save_butt_layout.addStretch() save_butt_layout.addWidget(self.save_butt) save_butt_layout.addWidget(self.save_butt_with_name) layout.addLayout(save_butt_layout) self.setLayout(layout) for profile in MEASUREMENT_DICT.values(): help_text = profile.get_description() lw = MeasurementListWidgetItem(profile.get_starting_leaf()) lw.setToolTip(help_text) self.profile_options.addItem(lw) self._refresh_profiles() def _refresh_profiles(self): item = self.profile_list.currentItem() items = list(self.settings.measurement_profiles.keys()) try: index = items.index(item.text()) except (ValueError, AttributeError): index = -1 self.profile_list.clear() self.profile_list.addItems(items) self.profile_list.setCurrentRow(index) if self.profile_list.count() == 0: self.export_profiles_butt.setDisabled(True) else: self.export_profiles_butt.setEnabled(True) def remove_element(self): elem = self.profile_options_chosen.currentItem() if elem is None: return index = self.profile_options_chosen.currentRow() self.profile_options_chosen.takeItem(index) if self.profile_options_chosen.count() == 0: self.move_down.setDisabled(True) self.move_up.setDisabled(True) self.remove_button.setDisabled(True) self.discard_butt.setDisabled(True) self.save_butt.setDisabled(True) self.save_butt_with_name.setDisabled(True) def delete_profile(self): item = self.profile_list.currentItem() del self.settings.measurement_profiles[str(item.text())] def profile_chosen(self): self.delete_profile_butt.setEnabled(True) if self.profile_list.count() == 0: self.profile_description.setText("") return item = self.profile_list.currentItem() if item is None: self.profile_description.setText("") return profile = self.settings.measurement_profiles[item.text()] self.profile_description.setText(str(profile)) def create_selection_changed(self): self.choose_butt.setEnabled(True) self.proportion_butt.setEnabled(True) def proportion_action(self): # TODO use get_parameters if self.chosen_element is None: item = self.profile_options.currentItem() self.chosen_element_area = self.get_parameters( deepcopy(item.stat), self.measurement_area_choose.currentEnum(), self.per_component.currentEnum(), self.power_num.value(), ) if self.chosen_element_area is None: return self.chosen_element = item item.setIcon(QIcon(os.path.join(icons_dir, "task-accepted.png"))) elif (self.profile_options.currentItem() == self.chosen_element and self.measurement_area_choose.currentEnum() == self.chosen_element_area.area and self.per_component.currentEnum() == self.chosen_element_area.per_component): self.chosen_element.setIcon(QIcon()) self.chosen_element = None else: item: MeasurementListWidgetItem = self.profile_options.currentItem( ) leaf = self.get_parameters( deepcopy(item.stat), self.measurement_area_choose.currentEnum(), self.per_component.currentEnum(), self.power_num.value(), ) if leaf is None: return lw = MeasurementListWidgetItem( Node(op="/", left=self.chosen_element_area, right=leaf)) lw.setToolTip("User defined") self._add_option(lw) self.chosen_element.setIcon(QIcon()) self.chosen_element = None self.chosen_element_area = None def _add_option(self, item: MeasurementListWidgetItem): for i in range(self.profile_options_chosen.count()): if item.text() == self.profile_options_chosen.item(i).text(): return self.profile_options_chosen.addItem(item) if self.good_name(): self.save_butt.setEnabled(True) self.save_butt_with_name.setEnabled(True) if self.profile_options.count() == 0: self.choose_butt.setDisabled(True) def create_selection_chosen_changed(self): # print(self.profile_options_chosen.count()) self.remove_button.setEnabled(True) if self.profile_options_chosen.count() == 0: self.move_down.setDisabled(True) self.move_up.setDisabled(True) self.remove_button.setDisabled(True) return self.discard_butt.setEnabled(True) if self.profile_options_chosen.currentRow() != 0: self.move_up.setEnabled(True) else: self.move_up.setDisabled(True) if self.profile_options_chosen.currentRow( ) != self.profile_options_chosen.count() - 1: self.move_down.setEnabled(True) else: self.move_down.setDisabled(True) def good_name(self): return str(self.profile_name.text()).strip() != "" def move_down_fun(self): row = self.profile_options_chosen.currentRow() item = self.profile_options_chosen.takeItem(row) self.profile_options_chosen.insertItem(row + 1, item) self.profile_options_chosen.setCurrentRow(row + 1) self.create_selection_chosen_changed() def move_up_fun(self): row = self.profile_options_chosen.currentRow() item = self.profile_options_chosen.takeItem(row) self.profile_options_chosen.insertItem(row - 1, item) self.profile_options_chosen.setCurrentRow(row - 1) self.create_selection_chosen_changed() def name_changed(self): if self.good_name() and self.profile_options_chosen.count() > 0: self.save_butt.setEnabled(True) self.save_butt_with_name.setEnabled(True) else: self.save_butt.setDisabled(True) self.save_butt_with_name.setDisabled(True) def form_dialog(self, arguments): return FormDialog(arguments, settings=self.settings, parent=self) def get_parameters(self, node: Union[Node, Leaf], area: AreaType, component: PerComponent, power: float): if isinstance(node, Node): return node node = node.replace_(power=power) if node.area is None: node = node.replace_(area=area) if node.per_component is None: node = node.replace_(per_component=component) with suppress(KeyError): arguments = MEASUREMENT_DICT[str(node.name)].get_fields() if len(arguments) > 0 and len(node.dict) == 0: dial = self.form_dialog(arguments) if dial.exec_(): node = node._replace(dict=dial.get_values()) else: return return node def choose_option(self): selected_item = self.profile_options.currentItem() # selected_row = self.profile_options.currentRow() if not isinstance(selected_item, MeasurementListWidgetItem): raise ValueError( f"Current item (type: {type(selected_item)} is not instance of MeasurementListWidgetItem" ) node = deepcopy(selected_item.stat) # noinspection PyTypeChecker node = self.get_parameters(node, self.measurement_area_choose.currentEnum(), self.per_component.currentEnum(), self.power_num.value()) if node is None: return lw = MeasurementListWidgetItem(node) lw.setToolTip(selected_item.toolTip()) self._add_option(lw) def discard_option(self): selected_item: MeasurementListWidgetItem = self.profile_options_chosen.currentItem( ) # selected_row = self.profile_options_chosen.currentRow() lw = MeasurementListWidgetItem(deepcopy(selected_item.stat)) lw.setToolTip(selected_item.toolTip()) self.create_selection_chosen_changed() for i in range(self.profile_options.count()): if lw.text() == self.profile_options.item(i).text(): return self.profile_options.addItem(lw) def edit_profile(self): item = self.profile_list.currentItem() if item is None: return profile = self.settings.measurement_profiles[str(item.text())] self.profile_options_chosen.clear() self.profile_name.setText(item.text()) for ch in profile.chosen_fields: self.profile_options_chosen.addItem( MeasurementListWidgetItem(ch.calculation_tree)) # self.gauss_img.setChecked(profile.use_gauss_image) self.save_butt.setEnabled(True) self.save_butt_with_name.setEnabled(True) def save_action(self): if self.profile_name.text() in self.settings.measurement_profiles: ret = QMessageBox.warning( self, "Profile exist", "Profile exist\nWould you like to overwrite it?", QMessageBox.No | QMessageBox.Yes, ) if ret == QMessageBox.No: return selected_values = [] for i in range(self.profile_options_chosen.count()): element: MeasurementListWidgetItem = self.profile_options_chosen.item( i) selected_values.append( MeasurementEntry(element.text(), element.stat)) stat_prof = MeasurementProfile(self.profile_name.text(), selected_values) self.settings.measurement_profiles[stat_prof.name] = stat_prof self.settings.dump() self.export_profiles_butt.setEnabled(True) def named_save_action(self): if self.profile_name.text() in self.settings.measurement_profiles: ret = QMessageBox.warning( self, "Profile exist", "Profile exist\nWould you like to overwrite it?", QMessageBox.No | QMessageBox.Yes, ) if ret == QMessageBox.No: return selected_values = [] for i in range(self.profile_options_chosen.count()): txt = str(self.profile_options_chosen.item(i).text()) selected_values.append((txt, str, txt)) val_dialog = MultipleInput("Set fields name", list(selected_values), parent=self) if val_dialog.exec_(): selected_values = [] for i in range(self.profile_options_chosen.count()): element: MeasurementListWidgetItem = self.profile_options_chosen.item( i) selected_values.append( MeasurementEntry(val_dialog.result[element.text()], element.stat)) stat_prof = MeasurementProfile(self.profile_name.text(), selected_values) self.settings.measurement_profiles[stat_prof.name] = stat_prof self.export_profiles_butt.setEnabled(True) def reset_action(self): self.profile_options.clear() self.profile_options_chosen.clear() self.profile_name.setText("") self.save_butt.setDisabled(True) self.save_butt_with_name.setDisabled(True) self.move_down.setDisabled(True) self.move_up.setDisabled(True) self.proportion_butt.setDisabled(True) self.choose_butt.setDisabled(True) self.discard_butt.setDisabled(True) for profile in MEASUREMENT_DICT.values(): help_text = profile.get_description() lw = MeasurementListWidgetItem(profile.get_starting_leaf()) lw.setToolTip(help_text) self.profile_options.addItem(lw) def soft_reset(self): # TODO rim should not be removed shift = 0 for i in range(self.profile_options.count()): item = self.profile_options.item(i - shift) if str(item.text()) not in MEASUREMENT_DICT: self.profile_options.takeItem(i - shift) if item == self.chosen_element: self.chosen_element = None del item shift += 1 self.create_selection_changed() def export_measurement_profiles(self): exp = ExportDialog(self.settings.measurement_profiles, StringViewer, parent=self) if not exp.exec_(): return dial = PSaveDialog( "Measurement profile (*.json)", settings=self.settings, path="io.export_directory", caption="Export settings profiles", ) dial.selectFile("measurements_profile.json") if dial.exec_(): file_path = str(dial.selectedFiles()[0]) data = { x: self.settings.measurement_profiles[x] for x in exp.get_export_list() } with open(file_path, "w", encoding="utf-8") as ff: json.dump(data, ff, cls=self.settings.json_encoder_class, indent=2) def import_measurement_profiles(self): dial = PLoadDialog( "Measurement profile (*.json)", settings=self.settings, path="io.export_directory", caption="Import settings profiles", parent=self, ) if dial.exec_(): file_path = str(dial.selectedFiles()[0]) stat, err = self.settings.load_part(file_path) if err: QMessageBox.warning( self, "Import error", "error during importing, part of data were filtered.") measurement_dict = self.settings.measurement_profiles imp = ImportDialog(stat, measurement_dict, StringViewer) if not imp.exec_(): return for original_name, final_name in imp.get_import_list(): measurement_dict[final_name] = stat[original_name] self.settings.dump()
class PMGListCtrl(BaseExtendedWidget): def __init__(self, layout_dir: str, title: str, initial_value: List[List[str]], new_id_func: Callable = None): super().__init__(layout_dir) self.choices = [] self.text_list = [] lab_title = QLabel(text=title) layout = QHBoxLayout() self.central_layout.addWidget(lab_title) self.on_check_callback = None self.list_widget = QListWidget() self.list_widget.mouseDoubleClickEvent = self.on_listwidget_double_cicked self.set_value(initial_value) layout_tools = QVBoxLayout() self.button_add_item = QPushButton('+') self.button_delete_item = QPushButton('-') self.button_delete_item.clicked.connect(self.delete_row) self.button_add_item.clicked.connect(self.add_row) self.button_add_item.setMaximumWidth(20) self.button_delete_item.setMaximumWidth(20) layout_tools.addWidget(self.button_add_item) layout_tools.addWidget(self.button_delete_item) layout.addLayout(layout_tools) layout.addWidget(self.list_widget) self.central_layout.addLayout(layout) self.data = initial_value self.new_id_func = new_id_func self.text_edit = QLineEdit(parent=self.list_widget) self.text_edit.setWindowFlags(self.text_edit.windowFlags() | Qt.Dialog | Qt.FramelessWindowHint) self.text_edit.hide() self.completer = QCompleter() self.text_edit.setCompleter(self.completer) self.completer.setCompletionMode(QCompleter.UnfilteredPopupCompletion) def set_completions(self, completions: List[str]): """ 设置补全内容 Args: completions: Returns: """ self.completer.setModel(QStringListModel(completions)) def new_id(self): if callable(self.new_id_func): return self.new_id_func() else: return None def add_row(self): self.data = self.get_value() self.data[0].append(self.new_id()) self.data[1].append('Unnamed') self.list_widget.addItem(QListWidgetItem('Unnamed')) def delete_row(self): index = self.list_widget.currentIndex().row() self.data[0].pop(index) self.data[1].pop(index) self.list_widget.takeItem(index) def on_listwidget_double_cicked(self, evt: QMouseEvent): print('edit', evt) pos = evt.globalPos() current_item: QListWidgetItem = self.list_widget.currentItem() def set_value(): current_item.setText(self.text_edit.text()) self.text_edit.hide() self.text_edit.returnPressed.disconnect(set_value) item: QListWidgetItem = self.list_widget.currentItem() self.text_edit.setGeometry(pos.x(), pos.y(), 200, 20) self.text_edit.returnPressed.connect(set_value) self.text_edit.show() # self.list_widget.editItem(item) def get_value(self): text = [] for i in range(self.list_widget.count()): text.append(self.list_widget.item(i).text()) self.data[1] = text assert len(self.data[1]) == len(self.data[0]), repr(self.data) return self.data def set_value(self, data: List[List[str]]): self.list_widget.clear() self.list_widget.addItems(data[1]) self.data = data for index in range(self.list_widget.count()): item: QListWidgetItem = self.list_widget.item(index)
def _setup_and_check(self, widget, data, title, readonly, **kwargs): """ Setup SessionComparator. Parameters ---------- widget: QWidget Parent widget. data: list or tuple of Session Sessions to compare. title: str Title. readonly: bool kwargs: * rtol: int or float * atol: int or float * nans_equal: bool * bg_gradient: str * names: list of str * colors: str """ sessions = data names = kwargs.get( 'names', ["Session{}".format(i) for i in range(len(sessions))]) assert all(isinstance(s, la.Session) for s in sessions) self.sessions = sessions self.stack_axis = la.Axis(names, 'session') layout = QVBoxLayout() widget.setLayout(layout) array_names = sorted( set.union(*[ set(s.filter(kind=DISPLAY_IN_GRID).names) for s in self.sessions ])) listwidget = QListWidget(self) listwidget.addItems(array_names) listwidget.currentItemChanged.connect(self.on_item_changed) for i, name in enumerate(array_names): arrays = self.get_arrays(name) first_array = arrays[0] if not all( a.equals(first_array, nans_equal=True) for a in arrays[1:]): listwidget.item(i).setForeground(Qt.red) self.listwidget = listwidget comparatorwidget = ComparatorWidget(self, **kwargs) self.arraywidget = comparatorwidget main_splitter = QSplitter(Qt.Horizontal) main_splitter.addWidget(listwidget) main_splitter.addWidget(comparatorwidget) main_splitter.setSizes([5, 95]) main_splitter.setCollapsible(1, False) self.widget_state_settings['main_splitter'] = main_splitter layout.addWidget(main_splitter) self.listwidget.setCurrentRow(0)
def refresh_profiles(list_widget: QListWidget, new_values: typing.List[str], index: int): list_widget.clear() list_widget.addItems(new_values) if index != -1: list_widget.setCurrentRow(index)
class CalculateInfo(QWidget): """ "widget to show information about plans and allow to se plan details :type settings: Settings """ plan_to_edit_signal = Signal() def __init__(self, settings: PartSettings): super(CalculateInfo, self).__init__() self.settings = settings self.calculate_plans = QListWidget(self) self.plan_view = PlanPreview(self) self.delete_plan_btn = QPushButton("Delete") self.edit_plan_btn = QPushButton("Edit") self.export_plans_btn = QPushButton("Export") self.import_plans_btn = QPushButton("Import") info_layout = QVBoxLayout() info_butt_layout = QGridLayout() info_butt_layout.setSpacing(0) info_butt_layout.addWidget(self.delete_plan_btn, 1, 1) info_butt_layout.addWidget(self.edit_plan_btn, 0, 1) info_butt_layout.addWidget(self.export_plans_btn, 1, 0) info_butt_layout.addWidget(self.import_plans_btn, 0, 0) info_layout.addLayout(info_butt_layout) info_chose_layout = QVBoxLayout() info_chose_layout.setSpacing(2) info_chose_layout.addWidget(QLabel("List of workflows:")) info_chose_layout.addWidget(self.calculate_plans) info_chose_layout.addWidget(QLabel("Preview:")) info_chose_layout.addWidget(self.plan_view) info_layout.addLayout(info_chose_layout) self.setLayout(info_layout) self.calculate_plans.addItems( list(sorted(self.settings.batch_plans.keys()))) self.protect = False self.plan_to_edit = None self.plan_view.header().close() self.calculate_plans.currentTextChanged.connect(self.plan_preview) self.delete_plan_btn.clicked.connect(self.delete_plan) self.edit_plan_btn.clicked.connect(self.edit_plan) self.export_plans_btn.clicked.connect(self.export_plans) self.import_plans_btn.clicked.connect(self.import_plans) def update_plan_list(self): new_plan_list = list(sorted(self.settings.batch_plans.keys())) if self.calculate_plans.currentItem() is not None: text = str(self.calculate_plans.currentItem().text()) try: index = new_plan_list.index(text) except ValueError: index = -1 else: index = -1 self.protect = True self.calculate_plans.clear() self.calculate_plans.addItems(new_plan_list) if index != -1: self.calculate_plans.setCurrentRow(index) else: pass # self.plan_view.setText("") self.protect = False def export_plans(self): choose = ExportDialog(self.settings.batch_plans, PlanPreview) if not choose.exec_(): return dial = QFileDialog(self, "Export calculation plans") dial.setFileMode(QFileDialog.AnyFile) dial.setAcceptMode(QFileDialog.AcceptSave) dial.setDirectory( dial.setDirectory( self.settings.get("io.batch_plan_directory", str(Path.home())))) dial.setNameFilter("Calculation plans (*.json)") dial.setDefaultSuffix("json") dial.selectFile("calculation_plans.json") dial.setHistory(dial.history() + self.settings.get_path_history()) if dial.exec_(): file_path = str(dial.selectedFiles()[0]) self.settings.set("io.batch_plan_directory", os.path.dirname(file_path)) self.settings.add_path_history(os.path.dirname(file_path)) data = { x: self.settings.batch_plans[x] for x in choose.get_export_list() } with open(file_path, "w") as ff: json.dump(data, ff, cls=self.settings.json_encoder_class, indent=2) def import_plans(self): dial = QFileDialog(self, "Import calculation plans") dial.setFileMode(QFileDialog.ExistingFile) dial.setAcceptMode(QFileDialog.AcceptOpen) dial.setDirectory( self.settings.get("io.open_directory", str(Path.home()))) dial.setNameFilter("Calculation plans (*.json)") dial.setDefaultSuffix("json") dial.setHistory(dial.history() + self.settings.get_path_history()) if dial.exec_(): file_path = dial.selectedFiles()[0] plans, err = self.settings.load_part(file_path) self.settings.set("io.batch_plan_directory", os.path.dirname(file_path)) self.settings.add_path_history(os.path.dirname(file_path)) if err: QMessageBox.warning( self, "Import error", "error during importing, part of data were filtered.") choose = ImportDialog(plans, self.settings.batch_plans, PlanPreview) if choose.exec_(): for original_name, final_name in choose.get_import_list(): self.settings.batch_plans[final_name] = plans[ original_name] self.update_plan_list() def delete_plan(self): if self.calculate_plans.currentItem() is None: return text = str(self.calculate_plans.currentItem().text()) if text == "": return if text in self.settings.batch_plans: del self.settings.batch_plans[text] self.update_plan_list() self.plan_view.clear() def edit_plan(self): if self.calculate_plans.currentItem() is None: return text = str(self.calculate_plans.currentItem().text()) if text == "": return if text in self.settings.batch_plans: self.plan_to_edit = self.settings.batch_plans[text] self.plan_to_edit_signal.emit() def plan_preview(self, text): if self.protect: return text = str(text) if text.strip() == "": return plan = self.settings.batch_plans[str(text)] # type: CalculationPlan self.plan_view.set_plan(plan)
class SelectFromListDialog(QDialog): """Dialog box for selecting multiple items. The original order of items are preserved in the candidate list. """ def __init__(self, parent=None, allItems=(), selectedItems=()): QDialog.__init__( self, parent, flags=Qt.WindowSystemMenuHint | Qt.WindowTitleHint) self.allItems = allItems self.fromKeys = list(range(len(allItems))) self.selectedKeys = [] for item in selectedItems: key = allItems.index(item) self.fromKeys.remove(key) self.selectedKeys.append(key) self.setAttribute(Qt.WA_DeleteOnClose) fromLabel = QLabel(_("Select from")) self.fromList = QListWidget(self) self.fromList.addItems(allItems[key] for key in self.fromKeys) selectedLabel = QLabel(_("Selected")) self.selectedList = QListWidget(self) self.selectedList.addItems(allItems[key] for key in self.selectedKeys) self.selectButton = QPushButton(_("Select")) self.deselectButton = QPushButton(_("Deselect")) self.selectBox = QDialogButtonBox(Qt.Vertical) self.selectBox.addButton(self.selectButton, QDialogButtonBox.ActionRole) self.selectBox.addButton(self.deselectButton, QDialogButtonBox.ActionRole) self.selectButton.clicked.connect(self.on_select) self.deselectButton.clicked.connect(self.on_deselect) self.buttonBox = QDialogButtonBox(self) self.buttonBox.setOrientation(Qt.Horizontal) self.buttonBox.setStandardButtons( QDialogButtonBox.Cancel|QDialogButtonBox.Ok) self.buttonBox.accepted.connect(self.accept) self.buttonBox.rejected.connect(self.reject) mainLayout = QGridLayout(self) mainLayout.addWidget(fromLabel, 0, 0) mainLayout.addWidget(selectedLabel, 0, 2) mainLayout.addWidget(self.fromList, 1, 0) mainLayout.addWidget(self.selectBox, 1, 1) mainLayout.addWidget(self.selectedList, 1, 2) mainLayout.addWidget(self.buttonBox, 2, 0, 1, 3) mainLayout.setAlignment(self.selectBox, Qt.AlignCenter) self.setLayout(mainLayout) def on_select(self): if len(self.fromList.selectedItems()): idx = self.fromList.currentRow() key = self.fromKeys.pop(idx) self.selectedKeys.append(key) item = self.fromList.takeItem(idx) self.selectedList.addItem(item) def on_deselect(self): if len(self.selectedList.selectedItems()): idx = self.selectedList.currentRow() item = self.selectedList.takeItem(idx) key = self.selectedKeys.pop(idx) idx = next((i for i, v in enumerate(self.fromKeys) if v > key), len(self.fromKeys)) self.fromKeys.insert(idx, key) self.fromList.insertItem(idx, item)
class GroupsModify(PyDialog): """ +-------------------------------+ | Groups : Modify | +-------------------------------+ | | | Name xxx Default | | Nodes xxx Default Show | | Color xxx Default | | Add xxx Add Show | | Remove xxx Remove Show | | | | Set OK Cancel | +-------------------------------+ """ def __init__(self, data, win_parent=None, model_name=None, group_active='main'): super(GroupsModify, self).__init__(data, win_parent) self.set_font_size(data['font_size']) self._updated_groups = False self.model_name = model_name self.actors = [] #self.out_data = data #print(data) self.keys = get_groups_sorted_by_name(data) self.active_key = self.keys.index(group_active) group_obj = data[self.active_key] unused_name = group_obj.name self.imain = 0 self.nrows = len(self.keys) self._default_name = group_obj.name self._default_elements = group_obj.element_str self.elements_pound = group_obj.elements_pound self.table = QListWidget(parent=None) self.table.clear() self.table.addItems(self.keys) self.setWindowTitle('Groups: Modify') self.create_widgets() self.create_layout() self.set_connections() self.on_set_as_main() def create_widgets(self): """creates the menu objects""" #icon = QtGui.QPixmap(os.path.join(ICON_PATH, 'node.png')) #icon = QtGui.QPixmap(os.path.join(ICON_PATH, 'element.png')) # Name self.pick_style_label = QLabel('Pick Style:') #self.pick_node_button = QPushButton('Node') self.pick_element_button = QPushButton('Element') self.pick_area_button = QPushButton('Area') #self.pick_node_button.setIcon(icon) #self.pick_area_button.setIcon(icon) # Name self.name_label = QLabel('Name:') self.name_set = QPushButton('Set') self.name_edit = QLineEdit(str(self._default_name).strip()) self.name_button = QPushButton('Default') # elements self.elements_label = QLabel('Element IDs:') self.elements_edit = QLineEdit(str(self._default_elements).strip()) self.elements_button = QPushButton('Default') self.elements_highlight_button = QPushButton('Show') # add self.add_label = QLabel('Add Elements:') self.add_edit = QElementEdit(self, self.model_name, pick_style='area') self.add_button = QPushButton('Add') self.add_highlight_button = QPushButton('Show') # remove self.remove_label = QLabel('Remove Elements:') self.remove_edit = QElementEdit(self, self.model_name, pick_style='area') self.remove_button = QPushButton('Remove') self.remove_highlight_button = QPushButton('Show') # applies a unique implicitly self.eids = parse_patran_syntax(str(self._default_elements), pound=self.elements_pound) # closing #self.apply_button = QPushButton('Apply') self.ok_button = QPushButton('Close') #self.cancel_button = QPushButton('Cancel') self.set_as_main_button = QPushButton('Set As Main') self.create_group_button = QPushButton('Create New Group') self.delete_group_button = QPushButton('Delete Group') self.name_label.setEnabled(False) self.name_set.setEnabled(False) self.name_edit.setEnabled(False) self.name_button.setEnabled(False) self.elements_label.setEnabled(False) self.elements_button.setEnabled(False) self.elements_edit.setEnabled(False) self.elements_highlight_button.setEnabled(False) self.add_label.setEnabled(False) self.add_button.setEnabled(False) self.add_edit.setEnabled(False) self.add_highlight_button.setEnabled(False) self.remove_label.setEnabled(False) self.remove_button.setEnabled(False) self.remove_edit.setEnabled(False) self.remove_highlight_button.setEnabled(False) #self.apply_button.setEnabled(False) #self.ok_button.setEnabled(False) def create_layout(self): """displays the menu objects""" hbox = QHBoxLayout() hbox.addWidget(self.pick_style_label) hbox.addWidget(self.pick_element_button) hbox.addWidget(self.pick_area_button) hbox.addStretch() grid = QGridLayout() irow = 0 grid.addWidget(self.name_label, irow, 0) grid.addWidget(self.name_edit, irow, 1) grid.addWidget(self.name_set, irow, 2) grid.addWidget(self.name_button, irow, 3) irow += 1 grid.addWidget(self.elements_label, irow, 0) grid.addWidget(self.elements_edit, irow, 1) grid.addWidget(self.elements_button, irow, 2) grid.addWidget(self.elements_highlight_button, irow, 3) irow += 1 grid.addWidget(self.add_label, irow, 0) grid.addWidget(self.add_edit, irow, 1) grid.addWidget(self.add_button, irow, 2) grid.addWidget(self.add_highlight_button, irow, 3) irow += 1 grid.addWidget(self.remove_label, irow, 0) grid.addWidget(self.remove_edit, irow, 1) grid.addWidget(self.remove_button, irow, 2) grid.addWidget(self.remove_highlight_button, irow, 3) irow += 1 ok_cancel_box = QHBoxLayout() #ok_cancel_box.addWidget(self.apply_button) ok_cancel_box.addWidget(self.ok_button) #ok_cancel_box.addWidget(self.cancel_button) main_create_delete = QHBoxLayout() main_create_delete.addWidget(self.set_as_main_button) main_create_delete.addWidget(self.create_group_button) main_create_delete.addWidget(self.delete_group_button) vbox = QVBoxLayout() vbox.addWidget(self.table) vbox.addLayout(hbox) vbox.addLayout(grid) vbox.addLayout(main_create_delete) vbox.addStretch() vbox.addLayout(ok_cancel_box) self.setLayout(vbox) def on_set_name(self): self.remove_highlight_actor() name = str(self.name_edit.text()).strip() if name not in self.keys: self.name_edit.setStyleSheet("QLineEdit{background: white;}") group = self.out_data[self.active_key] group.name = name self.keys[self.active_key] = name self.recreate_table() elif name != self.keys[self.active_key]: self.name_edit.setStyleSheet('QLineEdit{background: red;}') elif name == self.keys[self.active_key]: self.name_edit.setStyleSheet('QLineEdit{background: white;}') def on_pick_element(self): self.add_edit.pick_style = 'single' self.remove_edit.pick_style = 'single' def on_pick_area(self): self.add_edit.pick_style = 'area' self.remove_edit.pick_style = 'area' def set_connections(self): """creates the actions for the menu""" self.pick_element_button.clicked.connect(self.on_pick_element) self.pick_area_button.clicked.connect(self.on_pick_area) self.name_set.clicked.connect(self.on_set_name) self.name_button.clicked.connect(self.on_default_name) self.elements_button.clicked.connect(self.on_default_elements) self.elements_highlight_button.clicked.connect( self.on_highlight_elements) self.add_button.clicked.connect(self.on_add) self.add_highlight_button.clicked.connect(self.on_highlight_add) self.remove_button.clicked.connect(self.on_remove) self.remove_highlight_button.clicked.connect(self.on_highlight_remove) self.table.itemClicked.connect(self.on_update_active_key) self.ok_button.clicked.connect(self.on_ok) self.set_as_main_button.clicked.connect(self.on_set_as_main) self.create_group_button.clicked.connect(self.on_create_group) self.delete_group_button.clicked.connect(self.on_delete_group) def on_create_group(self): self.remove_highlight_actor() irow = self.nrows new_key = f'Group {irow:d}' while new_key in self.keys: irow += 1 new_key = f'Group {irow:d}' irow = self.nrows self.keys.append(new_key) group = Group(new_key, element_str='', elements_pound=self.elements_pound, editable=True) self.out_data[irow] = group self.table.reset() self.table.addItems(self.keys) self.nrows += 1 #---------------------------------- # update internal parameters #self.out_data = items if self.imain > self.active_key: self.imain += 1 #make the new group the default self.active_key = self.nrows - 1 self.keys = get_groups_sorted_by_name(self.out_data) self.recreate_table() def recreate_table(self): # update gui self.table.clear() self.table.addItems(self.keys) item = self.table.item(self.imain) bold = QtGui.QFont() bold.setBold(True) bold.setItalic(True) item.setFont(bold) self.table.update() # update key name = self.keys[self.active_key] self._update_active_key_by_name(name) def on_delete_group(self): self.remove_highlight_actor() if self.active_key == 0: return #self.deleted_groups.add(self.imain) items = {} j = 0 for i, key in sorted(self.out_data.items()): if isinstance(i, int): continue if i != self.active_key: items[j] = key j += 1 # update internal parameters self.out_data = items if self.imain >= self.active_key: self.imain = max(0, self.imain - 1) self.active_key = max(0, self.active_key - 1) self.nrows -= 1 self.keys = [group.name for key, group in sorted(items.items())] self.recreate_table() # update key name = self.keys[self.active_key] self._update_active_key_by_name(name) def on_set_as_main(self): bold = QtGui.QFont() bold.setBold(True) bold.setItalic(True) normal = QtGui.QFont() normal.setBold(False) normal.setItalic(False) obj = self.table.item(self.imain) obj.setFont(normal) self.imain = self.active_key obj = self.table.item(self.imain) obj.setFont(bold) group = self.out_data[self.imain] self._default_elements = group.element_str self._default_name = group.name self.on_update_main() def on_update_main(self): """adds/removes the elements to the main actor when add/remove is pressed""" group = self.out_data[self.imain] if self._default_name == group.name and self.win_parent is not None: # we're not testing the menu self.win_parent.post_group(group, update_groups=True) def closeEvent(self, event): """closes the window""" self.remove_highlight_actor() self.out_data['close'] = True event.accept() def remove_highlight_actor(self): """removes the highlighted actor""" gui = self.win_parent remove_actors_from_gui(gui, self.actors, render=True) self.actors = [] def on_highlight(self, nids=None, eids=None): """highlights the nodes""" gui = self.win_parent unused_name = self.keys[self.active_key] if gui is not None: self.remove_highlight_actor() ## TODO: super strange; doesn't work... mouse_actions = gui.mouse_actions grid = mouse_actions.get_grid_selected(self.model_name) #all_nodes = mouse_actions.node_ids all_elements = mouse_actions.element_ids actors = create_highlighted_actors(gui, grid, all_nodes=None, nodes=nids, all_elements=all_elements, elements=eids, add_actors=False) if actors: add_actors_to_gui(gui, actors, render=True) self.actors = actors def on_highlight_elements(self): """highlights the active elements""" self.on_highlight(eids=self.eids) def on_highlight_add(self): """highlights the elements to add""" eids, is_valid = check_patran_syntax(self.add_edit, pound=self.elements_pound) if not is_valid: #self.add_edit.setStyleSheet("QLineEdit{background: red;}") return self.on_highlight(eids=eids) def on_highlight_remove(self): """highlights the elements to remove""" eids, is_valid = check_patran_syntax(self.remove_edit) if not is_valid: #self.remove_edit.setStyleSheet("QLineEdit{background: red;}") return self.on_highlight(eids=eids) def on_add(self): self.remove_highlight_actor() eids, is_valid = check_patran_syntax(self.add_edit, pound=self.elements_pound) #adict, is_valid = check_patran_syntax_dict(self.add_edit) if not is_valid: #self.add_edit.setStyleSheet("QLineEdit{background: red;}") return self.eids = unique(hstack([self.eids, eids])) #self.eids = _add(adict, ['e', 'elem', 'element'], self.eids) #self.cids = _add(adict, ['c', 'cid', 'coord'], self.cids) self._apply_cids_eids() self.add_edit.clear() self.add_edit.setStyleSheet("QLineEdit{background: white;}") self.on_update_main() def _apply_cids_eids(self): #ctext = _get_collapsed_text(self.cids) etext = _get_collapsed_text(self.eids) #self.coords_edit.setText(str(ctext.lstrip())) self.elements_edit.setText(str(etext.lstrip())) self.out_data[self.active_key].element_ids = self.eids def on_remove(self): self.remove_highlight_actor() eids, is_valid = check_patran_syntax(self.remove_edit) #adict, is_valid = check_patran_syntax_dict(self.remove_edit) if not is_valid: #self.remove_edit.setStyleSheet("QLineEdit{background: red;}") return #self.eids = _remove(adict, ['e', 'elem', 'element'], self.eids) #self.cids = _remove(adict, ['c', 'cid', 'coord'], self.cids) self.eids = setdiff1d(self.eids, eids) self._apply_cids_eids() self.remove_edit.clear() self.remove_edit.setStyleSheet(QLINE_EDIT_BASIC) self.on_update_main() def on_default_name(self): self.remove_highlight_actor() name = str(self._default_name) self.name_edit.setText(name) self.name_edit.setStyleSheet("QLineEdit{background: white;}") def on_default_elements(self): self.remove_highlight_actor() element_str = str(self._default_elements) self.elements_edit.setText(element_str) self.elements_edit.setStyleSheet("QLineEdit{background: white;}") group = self.out_data[self.active_key] group.element_str = element_str def check_name(self, cell): cell_value = cell.text() try: text = str(cell_value).strip() except UnicodeEncodeError: cell.setStyleSheet("QLineEdit{background: red;}") return None, False if len(text): cell.setStyleSheet("QLineEdit{background: white;}") return text, True else: cell.setStyleSheet("QLineEdit{background: red;}") return None, False if self._default_name != text: if self._default_name in self.out_data: cell.setStyleSheet("QLineEdit{background: white;}") return text, True else: cell.setStyleSheet("QLineEdit{background: red;}") return None, False def on_validate(self): name, flag0 = self.check_name(self.name_edit) unused_elements, flag1 = check_patran_syntax(self.elements_edit, pound=self.elements_pound) #coords_value, flag2 = check_patran_syntax( #self.coords_edit, pound=self.coords_pound) if all([flag0, flag1]): self._default_name = name self._default_elements = self.eids self.out_data['clicked_ok'] = True self.out_data['close'] = True return True return False def on_apply(self, force=False): passed = self.on_validate() if passed or force: self.win_parent._apply_modify_groups(self.out_data) return passed def on_ok(self): passed = self.on_apply() if passed: self.close() #self.destroy() def on_cancel(self): self.remove_highlight_actor() self.out_data['close'] = True self.close() def on_update_active_key(self, index): self.update_active_key(index) #str(index.text()) def update_active_key(self, index): #old_obj = self.out_data[self.imain] name = str(index.text()) self._update_active_key_by_name(name) def _update_active_key_by_name(self, name): if name in self.keys: self.active_key = self.keys.index(name) else: # we (hopefully) just removed a row #self.active_key = self.keys[self.active_key] pass self.name_edit.setText(name) obj = self.out_data[self.active_key] self.eids = parse_patran_syntax(obj.element_str, pound=obj.elements_pound) self._default_elements = obj.element_str self._default_name = name self._apply_cids_eids() self.set_as_main_button.setEnabled(True) if name in ['main', 'anti-main']: enabled = False self.elements_edit.setEnabled(False) if name == 'anti-main': self.set_as_main_button.setEnabled(False) #self.apply_button.setEnabled(False) #self.ok_button.setEnabled(False) else: enabled = True self.elements_highlight_button.setEnabled(True) self.add_label.setEnabled(True) self.add_highlight_button.setEnabled(True) self.remove_highlight_button.setEnabled(True) #self.apply_button.setEnabled(True) #self.ok_button.setEnabled(True) self.name_label.setEnabled(enabled) self.name_set.setEnabled(enabled) self.name_edit.setEnabled(enabled) self.name_button.setEnabled(enabled) self.elements_label.setEnabled(enabled) self.elements_button.setEnabled(enabled) self.add_button.setEnabled(enabled) self.add_edit.setEnabled(enabled) self.remove_label.setEnabled(enabled) self.remove_button.setEnabled(enabled) self.remove_edit.setEnabled(enabled) self.delete_group_button.setEnabled(enabled)
class Devices(QFrame): def __init__(self, *args, **kwargs): super(Devices, self).__init__(*args, **kwargs) self.setFrameStyle(QFrame.Panel | QFrame.Raised) self.contentLayout = QGridLayout() self.devices = [] # Current Action Status self.status = {} self.devicePrefixFilterLabel = QLabel( "Device filter (Ion Pump not the channel!)") self.devicePrefixFilterInp = QLineEdit() self.devicePrefixFilterInp.setMaximumWidth(100) self.updateDeviceListButton = QPushButton("Apply Filter") self.updateDeviceListButton.clicked.connect(self.updateDeviceList) self.updateDeviceListButton.setToolTip( "Filter the device prefix list.") self.contentLayout.addWidget(self.devicePrefixFilterLabel, 0, 0, 1, 2) self.contentLayout.addWidget(self.devicePrefixFilterInp, 1, 0, 1, 1) self.contentLayout.addWidget(self.updateDeviceListButton, 1, 1, 1, 1) self.deviceList = QListWidget() self.contentLayout.addWidget(self.deviceList, 2, 0, 2, 2) self.deviceStatus = QTableWidget() self.deviceStatus.setColumnCount(2) self.deviceStatus.horizontalHeader().setSectionResizeMode( QHeaderView.Stretch) self.deviceStatus.setHorizontalHeaderLabels(["Device", "Status"]) self.deviceStatusLabel = QLabel("Status") self.contentLayout.addWidget(self.deviceStatusLabel, 0, 2, 1, 2) self.contentLayout.addWidget(self.deviceStatus, 1, 2, 4, 2) self.contentLayout.setRowStretch(2, 2) self.setLayout(self.contentLayout) self.deviceList.itemChanged.connect(self.highlightChecked) self.reloadData() def updateStatus(self, param): self.status[param["dev"]] = "{}".format(param["status"]) idx = 0 self.deviceStatus.setRowCount(len(self.status)) for k, v in self.status.items(): self.deviceStatus.setItem(idx, 0, QTableWidgetItem(k)) self.deviceStatus.setItem(idx, 1, QTableWidgetItem(v)) idx += 1 def getSelectedDevices(self): checked_devices = [] count = 0 while count < self.deviceList.count(): item = self.deviceList.item(count) if item.checkState() == Qt.Checked: checked_devices.append(item.text()) count += 1 _devices = [] for prefix in checked_devices: for device in self.devices: if device["prefix"] == prefix: _devices.append(device) break return _devices def highlightChecked(self, item): if item.checkState() == Qt.Checked: item.setBackground(QColor("#ffffb2")) else: item.setBackground(QColor("#ffffff")) def updateDeviceList(self): self.deviceList.clear() _filter = self.devicePrefixFilterInp.text() devs = [] for d in self.devices: if _filter == "" or _filter in d["prefix"]: devs.append(d["prefix"]) self.deviceList.addItems(devs) count = 0 while count < self.deviceList.count(): item = self.deviceList.item(count) item.setFlags(item.flags() | Qt.ItemIsUserCheckable) item.setCheckState(Qt.Unchecked) count += 1 def reloadData(self): data = getAgilent() self.devices = [d for d in getDevices(data)] self.updateDeviceList()
class GroupsModify(PyDialog): """ +--------------------------+ | Groups : Modify | +--------------------------+ | | | Name xxx Default | | Coords xxx Default | | Elements xxx Default | | Color xxx Default | | Add xxx Add | | Remove xxx Remove | | | | Set OK Cancel | +--------------------------+ """ def __init__(self, data, win_parent=None, group_active='main'): PyDialog.__init__(self, data, win_parent) self.set_font_size(data['font_size']) self._updated_groups = False #self.out_data = data #print(data) keys = [] self.keys = [ group.name for key, group in sorted(iteritems(data)) if isinstance(key, int) ] self.active_key = self.keys.index(group_active) group_obj = data[self.active_key] name = group_obj.name self.imain = 0 self.nrows = len(self.keys) self._default_name = group_obj.name self._default_elements = group_obj.element_str self.elements_pound = group_obj.elements_pound self.table = QListWidget(parent=None) self.table.clear() self.table.addItems(self.keys) self.setWindowTitle('Groups: Modify') self.create_widgets() self.create_layout() self.set_connections() self.on_set_as_main() def create_widgets(self): """creates the menu objects""" # Name self.name = QLabel("Name:") self.name_set = QPushButton("Set") self.name_edit = QLineEdit(str(self._default_name).strip()) self.name_button = QPushButton("Default") # elements self.elements = QLabel("Element IDs:") self.elements_edit = QLineEdit(str(self._default_elements).strip()) self.elements_button = QPushButton("Default") # add self.add = QLabel("Add Elements:") self.add_edit = QElementEdit(self, str('')) self.add_button = QPushButton("Add") # remove self.remove = QLabel("Remove Elements:") self.remove_edit = QElementEdit(self, str('')) self.remove_button = QPushButton("Remove") # applies a unique implicitly self.eids = parse_patran_syntax(str(self._default_elements), pound=self.elements_pound) # closing #self.apply_button = QPushButton("Apply") self.ok_button = QPushButton("Close") #self.cancel_button = QPushButton("Cancel") self.set_as_main_button = QPushButton("Set As Main") self.create_group_button = QPushButton('Create New Group') self.delete_group_button = QPushButton('Delete Group') self.name.setEnabled(False) self.name_set.setEnabled(False) self.name_edit.setEnabled(False) self.name_button.setEnabled(False) self.elements.setEnabled(False) self.elements_button.setEnabled(False) self.elements_edit.setEnabled(False) self.add.setEnabled(False) self.add_button.setEnabled(False) self.add_edit.setEnabled(False) self.remove.setEnabled(False) self.remove_button.setEnabled(False) self.remove_edit.setEnabled(False) self.delete_group_button.setEnabled(False) #self.apply_button.setEnabled(False) #self.ok_button.setEnabled(False) def create_layout(self): """displays the menu objects""" grid = QGridLayout() grid.addWidget(self.name, 0, 0) grid.addWidget(self.name_edit, 0, 1) grid.addWidget(self.name_set, 0, 2) grid.addWidget(self.name_button, 0, 3) grid.addWidget(self.elements, 2, 0) grid.addWidget(self.elements_edit, 2, 1) grid.addWidget(self.elements_button, 2, 2) grid.addWidget(self.add, 4, 0) grid.addWidget(self.add_edit, 4, 1) grid.addWidget(self.add_button, 4, 2) grid.addWidget(self.remove, 5, 0) grid.addWidget(self.remove_edit, 5, 1) grid.addWidget(self.remove_button, 5, 2) ok_cancel_box = QHBoxLayout() #ok_cancel_box.addWidget(self.apply_button) ok_cancel_box.addWidget(self.ok_button) #ok_cancel_box.addWidget(self.cancel_button) main_create_delete = QHBoxLayout() main_create_delete.addWidget(self.set_as_main_button) main_create_delete.addWidget(self.create_group_button) main_create_delete.addWidget(self.delete_group_button) vbox = QVBoxLayout() vbox.addWidget(self.table) vbox.addLayout(grid) vbox.addLayout(main_create_delete) vbox.addStretch() vbox.addLayout(ok_cancel_box) self.setLayout(vbox) def on_set_name(self): name = str(self.name_edit.text()).strip() if name not in self.keys: self.name_edit.setStyleSheet("QLineEdit{background: white;}") group = self.out_data[self.active_key] group.name = name self.keys[self.active_key] = name self.recreate_table() elif name != self.keys[self.active_key]: self.name_edit.setStyleSheet("QLineEdit{background: red;}") elif name == self.keys[self.active_key]: self.name_edit.setStyleSheet("QLineEdit{background: white;}") def set_connections(self): self.name_set.clicked.connect(self.on_set_name) self.name_button.clicked.connect(self.on_default_name) self.elements_button.clicked.connect(self.on_default_elements) self.add_button.clicked.connect(self.on_add) self.remove_button.clicked.connect(self.on_remove) self.table.itemClicked.connect(self.on_update_active_key) self.ok_button.clicked.connect(self.on_ok) self.set_as_main_button.clicked.connect(self.on_set_as_main) self.create_group_button.clicked.connect(self.on_create_group) self.delete_group_button.clicked.connect(self.on_delete_group) def on_create_group(self): irow = self.nrows new_key = 'Group %s' % irow while new_key in self.keys: irow += 1 new_key = 'Group %s' % irow irow = self.nrows self.keys.append(new_key) group = Group(new_key, element_str='', elements_pound=self.elements_pound, editable=True) self.out_data[irow] = group self.table.reset() self.table.addItems(self.keys) self.nrows += 1 #---------------------------------- # update internal parameters #self.out_data = items if self.imain > self.active_key: self.imain += 1 #make the new group the default self.active_key = self.nrows - 1 self.keys = [ group.name for key, group in sorted(iteritems(self.out_data)) if isinstance(key, int) ] self.recreate_table() def recreate_table(self): # update gui self.table.clear() self.table.addItems(self.keys) item = self.table.item(self.imain) bold = QtGui.QFont() bold.setBold(True) bold.setItalic(True) item.setFont(bold) self.table.update() # update key name = self.keys[self.active_key] self._update_active_key_by_name(name) def on_delete_group(self): if self.active_key == 0: return #self.deleted_groups.add(self.imain) items = {} j = 0 for i, key in sorted(iteritems(self.out_data)): if isinstance(i, int): continue if i != self.active_key: items[j] = key j += 1 # update internal parameters self.out_data = items if self.imain >= self.active_key: self.imain = max(0, self.imain - 1) self.active_key = max(0, self.active_key - 1) self.nrows -= 1 self.keys = [group.name for key, group in sorted(iteritems(items))] self.recreate_table() # update key name = self.keys[self.active_key] self._update_active_key_by_name(name) def on_set_as_main(self): bold = QtGui.QFont() bold.setBold(True) bold.setItalic(True) normal = QtGui.QFont() normal.setBold(False) normal.setItalic(False) obj = self.table.item(self.imain) obj.setFont(normal) self.imain = self.active_key obj = self.table.item(self.imain) obj.setFont(bold) group = self.out_data[self.imain] self._default_elements = group.element_str self._default_name = group.name self.on_update_main() def on_update_main(self): """adds/removes the elements to the main actor when add/remove is pressed""" group = self.out_data[self.imain] if self._default_name == group.name and self.win_parent is not None: # we're not testing the menu self.win_parent.post_group(group) def closeEvent(self, event): self.out_data['close'] = True event.accept() def on_add(self): eids, is_valid = self.check_patran_syntax(self.add_edit, pound=self.elements_pound) #adict, is_valid = self.check_patran_syntax_dict(self.add_edit) if not is_valid: #self.add_edit.setStyleSheet("QLineEdit{background: red;}") return self.eids = unique(hstack([self.eids, eids])) #self.eids = _add(adict, ['e', 'elem', 'element'], self.eids) #self.cids = _add(adict, ['c', 'cid', 'coord'], self.cids) self._apply_cids_eids() self.add_edit.clear() self.add_edit.setStyleSheet("QLineEdit{background: white;}") self.on_update_main() def _apply_cids_eids(self): #ctext = _get_collapsed_text(self.cids) etext = _get_collapsed_text(self.eids) #self.coords_edit.setText(str(ctext.lstrip())) self.elements_edit.setText(str(etext.lstrip())) self.out_data[self.active_key].element_ids = self.eids def on_remove(self): eids, is_valid = self.check_patran_syntax(self.remove_edit) #adict, is_valid = self.check_patran_syntax_dict(self.remove_edit) if not is_valid: #self.remove_edit.setStyleSheet("QLineEdit{background: red;}") return #self.eids = _remove(adict, ['e', 'elem', 'element'], self.eids) #self.cids = _remove(adict, ['c', 'cid', 'coord'], self.cids) self.eids = setdiff1d(self.eids, eids) self._apply_cids_eids() self.remove_edit.clear() self.remove_edit.setStyleSheet("QLineEdit{background: white;}") self.on_update_main() def on_default_name(self): name = str(self._default_name) self.name_edit.setText(name) self.name_edit.setStyleSheet("QLineEdit{background: white;}") def on_default_elements(self): element_str = str(self._default_elements) self.elements_edit.setText(element_str) self.elements_edit.setStyleSheet("QLineEdit{background: white;}") group = self.out_data[self.active_key] group.element_str = element_str def check_name(self, cell): cell_value = cell.text() try: text = str(cell_value).strip() except UnicodeEncodeError: cell.setStyleSheet("QLineEdit{background: red;}") return None, False if len(text): cell.setStyleSheet("QLineEdit{background: white;}") return text, True else: cell.setStyleSheet("QLineEdit{background: red;}") return None, False if self._default_name != text: if self._default_name in self.out_data: cell.setStyleSheet("QLineEdit{background: white;}") return text, True else: cell.setStyleSheet("QLineEdit{background: red;}") return None, False def on_validate(self): name, flag0 = self.check_name(self.name_edit) elements, flag1 = self.check_patran_syntax(self.elements_edit, pound=self.elements_pound) #coords_value, flag2 = self.check_patran_syntax(self.coords_edit, #pound=self.coords_pound) if all([flag0, flag1]): self._default_name = name self._default_elements = self.eids self.out_data['clicked_ok'] = True self.out_data['close'] = True return True return False def on_apply(self, force=False): passed = self.on_validate() if passed or force: self.win_parent._apply_modify_groups(self.out_data) return passed def on_ok(self): passed = self.on_apply() if passed: self.close() #self.destroy() def on_cancel(self): self.out_data['close'] = True self.close() def on_update_active_key(self, index): self.update_active_key(index) #str(index.text()) def update_active_key(self, index): #old_obj = self.out_data[self.imain] name = str(index.text()) self._update_active_key_by_name(name) def _update_active_key_by_name(self, name): if name in self.keys: self.active_key = self.keys.index(name) else: # we (hopefully) just removed a row #self.active_key = self.keys[self.active_key] pass self.name_edit.setText(name) obj = self.out_data[self.active_key] self.eids = parse_patran_syntax(obj.element_str, pound=obj.elements_pound) self._default_elements = obj.element_str self._default_name = name self._apply_cids_eids() self.set_as_main_button.setEnabled(True) if name in ['main', 'anti-main']: self.name.setEnabled(False) self.name_set.setEnabled(False) self.name_edit.setEnabled(False) self.name_button.setEnabled(False) self.elements.setEnabled(False) self.elements_button.setEnabled(False) self.elements_edit.setEnabled(False) self.add.setEnabled(False) self.add_button.setEnabled(False) self.add_edit.setEnabled(False) self.remove.setEnabled(False) self.remove_button.setEnabled(False) self.remove_edit.setEnabled(False) self.delete_group_button.setEnabled(False) if name == 'anti-main': self.set_as_main_button.setEnabled(False) #self.apply_button.setEnabled(False) #self.ok_button.setEnabled(False) else: self.name.setEnabled(True) self.name_set.setEnabled(True) self.name_edit.setEnabled(True) self.name_button.setEnabled(True) self.elements.setEnabled(True) self.elements_button.setEnabled(True) self.add.setEnabled(True) self.add_button.setEnabled(True) self.add_edit.setEnabled(True) self.remove.setEnabled(True) self.remove_button.setEnabled(True) self.remove_edit.setEnabled(True) self.delete_group_button.setEnabled(True)
class SegmentationInfoDialog(QWidget): def __init__(self, settings: StackSettings, set_parameters: Callable[[str, dict], None], additional_text=None): """ :param settings: :param set_parameters: Function which set parameters of chosen in dialog. :param additional_text: Additional text on top of Window. """ super().__init__() self.settings = settings self.parameters_dict = None self.set_parameters = set_parameters self.components = QListWidget() self.components.currentItemChanged.connect(self.change_component_info) self.description = QPlainTextEdit() self.description.setReadOnly(True) self.close_btn = QPushButton("Close") self.close_btn.clicked.connect(self.close) self.set_parameters_btn = QPushButton("Reuse parameters") self.set_parameters_btn.clicked.connect(self.set_parameter_action) self.additional_text_label = QLabel(additional_text) layout = QGridLayout() layout.addWidget(self.additional_text_label, 0, 0, 1, 2) if not additional_text: self.additional_text_label.setVisible(False) layout.addWidget(QLabel("Components:"), 1, 0) layout.addWidget(QLabel("segmentation parameters:"), 1, 1) layout.addWidget(self.components, 2, 0) layout.addWidget(self.description, 2, 1) layout.addWidget(self.close_btn, 3, 0) layout.addWidget(self.set_parameters_btn, 3, 1) self.setLayout(layout) self.setWindowTitle("Parameters preview") def set_parameters_dict(self, val: Optional[Dict[int, SegmentationProfile]]): self.parameters_dict = val def set_additional_text(self, text): self.additional_text_label.setText(text) self.additional_text_label.setVisible(bool(text)) @property def get_parameters(self): if self.parameters_dict: return self.parameters_dict return self.settings.components_parameters_dict def change_component_info(self): if self.components.currentItem() is None: return text = self.components.currentItem().text() parameters = self.get_parameters[int(text)] if parameters is None: self.description.setPlainText("None") else: self.description.setPlainText( f"Component {text}\n" + parameters.pretty_print(mask_algorithm_dict)) def set_parameter_action(self): if self.components.currentItem() is None: return text = self.components.currentItem().text() parameters = self.get_parameters[int(text)] self.set_parameters(parameters.algorithm, parameters.values) def event(self, event: QEvent): if event.type() == QEvent.WindowActivate: index = self.components.currentRow() self.components.clear() self.components.addItems(list(map(str, self.get_parameters.keys()))) self.components.setCurrentRow(index) return super().event(event)