class ActivityToggleItem(QWidget): """Toggle button for Activity Dialog. A progress indicator is displayed when there are active progress bars. """ def __init__(self, parent=None) -> None: super().__init__(parent=parent) self.setLayout(QHBoxLayout()) self._activityBtn = QToolButton() self._activityBtn.setObjectName("QtActivityButton") self._activityBtn.setToolButtonStyle(Qt.ToolButtonTextBesideIcon) self._activityBtn.setArrowType(Qt.UpArrow) self._activityBtn.setIconSize(QSize(11, 11)) self._activityBtn.setText(trans._('activity')) self._activityBtn.setCheckable(True) self._inProgressIndicator = QLabel(trans._("in progress..."), self) sp = self._inProgressIndicator.sizePolicy() sp.setRetainSizeWhenHidden(True) self._inProgressIndicator.setSizePolicy(sp) load_gif = str(Path(napari.resources.__file__).parent / "loading.gif") mov = QMovie(load_gif) mov.setScaledSize(QSize(18, 18)) self._inProgressIndicator.setMovie(mov) self._inProgressIndicator.hide() self.layout().addWidget(self._inProgressIndicator) self.layout().addWidget(self._activityBtn) self.layout().setContentsMargins(0, 0, 0, 0)
def create_toolbutton(parent, text=None, shortcut=None, icon=None, tip=None, toggled=None, triggered=None, autoraise=True, text_beside_icon=False): """Create a QToolButton""" button = QToolButton(parent) if text is not None: button.setText(text) if icon is not None: if is_text_string(icon): icon = get_icon(icon) button.setIcon(icon) if text is not None or tip is not None: button.setToolTip(text if tip is None else tip) if text_beside_icon: button.setToolButtonStyle(Qt.ToolButtonTextBesideIcon) button.setAutoRaise(autoraise) if triggered is not None: button.clicked.connect(triggered) if toggled is not None: button.toggled.connect(toggled) button.setCheckable(True) if shortcut is not None: button.setShortcut(shortcut) return button
def _add_toolbar_group(self, group, func, default_value): button_group = QButtonGroup(parent=self.toolbar) for element in group: icon = self.icons.get(element, None) button = QToolButton() button.setFixedSize(QSize(*self.icon_size)) button.setIcon(icon) button.setCheckable(True) if default_value is not None and element is default_value: button.setChecked(True) button.toggled.connect(DefaultFunction(func, element)) button_group.addButton(button) self.toolbar.addWidget(button)
def visualize(self, canvas, **canvases): from qtpy.QtWidgets import QToolButton from qtpy.QtGui import QIcon canvas = canvases["toolbar"] # type:QToolBar if callable(canvas): canvas = canvas() button = QToolButton() button.setIcon(QIcon(self.iconpath)) button.setCheckable(True) button.toggled.connect( lambda state: setattr(self.activated, self.targetattribute, state)) canvas.addWidget(button)
def paint(self, painter, option, index): if not self._parent.indexWidget(index): button = QToolButton(self.parent()) button.setText("i") button.setAutoRaise(True) button.setIcon(enableicon) button.setCheckable(True) sp = QSizePolicy() sp.setWidthForHeight(True) button.setSizePolicy(sp) button.clicked.connect(index.data()) self._parent.setIndexWidget(index, button)
def _add_toolbar_toggles(self): for toggle in Toggle: icon = self.icons.get(toggle, None) button = QToolButton() button.setFixedSize(QSize(*self.icon_size)) button.setIcon(icon) button.setCheckable(True) toggle_name = toggle.name.lower() default_value = self.params["builder"]["toggles"][toggle_name] func_name = "toggle_{}".format(toggle_name) func = getattr(self, func_name, None) assert callable(func) button.toggled.connect(func) button.setChecked(default_value) func(default_value) self.toolbar.addWidget(button)