def createEditor(self, parent, option, index): """ Create for the display and interaction with the user an editor. @param QtGui.QWidget parent: The parent object (probably QTableView) @param QtGui.QStyleOptionViewItemV4 option: This is a setting option which you can use for style configuration. @param QtCore.QModelIndex index: That index will be passed by the model object of the QTableView to the delegated object. This index contains information about the selected current cell. An editor can be in principle any QWidget, which you want to use to display the current (model-)data. Therefore the editor is like a container, which handles the passed entries from the user interface and should save the data to the model object of the QTableWidget. The setEditorData function reads data from the model. Do not save the created editor as a class variable! This consumes a lot of unneeded memory. It is way better to create an editor if it is needed. The inherent function closeEditor() of QStyledItemDelegate takes care of closing and destroying the editor for you, if it is not needed any longer. """ editor = ScienDSpinBox(parent=parent) if 'min' in self.item_dict: editor.setMinimum(self.item_dict['min']) if 'max' in self.item_dict: editor.setMaximum(self.item_dict['max']) if 'dec' in self.item_dict: editor.setDecimals(self.item_dict['dec']) if 'unit' in self.item_dict: editor.setSuffix(self.item_dict['unit']) editor.setGeometry(option.rect) editor.editingFinished.connect(self.commitAndCloseEditor) return editor
def createEditor(self, parent, option, index): """ Create for the display and interaction with the user an editor. @param QtGui.QWidget parent: The parent object, here QTableWidget @param QtGui.QStyleOptionViewItemV4 option: This is a setting option which you can use for style configuration. @param QtCore.QModelIndex index: That index will be passed by the model object of the QTableWidget to the delegated object. This index contains information about the selected current cell. An editor can be in principle any QWidget, which you want to use to display the current (model-)data. Therefore the editor is like a container, which handles the passed entries from the user interface and should save the data to the model object of the QTableWidget. The setEditorData function reads data from the model. Do not save the created editor as a class variable! This consumes a lot of unneeded memory. It is way better to create an editor if it is needed. The inherent function closeEditor() of QStyledItemDelegate takes care of closing and destroying the editor for you, if it is not needed any longer. """ editor = ScienDSpinBox(parent=parent) editor.setMinimum(self.item_dict['min'] / self.norm_val) editor.setMaximum(self.item_dict['max'] / self.norm_val) editor.setSingleStep(self.item_dict['view_stepsize'] / self.norm_val) editor.setDecimals(self.item_dict['dec']) editor.installEventFilter(self) editor.setValue(self.item_dict['init_val'] / self.norm_val) editor.setMaximumHeight(100) return editor
def createEditor(self, parent, option, index): """ Create for the display and interaction with the user an editor. @param QtGui.QWidget parent: The parent object, here QTableWidget @param QtGui.QStyleOptionViewItemV4 option: This is a setting option which you can use for style configuration. @param QtCore.QModelIndex index: That index will be passed by the model object of the QTableWidget to the delegated object. This index contains information about the selected current cell. An editor can be in principle any QWidget, which you want to use to display the current (model-)data. Therefore the editor is like a container, which handles the passed entries from the user interface and should save the data to the model object of the QTableWidget. The setEditorData function reads data from the model. Do not save the created editor as a class variable! This consumes a lot of unneeded memory. It is way better to create an editor if it is needed. The inherent function closeEditor() of QStyledItemDelegate takes care of closing and destroying the editor for you, if it is not needed any longer. """ editor = ScienDSpinBox(parent=parent) editor.setMinimum(self.item_dict['min']) editor.setMaximum(self.item_dict['max']) editor.setSingleStep(self.item_dict['view_stepsize']) editor.setDecimals(self.item_dict['dec']) editor.installEventFilter(self) editor.setValue(self.item_dict['init_val']) editor.setMaximumHeight(100) return editor
def __init__(self, parent=None, parameters_dict=None): super().__init__(parent) if parameters_dict is None: self._parameters = OrderedDict() else: self._parameters = parameters_dict self._width_hint = 90 * len(self._parameters) self._ach_widgets = OrderedDict() main_layout = QtGui.QHBoxLayout() for param in self._parameters: label = QtGui.QLabel(param) label.setAlignment(QtCore.Qt.AlignCenter) if self._parameters[param]['type'] == float: widget = ScienDSpinBox() widget.setMinimum(self._parameters[param]['min']) widget.setMaximum(self._parameters[param]['max']) widget.setDecimals(6, False) widget.setValue(self._parameters[param]['init']) widget.setSuffix(self._parameters[param]['unit']) # Set size constraints widget.setFixedWidth(90) # Forward editingFinished signal of child widget widget.editingFinished.connect(self.editingFinished) elif self._parameters[param]['type'] == int: widget = ScienSpinBox() widget.setValue(self._parameters[param]['init']) widget.setMinimum(self._parameters[param]['min']) widget.setMaximum(self._parameters[param]['max']) widget.setSuffix(self._parameters[param]['unit']) # Set size constraints widget.setFixedWidth(90) # Forward editingFinished signal of child widget widget.editingFinished.connect(self.editingFinished) elif self._parameters[param]['type'] == str: widget = QtGui.QLineEdit() widget.setText(self._parameters[param]['init']) # Set size constraints widget.setFixedWidth(90) # Forward editingFinished signal of child widget widget.editingFinished.connect(self.editingFinished) elif self._parameters[param]['type'] == bool: widget = QtGui.QCheckBox() widget.setChecked(self._parameters[param]['init']) # Set size constraints widget.setFixedWidth(90) # Forward editingFinished signal of child widget widget.stateChanged.connect(self.editingFinished) elif issubclass(self._parameters[param]['type'], Enum): widget = QtGui.QComboBox() for option in self._parameters[param]['type']: widget.addItem(option.name, option) widget.setCurrentText(self._parameters[param]['init'].name) # Set size constraints widget.setFixedWidth(90) # Forward editingFinished signal of child widget widget.currentIndexChanged.connect(self.editingFinished) self._ach_widgets[param] = {'label': label, 'widget': widget} v_layout = QtGui.QVBoxLayout() v_layout.addWidget(label) v_layout.addWidget(widget) v_layout.setAlignment(label, QtCore.Qt.AlignHCenter) v_layout.setAlignment(widget, QtCore.Qt.AlignHCenter) main_layout.addLayout(v_layout) main_layout.addStretch(1) main_layout.setSpacing(0) main_layout.setContentsMargins(0, 0, 0, 0) self.setLayout(main_layout)
class ColorbarWidget(QtWidgets.QWidget): """ Create the widget, based on the corresponding *.ui file.""" def __init__(self, image_widget, unit='c/s'): # Get the path to the *.ui file this_dir = os.path.dirname(__file__) ui_file = os.path.join(this_dir, 'ui_colorbar.ui') # Load it super(ColorbarWidget, self).__init__() uic.loadUi(ui_file, self) self._cb_min = 0 self._cb_max = 100 self.unit = unit self.init_spin_box() self.init_colorbar() self.set_image(image_widget) self.percentile.clicked.emit() self.percentile.setChecked(True) def init_spin_box(self): """ Initialize all the spinboxes """ self._min_percentile = ScienDSpinBox() self._min_manual = ScienDSpinBox() self._max_percentile = ScienDSpinBox() self._max_manual = ScienDSpinBox() self._min_percentile.setSuffix('%') self._min_percentile.setMinimum(0) self._min_percentile.setMaximum(100) self._min_percentile.setValue(0) self._min_manual.setSuffix(self.unit) self._max_percentile.setSuffix('%') self._max_percentile.setMinimum(0) self._max_percentile.setMaximum(100) self._max_percentile.setValue(100) self._max_manual.setSuffix(self.unit) self.min.addWidget(self._min_manual) self.min.addWidget(self._min_percentile) self.max.addWidget(self._max_percentile) self.max.addWidget(self._max_manual) self._min_percentile.valueChanged.connect(self.shortcut_to_cb_centiles) self._min_manual.valueChanged.connect(self.shortcut_to_cb_manual) self._max_percentile.valueChanged.connect(self.shortcut_to_cb_centiles) self._max_manual.valueChanged.connect(self.shortcut_to_cb_manual) self.manual.clicked.connect(self.update_cb_range) self.percentile.clicked.connect(self.update_cb_range) def init_colorbar(self): """ Create the colorbar """ self.my_colors = ColorScaleInferno() self._color_map = ColorScaleMagma() self._cb = ColorBar(self.my_colors.cmap_normed, width=100, cb_min=self._cb_min, cb_max=self._cb_max) self.colorbar.addItem(self._cb) self.colorbar.hideAxis('bottom') self.colorbar.setLabel('left', 'Intensity', units=self.unit) self.colorbar.setMouseEnabled(x=False, y=False) def set_image(self, image_widget): """ Set the image widget associated to the colorbar """ self._image = image_widget self._min_manual.setValue(np.min(self._image.image)) self._min_percentile.setValue(0) self._max_manual.setValue(np.max(self._image.image)) self._max_percentile.setValue(100) self.refresh_colorbar() def get_cb_range(self): """ Determines the cb_min and cb_max values for the image """ # If "Manual" is checked, or the image data is empty (all zeros), then take manual cb range. if self.manual.isChecked() or np.count_nonzero(self._image.image) < 1: cb_min = self._min_manual.value() cb_max = self._max_manual.value() # Otherwise, calculate cb range from percentiles. else: # Exclude any zeros (which are typically due to unfinished scan) image_nonzero = self._image.image[np.nonzero(self._image.image)] # Read centile range low_centile = self._min_percentile.value() high_centile = self._max_percentile.value() cb_min = np.percentile(image_nonzero, low_centile) cb_max = np.percentile(image_nonzero, high_centile) cb_range = [cb_min, cb_max] return cb_range def refresh_colorbar(self): """ Adjust the colorbar. """ cb_range = self.get_cb_range() self._cb.refresh_colorbar(cb_range[0], cb_range[1]) def refresh_image(self): """ Update the image colors range.""" image_data = self._image.image cb_range = self.get_cb_range() self._image.setImage(image=image_data, levels=(cb_range[0], cb_range[1])) self.refresh_colorbar() def update_cb_range(self): """ Redraw colour bar and image.""" self.refresh_colorbar() self.refresh_image() def shortcut_to_cb_manual(self): """ Someone edited the absolute counts range for the colour bar, better update.""" self.manual.setChecked(True) self.update_cb_range() def shortcut_to_cb_centiles(self): """Someone edited the centiles range for the colour bar, better update.""" self.percentile.setChecked(True) self.update_cb_range()