def create_widgets(self, parent, refresh_callback): self._refresh_callback = refresh_callback self.prefix_label = QLabel() font = self.prefix_label.font() font.setPointSize(font.pointSize() + self._size_offset) self.prefix_label.setFont(font) self.button = QPushButton() self.button.setIcon(get_icon("settings.png")) self.button.setToolTip( _("Edit '%s' fit parameter properties") % self.name) self.button.clicked.connect(lambda: self.edit_param(parent)) self.lineedit = QLineEdit() self.lineedit.editingFinished.connect(self.line_editing_finished) self.unit_label = QLabel(self.unit) self.slider = QSlider() self.slider.setOrientation(Qt.Horizontal) self.slider.setRange(0, self.steps - 1) self.slider.valueChanged.connect(self.slider_value_changed) self.update(refresh=False) self.add_widgets([ self.prefix_label, self.lineedit, self.unit_label, self.slider, self.button, ])
def __init__(self, **kwargs): super(VideoPlayerWidget, self).__init__(**kwargs) self._video = None self._playing = False self._moving_seekbar = False self.view = GraphicsView() self.play_button = QPushButton() self.play_button.setIcon(get_standard_icon(QStyle.SP_MediaPlay)) self.play_button.clicked.connect(self.on_play_button_clicked) self.seekbar = QSlider(Qt.Horizontal) self.seekbar.setMinimum(0) self.seekbar.setMaximum(0) self.seekbar.sliderPressed.connect(self.on_seekbar_sliderPressed) self.seekbar.sliderMoved.connect(self.on_seekbar_sliderMoved) self.seekbar.sliderReleased.connect(self.on_seekbar_sliderReleased) self.seekbar.valueChanged.connect(self.on_seekbar_valueChanged) self.nframes_label = QLabel('----- / -----') self.update_timer = QTimer(self) self.update_timer.timeout.connect(self.fetch_frame) layout = vbox([ self.view, hbox([self.play_button, self.seekbar, self.nframes_label]) ]) self.setLayout(layout)
def createBottomRightGroupBox(self): self.bottomRightGroupBox = QGroupBox("Group 3") self.bottomRightGroupBox.setCheckable(True) self.bottomRightGroupBox.setChecked(True) lineEdit = QLineEdit('s3cRe7') lineEdit.setEchoMode(QLineEdit.Password) spinBox = QSpinBox(self.bottomRightGroupBox) spinBox.setValue(50) dateTimeEdit = QDateTimeEdit(self.bottomRightGroupBox) dateTimeEdit.setDateTime(QDateTime.currentDateTime()) slider = QSlider(Qt.Horizontal, self.bottomRightGroupBox) slider.setValue(40) scrollBar = QScrollBar(Qt.Horizontal, self.bottomRightGroupBox) scrollBar.setValue(60) dial = QDial(self.bottomRightGroupBox) dial.setValue(30) dial.setNotchesVisible(True) layout = QGridLayout() layout.addWidget(lineEdit, 0, 0, 1, 2) layout.addWidget(spinBox, 1, 0, 1, 2) layout.addWidget(dateTimeEdit, 2, 0, 1, 2) layout.addWidget(slider, 3, 0) layout.addWidget(scrollBar, 4, 0) layout.addWidget(dial, 3, 1, 2, 1) layout.setRowStretch(5, 1) self.bottomRightGroupBox.setLayout(layout)
def __init__(self, layer): super().__init__() self.layer = layer layer.events.blending.connect(self._on_blending_change) layer.events.opacity.connect(self._on_opacity_change) self.setObjectName('layer') self.setMouseTracking(True) self.grid_layout = QGridLayout(self) self.grid_layout.setContentsMargins(0, 0, 0, 0) self.grid_layout.setSpacing(2) self.grid_layout.setColumnMinimumWidth(0, 86) self.grid_layout.setColumnStretch(1, 1) self.setLayout(self.grid_layout) sld = QSlider(Qt.Horizontal, parent=self) sld.setFocusPolicy(Qt.NoFocus) sld.setMinimum(0) sld.setMaximum(100) sld.setSingleStep(1) sld.valueChanged.connect(self.changeOpacity) self.opacitySlider = sld self._on_opacity_change() blend_comboBox = QComboBox(self) blend_comboBox.addItems(Blending.keys()) index = blend_comboBox.findText( self.layer.blending, Qt.MatchFixedString ) blend_comboBox.setCurrentIndex(index) blend_comboBox.activated[str].connect(self.changeBlending) self.blendComboBox = blend_comboBox
def __init__(self, viewer: Viewer, parent=None): super().__init__(parent=parent) # Store reference to viewer and create animation self.viewer = viewer self.animation = Animation(self.viewer) # Initialise User Interface self.keyframesListControlWidget = KeyFrameListControlWidget( animation=self.animation, parent=self) self.keyframesListWidget = KeyFramesListWidget( self.animation.key_frames, parent=self) self.frameWidget = FrameWidget(parent=self) self.saveButton = QPushButton("Save Animation", parent=self) self.animationSlider = QSlider(Qt.Horizontal, parent=self) self.animationSlider.setToolTip("Scroll through animation") self.animationSlider.setRange(0, len(self.animation._frames) - 1) # Create layout self.setLayout(QVBoxLayout()) self.layout().addWidget(self.keyframesListControlWidget) self.layout().addWidget(self.keyframesListWidget) self.layout().addWidget(self.frameWidget) self.layout().addWidget(self.saveButton) self.layout().addWidget(self.animationSlider) # establish key bindings and callbacks self._add_keybind_callbacks() self._add_callbacks()
def __init__(self, name, a, b, callback): QWidget.__init__(self) self.name = name self.callback = callback self.a = a self.b = b self.manually_triggered = False self.slider = QSlider() self.slider.setRange(0, 1000) self.slider.setValue(500) self.slider.valueChanged.connect(self.slider_changed) self.name_label = QLabel() self.name_label.setText(self.name) self.name_label.setAlignment(QtCore.Qt.AlignCenter) self.value_label = QLabel() self.value_label.setText('%2.2f' % (self.slider.value() * self.a + self.b)) self.value_label.setAlignment(QtCore.Qt.AlignCenter) self.layout = QGridLayout(self) self.layout.addWidget(self.name_label, 0, 0) self.layout.addWidget(self.slider, 1, 0, QtCore.Qt.AlignHCenter) self.layout.addWidget(self.value_label, 2, 0)
def __init__(self, text, *args, minimum=1, maximum=10, parent=None, **kwargs): super(LabeledSlider, self).__init__(parent) self.label = QLabel(text) self.slider = QSlider(*args, **kwargs) self.slider.setMinimum(minimum) self.slider.setMaximum(maximum) self.slider.setTickInterval(5) self.valuebox = QSpinBox() self.valuebox.setRange(minimum, maximum) self.valuebox.setValue(self.slider.value()) self.slider.valueChanged.connect(self.valuebox.setValue) self.valuebox.valueChanged.connect(self.slider.setValue) layout = QHBoxLayout() layout.addWidget(self.label) layout.addWidget(self.slider) layout.addWidget(self.valuebox) self.setLayout(layout)
def __init__(self, *args, **kwargs): #super(QJumpSlider, self).__init__(self, *args, **kwargs) QSlider.__init__(self, *args, **kwargs) self.is_forward_obj = False #self.is_backward_obj = False # two way connection self.debug = False self.dtype = None self.forward_obj = None
class Viewer(QWidget): def __init__(self, video): super().__init__() self.framenum = 0 self.video = video self.label = QLabel(self) self.vidlength = video.shape[0]-1 lisbon = Lisbon() p = self.palette() p.setColor(self.backgroundRole(), Qt.cyan) self.setPalette(p) self.setAutoFillBackground(True) self.initSlider() self.initUI() def initUI(self): #initialise UI self.update() self.slider.setGeometry(10, self.pixmap.height()+10, self.pixmap.width(), 50) self.label.setMargin(10) self.setGeometry(0, 0, self.pixmap.width()+20, (self.pixmap.height()+self.slider.height()*2)) self.slider.valueChanged.connect(self.valuechange) def update(self): #Update displayed image lisbon = Lisbon() self.npimage = self.video[self.framenum] self.image = self.np2qt(self.npimage) self.pixmap = QPixmap(QPixmap.fromImage(self.image)) self.label.setPixmap(self.pixmap) def wheelEvent(self, event): #scroll through slices and go to beginning if reached max self.framenum = self.framenum + int(event.angleDelta().y()/120) if self.framenum > self.vidlength: self.framenum = 0 if self.framenum < 0: self.framenum = self.vidlength self.slider.setValue(self.framenum) self.update() def np2qt(self, image): #transform np cv2 image to qt format height, width, channel = image.shape bytesPerLine = 3 * width return QImage(image.data, width, height, bytesPerLine, QImage.Format_RGB888) def initSlider(self): self.slider = QSlider(Qt.Horizontal, self) self.slider.setMinimum(0) self.slider.setMaximum(self.vidlength) def valuechange(self): self.framenum = self.slider.value() self.update()
def textToSpeechUtility(self): box = QGroupBox(config.thisTranslation["tts_utility"]) layout = QVBoxLayout() subLayout = QHBoxLayout() self.ttsEdit = QLineEdit() self.ttsEdit.setClearButtonEnabled(True) self.ttsEdit.setToolTip(config.thisTranslation["enter_text_here"]) self.ttsEdit.returnPressed.connect(self.speakText) subLayout.addWidget(self.ttsEdit) layout.addLayout(subLayout) self.ttsSlider = QSlider(Qt.Horizontal) self.ttsSlider.setToolTip(config.thisTranslation["adjustSpeed"]) self.ttsSlider.setMinimum(10) self.ttsSlider.setMaximum(310) self.ttsSlider.setValue(config.espeakSpeed if config.espeak else ( 160 + config.qttsSpeed * 150)) self.ttsSlider.valueChanged.connect( self.changeEspeakSpeed if config.espeak else self.changeQttsSpeed) layout.addWidget(self.ttsSlider) subLayout = QHBoxLayout() self.languageCombo = QComboBox() subLayout.addWidget(self.languageCombo) if config.espeak: languages = TtsLanguages().isoLang2epeakLang else: languages = TtsLanguages().isoLang2qlocaleLang self.languageCodes = list(languages.keys()) for code in self.languageCodes: self.languageCombo.addItem(languages[code][1]) # Check if selected tts engine has the language user specify. if not (config.ttsDefaultLangauge in self.languageCodes): config.ttsDefaultLangauge = "en" # Set initial index # It is essential. Otherwise, default tts language is changed by defaultTtsLanguageChanged method. ttsLanguageIndex = self.languageCodes.index(config.ttsDefaultLangauge) self.languageCombo.setCurrentIndex(ttsLanguageIndex) # Change default tts language as users select a new language self.languageCombo.currentIndexChanged.connect( self.defaultTtsLanguageChanged) button = QPushButton(config.thisTranslation["speak"]) button.setToolTip(config.thisTranslation["speak"]) button.clicked.connect(self.speakText) subLayout.addWidget(button) button = QPushButton(config.thisTranslation["stop"]) button.setToolTip(config.thisTranslation["stop"]) button.clicked.connect( self.parent.parent.textCommandParser.stopTtsAudio) subLayout.addWidget(button) layout.addLayout(subLayout) box.setLayout(layout) return box
def __init__(self, dim_info, number=0, state=State.NONE, parent=None): super().__init__(parent) self.minimum = dim_info['minimum'] self.nbins = dim_info['number_of_bins'] self.width = dim_info['width'] self.number = number self.layout = QHBoxLayout(self) self.layout.setContentsMargins(0, 2, 0, 0) self.name = QLabel(dim_info['name']) self.units = QLabel(dim_info['units']) self.x = QPushButton('X') self.x.setCheckable(True) self.x.clicked.connect(self.x_clicked) # square button based on height. Default sizeHint is too large self.x.setFixedWidth(self.x.sizeHint().height()) self.x.setToolTip("Swap X and Y axes") self.y = QPushButton('Y') self.y.setCheckable(True) self.y.clicked.connect(self.y_clicked) self.y.setFixedWidth(self.y.sizeHint().height()) self.y.setToolTip("Swap X and Y axes") self.slider = QSlider(Qt.Horizontal) self.slider.setRange(0, self.nbins - 1) self.slider.valueChanged.connect(self.slider_changed) self.update_value_from_slider = True self.spinbox = QDoubleSpinBox() self.spinbox.setDecimals(3) self.spinbox.setRange(self.get_bin_center(0), self.get_bin_center(self.nbins - 1)) self.spinbox.setSingleStep(self.width) self.set_value(self.spinbox.minimum()) self.update_spinbox( ) # not updated with set_value unless instance of DimensionNonIntegrated self.spinbox.editingFinished.connect(self.spinbox_changed) self.layout.addWidget(self.name) self.button_layout = QHBoxLayout() self.button_layout.setContentsMargins(0, 0, 0, 0) self.button_layout.setSpacing(0) self.button_layout.addWidget(self.x) self.button_layout.addWidget(self.y) self.layout.addLayout(self.button_layout) self.layout.addWidget(self.slider, stretch=1) self.layout.addStretch(0) self.layout.addWidget(self.spinbox) self.layout.addWidget(self.units) if self.nbins < 2: state = State.DISABLE self.set_state(state)
def __init__(self, theme='dark', emphasized=False): super().__init__(None) self.setProperty('emphasized', emphasized) self.setStyleSheet(template(raw_stylesheet, **palettes[theme])) lay = QVBoxLayout() self.setLayout(lay) lay.addWidget(QPushButton('push button')) box = QComboBox() box.addItems(['a', 'b', 'c', 'cd']) lay.addWidget(box) lay.addWidget(QFontComboBox()) hbox = QHBoxLayout() chk = QCheckBox('tristate') chk.setToolTip('I am a tooltip') chk.setTristate(True) chk.setCheckState(Qt.PartiallyChecked) chk3 = QCheckBox('checked') chk3.setChecked(True) hbox.addWidget(QCheckBox('unchecked')) hbox.addWidget(chk) hbox.addWidget(chk3) lay.addLayout(hbox) lay.addWidget(TabDemo(emphasized=emphasized)) sld = QSlider(Qt.Horizontal) sld.setValue(50) lay.addWidget(sld) scroll = QScrollBar(Qt.Horizontal) scroll.setValue(50) lay.addWidget(scroll) lay.addWidget(QHRangeSlider(parent=self)) text = QTextEdit() text.setMaximumHeight(100) text.setHtml(blurb) lay.addWidget(text) lay.addWidget(QTimeEdit()) edit = QLineEdit() edit.setPlaceholderText('LineEdit placeholder...') lay.addWidget(edit) lay.addWidget(QLabel('label')) prog = QProgressBar() prog.setValue(50) lay.addWidget(prog) groupBox = QGroupBox("Exclusive Radio Buttons") radio1 = QRadioButton("&Radio button 1") radio2 = QRadioButton("R&adio button 2") radio3 = QRadioButton("Ra&dio button 3") radio1.setChecked(True) hbox = QHBoxLayout() hbox.addWidget(radio1) hbox.addWidget(radio2) hbox.addWidget(radio3) hbox.addStretch(1) groupBox.setLayout(hbox) lay.addWidget(groupBox)
def _init_ui(self): # Widgets self._rdb_sum = QRadioButton("Sum") self._rdb_sum.setChecked(True) self._rdb_max = QRadioButton("Maximum") self._rdb_single = QRadioButton("Single") self._rdb_range = QRadioButton("Range") self._sld_start = QSlider(Qt.Horizontal) self._sld_start.setTickPosition(QSlider.TicksBelow) self._sld_start.setEnabled(False) self._sld_end = QSlider(Qt.Horizontal) self._sld_end.setTickPosition(QSlider.TicksBelow) self._sld_end.setEnabled(False) self._wdg_imageraster2d = self._create_imageraster2d_widget() self._wdg_analysis = self._create_analysis1d_widget() # Layouts layout = _DatumWidget._init_ui(self) sublayout = QHBoxLayout() sublayout.addWidget(self._rdb_sum) sublayout.addWidget(self._rdb_max) sublayout.addWidget(self._rdb_single) sublayout.addWidget(self._rdb_range) layout.addLayout(sublayout) sublayout = QFormLayout() sublayout.setFieldGrowthPolicy(QFormLayout.ExpandingFieldsGrow) # Fix for Mac OS sublayout.addRow('Channels (Start)', self._sld_start) sublayout.addRow('Channels (End)', self._sld_end) layout.addLayout(sublayout) splitter = QSplitter() splitter.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) splitter.addWidget(self._wdg_imageraster2d) splitter.addWidget(self._wdg_analysis) layout.addWidget(splitter) # Signals self._rdb_sum.toggled.connect(self._on_mode_sum) self._rdb_max.toggled.connect(self._on_mode_max) self._rdb_single.toggled.connect(self._on_mode_single) self._rdb_range.toggled.connect(self._on_mode_range) self._sld_start.valueChanged.connect(self._on_slide_start) self._sld_end.valueChanged.connect(self._on_slide_end) self._wdg_imageraster2d.valueSelected.connect(self._on_value_selected) # Defaults self.setMode(self.MODE_SUM) return layout
def __init__(self, vpoints: Sequence[VPoint], vlinks: Sequence[VLink], path: _Paths, slider_path: _SliderPaths, monochrome: bool, parent: QWidget): super(AnimateDialog, self).__init__(parent) self.setWindowTitle("Vector Animation") self.setWindowFlags(self.windowFlags() | Qt.WindowMaximizeButtonHint & ~Qt.WindowContextHelpButtonHint) self.setMinimumSize(800, 600) self.setModal(True) main_layout = QVBoxLayout(self) self.canvas = _DynamicCanvas(vpoints, vlinks, path, slider_path, self) self.canvas.set_monochrome_mode(monochrome) self.canvas.update_pos.connect(self.__set_pos) layout = QHBoxLayout(self) pt_option = QComboBox(self) pt_option.addItems([f"P{p}" for p in range(len(vpoints))]) layout.addWidget(pt_option) value_label = QLabel(self) @Slot(int) def show_values(ind: int): vel, vel_deg = self.canvas.get_vel(ind) acc, acc_deg = self.canvas.get_acc(ind) value_label.setText( f"Velocity: {vel:.04f} ({vel_deg:.04f}deg) | " f"Acceleration: {acc:.04f} ({acc_deg:.04f}deg)") pt_option.currentIndexChanged.connect(show_values) layout.addWidget(value_label) self.pos_label = QLabel(self) layout.addItem( QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)) layout.addWidget(self.pos_label) main_layout.addLayout(layout) main_layout.addWidget(self.canvas) layout = QHBoxLayout(self) self.play = QPushButton(QIcon(QPixmap(":/icons/play.png")), "", self) self.play.setCheckable(True) self.play.clicked.connect(self.__play) layout.addWidget(self.play) self.slider = QSlider(Qt.Horizontal, self) self.slider.setMaximum(max(len(p) for p in path) - 1) self.slider.valueChanged.connect(self.canvas.set_index) layout.addWidget(self.slider) layout.addWidget(QLabel("Total times:", self)) factor = QDoubleSpinBox(self) factor.valueChanged.connect(self.canvas.set_factor) factor.setSuffix('s') factor.setRange(0.01, 999999) factor.setValue(10) layout.addWidget(factor) main_layout.addLayout(layout) self.timer = QTimer() self.timer.setInterval(10) self.timer.timeout.connect(self.__move_ind)
def __init__(self, dim_info, number=0, state=State.NONE, parent=None): super().__init__(parent) self.minimum = dim_info['minimum'] self.nbins = dim_info['number_of_bins'] self.width = dim_info['width'] self.number = number self.layout = QHBoxLayout(self) self.layout.setContentsMargins(0, 2, 0, 0) self.name = QLabel(dim_info['name']) self.units = QLabel(dim_info['units']) self.x = QPushButton('X') self.x.setFixedSize(26, 26) self.x.setCheckable(True) self.x.clicked.connect(self.x_clicked) self.y = QPushButton('Y') self.y.setFixedSize(26, 26) self.y.setCheckable(True) self.y.clicked.connect(self.y_clicked) self.slider = QSlider(Qt.Horizontal) self.slider.setRange(0, self.nbins - 1) self.slider.valueChanged.connect(self.slider_changed) self.spinbox = QDoubleSpinBox() self.spinbox.setDecimals(3) self.spinbox.setRange(self.get_bin_center(0), self.get_bin_center(self.nbins - 1)) self.spinbox.setSingleStep(self.width) self.spinbox.editingFinished.connect(self.spinbox_changed) self.layout.addWidget(self.name) self.button_layout = QHBoxLayout() self.button_layout.setContentsMargins(0, 0, 0, 0) self.button_layout.setSpacing(0) self.button_layout.addWidget(self.x) self.button_layout.addWidget(self.y) self.layout.addLayout(self.button_layout) self.layout.addWidget(self.slider, stretch=1) self.layout.addStretch(0) self.layout.addWidget(self.spinbox) self.layout.addWidget(self.units) self.set_value(0) if self.nbins < 2: state = State.DISABLE self.set_state(state)
def __init__(self, item, parent_layout): super(SliderWidget, self).__init__(item, parent_layout) self.slider = self.vmin = self.vmax = None if item.get_prop_value("display", "slider"): self.vmin = item.get_prop_value("data", "min") self.vmax = item.get_prop_value("data", "max") assert ( self.vmin is not None and self.vmax is not None ), "SliderWidget requires that item min/max have been defined" self.slider = QSlider() self.slider.setOrientation(Qt.Horizontal) self.setup_slider(item) self.slider.valueChanged.connect(self.value_changed) self.group.addWidget(self.slider)
class QLabeledSlider(QWidget): """ A labeled slider widget """ range = None integer = None def __init__(self, parent=None): super(QLabeledSlider, self).__init__(parent) self._range = range self._slider = QSlider() self._slider.setMinimum(0) self._slider.setMaximum(100) self._slider.setOrientation(Qt.Horizontal) self._label = QLabel('') self._layout = QHBoxLayout() self._layout.setContentsMargins(2, 2, 2, 2) self._layout.addWidget(self._slider) self._layout.addWidget(self._label) self._slider.valueChanged.connect(self._update_label) self.setLayout(self._layout) def _update_label(self, *args): self._label.setText(str(self.value())) @property def valueChanged(self): return self._slider.valueChanged def value(self, layer=None, view=None): value = self._slider.value() / 100. * (self.range[1] - self.range[0]) + self.range[0] if self.integer: return int(value) else: return (value) _in_set_value = False def setValue(self, value): if self._in_set_value: return self._in_set_value = True value = int(100 * (value - self.range[0]) / (self.range[1] - self.range[0])) self._slider.setValue(value) self._in_set_value = False
class IntelligentSlider(QWidget): ''' A slider that adds a 'name' attribute and calls a callback with 'name' as an argument to the registerd callback. This allows you to create large groups of sliders in a loop, but still keep track of the individual events It also prints a label below the slider. The range of the slider is hardcoded from zero - 1000, but it supports a conversion factor so you can scale the results''' def __init__(self, name, a, b, callback): QWidget.__init__(self) self.name = name self.callback = callback self.a = a self.b = b self.manually_triggered = False self.slider = QSlider() self.slider.setRange(0, 1000) self.slider.setValue(500) self.slider.valueChanged.connect(self.slider_changed) self.name_label = QLabel() self.name_label.setText(self.name) self.name_label.setAlignment(QtCore.Qt.AlignCenter) self.value_label = QLabel() self.value_label.setText('%2.2f' % (self.slider.value() * self.a + self.b)) self.value_label.setAlignment(QtCore.Qt.AlignCenter) self.layout = QGridLayout(self) self.layout.addWidget(self.name_label, 0, 0) self.layout.addWidget(self.slider, 1, 0, QtCore.Qt.AlignHCenter) self.layout.addWidget(self.value_label, 2, 0) # bind this to the valueChanged signal of the slider def slider_changed(self, val): val = self.val() self.value_label.setText(str(val)[:4]) if not self.manually_triggered: self.callback(self.name, val) def set_conv_fac(self, a, b): self.a = a self.b = b def set_value(self, val): self.manually_triggered = True self.slider.setValue(int((val - self.b) / self.a)) self.value_label.setText('%2.2f' % val) self.manually_triggered = False def val(self): return self.slider.value() * self.a + self.b
class IntelligentSlider(QWidget): ''' A slider that adds a 'name' attribute and calls a callback with 'name' as an argument to the registerd callback. This allows you to create large groups of sliders in a loop, but still keep track of the individual events It also prints a label below the slider. The range of the slider is hardcoded from zero - 1000, but it supports a conversion factor so you can scale the results''' def __init__(self, name, a, b, callback): QWidget.__init__(self) self.name = name self.callback = callback self.a = a self.b = b self.manually_triggered = False self.slider = QSlider() self.slider.setRange(0, 1000) self.slider.setValue(500) self.slider.valueChanged.connect(self.slider_changed) self.name_label = QLabel() self.name_label.setText(self.name) self.name_label.setAlignment(QtCore.Qt.AlignCenter) self.value_label = QLabel() self.value_label.setText('%2.2f' % (self.slider.value() * self.a + self.b)) self.value_label.setAlignment(QtCore.Qt.AlignCenter) self.layout = QGridLayout(self) self.layout.addWidget(self.name_label, 0, 0) self.layout.addWidget(self.slider, 1, 0, QtCore.Qt.AlignHCenter) self.layout.addWidget(self.value_label, 2, 0) # bind this to the valueChanged signal of the slider def slider_changed(self, val): val = self.val() self.value_label.setText(str(val)[:4]) if not self.manually_triggered: self.callback(self.name, val) def set_conv_fac(self, a, b): self.a = a self.b = b def set_value(self, val): self.manually_triggered = True self.slider.setValue(int((val - self.b) / self.a)) self.value_label.setText('%2.2f' % val) self.manually_triggered = False def val(self): return self.slider.value() * self.a + self.b
def initUI(self): lcd = QLCDNumber(self) sld = QSlider(Qt.Horizontal, self) self.lable = QLabel(self.text, self) btn = QPushButton('bt1', self) btn2 = QPushButton('bt2', self) self.lable2 = QLabel(self.pushRecord, self) hbox = QHBoxLayout() hbox.addWidget(btn) hbox.addWidget(btn2) vbox = QVBoxLayout() vbox.addWidget(lcd) vbox.addWidget(sld) vbox.addWidget(self.lable) vbox.addLayout(hbox) vbox.addWidget(self.lable2) self.setLayout(vbox) sld.valueChanged.connect(lcd.display) # sldçš„valueä¼šä¼ å…¥display self.setMouseTracking(True) btn.clicked.connect(self.buttonClicked) btn2.clicked.connect(self.buttonClicked) self.setGeometry(300, 300, 250, 150) self.setWindowTitle('Event handler') self.show()
def __init__(self, name, a, b, callback): QWidget.__init__(self) self.name = name self.callback = callback self.a = a self.b = b self.manually_triggered = False self.slider = QSlider() self.slider.setRange(0, 1000) self.slider.setValue(500) self.slider.valueChanged.connect(self.slider_changed) self.name_label = QLabel() self.name_label.setText(self.name) self.name_label.setAlignment(QtCore.Qt.AlignCenter) self.value_label = QLabel() self.value_label.setText('%2.2f' % (self.slider.value() * self.a + self.b)) self.value_label.setAlignment(QtCore.Qt.AlignCenter) self.layout = QGridLayout(self) self.layout.addWidget(self.name_label, 0, 0) self.layout.addWidget(self.slider, 1, 0, QtCore.Qt.AlignHCenter) self.layout.addWidget(self.value_label, 2, 0)
def __init__(self, vpoints: Sequence[VPoint], path: Sequence[Sequence[Tuple[float, float]]], monochrome: bool, parent: QWidget): super(AnimateDialog, self).__init__(parent) self.setWindowTitle("Animation") self.setWindowFlags(self.windowFlags() | Qt.WindowMaximizeButtonHint) self.setMinimumSize(800, 600) self.setModal(True) layout = QVBoxLayout(self) canvas = _DynamicCanvas(vpoints, path, self) canvas.set_monochrome_mode(monochrome) layout.addWidget(canvas) slider = QSlider(Qt.Horizontal, self) slider.setMaximum(max(len(p) for p in path)) slider.valueChanged.connect(canvas.set_index) layout.addWidget(slider)
class QLabeledSlider(QWidget): """ A labeled slider widget """ range = None integer = None def __init__(self, parent=None): super(QLabeledSlider, self).__init__(parent) self._range = range self._slider = QSlider() self._slider.setMinimum(0) self._slider.setMaximum(100) self._slider.setOrientation(Qt.Horizontal) self._label = QLabel('') self._layout = QHBoxLayout() self._layout.setContentsMargins(2, 2, 2, 2) self._layout.addWidget(self._slider) self._layout.addWidget(self._label) self._slider.valueChanged.connect(self._update_label) self.setLayout(self._layout) def _update_label(self, *args): self._label.setText(str(self.value())) @property def valueChanged(self): return self._slider.valueChanged def value(self, layer=None, view=None): value = self._slider.value() / 100. * (self.range[1] - self.range[0]) + self.range[0] if self.integer: return int(value) else: return(value) _in_set_value = False def setValue(self, value): if self._in_set_value: return self._in_set_value = True value = int(100 * (value - self.range[0]) / (self.range[1] - self.range[0])) self._slider.setValue(value) self._in_set_value = False
def __init__(self, parent=None, init_channel=None): QFrame.__init__(self, parent) PyDMWritableWidget.__init__(self, init_channel=init_channel) self.alarmSensitiveContent = True self.alarmSensitiveBorder = False # Internal values for properties self._ignore_mouse_wheel = False self._show_limit_labels = True self._show_value_label = True self._user_defined_limits = False self._needs_limit_info = True self._minimum = None self._maximum = None self._user_minimum = -10.0 self._user_maximum = 10.0 self._num_steps = 101 self._orientation = Qt.Horizontal # Set up all the internal widgets that make up a PyDMSlider. # We'll add all these things to layouts when we call setup_widgets_for_orientation label_size_policy = QSizePolicy(QSizePolicy.Maximum, QSizePolicy.Fixed) self.low_lim_label = QLabel(self) self.low_lim_label.setObjectName("lowLimLabel") self.low_lim_label.setSizePolicy(label_size_policy) self.low_lim_label.setAlignment(Qt.AlignLeft | Qt.AlignTrailing | Qt.AlignVCenter) self.value_label = QLabel(self) self.value_label.setObjectName("valueLabel") self.value_label.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter) self.high_lim_label = QLabel(self) self.high_lim_label.setObjectName("highLimLabel") self.high_lim_label.setSizePolicy(label_size_policy) self.high_lim_label.setAlignment(Qt.AlignRight | Qt.AlignTrailing | Qt.AlignVCenter) self._slider = QSlider(parent=self) self._slider.setOrientation(Qt.Horizontal) self._orig_wheel_event = self._slider.wheelEvent self._slider.sliderMoved.connect(self.internal_slider_moved) self._slider.sliderPressed.connect(self.internal_slider_pressed) self._slider.sliderReleased.connect(self.internal_slider_released) self._slider.valueChanged.connect(self.internal_slider_value_changed) # self.vertical_layout.addWidget(self._slider) # Other internal variables and final setup steps self._slider_position_to_value_map = None self._mute_internal_slider_changes = False self.setup_widgets_for_orientation(self._orientation) self.reset_slider_limits()
class SliderWidget(HLayoutMixin, LineEditWidget): """ IntItem with Slider """ DATA_TYPE = int def __init__(self, item, parent_layout): super(SliderWidget, self).__init__(item, parent_layout) self.slider = self.vmin = self.vmax = None if item.get_prop_value("display", "slider"): self.vmin = item.get_prop_value("data", "min") self.vmax = item.get_prop_value("data", "max") assert ( self.vmin is not None and self.vmax is not None ), "SliderWidget requires that item min/max have been defined" self.slider = QSlider() self.slider.setOrientation(Qt.Horizontal) self.setup_slider(item) self.slider.valueChanged.connect(self.value_changed) self.group.addWidget(self.slider) def value_to_slider(self, value): return value def slider_to_value(self, value): return value def setup_slider(self, item): self.slider.setRange(self.vmin, self.vmax) def update(self, value): """Reimplement LineEditWidget method""" LineEditWidget.update(self, value) if self.slider is not None and isinstance(value, self.DATA_TYPE): self.slider.blockSignals(True) self.slider.setValue(self.value_to_slider(value)) self.slider.blockSignals(False) def value_changed(self, ivalue): """Update the lineedit""" value = str(self.slider_to_value(ivalue)) self.edit.setText(value) self.update(value)
def __init__(self, parent=None): super(QLabeledSlider, self).__init__(parent) self._range = range self._slider = QSlider() self._slider.setMinimum(0) self._slider.setMaximum(100) self._slider.setOrientation(Qt.Horizontal) self._label = QLabel('') self._layout = QHBoxLayout() self._layout.setContentsMargins(2, 2, 2, 2) self._layout.addWidget(self._slider) self._layout.addWidget(self._label) self._slider.valueChanged.connect(self._update_label) self.setLayout(self._layout)
def __init__(self): super(QTiffStackView, self).__init__() #add the image display. This is a subclass of QLabel, where paintEvent is overriden. self.frame_view = FrameView() #self.frame_view.setSizePolicy(QSizePolicy.Expanding,QSizePolicy.Expanding) #add the slide bar which allows the user to manual flick through self.slideBar = QSlider(Qt.Horizontal) self.slideBar.setTickPosition(QSlider.TicksAbove) self.slideBar.setTracking(True) self.slideBar.setTickInterval(100) #add a counter which displays the frame which is currently displayed self.counter = QSpinBox() self.counter.setSingleStep(1) self.counter.setRange(self.slideBar.minimum(), self.slideBar.maximum()) #self explanatory self.play = QPushButton('Play') #when play button is pressed the timer takes control of the displaying of frames self.frametimer = QTimer() frame_rate = 30 self.frametimer.setInterval(frame_rate) #Add a sublayout to align the slidebar and frame counter next to eachother slidelyt = QHBoxLayout() slidelyt.addWidget(self.slideBar) slidelyt.addWidget(self.counter) #Add the main layout for the widget lyt = QVBoxLayout() lyt.addWidget(self.frame_view) lyt.addLayout(slidelyt) lyt.addWidget(self.play) self.setLayout(lyt)
def create_widget(self): self.check_param(self.param, assert_error=True) self.default = self.param['range'][0] if 'default' in self.param.keys(): if self.param['default']: self.default = self.param['default'] self.widget_group = QWidget() slider = QSlider(Qt.Horizontal) slider.setValue(self.default) slider.setRange(self.param['range'][0], self.param['range'][1]) self.widget = QSpinBox() self.widget.setValue(self.default) self.widget.setRange(self.param['range'][0], self.param['range'][1]) self.widget.valueChanged.connect(slider.setValue) slider.valueChanged.connect(self.widget.setValue) h_box = QHBoxLayout() h_box.addWidget(slider) h_box.addWidget(self.widget) h_box.addStretch(1) self.widget_group.setLayout(h_box)
def __init__(self, layer): super().__init__() self.layer = layer self.layer.events.blending.connect(self._on_blending_change) self.layer.events.opacity.connect(self._on_opacity_change) self.setAttribute(Qt.WA_DeleteOnClose) self.setObjectName('layer') self.setMouseTracking(True) self.grid_layout = QGridLayout(self) self.grid_layout.setContentsMargins(0, 0, 0, 0) self.grid_layout.setSpacing(2) self.grid_layout.setColumnMinimumWidth(0, 86) self.grid_layout.setColumnStretch(1, 1) self.setLayout(self.grid_layout) sld = QSlider(Qt.Horizontal, parent=self) sld.setFocusPolicy(Qt.NoFocus) sld.setMinimum(0) sld.setMaximum(100) sld.setSingleStep(1) sld.valueChanged.connect(self.changeOpacity) self.opacitySlider = sld self._on_opacity_change() blend_comboBox = QComboBox(self) for index, (data, text) in enumerate(BLENDING_TRANSLATIONS.items()): data = data.value blend_comboBox.addItem(text, data) if data == self.layer.blending: blend_comboBox.setCurrentIndex(index) blend_comboBox.activated[str].connect(self.changeBlending) self.blendComboBox = blend_comboBox
def initUI(self): lcd = QLCDNumber(self) sld = QSlider(Qt.Horizontal, self) vbox = QVBoxLayout() vbox.addWidget(lcd) vbox.addWidget(sld) self.setLayout(vbox) sld.valueChanged.connect(lambda x: lcd.display(2**x)) self.setGeometry(300, 300, 250, 150) self.setWindowTitle('Signal & slot') self.show()
def __init__(self, dim_info, number=0, state=State.NONE, parent=None): super(Dimension, self).__init__(parent) self.minimum = dim_info['minimum'] self.nbins = dim_info['number_of_bins'] self.width = dim_info['width'] self.number = number self.layout = QHBoxLayout(self) self.name = QLabel(dim_info['name']) self.units = QLabel(dim_info['units']) self.x = QPushButton('X') self.x.setFixedSize(32,32) self.x.setCheckable(True) self.x.clicked.connect(self.x_clicked) self.y = QPushButton('Y') self.y.setFixedSize(32,32) self.y.setCheckable(True) self.y.clicked.connect(self.y_clicked) self.slider = QSlider(Qt.Horizontal) self.slider.setRange(0, self.nbins-1) self.slider.valueChanged.connect(self.slider_changed) self.spinbox = QDoubleSpinBox() self.spinbox.setDecimals(3) self.spinbox.setRange(self.get_bin_center(0), self.get_bin_center(self.nbins-1)) self.spinbox.setSingleStep(self.width) self.spinbox.valueChanged.connect(self.spinbox_changed) self.layout.addWidget(self.name) self.layout.addWidget(self.x) self.layout.addWidget(self.y) self.layout.addWidget(self.slider, stretch=1) self.layout.addStretch(0) self.layout.addWidget(self.spinbox) self.layout.addWidget(self.units) self.set_value(0) if self.nbins < 2: state = State.DISABLE self.set_state(state)
def test_library_widget(qtbot): w = QWidget() w.setLayout(QHBoxLayout()) l = LibraryWidget() for i in range(15): l.add_image(np.random.random((1000, 1000)), f"Sample {i+1}") s = QSlider() s.valueChanged.connect(partial(l.set_slice, axis="E")) w.layout().addWidget(l) w.layout().addWidget(s) w.show() qtbot.addWidget(w)
def test_library_view(qtbot, random_data_catalog): from xicam.plugins.catalogplugin import CatalogModel model = CatalogModel(random_data_catalog) w = QWidget() w.setLayout(QHBoxLayout()) l = LibraryView(model, slice={"E": 0}) s = QSlider() s.valueChanged.connect(partial(l.set_slice, axis="E")) w.layout().addWidget(l) w.layout().addWidget(s) w.show() qtbot.addWidget(w)
def __init__(self, parent=None, init_channel=None): QFrame.__init__(self, parent) PyDMWritableWidget.__init__(self, init_channel=init_channel) self.alarmSensitiveContent = True self.alarmSensitiveBorder = False # Internal values for properties self._show_limit_labels = True self._show_value_label = True self._user_defined_limits = False self._needs_limit_info = True self._minimum = None self._maximum = None self._user_minimum = -10.0 self._user_maximum = 10.0 self._num_steps = 101 self._orientation = Qt.Horizontal # Set up all the internal widgets that make up a PyDMSlider. # We'll add all these things to layouts when we call setup_widgets_for_orientation label_size_policy = QSizePolicy(QSizePolicy.Maximum, QSizePolicy.Fixed) self.low_lim_label = QLabel(self) self.low_lim_label.setObjectName("lowLimLabel") self.low_lim_label.setSizePolicy(label_size_policy) self.low_lim_label.setAlignment(Qt.AlignLeft | Qt.AlignTrailing | Qt.AlignVCenter) self.value_label = QLabel(self) self.value_label.setObjectName("valueLabel") self.value_label.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter) self.high_lim_label = QLabel(self) self.high_lim_label.setObjectName("highLimLabel") self.high_lim_label.setSizePolicy(label_size_policy) self.high_lim_label.setAlignment(Qt.AlignRight | Qt.AlignTrailing | Qt.AlignVCenter) self._slider = QSlider(parent=self) self._slider.setOrientation(Qt.Horizontal) self._slider.sliderMoved.connect(self.internal_slider_moved) self._slider.sliderPressed.connect(self.internal_slider_pressed) self._slider.sliderReleased.connect(self.internal_slider_released) self._slider.valueChanged.connect(self.internal_slider_value_changed) # self.vertical_layout.addWidget(self._slider) # Other internal variables and final setup steps self._slider_position_to_value_map = None self._mute_internal_slider_changes = False self.setup_widgets_for_orientation(self._orientation) self.reset_slider_limits()
def __init__(self, parent=None, value_range=(0, 100), slider_steps=100, spinbox_steps=1000, decimals=None): QWidget.__init__(self, parent) layout = QHBoxLayout(self) self.setLayout(layout) self.horizontalSlider = QSlider(Qt.Horizontal, self) self.doubleSpinBox = QDoubleSpinBox(self) self.decimals = decimals layout.setMargin(0) layout.addWidget(self.doubleSpinBox) layout.addWidget(self.horizontalSlider) self.doubleSpinBox.valueChanged.connect(self.spinbox_changed) self.horizontalSlider.valueChanged.connect(self.slider_changed) self.wt = [] self.changer = None self.slider_steps = slider_steps self.horizontalSlider.setMaximum(slider_steps) self.spinbox_steps = spinbox_steps self.set_range(value_range)
def __init__(self, parent=None): super(QLabeledSlider, self).__init__(parent) self._range = range self._slider = QSlider() self._slider.setMinimum(0) self._slider.setMaximum(100) self._slider.setOrientation(Qt.Horizontal) self._label = QLabel('') self._layout = QHBoxLayout() self._layout.setContentsMargins(2, 2, 2, 2) self._layout.addWidget(self._slider) self._layout.addWidget(self._label) self._slider.valueChanged.connect(self._update_label) self.setLayout(self._layout)
def open_perspective_popup(self): """Show a slider to control the viewer `camera.perspective`.""" if self.viewer.dims.ndisplay != 3: return # make slider connected to perspective parameter sld = QSlider(Qt.Horizontal, self) sld.setRange(0, max(90, self.viewer.camera.perspective)) sld.setValue(self.viewer.camera.perspective) sld.valueChanged.connect( lambda v: setattr(self.viewer.camera, 'perspective', v)) # make layout layout = QHBoxLayout() layout.addWidget(QLabel(trans._('Perspective'), self)) layout.addWidget(sld) # popup and show pop = QtPopup(self) pop.frame.setLayout(layout) pop.show_above_mouse()
def __init__(self, parent): super(MainWidget, self).__init__(parent) self._layout = QHBoxLayout(self) self.setLayout(self._layout) roundbar = QRoundProgressBar(self) roundbar.setBarStyle(QRoundProgressBar.BarStyle.PIE) roundbar.setFixedWidth(300) roundbar.setFixedHeight(300) roundbar.setDecimals(1) self._layout.addWidget(roundbar) self._controlWidget = QWidget(self) self._controlWidget.setMaximumHeight(200) self._controlWidgetLayout = QFormLayout(self._controlWidget) self._controlWidget.setLayout(self._controlWidgetLayout) bar_style_label = QLabel("Bar style", self._controlWidget) style = QPushButton(self._controlWidget) style.setText("Change bar style") self._controlWidgetLayout.addRow(bar_style_label, style) app_style_label = QLabel("App style", self._controlWidget) picker = StylePickerHorizontal(self._controlWidget) self._controlWidgetLayout.addRow(app_style_label, picker) style.clicked.connect(lambda: roundbar.setBarStyle(get_style())) slider_label = QLabel("Progress", self._controlWidget) slider = QSlider(Qt.Horizontal, self._controlWidget) slider.setRange(0, 100) slider.valueChanged.connect(roundbar.setValue) slider.setValue(28) self._controlWidgetLayout.addRow(slider_label, slider) self._layout.addWidget(self._controlWidget)
class _ImageRaster2DSpectralWidget(_DatumWidget): MODE_SUM = 'sum' MODE_MAX = 'max' MODE_SINGLE = 'single' MODE_RANGE = 'range' def __init__(self, controller, datum=None, parent=None): self._datum = datum _DatumWidget.__init__(self, ImageRaster2DSpectral, controller, datum, parent) def _init_ui(self): # Widgets self._rdb_sum = QRadioButton("Sum") self._rdb_sum.setChecked(True) self._rdb_max = QRadioButton("Maximum") self._rdb_single = QRadioButton("Single") self._rdb_range = QRadioButton("Range") self._sld_start = QSlider(Qt.Horizontal) self._sld_start.setTickPosition(QSlider.TicksBelow) self._sld_start.setEnabled(False) self._sld_end = QSlider(Qt.Horizontal) self._sld_end.setTickPosition(QSlider.TicksBelow) self._sld_end.setEnabled(False) self._wdg_imageraster2d = self._create_imageraster2d_widget() self._wdg_analysis = self._create_analysis1d_widget() # Layouts layout = _DatumWidget._init_ui(self) sublayout = QHBoxLayout() sublayout.addWidget(self._rdb_sum) sublayout.addWidget(self._rdb_max) sublayout.addWidget(self._rdb_single) sublayout.addWidget(self._rdb_range) layout.addLayout(sublayout) sublayout = QFormLayout() sublayout.setFieldGrowthPolicy(QFormLayout.ExpandingFieldsGrow) # Fix for Mac OS sublayout.addRow('Channels (Start)', self._sld_start) sublayout.addRow('Channels (End)', self._sld_end) layout.addLayout(sublayout) splitter = QSplitter() splitter.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) splitter.addWidget(self._wdg_imageraster2d) splitter.addWidget(self._wdg_analysis) layout.addWidget(splitter) # Signals self._rdb_sum.toggled.connect(self._on_mode_sum) self._rdb_max.toggled.connect(self._on_mode_max) self._rdb_single.toggled.connect(self._on_mode_single) self._rdb_range.toggled.connect(self._on_mode_range) self._sld_start.valueChanged.connect(self._on_slide_start) self._sld_end.valueChanged.connect(self._on_slide_end) self._wdg_imageraster2d.valueSelected.connect(self._on_value_selected) # Defaults self.setMode(self.MODE_SUM) return layout def _create_analysis1d_widget(self): raise NotImplementedError def _create_imageraster2d_widget(self): raise NotImplementedError def _on_mode_sum(self, checked): if checked: self.setMode(self.MODE_SUM) def _on_mode_max(self, checked): if checked: self.setMode(self.MODE_MAX) def _on_mode_single(self, checked): if checked: self.setMode(self.MODE_SINGLE) def _on_mode_range(self, checked): if checked: self.setMode(self.MODE_RANGE) def _update_data(self, mode=None): if mode is None: mode = self.mode() if mode == self.MODE_SUM: self._update_mode_sum() elif mode == self.MODE_MAX: self._update_mode_max() elif mode == self.MODE_SINGLE: self._update_mode_single() elif mode == self.MODE_RANGE: self._update_mode_range() def _update_mode_sum(self): if self._datum is None: return subdatum = np.sum(self._datum, 2) self._wdg_imageraster2d.setDatum(subdatum) def _update_mode_max(self): if self._datum is None: return subdatum = np.amax(self._datum, 2) self._wdg_imageraster2d.setDatum(subdatum) def _update_mode_single(self): if self._datum is None: return channel = self._sld_start.value() subdatum = self._datum[:, :, channel] self._wdg_imageraster2d.setDatum(subdatum) def _update_mode_range(self): if self._datum is None: return start = self._sld_start.value() end = self._sld_end.value() start2 = min(start, end) end2 = max(start, end) subdatum = np.sum(self._datum[:, :, start2:end2 + 1], 2) self._wdg_imageraster2d.setDatum(subdatum) def _on_slide_start(self, channel): if self._rdb_single.isChecked(): self._update_mode_single() elif self._rdb_range.isChecked(): self._update_mode_range() def _on_slide_end(self, channel): self._update_mode_range() def _on_value_selected(self, x, y): if self._datum is None: return subdatum = self._datum[x, y] self._wdg_analysis.setDatum(subdatum.view(Analysis1D)) def setDatum(self, datum): _DatumWidget.setDatum(self, datum) self._datum = datum maximum = datum.channels - 1 if datum is not None else 0 self._sld_start.setMaximum(maximum) self._sld_end.setMaximum(maximum) self._update_data() def setMode(self, mode): rsum = rmax = rsingle = rrange = False sstart = send = False if mode == self.MODE_SUM: rsum = True elif mode == self.MODE_MAX: rmax = True elif mode == self.MODE_SINGLE: rsingle = True sstart = True elif mode == self.MODE_RANGE: rrange = True sstart = send = True else: raise ValueError('Unknown mode') self._rdb_sum.setChecked(rsum) self._rdb_max.setChecked(rmax) self._rdb_single.setChecked(rsingle) self._rdb_range.setChecked(rrange) self._sld_start.setEnabled(sstart) self._sld_end.setEnabled(send) self._update_data(mode) def mode(self): if self._rdb_sum.isChecked(): return self.MODE_SUM elif self._rdb_max.isChecked(): return self.MODE_MAX elif self._rdb_single.isChecked(): return self.MODE_SINGLE elif self._rdb_range.isChecked(): return self.MODE_RANGE else: raise ValueError('Unknown mode')
class SliderSpinBox(QWidget): valueChanged = Signal(float) def __init__(self, parent=None, value_range=(0, 100), slider_steps=100, spinbox_steps=1000, decimals=None): QWidget.__init__(self, parent) layout = QHBoxLayout(self) self.setLayout(layout) self.horizontalSlider = QSlider(Qt.Horizontal, self) self.doubleSpinBox = QDoubleSpinBox(self) self.decimals = decimals layout.setMargin(0) layout.addWidget(self.doubleSpinBox) layout.addWidget(self.horizontalSlider) self.doubleSpinBox.valueChanged.connect(self.spinbox_changed) self.horizontalSlider.valueChanged.connect(self.slider_changed) self.wt = [] self.changer = None self.slider_steps = slider_steps self.horizontalSlider.setMaximum(slider_steps) self.spinbox_steps = spinbox_steps self.set_range(value_range) def set_value(self, value): self.doubleSpinBox.setValue(value) value = property(lambda self: self.doubleSpinBox.value(), set_value) def set_range(self, value_range): self.changer = "set_range" spinbox_step_decimal = np.ceil(np.log10(1. / (float(value_range[1] - value_range[0]) / self.spinbox_steps))) self.slider_decimals = int(np.ceil(np.log10(self.slider_steps) - np.log10(value_range[1] - value_range[0]))) if self.decimals is None: self.doubleSpinBox.setDecimals(spinbox_step_decimal + 1) else: self.doubleSpinBox.setDecimals(self.decimals) spinbox_step_decimal = min(self.decimals, spinbox_step_decimal) self.slider_decimals = min(self.decimals, self.slider_decimals) self.doubleSpinBox.setSingleStep(10 ** -spinbox_step_decimal) self.value_range = value_range self.doubleSpinBox.setMinimum(value_range[0]) self.doubleSpinBox.setMaximum(value_range[1]) self.horizontalSlider.setValue(np.floor(value_range[0])) self.changer = None self.doubleSpinBox.setValue(value_range[0]) def range(self): return self.doubleSpinBox.minimum(), self.doubleSpinBox.maximum() def slider_changed(self, value): if self.changer is None: self.changer = 'slider' value = np.round(value * (self.value_range[1] - self.value_range[0]) / self.slider_steps + self.value_range[0], self.slider_decimals) self.doubleSpinBox.setValue(value) self.changer = None self.value_changed(value) def spinbox_changed(self, value): if self.changer is None: self.changer = 'spinbox' slider_value = .5 + (value - self.value_range[0]) * self.slider_steps / (self.value_range[1] - self.value_range[0]) self.horizontalSlider.setValue(slider_value) self.changer = None self.value_changed(value) @postpone_until_last_call_finishes def value_changed(self, value): self.valueChanged.emit(value)
class PyDMSlider(QFrame, TextFormatter, PyDMWritableWidget): """ A QSlider with support for Channels and more from PyDM. Parameters ---------- parent : QWidget The parent widget for the Label init_channel : str, optional The channel to be used by the widget. """ actionTriggered = Signal(int) rangeChanged = Signal(float, float) sliderMoved = Signal(float) sliderPressed = Signal() sliderReleased = Signal() valueChanged = Signal(float) def __init__(self, parent=None, init_channel=None): QFrame.__init__(self, parent) PyDMWritableWidget.__init__(self, init_channel=init_channel) self.alarmSensitiveContent = True self.alarmSensitiveBorder = False # Internal values for properties self._show_limit_labels = True self._show_value_label = True self._user_defined_limits = False self._needs_limit_info = True self._minimum = None self._maximum = None self._user_minimum = -10.0 self._user_maximum = 10.0 self._num_steps = 101 self._orientation = Qt.Horizontal # Set up all the internal widgets that make up a PyDMSlider. # We'll add all these things to layouts when we call setup_widgets_for_orientation label_size_policy = QSizePolicy(QSizePolicy.Maximum, QSizePolicy.Fixed) self.low_lim_label = QLabel(self) self.low_lim_label.setObjectName("lowLimLabel") self.low_lim_label.setSizePolicy(label_size_policy) self.low_lim_label.setAlignment(Qt.AlignLeft | Qt.AlignTrailing | Qt.AlignVCenter) self.value_label = QLabel(self) self.value_label.setObjectName("valueLabel") self.value_label.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter) self.high_lim_label = QLabel(self) self.high_lim_label.setObjectName("highLimLabel") self.high_lim_label.setSizePolicy(label_size_policy) self.high_lim_label.setAlignment(Qt.AlignRight | Qt.AlignTrailing | Qt.AlignVCenter) self._slider = QSlider(parent=self) self._slider.setOrientation(Qt.Horizontal) self._slider.sliderMoved.connect(self.internal_slider_moved) self._slider.sliderPressed.connect(self.internal_slider_pressed) self._slider.sliderReleased.connect(self.internal_slider_released) self._slider.valueChanged.connect(self.internal_slider_value_changed) # self.vertical_layout.addWidget(self._slider) # Other internal variables and final setup steps self._slider_position_to_value_map = None self._mute_internal_slider_changes = False self.setup_widgets_for_orientation(self._orientation) self.reset_slider_limits() def init_for_designer(self): """ Method called after the constructor to tweak configurations for when using the widget with the Qt Designer """ self.value = 0.0 @Property(Qt.Orientation) def orientation(self): """ The slider orientation (Horizontal or Vertical) Returns ------- int Qt.Horizontal or Qt.Vertical """ return self._orientation @orientation.setter def orientation(self, new_orientation): """ The slider orientation (Horizontal or Vertical) Parameters ---------- new_orientation : int Qt.Horizontal or Qt.Vertical """ self._orientation = new_orientation self.setup_widgets_for_orientation(new_orientation) def setup_widgets_for_orientation(self, new_orientation): """ Reconstruct the widget given the orientation. Parameters ---------- new_orientation : int Qt.Horizontal or Qt.Vertical """ if new_orientation not in (Qt.Horizontal, Qt.Vertical): logger.error("Invalid orientation '{0}'. The existing layout will not change.".format(new_orientation)) return layout = None if new_orientation == Qt.Horizontal: layout = QVBoxLayout() layout.setContentsMargins(4, 0, 4, 4) label_layout = QHBoxLayout() label_layout.addWidget(self.low_lim_label) label_layout.addStretch(0) label_layout.addWidget(self.value_label) label_layout.addStretch(0) label_layout.addWidget(self.high_lim_label) layout.addLayout(label_layout) self._slider.setOrientation(new_orientation) layout.addWidget(self._slider) elif new_orientation == Qt.Vertical: layout = QHBoxLayout() layout.setContentsMargins(0, 4, 4, 4) label_layout = QVBoxLayout() label_layout.addWidget(self.high_lim_label) label_layout.addStretch(0) label_layout.addWidget(self.value_label) label_layout.addStretch(0) label_layout.addWidget(self.low_lim_label) layout.addLayout(label_layout) self._slider.setOrientation(new_orientation) layout.addWidget(self._slider) if self.layout() is not None: # Trick to remove the existing layout by re-parenting it in an empty widget. QWidget().setLayout(self.layout()) self.setLayout(layout) def update_labels(self): """ Update the limits and value labels with the correct values. """ def set_label(value, label_widget): if value is None: label_widget.setText("") else: label_widget.setText(self.format_string.format(value)) set_label(self.minimum, self.low_lim_label) set_label(self.maximum, self.high_lim_label) set_label(self.value, self.value_label) def reset_slider_limits(self): """ Reset the limits and adjust the labels properly for the slider. """ if self.minimum is None or self.maximum is None: self._needs_limit_info = True self.set_enable_state() return self._needs_limit_info = False self._slider.setMinimum(0) self._slider.setMaximum(self._num_steps - 1) self._slider.setSingleStep(1) self._slider.setPageStep(10) self._slider_position_to_value_map = np.linspace(self.minimum, self.maximum, num=self._num_steps) self.update_labels() self.set_slider_to_closest_value(self.value) self.rangeChanged.emit(self.minimum, self.maximum) self.set_enable_state() def find_closest_slider_position_to_value(self, val): """ Find and returns the index for the closest position on the slider for a given value. Parameters ---------- val : float Returns ------- int """ diff = abs(self._slider_position_to_value_map - float(val)) return np.argmin(diff) def set_slider_to_closest_value(self, val): """ Set the value for the slider according to a given value. Parameters ---------- val : float """ if val is None or self._needs_limit_info: return # When we set the slider to the closest value, it may end up at a slightly # different position than val (if val is not in self._slider_position_to_value_map) # We don't want that slight difference to get broacast out and put the channel # somewhere new. For example, if the slider can only be at 0.4 or 0.5, but a # new value comes in of 0.45, its more important to keep the 0.45 than to change # it to where the slider gets set. Therefore, we mute the internal slider changes # so that its valueChanged signal doesn't cause us to emit a signal to PyDM to change # the value of the channel. self._mute_internal_slider_changes = True self._slider.setValue(self.find_closest_slider_position_to_value(val)) self._mute_internal_slider_changes = False def value_changed(self, new_val): """ Callback invoked when the Channel value is changed. Parameters ---------- new_val : int or float The new value from the channel. """ PyDMWritableWidget.value_changed(self, new_val) if hasattr(self, "value_label"): self.value_label.setText(self.format_string.format(self.value)) if not self._slider.isSliderDown(): self.set_slider_to_closest_value(self.value) def ctrl_limit_changed(self, which, new_limit): """ Callback invoked when the Channel receives new control limit values. Parameters ---------- which : str Which control limit was changed. "UPPER" or "LOWER" new_limit : float New value for the control limit """ PyDMWritableWidget.ctrl_limit_changed(self, which, new_limit) if not self.userDefinedLimits: self.reset_slider_limits() def update_format_string(self): """ Reconstruct the format string to be used when representing the output value. Returns ------- format_string : str The format string to be used including or not the precision and unit """ fs = super(PyDMSlider, self).update_format_string() self.update_labels() return fs def set_enable_state(self): """ Determines wether or not the widget must be enabled or not depending on the write access, connection state and presence of limits information """ # Even though by documentation disabling parent QFrame (self), should disable internal # slider, in practice it still remains interactive (PyQt 5.12.1). Disabling explicitly, solves # the problem. should_enable = self._write_access and self._connected and not self._needs_limit_info self.setEnabled(should_enable) self._slider.setEnabled(should_enable) @Slot(int) def internal_slider_action_triggered(self, action): self.actionTriggered.emit(action) @Slot(int) def internal_slider_moved(self, val): """ Method invoked when the slider is moved. Parameters ---------- val : float """ # Avoid potential crash if limits are undefined if self._slider_position_to_value_map is None: return # The user has moved the slider, we need to update our value. # Only update the underlying value, not the self.value property, # because we don't need to reset the slider position. If we change # self.value, we can get into a loop where the position changes, which # updates the value, which changes the position again, etc etc. self.value = self._slider_position_to_value_map[val] self.sliderMoved.emit(self.value) @Slot() def internal_slider_pressed(self): """ Method invoked when the slider is pressed """ self.sliderPressed.emit() @Slot() def internal_slider_released(self): """ Method invoked when the slider is released """ self.sliderReleased.emit() @Slot(int) def internal_slider_value_changed(self, val): """ Method invoked when a new value is selected on the slider. This will cause the new value to be emitted to the signal unless `mute_internal_slider_changes` is True. Parameters ---------- val : int """ # At this point, our local copy of the value reflects the position of the # slider, now all we need to do is emit a signal to PyDM so that the data # plugin will send a put to the channel. Don't update self.value or self._value # in here, it is pointless at best, and could cause an infinite loop at worst. if not self._mute_internal_slider_changes: self.send_value_signal[float].emit(self.value) @Property(bool) def showLimitLabels(self): """ Whether or not the high and low limits should be displayed on the slider. Returns ------- bool """ return self._show_limit_labels @showLimitLabels.setter def showLimitLabels(self, checked): """ Whether or not the high and low limits should be displayed on the slider. Parameters ---------- checked : bool """ self._show_limit_labels = checked if checked: self.low_lim_label.show() self.high_lim_label.show() else: self.low_lim_label.hide() self.high_lim_label.hide() @Property(bool) def showValueLabel(self): """ Whether or not the current value should be displayed on the slider. Returns ------- bool """ return self._show_value_label @showValueLabel.setter def showValueLabel(self, checked): """ Whether or not the current value should be displayed on the slider. Parameters ---------- checked : bool """ self._show_value_label = checked if checked: self.value_label.show() else: self.value_label.hide() @Property(QSlider.TickPosition) def tickPosition(self): """ Where to draw tick marks for the slider. Returns ------- QSlider.TickPosition """ return self._slider.tickPosition() @tickPosition.setter def tickPosition(self, position): """ Where to draw tick marks for the slider. Parameter --------- position : QSlider.TickPosition """ self._slider.setTickPosition(position) @Property(bool) def userDefinedLimits(self): """ Wether or not to use limits defined by the user and not from the channel Returns ------- bool """ return self._user_defined_limits @userDefinedLimits.setter def userDefinedLimits(self, user_defined_limits): """ Wether or not to use limits defined by the user and not from the channel Parameters ---------- user_defined_limits : bool """ self._user_defined_limits = user_defined_limits self.reset_slider_limits() @Property(float) def userMinimum(self): """ Lower user defined limit value Returns ------- float """ return self._user_minimum @userMinimum.setter def userMinimum(self, new_min): """ Lower user defined limit value Parameters ---------- new_min : float """ self._user_minimum = float(new_min) if new_min is not None else None if self.userDefinedLimits: self.reset_slider_limits() @Property(float) def userMaximum(self): """ Upper user defined limit value Returns ------- float """ return self._user_maximum @userMaximum.setter def userMaximum(self, new_max): """ Upper user defined limit value Parameters ---------- new_max : float """ self._user_maximum = float(new_max) if new_max is not None else None if self.userDefinedLimits: self.reset_slider_limits() @property def minimum(self): """ The current value being used for the lower limit Returns ------- float """ if self.userDefinedLimits: return self._user_minimum return self._lower_ctrl_limit @property def maximum(self): """ The current value being used for the upper limit Returns ------- float """ if self.userDefinedLimits: return self._user_maximum return self._upper_ctrl_limit @Property(int) def num_steps(self): """ The number of steps on the slider Returns ------- int """ return self._num_steps @num_steps.setter def num_steps(self, new_steps): """ The number of steps on the slider Parameters ---------- new_steps : int """ self._num_steps = int(new_steps) self.reset_slider_limits()
class Dimension(QWidget): stateChanged = Signal(int) valueChanged = Signal() """ pass in dimension state: one of (State.X, State.Y, State.NONE, State.DISBALE) Can be run independently by: from mantidqt.widgets.sliceviewer.dimensionwidget import Dimension from qtpy.QtWidgets import QApplication app = QApplication([]) window = Dimension({'minimum':-1.1, 'number_of_bins':11, 'width':0.2, 'name':'Dim0', 'units':'A'}) window.show() app.exec_() """ def __init__(self, dim_info, number=0, state=State.NONE, parent=None): super(Dimension, self).__init__(parent) self.minimum = dim_info['minimum'] self.nbins = dim_info['number_of_bins'] self.width = dim_info['width'] self.number = number self.layout = QHBoxLayout(self) self.name = QLabel(dim_info['name']) self.units = QLabel(dim_info['units']) self.x = QPushButton('X') self.x.setFixedSize(32,32) self.x.setCheckable(True) self.x.clicked.connect(self.x_clicked) self.y = QPushButton('Y') self.y.setFixedSize(32,32) self.y.setCheckable(True) self.y.clicked.connect(self.y_clicked) self.slider = QSlider(Qt.Horizontal) self.slider.setRange(0, self.nbins-1) self.slider.valueChanged.connect(self.slider_changed) self.spinbox = QDoubleSpinBox() self.spinbox.setDecimals(3) self.spinbox.setRange(self.get_bin_center(0), self.get_bin_center(self.nbins-1)) self.spinbox.setSingleStep(self.width) self.spinbox.valueChanged.connect(self.spinbox_changed) self.layout.addWidget(self.name) self.layout.addWidget(self.x) self.layout.addWidget(self.y) self.layout.addWidget(self.slider, stretch=1) self.layout.addStretch(0) self.layout.addWidget(self.spinbox) self.layout.addWidget(self.units) self.set_value(0) if self.nbins < 2: state = State.DISABLE self.set_state(state) def set_state(self, state): self.state = state if self.state == State.X: self.x.setChecked(True) self.y.setChecked(False) self.slider.hide() self.spinbox.hide() self.units.hide() elif self.state == State.Y: self.x.setChecked(False) self.y.setChecked(True) self.slider.hide() self.spinbox.hide() self.units.hide() elif self.state == State.NONE: self.x.setChecked(False) self.y.setChecked(False) self.slider.show() self.spinbox.show() self.units.show() else: self.x.setChecked(False) self.x.setDisabled(True) self.y.setChecked(False) self.y.setDisabled(True) self.slider.hide() self.spinbox.show() self.spinbox.setDisabled(True) self.units.show() def get_state(self): return self.state def x_clicked(self): old_state = self.state self.set_state(State.X) if self.state != old_state: self.stateChanged.emit(self.number) def y_clicked(self): old_state = self.state self.set_state(State.Y) if self.state != old_state: self.stateChanged.emit(self.number) def spinbox_changed(self): self.value = self.spinbox.value() self.update_slider() self.valueChanged.emit() def slider_changed(self): self.value = self.get_bin_center(self.slider.value()) self.update_spinbox() self.valueChanged.emit() def get_bin_center(self, n): return (n+0.5)*self.width+self.minimum def update_slider(self): i = (self.value-self.minimum)/self.width self.slider.setValue(int(min(max(i, 0), self.nbins-1))) def update_spinbox(self): self.spinbox.setValue(self.value) def set_value(self, value): self.value = value self.update_slider() self.update_spinbox() def get_value(self): return self.value