def paintEvent(self, _): option = QStyleOptionButton() self.initStyleOption(option) painter = QStylePainter(self) if self._rotateAnimationStarted: option.text = "" painter.drawControl(QStyle.CE_PushButton, option) if not self._rotateAnimationStarted: return painter.save() font = self.font() font.setPointSize(self.fontSize) font.setFamily("FontAwesome") painter.setFont(font) # 变换坐标为正中间 painter.translate(self.rect().center()) # 旋转90度 painter.rotate(self._rotateAnimation.currentValue() * 30) fm = self.fontMetrics() # 在变换坐标后的正中间画文字 w = fm.width(self.LoadingText) h = fm.height() painter.drawText(QRectF(0 - w * 2, 0 - h, w * 2 * 2, h * 2), Qt.AlignCenter, self.LoadingText) painter.restore()
def paintEvent(self, event): """绘制事件""" text = self.text() option = QStyleOptionButton() self.initStyleOption(option) option.text = '' # 不绘制文字 painter = QStylePainter(self) painter.setRenderHint(QStylePainter.Antialiasing) painter.setRenderHint(QStylePainter.HighQualityAntialiasing) painter.setRenderHint(QStylePainter.SmoothPixmapTransform) painter.drawControl(QStyle.CE_PushButton, option) # 变换坐标为正中间 painter.translate(self.rect().center()) painter.rotate(self._angle) # 旋转 # 绘制图片 if self._pixmap and not self._pixmap.isNull(): w = self.width() h = self.height() pos = QPointF(-self._pixmap.width() / 2, -self._pixmap.height() / 2) painter.drawPixmap(pos, self._pixmap) elif text: # 在变换坐标后的正中间画文字 fm = self.fontMetrics() w = fm.width(text) h = fm.height() rect = QRectF(0 - w * 2, 0 - h, w * 2 * 2, h * 2) painter.drawText(rect, Qt.AlignCenter, text) else: super(RotateButton, self).paintEvent(event)
def paintEvent(self, event): """Paint event.""" qp = QPainter() qp.begin(self) # ---- get default style ---- opt = QStyleOptionButton() self.initStyleOption(opt) # ---- scale icon to button size ---- Rect = opt.rect h = Rect.height() w = Rect.width() iconSize = max(min(h, w) - 2 * self.pad, self.minSize) opt.iconSize = QSize(iconSize, iconSize) # ---- draw button ---- self.style().drawControl(QStyle.CE_PushButton, opt, qp, self) qp.end()
def paint(self, painter, option, index): """Re-implemented paint method""" item = index.internalPointer() col = index.column() if not item.parent and (col == 3 or col == 4): s = QStyleOptionButton() checkbox_rect = QApplication.style(). \ subElementRect(QStyle.SE_CheckBoxIndicator, option) s.rect = option.rect center_offset = s.rect.width() / 2 - checkbox_rect.width() / 2 s.rect.adjust(center_offset, 0, 0, 0) if col == 3: if not item.doing_transaction(): s.state = QStyle.State_Enabled if item.logging_started(): s.state |= QStyle.State_On if col == 4: s.state = QStyle.State_Enabled if item.writing_to_file(): s.state |= QStyle.State_On self._paint_checkbox(s, painter) else: super(CheckboxDelegate, self).paint(painter, option, index)
def paint(self, painter, option, index): row = index.row() col = index.column() if col == SongModel.SECLECTION_INDEX: # 绘制 checkbox self.paint_hover_row(painter, option, index) selected_button = QStyleOptionButton() selected_button.state |= QStyle.State_Enabled selected_button.state |= QStyle.State_On if index.data( Qt.DisplayRole) else QStyle.State_Off selected_button.rect = get_center_rect(option.rect) QApplication.style().drawPrimitive( QStyle.PE_IndicatorCheckBox, selected_button, painter) return if col == SongModel.NAME_INDEX: if row == self.hover_row: # 设置背景颜色 super().paint_hover_row(painter, option, index) if not index.data(Qt.UserRole).available: super().paint(painter, option, index) return # 绘制下载按钮 self.download_button.rect = QRect(option.rect.right() - ButtonConfig.BUTTON_WIDTH, option.rect.top(), ButtonConfig.BUTTON_WIDTH, option.rect.height()) w = int(min(self.download_button.rect.width(), self.download_button.rect.height()) * 0.8) self.download_button.iconSize = QSize(w, w) QApplication.style().drawControl( QStyle.CE_PushButtonLabel, self.download_button, painter) # 绘制播放按钮 self.play_button.rect = QRect(self.download_button.rect.left() - ButtonConfig.BUTTON_WIDTH - ButtonConfig.BUTTON_MARGIN, option.rect.top(), ButtonConfig.BUTTON_WIDTH, option.rect.height()) if row != self.playing_row: self.play_button.icon = ButtonConfig.PLAY_ICON else: self.play_button.icon = ButtonConfig.PAUSE_ICON \ if self.play_manager.play_control.state() == QMediaPlayer.PlayingState \ else ButtonConfig.PLAY_ICON self.play_button.iconSize = QSize(w, w) QApplication.style().drawControl( QStyle.CE_PushButtonLabel, self.play_button, painter) # 调整文字绘制区域 option.rect.setRight( self.play_button.rect.left() - ButtonConfig.BUTTON_MARGIN) super().paint(painter, option, index)
def paint(self, painter: QtGui.QPainter, option: 'QStyleOptionViewItem', index: QtCore.QModelIndex) -> None: viewOption = QStyleOptionViewItem(option) self.initStyleOption(viewOption, index) QStyledItemDelegate.paint(self, painter, viewOption, index) item = index.model().data(index, Qt.UserRole) if isinstance(item, QVariant): return value = item['value'] type = item['type'] if type == 'checkbox': # 数据转换 value = True if value == 1 else 0 # 绘制单选框 checkBoxStyle = QStyleOptionButton() checkBoxStyle.state = QStyle.State_On if value else QStyle.State_Off checkBoxStyle.state |= QStyle.State_Enabled # 计算位置 size = item['size'] rect = calculate_middle_rect(option.rect, size, size) checkBoxStyle.rect = rect checkBox = QCheckBox() QApplication.style().drawPrimitive(QStyle.PE_IndicatorCheckBox, checkBoxStyle, painter, checkBox) if type == 'tag': painter.setRenderHint(QPainter.Antialiasing, True) painter.setRenderHints(QPainter.SmoothPixmapTransform) path = QPainterPath() # 绘制文本 self.font = ResourceLoader().qt_font_text_tag text_color = item['text_color'] border_color = item['border_color'] text = value padding_v = 2 padding_h = 4 border_radius = 2 border_width = 1 painter.setFont(item['font']) painter.setPen(text_color) fm = QFontMetrics(painter.font()) w = fm.width(text) h = fm.height() # 计算位置 rect = calculate_middle_rect(option.rect, w + padding_h * 2, h + padding_v * 2) rectf = QRectF(rect.x(), rect.y(), rect.width(), rect.height()) painter.drawText( QRect(rectf.x() + padding_h, rectf.y() + padding_v, w, h), Qt.TextWordWrap, text) # 绘制边框 path.addRoundedRect(rectf, border_radius, border_radius) painter.strokePath(path, QPen(border_color, border_width))
def getSyleOptions(self) -> QStyleOptionButton: options = QStyleOptionButton() options.initFrom(self) size = options.rect.size() size.transpose() options.rect.setSize(size) try: options.features = QStyleOptionButton.None_ except AttributeError: # Allow for bug in PyQt 5.4 options.features = getattr(QStyleOptionButton, "None") if self.isFlat(): options.features |= QStyleOptionButton.Flat if self.menu(): options.features |= QStyleOptionButton.HasMenu if self.autoDefault() or self.isDefault(): options.features |= QStyleOptionButton.AutoDefaultButton if self.isDefault(): options.features |= QStyleOptionButton.DefaultButton if self.isDown() or (self.menu() and self.menu().isVisible()): options.state |= QStyle.State_Sunken if self.isChecked(): options.state |= QStyle.State_On if not self.isFlat() and not self.isDown(): options.state |= QStyle.State_Raised options.text = self.text() options.icon = self.icon() options.iconSize = self.iconSize() return options
def paintCheckBoxColumn(self, painter, option, index): opt = QStyleOptionViewItem(option) buttonStyle = QStyleOptionButton() buttonStyle.state = QStyle.State_On if index.model().data(index, Qt.CheckStateRole) == QVariant(Qt.Checked) else QStyle.State_Off if option.state & QStyle.State_MouseOver or option.state & QStyle.State_HasFocus: buttonStyle.state |= QStyle.State_HasFocus buttonStyle.rect = opt.rect.adjusted(4, -opt.rect.height() + ROW_HEIGHT, 0, 0) PackageDelegate.AppStyle().drawControl(QStyle.CE_CheckBox, buttonStyle, painter, None)
def paint(self, painter, option, index): if not index.parent().isValid(): QStyledItemDelegate.paint(self, painter, option, index) else: widget = option.widget style = widget.style() if widget else QApplication.style() opt = QStyleOptionButton() opt.rect = option.rect opt.text = index.data() opt.state |= QStyle.State_On if index.data( Qt.CheckStateRole) else QStyle.State_Off style.drawControl(QStyle.CE_RadioButton, opt, painter, widget)
def paintEvent(self, event): p = QStylePainter(self) p.rotate(90) p.translate(0, -self.width()) opt = QStyleOptionButton() opt.initFrom(self) opt.text = self.text() if self.isChecked(): opt.state |= QStyle.State_On s = opt.rect.size().transposed() opt.rect.setSize(s) p.drawControl(QStyle.CE_PushButton, opt)
def paintEvent(self, event): p = QStylePainter(self) p.rotate(90) p.translate(0, - self.width()) opt = QStyleOptionButton() opt.initFrom(self) opt.text = self.text() if self.isChecked(): opt.state |= QStyle.State_On s = opt.rect.size().transposed() opt.rect.setSize(s) p.drawControl(QStyle.CE_PushButton, opt)
def paint(self, painter, option, index): self.initStyleOption(option, index) painter.save() if option.state & QStyle.State_Selected: painter.fillRect(option.rect, option.palette.highlight()) checkBox = QStyleOptionButton() checkBox.rect = self.getCheckBoxRect(option) checked = bool(index.model().data(index, roles['checked'])) if checked: checkBox.state |= QStyle.State_On else: checkBox.state |= QStyle.State_Off QApplication.style().drawControl(QStyle.CE_CheckBox, checkBox, painter) headerText = index.data(roles['from']) subjectText = index.data(roles['subject']) headerFont = QFont("times", 10) headerFont.setBold(True) subjectFont = QFont("times", 10) headerFm = QFontMetrics(headerFont) subjectFm = QFontMetrics(subjectFont) headerRect = headerFm.boundingRect( option.rect.left() + self.iconSize.width(), option.rect.top() + self.padding, option.rect.width() - self.iconSize.width(), 0, Qt.AlignLeft | Qt.AlignTop | Qt.TextWordWrap, headerText) subjectRect = subjectFm.boundingRect( headerRect.left(), headerRect.bottom() + self.padding, option.rect.width() - self.iconSize.width(), 0, Qt.AlignLeft | Qt.AlignTop | Qt.TextWordWrap, subjectText) painter.setPen(Qt.black) painter.setFont(headerFont) painter.drawText(headerRect, Qt.AlignLeft | Qt.AlignTop | Qt.TextWordWrap, headerText) painter.setFont(subjectFont) painter.drawText(subjectRect, Qt.AlignLeft | Qt.AlignTop | Qt.TextWordWrap, subjectText) painter.setPen(Qt.gray) painter.drawLine(option.rect.left(), option.rect.bottom(), option.rect.right(), option.rect.bottom()) painter.restore()
def paintEvent(self, event): opt = QStyleOptionButton() if self.state == _state_up: opt.state = QStyle.State_Active | QStyle.State_Enabled elif self.state in [_state_pressing, _state_down]: opt.state = QStyle.State_Active | QStyle.State_Enabled | QStyle.State_Sunken else: assert (False) if self.hover: opt.state |= QStyle.State_MouseOver opt.rect = self.frameRect() self.style().drawControl(QStyle.CE_PushButton, opt, QPainter(self)) _QFrame.paintEvent(self, event)
def __init__(self, parent=None, download_manager=None, play_manager=None): super().__init__(parent) self.playing_row = -1 # 当前播放行 self.download_button = QStyleOptionButton() self.download_button.state |= QStyle.State_Enabled self.download_button.icon = ButtonConfig.DOWNLOAD_ICON self.play_button = QStyleOptionButton() self.play_button.state |= QStyle.State_Enabled self.download_manager = download_manager self.play_manager = play_manager
def paintSection(self, painter: QPainter, rect: QRect, logicalIndex: int) -> None: painter.save() super().paintSection(painter, rect, logicalIndex) painter.restore() if logicalIndex == 0: option = QStyleOptionButton() option.rect = self.rect option.state = QStyle.State_Active if self.isEnabled(): option.state |= QStyle.State_Enabled option.state |= self.checkStateToStyle() self.style().drawControl(QStyle.CE_CheckBox, option, painter)
def paintEvent(self, event): opt = QStyleOptionButton() if self.state == _state_up: opt.state = QStyle.State_Active | QStyle.State_Enabled elif self.state in [_state_pressing, _state_down]: opt.state = QStyle.State_Active | QStyle.State_Enabled | QStyle.State_Sunken else: assert(False) if self.hover: opt.state |= QStyle.State_MouseOver opt.rect = self.frameRect() self.style().drawControl(QStyle.CE_PushButton, opt, QPainter(self)); _QFrame.paintEvent(self, event)
def paintSection(self, painter, rect, logicalIndex): painter.save() QHeaderView.paintSection(self, painter, rect, logicalIndex) painter.restore() if logicalIndex == 0: option = QStyleOptionButton() option.rect = QRect(10, 10, 10, 10) if self.isOn: option.state = QStyle.State_On else: option.state = QStyle.State_Off self.style().drawControl(QStyle.CE_CheckBox, option, painter) self.resizeSection(0, 34) self.resizeSection(3, 50)
def getStyleOptions(self): options = QStyleOptionButton() options.initFrom(self) size = options.rect.size() if self.orientation == self.Vertical: size.transpose() options.rect.setSize(size) options.features = QStyleOptionButton.None_ if self.isFlat(): options.features |= QStyleOptionButton.Flat if self.menu(): options.features |= QStyleOptionButton.HasMenu if self.autoDefault() or self.isDefault(): options.features |= QStyleOptionButton.AutoDefaultButton if self.isDefault(): options.features |= QStyleOptionButton.DefaultButton if self.isDown() or (self.menu() and self.menu().isVisible()): options.state |= QStyle.State_Sunken if self.isChecked(): options.state |= QStyle.State_On if not self.isFlat() and not self.isDown(): options.state |= QStyle.State_Raised options.text = self.text() options.icon = self.icon() options.iconSize = self.iconSize() return options
def paintEvent(self, event): """QToolBar.paintEvent reimplementation Draws buttons, dock icon and text """ rect = self._spacer.rect() painter = QPainter(self) transform = QTransform() transform.translate(self._spacer.pos().x(), self._spacer.pos().y()) painter.setTransform(transform) """ Not supported currently if self._dock.features() & QDockWidget.DockWidgetVerticalTitleBar : transform = QTransform() rect.setSize(QSize(rect.height(), rect.width())) transform.rotate(-90) transform.translate(-rect.width(), 0) painter.setTransform(transform) """ # icon / title optionB = QStyleOptionButton() optionB.initFrom(self._dock) optionB.rect = rect optionB.text = self._dock.windowTitle() optionB.iconSize = self.iconSize() optionB.icon = self._dock.windowIcon() self.style().drawControl(QStyle.CE_PushButtonLabel, optionB, painter, self._dock)
def paintEvent(self, event): self._initAnimate() painter = QStylePainter(self) painter.setRenderHint(QPainter.Antialiasing, True) painter.setRenderHint(QPainter.HighQualityAntialiasing, True) painter.setRenderHint(QPainter.SmoothPixmapTransform, True) painter.setBrush(QColor(self._bgcolor)) painter.setPen(QColor(self._bgcolor)) painter.drawEllipse(QRectF( (self.minimumWidth() - self._width) / 2, (self.minimumHeight() - self._height) / 2, self._width, self._height )) # 绘制本身的文字和图标 options = QStyleOptionButton() options.initFrom(self) size = options.rect.size() size.transpose() options.rect.setSize(size) options.features = QStyleOptionButton.Flat options.text = self.text() options.icon = self.icon() options.iconSize = self.iconSize() painter.drawControl(QStyle.CE_PushButton, options) event.accept()
def paintEvent(self, event): qp = QPainter() qp.begin(self) # get default style opt = QStyleOptionButton() self.initStyleOption(opt) # scale icon to button size h = opt.rect.height() w = opt.rect.width() icon_size = max(min(h, w) - 2 * self.pad, self.minSize) opt.iconSize = QSize(icon_size, icon_size) # draw button self.style().drawControl(QStyle.CE_PushButton, opt, qp, self) qp.end()
def paint(self, painter, option, index): """ Draw button in cell. """ opts = QStyleOptionButton() opts.state |= QStyle.State_Active opts.state |= QStyle.State_Enabled if QT_VERSION_STR[0] == '4': opts.state |= (QStyle.State_Sunken if self._isMousePressed else QStyle.State_Raised) elif QT_VERSION_STR[0] == '5': # When raised in PyQt5, white text cannot be seen on white background. # Should probably fix this by initializing form styled button, but for now I'll just sink it all the time. opts.state |= QStyle.State_Sunken opts.rect = option.rect opts.text = self.text QApplication.style().drawControl(QStyle.CE_PushButton, opts, painter)
def drawSelection(self, page, painter): """Draws the state (selected or not) for the page.""" option = QStyleOptionButton() option.initFrom(self) option.rect = QRect(0, 0, QStyle.PM_IndicatorWidth, QStyle.PM_IndicatorHeight) pageNum = self.pageLayout().index(page) + 1 option.state |= QStyle.State_On if pageNum in self._selection else QStyle.State_Off scale = None # in the unlikely case the checkboxes are larger than the page, scale them down if option.rect not in page.rect(): scale = min(page.width / option.rect.width(), page.height / option.rect.height()) painter.save() painter.scale(scale, scale) self.style().drawPrimitive(QStyle.PE_IndicatorCheckBox, option, painter, self) if scale is not None: painter.restore()
def getCheckBoxRect(self, option): check_box_style_option = QStyleOptionButton() check_box_rect = QApplication.style().subElementRect( QStyle.SE_CheckBoxIndicator, check_box_style_option, None) check_box_point = QPoint(option.rect.x() + check_box_rect.width() / 2, option.rect.y() + check_box_rect.height() / 2) return QRect(check_box_point, check_box_rect.size())
def paintCheckBoxColumn(self, painter, option, index): opt = QStyleOptionViewItem(option) buttonStyle = QStyleOptionButton() buttonStyle.state = QStyle.State_On if index.model().data( index, Qt.CheckStateRole) == QVariant( Qt.Checked) else QStyle.State_Off if option.state & QStyle.State_MouseOver or option.state & QStyle.State_HasFocus: buttonStyle.state |= QStyle.State_HasFocus buttonStyle.rect = opt.rect.adjusted(4, -opt.rect.height() + ROW_HEIGHT, 0, 0) PackageDelegate.AppStyle().drawControl(QStyle.CE_CheckBox, buttonStyle, painter, None)
def paintSection(self, painter, rect, logicalIndex): painter.save() super(CheckBoxHeader, self).paintSection(painter, rect, logicalIndex) painter.restore() self._y_offset = int((rect.height()-self._width)/2.) if logicalIndex == 0: option = QStyleOptionButton() option.rect = QRect(rect.x() + self._x_offset, rect.y() + self._y_offset, self._width, self._height) option.state = QStyle.State_Enabled | QStyle.State_Active if self.isOn: option.state |= QStyle.State_On else: option.state |= QStyle.State_Off self.style().drawControl(QStyle.CE_CheckBox, option, painter)
def paint(self, painter, option, index): """ Paint a checkbox without the label. """ checked = bool(index.model().data(index, Qt.DisplayRole)) opts = QStyleOptionButton() opts.state |= QStyle.State_Active if index.flags() & Qt.ItemIsEditable: opts.state |= QStyle.State_Enabled else: opts.state |= QStyle.State_ReadOnly if checked: opts.state |= QStyle.State_On else: opts.state |= QStyle.State_Off opts.rect = self.getCheckBoxRect(option) QApplication.style().drawControl(QStyle.CE_CheckBox, opts, painter)
def paint(self, painter: QPainter, option: QStyleOptionViewItem, model_index: QModelIndex): column = model_index.column() new_rect = QRect(option.rect) if column == NAME_COL: # Part Name option.displayAlignment = Qt.AlignVCenter QStyledItemDelegate.paint(self, painter, option, model_index) if column == LOCKED_COL: # Visibility element = _QCOMMONSTYLE.PE_IndicatorCheckBox styleoption = QStyleOptionButton() styleoption.rect = new_rect checked = model_index.model().data(model_index, Qt.EditRole) styleoption.state |= QStyle.State_On if checked else QStyle.State_Off # make the check box look a little more active by changing the pallete styleoption.palette.setBrush(QPalette.Button, Qt.white) styleoption.palette.setBrush(QPalette.HighlightedText, Qt.black) _QCOMMONSTYLE.drawPrimitive(element, styleoption, painter) if checked: # element = _QCOMMONSTYLE.PE_IndicatorMenuCheckMark # _QCOMMONSTYLE.drawPrimitive(element, styleoption, painter) # _QCOMMONSTYLE.drawItemText(painter, new_rect, Qt.AlignCenter, styleoption.palette, True, 'L') icon = QPixmap(":/outlinericons/lock") _QCOMMONSTYLE.drawItemPixmap(painter, new_rect, Qt.AlignCenter, icon) if column == VISIBLE_COL: # Visibility element = _QCOMMONSTYLE.PE_IndicatorCheckBox styleoption = QStyleOptionButton() styleoption.rect = new_rect checked = model_index.model().data(model_index, Qt.EditRole) styleoption.state |= QStyle.State_On if checked else QStyle.State_Off # make the check box look a little more active by changing the pallete styleoption.palette.setBrush(QPalette.Button, Qt.white) styleoption.palette.setBrush(QPalette.HighlightedText, Qt.black) _QCOMMONSTYLE.drawPrimitive(element, styleoption, painter) if checked: # element = _QCOMMONSTYLE.PE_IndicatorMenuCheckMark # _QCOMMONSTYLE.drawPrimitive(element, styleoption, painter) icon = QPixmap(":/outlinericons/eye") _QCOMMONSTYLE.drawItemPixmap(painter, new_rect, Qt.AlignCenter, icon) elif column == COLOR_COL: # Color # Alternate way to get color # outline_tw = self.parent() # item = outline_tw.itemFromIndex(model_index) # color = item.getColor() # print("COLOR_COL", item) color = model_index.model().data(model_index, Qt.EditRole) element = _QCOMMONSTYLE.PE_IndicatorCheckBox styleoption = QStyleOptionViewItem() brush = getBrushObj(color) styleoption.palette.setBrush(QPalette.Button, brush) styleoption.rect = new_rect _QCOMMONSTYLE.drawPrimitive(element, styleoption, painter) else: QStyledItemDelegate.paint(self, painter, option, model_index)
def getCheckBoxRect(self, option): """Calculate the size and position of the checkbox.""" cb_rect = QApplication.style().subElementRect( QStyle.SE_CheckBoxIndicator, QStyleOptionButton(), None) x = option.rect.x() + option.rect.width() / 2 - cb_rect.width() / 2 y = option.rect.y() + option.rect.height() / 2 - cb_rect.height() / 2 return QRect(QPoint(x, y), cb_rect.size())
def __init__(self, orientation, model, parent=None): super().__init__(orientation, parent) self.model = model self.selected_button = QStyleOptionButton() self.selected_button.state |= QStyle.State_Enabled self.selected_button.state |= QStyle.State_Off
def __init__(self, parent): QItemDelegate.__init__(self, parent) checkboxRect = QRect( QApplication.style().subElementRect( QStyle.SE_CheckBoxIndicator, QStyleOptionButton(), None ) ) self.checkboxHalfWidth = int(checkboxRect.width() / 2)
def paint(self, painter, option, index): if index.column() == 2: button = QStyleOptionButton() r = option.rect # getting the rect of the cell x = r.left() y = r.top() w = r.width() h = r.height() button.rect = QRect(x, y, w, h) button.text = "X" button.state = QStyle.State_Enabled; QApplication.style().drawControl(QStyle.CE_PushButton, button, painter) else: QStyledItemDelegate.paint(self, painter, option, index)
def hitText(self, point): """only change if text is clicked""" style = QStyle.SE_CheckBoxContents option = QStyleOptionButton() self.initStyleOption(option) ret = QApplication.style().subElementRect(style, option, self).contains(point) return ret
def getSyleOptions(self): options = QStyleOptionButton() options.initFrom(self) size = options.rect.size() size.transpose() options.rect.setSize(size) options.features = QStyleOptionButton.DefaultButton options.text = self.text() options.icon = self.icon() options.iconSize = self.iconSize() return options
def paintEvent(self, e): painter = QPainter(self) # Draw empty button (no label or icon). buttonStyle = QStyleOptionButton() self.initStyleOption(buttonStyle) buttonStyle.text = '' buttonStyle.icon = QIcon() buttonStyle.iconSize = QSize(-1, -1) self.style().drawControl(QStyle.CE_PushButton, buttonStyle, painter, self) # Get button label style. labelStyle = QStyleOptionButton() self.initStyleOption(labelStyle) labelStyle.rect = self.style().subElementRect(QStyle.SE_PushButtonContents, labelStyle, self) # Clip everything we paint to label area. painter.setClipRect(labelStyle.rect) painter.setClipping(True) # TODO painter.translate(-200, 0); # Has to happen after we set clipping. # Draw label (optionally with icon), similar to how Qt does it (src/widgets/styles/qcommonstyle.cpp in Qt 5.3). self.style().drawControl(QStyle.CE_PushButtonLabel, labelStyle, painter, self)
def getSyleOptions(self): """QPushButton的Options""" options = QStyleOptionButton() options.initFrom(self.clazz) size = options.rect.size() size.transpose() options.rect.setSize(size) options.features = QStyleOptionButton.DefaultButton options.text = self.clazz.text() options.icon = self.clazz.icon() options.iconSize = self.clazz.iconSize() return options
def paint(self, painter, option, index): """Paint a checkbox without the label.""" # print(index.data()) # checked = True check_box_style_option = QStyleOptionButton() if int(index.flags() & Qt.ItemIsEditable) > 0: check_box_style_option.state |= QStyle.State_Enabled else: check_box_style_option.state |= QStyle.State_ReadOnly if bool(index.data()) is True: check_box_style_option.state |= QStyle.State_On else: check_box_style_option.state |= QStyle.State_Off check_box_style_option.rect = self.getCheckBoxRect(option) check_box_style_option.state |= QStyle.State_Enabled QApplication.style().drawControl(QStyle.CE_CheckBox, check_box_style_option, painter)
def paintInfoColumn(self, painter, option, index, width_limit = 0): left = option.rect.left() + 3 top = option.rect.top() width = option.rect.width() - width_limit pixmap = QPixmap(option.rect.size()) pixmap.fill(Qt.transparent) p = QPainter(pixmap) p.setRenderHint(QPainter.Antialiasing, True) p.translate(-option.rect.topLeft()) textInner = 2 * ICON_PADDING + ROW_HEIGHT - 10 itemHeight = ROW_HEIGHT + 2 * ICON_PADDING margin = left + ICON_PADDING - 10 title = QVariant.value(index.model().data(index, NameRole)) summary = QVariant.value(index.model().data(index, SummaryRole)) ptype = QVariant.value(index.model().data(index, TypeRole)) rate = int(QVariant.value(index.model().data(index, RateRole))) if QVariant.value(index.model().data(index, RateRole))!= None else 0 installed = True if QVariant.value(index.model().data(index, InstalledRole))=="True" else False # We need to request update if its not possible to get meta data about the package try: # Get Package Icon if exists _icon = QVariant.value(index.model().data(index, Qt.DecorationRole)) except: p.end() painter.drawPixmap(option.rect.topLeft(), pixmap) self.parent.requestUpdate() return icon = None if _icon: overlay = [CHECK_ICON] if installed else [] KIconLoader._forceCache = True pix = KIconLoader.loadOverlayed(_icon, overlay, 32) if not pix.isNull(): icon = QIcon(pix.scaled(QSize(32, 32), Qt.KeepAspectRatio, Qt.SmoothTransformation)) KIconLoader._forceCache = False if not icon: icon = self.defaultIcon if not installed else self.defaultInstalledIcon # Paint the Icon icon.paint(p, margin, top + ICON_PADDING, ROW_HEIGHT, ROW_HEIGHT, Qt.AlignCenter) fix_pos = 0 if index.model().columnCount() <= 1: fix_pos = 22 if config.USE_APPINFO: # Rating Stars for _rt_i in range(5): self._rt_0.paint(p, width + 10 * _rt_i - 30 - fix_pos, top + ROW_HEIGHT / 4, 10, 10, Qt.AlignCenter) for _rt_i in range(rate): self._rt_1.paint(p, width + 10 * _rt_i - 30 - fix_pos, top + ROW_HEIGHT / 4, 10, 10, Qt.AlignCenter) foregroundColor = option.palette.color(QPalette.Text) p.setPen(foregroundColor) # Package Name p.setFont(self.boldFont) p.drawText(left + textInner, top, width - textInner, itemHeight / 2,Qt.AlignBottom | Qt.AlignLeft, title) # tagWidth = 0 _component_width = 0 if self.parent.showComponents: component = str(QVariant.value(index.model().data(index, ComponentRole))) widthOfTitle = self.boldFontFM.width(title) + 6 + left + textInner p.setFont(self.tagFont) rect = self.tagFontFM.boundingRect(option.rect, Qt.TextWordWrap, component) p.setPen(LIGHTGREEN) p.setBrush(LIGHTGREEN) p.drawRoundedRect(widthOfTitle , top + 12, rect.width() + 4, rect.height(), 10, 10) p.setPen(DARKGREEN) p.drawText(widthOfTitle + 2, top + 12, rect.width(), rect.height(), Qt.AlignCenter, component) p.setPen(foregroundColor) _component_width = rect.width() + 8 if self.parent.showIsA: isa = str(QVariant.value(index.model().data(index, IsaRole))) if not isa == '': widthOfTitle = self.boldFontFM.width(title) + 6 + left + textInner + _component_width p.setFont(self.tagFont) rect = self.tagFontFM.boundingRect(option.rect, Qt.TextWordWrap, isa) p.setPen(LIGHTBLUE) p.setBrush(LIGHTBLUE) p.drawRoundedRect(widthOfTitle , top + 12, rect.width() + 4, rect.height(), 10, 10) p.setPen(DARKVIOLET) p.drawText(widthOfTitle + 2, top + 12, rect.width(), rect.height(), Qt.AlignCenter, isa) p.setPen(foregroundColor) _component_width += rect.width() + 8 if ptype not in ('None', 'normal'): widthOfTitle = self.boldFontFM.width(title) + 6 + left + textInner + _component_width p.setFont(self.tagFont) rect = self.tagFontFM.boundingRect(option.rect, Qt.TextWordWrap, self.types[ptype][1]) p.setPen(self.types[ptype][0]) p.setBrush(self.types[ptype][0]) p.drawRoundedRect(widthOfTitle, top + 12, rect.width() + 4, rect.height(), 10, 10) p.setPen(WHITE) p.drawText(widthOfTitle + 2, top + 12, rect.width(), rect.height(), Qt.AlignCenter, self.types[ptype][1]) p.setPen(foregroundColor) tagWidth = rect.width() # Package Summary p.setFont(self.normalFont) foregroundColor.setAlpha(160) p.setPen(foregroundColor) elided_summary = self.normalFontFM.elidedText(summary, Qt.ElideRight, width - textInner - tagWidth - 22) p.drawText(left + textInner, top + itemHeight / 2, width - textInner, itemHeight / 2, Qt.TextDontClip, elided_summary) foregroundColor.setAlpha(255) p.setPen(foregroundColor) buttonStyle = None if self.rowAnimator.currentRow() == index.row(): description = str(QVariant.value(index.model().data(index, DescriptionRole))) size = str(QVariant.value(index.model().data(index, SizeRole))) homepage = str(QVariant.value(index.model().data(index, HomepageRole))) installedVersion = str(QVariant.value(index.model().data(index, InstalledVersionRole))) version = str(QVariant.value(index.model().data(index, VersionRole))) # Package Detail Label position = top + ROW_HEIGHT p.setFont(self.normalDetailFont) baseRect = QRect(left, position, width - 8, option.rect.height()) rect = self.normalDetailFontFM.boundingRect(baseRect, Qt.TextWordWrap | Qt.TextDontClip, description) p.drawText(left + 2, position, width - 8, rect.height(), Qt.TextWordWrap | Qt.TextDontClip, description) # Package Detail Homepage position += rect.height() + 4 p.setFont(self.boldDetailFont) p.drawText(left + ICON_SIZE , position, width - textInner, itemHeight / 2, Qt.AlignLeft, self._titles['website']) p.setFont(self.normalDetailFont) homepage = self.normalDetailFontFM.elidedText(homepage, Qt.ElideRight, width - self._titleFM['website']) rect = self.normalDetailFontFM.boundingRect(option.rect, Qt.TextSingleLine, homepage) self.rowAnimator.hoverLinkFilter.link_rect = QRect(left + self._titleFM['website'] + 2, position + 2 + 32, rect.width(), rect.height()) p.setPen(option.palette.color(QPalette.Link)) p.drawText(left + self._titleFM['website'], position, width, rect.height(), Qt.TextSingleLine, homepage) p.setPen(foregroundColor) # Package Detail Version position += rect.height() p.setFont(self.boldDetailFont) p.drawText(left + ICON_SIZE , position, width - textInner, itemHeight / 2, Qt.AlignLeft, self._titles['release']) p.setFont(self.normalDetailFont) rect = self.normalDetailFontFM.boundingRect(option.rect, Qt.TextWordWrap, version) p.drawText(left + self._titleFM['release'], position, width, rect.height(), Qt.TextWordWrap, version) if not installedVersion == '' or not installedVersion == None: position += rect.height() p.setFont(self.boldDetailFont) p.drawText(left + ICON_SIZE , position, width - textInner, itemHeight / 2, Qt.AlignLeft, self._titles['installVers']) p.setFont(self.normalDetailFont) rect = self.normalDetailFontFM.boundingRect(option.rect, Qt.TextWordWrap, installedVersion) p.drawText(left + self._titleFM['installVers'], position, width, rect.height(), Qt.TextWordWrap, installedVersion) # Package Detail Repository repository = QVariant.value(index.model().data(index, RepositoryRole)) if not repository == '': repository = _translate("Packaga Manager",'Unknown') if repository == 'N/A' else repository position += rect.height() p.setFont(self.boldDetailFont) p.drawText(left + ICON_SIZE , position, width - textInner, itemHeight / 2, Qt.AlignLeft, self._titles['repository']) p.setFont(self.normalDetailFont) p.drawText(left + self._titleFM['repository'], position, width, itemHeight / 2, Qt.TextWordWrap, repository) # Package Detail Size position += rect.height() p.setFont(self.boldDetailFont) p.drawText(left + ICON_SIZE , position, width - textInner, itemHeight / 2, Qt.AlignLeft, self._titles['size']) p.setFont(self.normalDetailFont) p.drawText(left + self._titleFM['size'], position, width, itemHeight / 2, Qt.TextWordWrap, size) position += rect.height() self.rowAnimator.max_height = position - top + 8 # Package More info button opt = QStyleOptionViewItem(option) buttonStyle = QStyleOptionButton() if option.state & QStyle.State_MouseOver or option.state & QStyle.State_HasFocus: buttonStyle.state |= QStyle.State_HasFocus buttonStyle.state |= QStyle.State_Enabled buttonStyle.text = _translate("Packaga Manager","Details") buttonStyle.rect = QRect(width - 100, position - 22, 100, 22) p.end() # FIXME # if option.state & QStyle.State_HasFocus and self.animatable: # option.state |= QStyle.State_MouseOver # Use Plastique style to draw focus rect like MouseOver effect of Oxygen. # self.plastik.drawPrimitive(QStyle.PE_FrameLineEdit, option, painter, None) if not self.rowAnimator.running() and buttonStyle: if self.show_details_button and (installed or config.USE_APPINFO): PackageDelegate.AppStyle().drawControl(QStyle.CE_PushButton, buttonStyle, painter, None) self.rowAnimator.hoverLinkFilter.button_rect = QRect(buttonStyle.rect) painter.drawPixmap(option.rect.topLeft(), pixmap) del pixmap