def paintEvent(self, event): p = QStylePainter(self) opt = QStyleOptionToolButton() self.initStyleOption(opt) if self.__text: # Replace the text opt.text = self.__text p.drawComplexControl(QStyle.CC_ToolButton, opt) p.end()
def paint(self, painter, option, index): """Paint a toolbutton with an icon.""" super(ToolButtonDelegate, self).paint(painter, option, index) opt = QStyleOptionToolButton() opt.rect = self.get_btn_rect(option) opt.iconSize = icons.get_iconsize('small') opt.state |= QStyle.State_Enabled | QStyle.State_Raised opt.icon = icons.get_icon('erase-right') QApplication.style().drawControl(QStyle.CE_ToolButtonLabel, opt, painter)
def __textLayout(self): fm = QFontMetrics(self.font()) text = six.text_type(self.defaultAction().iconText()) words = deque(text.split()) lines = [] curr_line = "" curr_line_word_count = 0 option = QStyleOptionToolButton() option.initFrom(self) margin = self.style().pixelMetric(QStyle.PM_ButtonMargin, option, self) width = self.width() - 2 * margin while words: w = words.popleft() if curr_line_word_count: line_extended = " ".join([curr_line, w]) else: line_extended = w line_w = fm.boundingRect(line_extended).width() if line_w >= width: if curr_line_word_count == 0 or len(lines) == 1: # A single word that is too long must be elided. # Also if the text overflows 2 lines # Warning: hardcoded max lines curr_line = fm.elidedText(line_extended, Qt.ElideRight, width) curr_line = six.text_type(curr_line) else: # Put the word back words.appendleft(w) lines.append(curr_line) curr_line = "" curr_line_word_count = 0 if len(lines) == 2: break else: curr_line = line_extended curr_line_word_count += 1 if curr_line: lines.append(curr_line) text = "\n".join(lines) self.__text = text
def paintEvent(self, event) -> None: qp = QPainter() qp.begin(self) # Get default style. opt = QStyleOptionToolButton() self.initStyleOption(opt) # Scale icon to button size. Rect = opt.rect h = Rect.height() w = Rect.width() iconSize = max(h, w) # - 2 * self.pad opt.iconSize = QSize(iconSize, iconSize) # Draw self.style().drawComplexControl(QStyle.CC_ToolButton, opt, qp, self) qp.end()
def paintEvent(self, arg1): p = QStylePainter(self) opt = QStyleOptionToolButton() self.initStyleOption(opt) # Disable the menu arrow, since we already got a down arrow icon opt.features &= ~QStyleOptionToolButton.HasMenu p.drawComplexControl(QStyle.CC_ToolButton, opt)
def __init__(self, parent=None): """ Constructor @param parent reference to the parent widget @type QWidget """ super(E5ToolButton, self).__init__(parent) self.setMinimumWidth(16) self.__menu = None self.__options = E5ToolButton.NoOptions self.__badgeLabel = QLabel(self) font = self.__badgeLabel.font() font.setPixelSize(self.__badgeLabel.height() / 2.5) self.__badgeLabel.setFont(font) self.__badgeLabel.hide() opt = QStyleOptionToolButton() self.initStyleOption(opt) self.__pressTimer = QTimer() self.__pressTimer.setSingleShot(True) self.__pressTimer.setInterval(QApplication.style().styleHint( QStyle.SH_ToolButton_PopupDelay, opt, self)) self.__pressTimer.timeout.connect(self.__showMenu)
def paintEvent(self, event): if self.__flat: opt = QStyleOptionToolButton() self.initStyleOption(opt) p = QStylePainter(self) p.drawControl(QStyle.CE_ToolButtonLabel, opt) else: QToolButton.paintEvent(self, event)
def sizeHint(self): opt = QStyleOptionToolButton() self.initStyleOption(opt) if self.__showMenuIndicator and self.isChecked(): opt.features |= QStyleOptionToolButton.HasMenu style = self.style() hint = style.sizeFromContents(QStyle.CT_ToolButton, opt, opt.iconSize, self) return hint
def paintEvent(self, event: QtGui.QPaintEvent) -> None: if not self.paint: return sp = QStylePainter(self) opt = QStyleOptionToolButton() self.initStyleOption(opt) opt.state |= (QStyle.State_MouseOver | QStyle.State_AutoRaise | QStyle.State_Raised) # type: ignore opt.activeSubControls |= QStyle.SC_ToolButton # type: ignore sp.drawComplexControl(QStyle.CC_ToolButton, opt)
def mousePressEvent_no(self, event): if event.button() == Qt.LeftButton and self.popupMode( ) == QToolButton.MenuButtonPopup: option = QStyleOptionToolButton() self.initStyleOption(option) position = self.style().subControlRect(QStyle.CC_ToolButton, option, QStyle.SC_ToolButtonMenu, self).center() event = event.__class__(event.type(), position, self.mapToGlobal(position), event.button(), event.buttons(), event.modifiers()) return super(AccountState, self).mousePressEvent(event)
def paintEvent(self, event): p = QPainter(self) r = self.rect() opt = QStyleOptionToolButton() opt.init(self) opt.state |= QStyle.State_AutoRaise if self.isEnabled() and self.underMouse() and \ not self.isChecked() and not self.isDown(): opt.state |= QStyle.State_Raised if self.isChecked(): opt.state |= QStyle.State_On if self.isDown(): opt.state |= QStyle.State_Sunken self.style().drawPrimitive(QStyle.PE_PanelButtonTool, opt, p, self) opt.icon = self.icon() opt.subControls = QStyle.SubControls() opt.activeSubControls = QStyle.SubControls() opt.features = QStyleOptionToolButton.None opt.arrowType = Qt.NoArrow size = self.style().pixelMetric(QStyle.PM_SmallIconSize, None, self) opt.iconSize = QSize(size, size) self.style().drawComplexControl(QStyle.CC_ToolButton, opt, p, self)
def paintEvent(self, event): opt = QStyleOptionToolButton() self.initStyleOption(opt) if self.__showMenuIndicator and self.isChecked(): opt.features |= QStyleOptionToolButton.HasMenu if self.__flat: # Use default widget background/border styling. StyledWidget_paintEvent(self, event) p = QStylePainter(self) p.drawControl(QStyle.CE_ToolButtonLabel, opt) else: p = QStylePainter(self) p.drawComplexControl(QStyle.CC_ToolButton, opt)
def paintEvent(self, event: QPaintEvent): painter = QPainter() painter.begin(self) option = QStyleOptionToolButton() self.initStyleOption(option) painter.setRenderHint(QPainter.Antialiasing) painter.setPen(QPen(self.pen(option), 1.5)) painter.setBrush(self.brush(option)) painter.drawEllipse(self.rect().adjusted(1, 1, -1, -1)) self.style().drawControl(QStyle.CE_ToolButtonLabel, option, painter, self)
def setup_elapsed_timer(self): """Setup a digital clock to show the elpased time.""" self.elap_timer = ElapsedTimeLCDNumber() self.elap_timer.setToolTip( "<b>Elapsed Time</b><br><br>" "Time elapsed since the start of the activity currently" " being monitored.<br><br>" "The reference time used to calculate the elapsed time depends" " on the option selected in the \"Start from\" menu located in" " the bottom toolbar.") size_hint = self.elap_timer.sizeHint() size_ratio = size_hint.width() / size_hint.height() fix_height = self.buttons['start'].style().sizeFromContents( QStyle.CT_ToolButton, QStyleOptionToolButton(), icons.get_iconsize(self.__iconsize)).height() fix_width = fix_height * size_ratio self.elap_timer.setFixedSize(fix_width, fix_height) return self.elap_timer
# %% if __name__ == '__main__' if __name__ == '__main__': app = QApplication(sys.argv) drop_down_btn = DropDownToolButton(style=('text_beside')) drop_down_btn.setSizePolicy( QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)) drop_down_btn.addItems( ['round to 1min', 'round to 5min', 'round to 10min']) onoff_button = OnOffToolButton('process_start', 'process_stop', 'normal') toolbar = ToolBarWidget() toolbar.addStretch(100) toolbar.addWidget(onoff_button) toolbar.addWidget(drop_down_btn) toolbar.addWidget(QToolButtonNormal('home')) toolbar.addWidget(None) toolbar.addWidget(HistoryNavigationWidget('tiny')) toolbar.show() size = toolbar.style().sizeFromContents(QStyle.CT_ToolButton, QStyleOptionToolButton(), icons.get_iconsize('normal')) print(size) sys.exit(app.exec_())
def __paintEventNoStyle(self): p = QPainter(self) opt = QStyleOptionToolButton() self.initStyleOption(opt) fm = QFontMetrics(opt.font) palette = opt.palette # highlight brush is used as the background for the icon and background # when the tab is expanded and as mouse hover color (lighter). brush_highlight = palette.highlight() foregroundrole = QPalette.ButtonText if opt.state & QStyle.State_Sunken: # State 'down' pressed during a mouse press (slightly darker). background_brush = brush_darker(brush_highlight, 110) foregroundrole = QPalette.HighlightedText elif opt.state & QStyle.State_MouseOver: background_brush = brush_darker(brush_highlight, 95) foregroundrole = QPalette.HighlightedText elif opt.state & QStyle.State_On: background_brush = brush_highlight foregroundrole = QPalette.HighlightedText else: # The default button brush. background_brush = palette.button() rect = opt.rect icon_area_rect = QRect(rect) icon_area_rect.setRight(int(icon_area_rect.height() * 1.26)) text_rect = QRect(rect) text_rect.setLeft(icon_area_rect.right() + 10) # Background (TODO: Should the tab button have native # toolbutton shape, drawn using PE_PanelButtonTool or even # QToolBox tab shape) # Default outline pen pen = QPen(palette.color(QPalette.Mid)) p.save() p.setPen(Qt.NoPen) p.setBrush(QBrush(background_brush)) p.drawRect(rect) # Draw the background behind the icon if the background_brush # is different. if not opt.state & QStyle.State_On: p.setBrush(brush_highlight) p.drawRect(icon_area_rect) # Line between the icon and text p.setPen(pen) p.drawLine(icon_area_rect.topRight(), icon_area_rect.bottomRight()) if opt.state & QStyle.State_HasFocus: # Set the focus frame pen and draw the border pen = QPen(QColor(FOCUS_OUTLINE_COLOR)) p.setPen(pen) p.setBrush(Qt.NoBrush) # Adjust for pen rect = rect.adjusted(0, 0, -1, -1) p.drawRect(rect) else: p.setPen(pen) # Draw the top/bottom border if self.position == QStyleOptionToolBox.OnlyOneTab or \ self.position == QStyleOptionToolBox.Beginning or \ self.selected & \ QStyleOptionToolBox.PreviousIsSelected: p.drawLine(rect.topLeft(), rect.topRight()) p.drawLine(rect.bottomLeft(), rect.bottomRight()) p.restore() p.save() text = fm.elidedText(opt.text, Qt.ElideRight, text_rect.width()) p.setPen(QPen(palette.color(foregroundrole))) p.setFont(opt.font) p.drawText(text_rect, int(Qt.AlignVCenter | Qt.AlignLeft) | \ int(Qt.TextSingleLine), text) if not opt.icon.isNull(): if opt.state & QStyle.State_Enabled: mode = QIcon.Normal else: mode = QIcon.Disabled if opt.state & QStyle.State_On: state = QIcon.On else: state = QIcon.Off icon_area_rect = icon_area_rect icon_rect = QRect(QPoint(0, 0), opt.iconSize) icon_rect.moveCenter(icon_area_rect.center()) opt.icon.paint(p, icon_rect, Qt.AlignCenter, mode, state) p.restore()
def paintEvent(self, event): painter = QStylePainter(self) option = QStyleOptionToolButton() self.initStyleOption(option) option.icon = self.animation_icons[self.animation_icon_index] painter.drawComplexControl(QStyle.CC_ToolButton, option)
def drawComplexControl( self, cc: QStyle.ComplexControl, opt: QStyleOptionComplex, p: QPainter, widget: QWidget, ): if cc == QStyle.CC_ToolButton: toolbutton = opt if toolbutton: button = super().subControlRect( cc, toolbutton, QStyle.SC_ToolButton, widget ) menuarea = super().subControlRect( cc, toolbutton, QStyle.SC_ToolButtonMenu, widget ) bflags = toolbutton.state & ~QStyle.State_Sunken # type: ignore if bflags & QStyle.State_AutoRaise: if not (bflags & QStyle.State_MouseOver) or ( not (bflags & QStyle.State_Enabled) ): bflags &= ~QStyle.State_Raised mflags = bflags if toolbutton.state & QStyle.State_Sunken: # type: ignore if toolbutton.activeSubControls & QStyle.SC_ToolButton: # type: ignore bflags |= QStyle.State_Sunken mflags |= QStyle.State_Sunken tool = QStyleOption() tool.palette = toolbutton.palette if toolbutton.subControls & QStyle.SC_ToolButton: # type: ignore if bflags & ( QStyle.State_Sunken | QStyle.State_On | QStyle.State_Raised ): tool.rect = button tool.state = bflags # type: ignore self.proxy().drawPrimitive( QStyle.PE_PanelButtonTool, tool, p, widget ) if toolbutton.state & QStyle.State_HasFocus: # type: ignore fr = QStyleOptionFocusRect(toolbutton) # type: ignore fr.state = toolbutton.state fr.direction = toolbutton.direction fr.rect = QtCore.QRect(toolbutton.rect) fr.fontMetrics = toolbutton.fontMetrics fr.palette = toolbutton.palette fr.rect.adjust(3, 3, -3, -3) if toolbutton.features & QStyleOptionToolButton.MenuButtonPopup: # type: ignore fr.rect.adjust( 0, 0, -self.proxy().pixelMetric( QStyle.PM_MenuButtonIndicator, toolbutton, widget ), 0, ) self.proxy().drawPrimitive( QStyle.PE_FrameFocusRect, fr, p, widget ) label = QStyleOptionToolButton(toolbutton) # type: ignore label.state = bflags # type: ignore fw = self.proxy().pixelMetric(QStyle.PM_DefaultFrameWidth, opt, widget) label.rect = button.adjusted(fw, fw, -fw, -fw) self.proxy().drawControl(QStyle.CE_ToolButtonLabel, label, p, widget) if toolbutton.subControls & QStyle.SC_ToolButtonMenu: # type: ignore tool.rect = menuarea tool.state = mflags # type: ignore if mflags & ( QStyle.State_Sunken | QStyle.State_On | QStyle.State_Raised ): self.proxy().drawPrimitive( QStyle.PE_IndicatorButtonDropDown, tool, p, widget ) self.proxy().drawPrimitive( QStyle.PE_IndicatorArrowDown, tool, p, widget ) elif toolbutton.features & QStyleOptionToolButton.HasMenu: # type: ignore mbi = self.proxy().pixelMetric( QStyle.PM_MenuButtonIndicator, toolbutton, widget ) ir = toolbutton.rect new_toolbutton = toolbutton new_toolbutton.rect = QtCore.QRect( int(ir.center().x() + 1 - (mbi - 6) / 2), ir.y() + ir.height() - mbi + 4, mbi - 6, mbi - 6, ) self.proxy().drawPrimitive( QStyle.PE_IndicatorArrowDown, new_toolbutton, p, widget ) return return super(TTToolButtonStyle, self).drawComplexControl(cc, opt, p, widget)
def paintEvent(self, event): painter = QStylePainter(self) option = QStyleOptionToolButton() self.initStyleOption(option) option.features &= ~QStyleOptionToolButton.HasMenu painter.drawComplexControl(QStyle.CC_ToolButton, option)