def _convert_float(self, param: xappt.Parameter) -> QtWidgets.QWidget: w = QtWidgets.QDoubleSpinBox(parent=self) minimum = param.options.get("minimum", -999999999.0) maximum = param.options.get("maximum", 999999999.0) w.setMinimum(minimum) w.setMaximum(maximum) if param.default is not None: w.setValue(param.default) param.value = w.value() w.valueChanged[float].connect( lambda x: self.update_tool_param(param.name, x)) param.metadata['ui-setter'] = w.setValue return w
def _convert_list(self, param: xappt.Parameter) -> QtWidgets.QWidget: w = CheckList(searchable=param.options.get('searchable', False)) if param.choices is not None: w.add_items(param.choices) for v in (param.value, param.default): if v is not None: w.check_items(v) break param.value = list(w.checked_items()) w.item_changed.connect(lambda: self.update_list_param(param.name)) param.metadata[ 'ui-setter'] = lambda value, widget=w: self.set_list_value( value, widget) return w
def widget_options_updated(param: xappt.Parameter): label: Optional[QtWidgets.QLabel] = param.metadata.get('label') widget: Optional[QtWidgets.QWidget] = param.metadata.get('widget') error: Optional[ErrorLabel] = param.metadata.get('error') visible = param.option("visible", True) if label is not None: label.setVisible(visible) if widget is not None: widget.setVisible(visible) if error is not None: error.setVisible(visible) enabled = param.option("enabled", True) if widget is not None: widget.setEnabled(enabled)
def _convert_str_edit(self, param: xappt.Parameter) -> QtWidgets.QWidget: ui = param.options.get("ui") if ui == "folder-select": w = FileEdit(mode=FileEdit.MODE_CHOOSE_DIR) w.onSetFile.connect( lambda x: self.update_tool_param(param.name, x)) elif ui == "file-open": w = FileEdit(accept=param.options.get("accept"), mode=FileEdit.MODE_OPEN_FILE) w.onSetFile.connect( lambda x: self.update_tool_param(param.name, x)) elif ui == "file-save": w = FileEdit(accept=param.options.get("accept"), mode=FileEdit.MODE_SAVE_FILE) w.onSetFile.connect( lambda x: self.update_tool_param(param.name, x)) elif ui == "multi-line": w = TextEdit() w.editingFinished.connect(lambda widget=w: self.update_tool_param( param.name, widget.text())) else: w = QtWidgets.QLineEdit() w.editingFinished.connect(lambda widget=w: self.update_tool_param( param.name, widget.text())) for v in (param.value, param.default): if v is not None: w.setText(v) break else: w.setText("") param.metadata['ui-setter'] = w.setText return w
def _convert_int_spin(self, param: xappt.Parameter) -> QtWidgets.QWidget: w = QtWidgets.QSpinBox(parent=self) minimum = param.options.get("minimum", -999999999) maximum = param.options.get("maximum", 999999999) w.setMinimum(minimum) w.setMaximum(maximum) for v in (param.value, param.default): if v is not None: w.setValue(v) break else: param.value = w.value() w.valueChanged[int].connect( lambda x: self.update_tool_param(param.name, x)) param.metadata['ui-setter'] = w.setValue return w
def _convert_bool(self, param: xappt.Parameter) -> QtWidgets.QWidget: if param.options.get("ui") == "button": w = QtWidgets.QPushButton() w.setText(self.get_caption(param)) w.clicked.connect( lambda x: self.update_tool_param(param.name, param.value)) param.metadata['label'].setText("") else: w = QtWidgets.QCheckBox() for v in (param.value, param.default): if v is not None: w.setChecked(v) break else: param.value = w.isChecked() w.stateChanged.connect(lambda x: self.update_tool_param( param.name, x == QtCore.Qt.Checked)) param.metadata['ui-setter'] = w.setChecked return w
def _convert_str_choice(self, param: xappt.Parameter) -> QtWidgets.QWidget: w = QtWidgets.QComboBox() w.addItems(param.choices) for v in (param.value, param.default): if v is not None and v in param.choices: index = w.findText(v) w.setCurrentIndex(index) break else: param.value = w.currentText() if param.options.get("searchable"): self.setup_combo_completer(w) w.currentIndexChanged[str].connect( lambda x: self.update_tool_param(param.name, x)) param.metadata[ 'ui-setter'] = lambda s, widget=w: widget.setCurrentIndex( widget.findText(s)) return w
def _convert_int_choice(self, param: xappt.Parameter) -> QtWidgets.QWidget: w = QtWidgets.QComboBox() w.addItems(param.choices) for v in (param.value, param.default): if v is not None: if isinstance(v, str): if v in param.choices: index = w.findText(v) w.setCurrentIndex(index) elif isinstance(v, int): if 0 <= v < w.count(): w.setCurrentIndex(v) break else: param.value = w.currentIndex() if param.options.get("searchable"): self.setup_combo_completer(w) w.currentIndexChanged[str].connect( lambda x: self.update_tool_param(param.name, x)) param.metadata['ui-setter'] = w.setCurrentIndex return w
def update_tool_choices(self, param: xappt.Parameter): """ Given that multiple parameter types can be updated at runtime, it's easier just to remove and recreate the widget rather than reimplementing a lot of the same functionality to update existing widgets. """ widget = param.metadata.get('widget') if widget is None: return # find and remove existing widget index = self.grid.indexOf(widget) row, column, *_ = self.grid.getItemPosition(index) self.grid.takeAt(index) widget.deleteLater() param.on_choices_changed.clear() # create a new widget to replace it new_widget = self.convert_parameter(param) self.grid.addWidget(new_widget, row, column) param.metadata['widget'] = new_widget param.on_choices_changed.add(self.update_tool_choices)