def drawFocus(self, painter, option, rect, widget=None): # NOTE not used if (option.state & QStyle.StateFlag.State_HasFocus) == 0 or not rect.isValid(): return o = QStyleOptionFocusRect() o.state = option.state o.direction = option.direction o.rect = option.rect o.fontMetrics = option.fontMetrics o.palette = option.palette o.state |= QStyle.StateFlag.State_KeyboardFocusChange o.state |= QStyle.StateFlag.State_Item cg = QPalette.ColorGroup.Active if ( option.state & QStyle.StateFlag.State_Enabled) else QPalette.ColorGroup.Disabled o.backgroundColor = option.palette.color( cg, QPalette.ColorRole.Highlight if (option.state & QStyle.StateFlag.State_Selected) else QPalette.ColorRole.Window) style = widget.style() if widget else QApplication.style() style.drawPrimitive(QStyle.PrimitiveElement.PE_FrameFocusRect, o, painter, widget)
def paint(self, painter, option, index): painter.save() options = QStyleOptionViewItem(option) self.initStyleOption(options, index) self.doc.setHtml(options.text) options.text = "" # 原字符 style = QApplication.style( ) if options.widget is None else options.widget.style() style.drawControl(QStyle.ControlElement.CE_ItemViewItem, options, painter) ctx = QAbstractTextDocumentLayout.PaintContext() if option.state & QStyle.StateFlag.State_Selected: ctx.palette.setColor( QPalette.ColorRole.Text, option.palette.color(QPalette.ColorGroup.Active, QPalette.ColorRole.HighlightedText)) else: ctx.palette.setColor( QPalette.ColorRole.Text, option.palette.color(QPalette.ColorGroup.Active, QPalette.ColorRole.Text)) text_rect = style.subElementRect(QStyle.SubElement.SE_ItemViewItemText, options) the_fuck_your_shit_up_constant = 3 #  ̄へ ̄ # margin = (option.rect.height() - options.fontMetrics.height()) // 2 margin = margin - the_fuck_your_shit_up_constant text_rect.setTop(text_rect.top() + margin) painter.translate(text_rect.topLeft()) painter.setClipRect(text_rect.translated(-text_rect.topLeft())) self.doc.documentLayout().draw(painter, ctx) painter.restore()
def __init__(self, parent=None): super().__init__(parent) self.setupUi(self) self.TRAY = QSystemTrayIcon(self) self.TRAY.setIcon(QApplication.style().standardIcon( QStyle.StandardPixmap.SP_MessageBoxInformation)) self.TRAY.setToolTip('FGO-py') self.MENU_TRAY = QMenu(self) self.MENU_TRAY_QUIT = QAction('退出', self.MENU_TRAY) self.MENU_TRAY.addAction(self.MENU_TRAY_QUIT) self.MENU_TRAY_FORCEQUIT = QAction('强制退出', self.MENU_TRAY) self.MENU_TRAY.addAction(self.MENU_TRAY_FORCEQUIT) self.TRAY.setContextMenu(self.MENU_TRAY) self.TRAY.show() self.reloadTeamup() self.config = Config({ 'stopOnDefeated': (self.MENU_SETTINGS_DEFEATED, fgoKernel.schedule.stopOnDefeated), 'stopOnKizunaReisou': (self.MENU_SETTINGS_KIZUNAREISOU, fgoKernel.schedule.stopOnKizunaReisou), 'closeToTray': (self.MENU_CONTROL_TRAY, None), 'stayOnTop': (self.MENU_CONTROL_STAYONTOP, lambda x: self.setWindowFlag( Qt.WindowType.WindowStaysOnTopHint, x)), 'notifyEnable': (self.MENU_CONTROL_NOTIFY, None) }) self.notifier = ServerChann(**self.config['notifyParam']) self.worker = Thread() self.signalFuncBegin.connect(self.funcBegin) self.signalFuncEnd.connect(self.funcEnd) self.TRAY.activated.connect(lambda reason: self.show( ) if reason == QSystemTrayIcon.ActivationReason.Trigger else None) self.MENU_TRAY_QUIT.triggered.connect(lambda: QApplication.quit() if self.askQuit() else None) self.MENU_TRAY_FORCEQUIT.triggered.connect(QApplication.quit) self.getDevice()
def paint(self, painter: 'QPainter', option: QStyleOptionViewItem, index: 'QModelIndex'): """Highlight dates in red - TODO clean this up a bit, put stuff at class level - TODO could do this for all cell delegates so focus rect is same - and no text shifting happens """ date_color = self.color painter.save() parent = self.parent options = QStyleOptionViewItem(option) self.initStyleOption(options, index) # otherwise text draws twice options.text = '' style = QApplication.style( ) if options.widget is None else options.widget.style() ctx = QAbstractTextDocumentLayout.PaintContext() if index.row() == self.parent.data_model.current_row: # yellow selected row ctx.palette.setColor(QPalette.ColorRole.Text, QColor('black')) color = self.color_hl_bg_mouseover if option.state & QStyle.StateFlag.State_MouseOver else self.color_hl_bg options.palette.setColor(QPalette.ColorRole.Highlight, QColor(color)) elif option.state & QStyle.StateFlag.State_MouseOver and not option.state & QStyle.StateFlag.State_HasFocus: # mouse hover, but not active row ctx.palette.setColor(QPalette.ColorRole.Text, QColor(self.color_mouseover_text)) options.backgroundBrush = QBrush(QColor(self.color_mouseover_bg)) date_color = self.color_mouseover_text_date else: # not selected or hovered, just normal cell in column ctx.palette.setColor( QPalette.ColorRole.Text, option.palette.color(QPalette.ColorGroup.Active, QPalette.ColorRole.Text)) # need to set background color before drawing row style.drawControl(QStyle.ControlElement.CE_ItemViewItem, options, painter) if option.state & QStyle.StateFlag.State_HasFocus: pen = painter.pen() pen.setColor(QColor('red')) pen.setWidth(self.border_width) painter.setPen(pen) rect = option.rect.adjusted(1, 1, -1, -1) # rect = option.rect painter.drawRect(rect) textRect = style.subElementRect(QStyle.SubElement.SE_ItemViewItemText, options) # shift text up 4 pixels so don't have to shift with red focus box # this matches super().paint alignment if index.column() != 0: textRect.adjust(-1, -4, 0, 0) # this sets the html doc text in the correct place, but modifies location of painter, call close to last painter.translate(textRect.topLeft()) painter.setClipRect(textRect.translated(-textRect.topLeft())) # regex replace font color for dates, set to html text = index.data() res = re.sub(self.date_expr, rf'<font color="{date_color}">\1</font>', text) \ .replace('\n', '<br>') self.doc.setHtml(res) # define max width textDoc can occupy, eg word wrap self.doc.setTextWidth(options.rect.width()) self.doc.documentLayout().draw(painter, ctx) painter.restore()