class PowerBar(QWidget): """Custom Qt Widget to show a power bar and dial. Left-clicking the button shows the color-chooser, while right-clicking resets the color to None (no-color).""" colorChanged = QtCore.Signal() def __init__(self, steps: Union[int, List[AllowedColorType]] = 5) -> None: super().__init__() layout = QVBoxLayout() self._bar = _Bar(steps) layout.addWidget(self._bar) # Create the QDial widget and set up defaults. # - we provide accessors on this class to override. self._dial = QDial() self._dial.setNotchesVisible(True) self._dial.setWrapping(False) self._dial.valueChanged.connect(self._bar._trigger_refresh) # Take feedback from click events on the meter. self._bar.clickedValue.connect(self._dial.setValue) layout.addWidget(self._dial) self.setLayout(layout) def __getattr__(self, name: str) -> Any: if name in self.__dict__: return self[name] else: return getattr(self._dial, name) def setColor(self, color: AllowedColorType) -> None: self._bar.steps = [color] * self._bar.n_steps self._bar.update() def setColors(self, colors: List[AllowedColorType]) -> None: self._bar.n_steps = len(colors) self._bar.steps = colors self._bar.update() def setBarPadding(self, i: float) -> None: self._bar._padding = int(i) self._bar.update() def setBarSolidPercent(self, f: float) -> None: self._bar._bar_solid_percent = float(f) self._bar.update() def setBackgroundColor(self, color: AllowedColorType) -> None: self._bar._background_color = QtGui.QColor(color) self._bar.update()
def init_item_rotation_layout(item): current_rotation = item.rotation() label_layout = QHBoxLayout() label_layout.addWidget(QLabel("Rotation")) rotation_value_label = QLabel(str(current_rotation) + " \N{DEGREE SIGN}") label_layout.addWidget(rotation_value_label) rotation_layout = QVBoxLayout() rotation_layout.addLayout(label_layout) rotation_input = QDial() rotation_input.setMinimum(0) rotation_input.setMaximum(359) rotation_input.setSingleStep(1) rotation_input.setWrapping(True) rotation_input.setValue(int(current_rotation)) rotation_input.valueChanged.connect(lambda current_value: set_item_rotation( current_value, item, rotation_value_label)) rotation_layout.addWidget(rotation_input) return rotation_layout
class UhrWindow(QDialog): """Uhr dialog menu with dial""" def __init__(self, master, uhr_position): """ Uhr plugboard device :param master: Qt parent widget :param uhr_position: {callable} Sets the indicator to the current uhr position (if any) """ super().__init__(master) # QT WINDOW SETTINGS =================================================== self.setWindowTitle("Uhr") self.setFixedSize(300, 400) main_layout = QVBoxLayout(self) self.setLayout(main_layout) # UHR POSITION DIAL ==================================================== self.__indicator = QLabel("00") self.__indicator.setStyleSheet( "font-size: 20px; text-align: center; background-color: white") self.__indicator.setFrameStyle(QFrame.Panel | QFrame.Sunken) self.__indicator.setFixedSize(40, 40) self.__indicator.setLineWidth(2) self.__dial = QDial() self.__dial.setWrapping(True) self.__dial.setRange(0, 39) self.__dial.setFixedSize(280, 280) self.__dial.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) position = 0 try: position = uhr_position() logging.info("Successfully loaded Uhr position %d from plug...", position) except ValueError: logging.info( "Failed loading Uhr position from plug, setting to 0...") self.__dial.setValue(position) self.__old_position = position self.__indicator.setText("%02d" % position) self.__dial.valueChanged.connect(self.refresh_indicator) # BUTTONS ============================================================== apply_btn = QPushButton("Apply") apply_btn.clicked.connect(self.apply) storno_btn = QPushButton("Storno") storno_btn.clicked.connect(self.storno) btn_frame = QFrame(self) btn_layout = QHBoxLayout(btn_frame) # SHOW WIDGETS ========================================================= btn_layout.addWidget(storno_btn) btn_layout.addWidget(apply_btn) main_layout.addWidget(btn_frame) main_layout.addWidget(self.__indicator, alignment=Qt.AlignHCenter) main_layout.addWidget(self.__dial, alignment=Qt.AlignHCenter) main_layout.addWidget(btn_frame) def refresh_indicator(self): """Sets displayed indicator value to current dial value""" self.__indicator.setText("%02d" % self.__dial.value()) def position(self): """Returns current Uhr dial setting""" return self.__dial.value() def apply(self): """Sets currently selected position to be collected when applying settings""" self.__old_position = self.__dial.value() logging.info("New Uhr position %d applied, closing...", self.__old_position) self.close() def storno(self): """Undoes current changes""" logging.info("Storno, reverting to old position %d...", self.__old_position) self.__dial.setValue(self.__old_position) self.close()