def paint_emblems(self, painter, rect, emblems): gutter = self.emblem_size + self.MARGIN grect = QRect(rect) gpos = self.gutter_position if gpos is self.TOP: grect.setBottom(grect.top() + gutter) rect.setTop(rect.top() + gutter) elif gpos is self.BOTTOM: grect.setTop(grect.bottom() - gutter + self.MARGIN) rect.setBottom(rect.bottom() - gutter) elif gpos is self.LEFT: grect.setRight(grect.left() + gutter) rect.setLeft(rect.left() + gutter) else: grect.setLeft(grect.right() - gutter + self.MARGIN) rect.setRight(rect.right() - gutter) horizontal = gpos in (self.TOP, self.BOTTOM) painter.save() painter.setClipRect(grect) try: for i, emblem in enumerate(emblems): delta = 0 if i == 0 else self.emblem_size + self.MARGIN grect.moveLeft(grect.left() + delta) if horizontal else grect.moveTop(grect.top() + delta) rect = QRect(grect) rect.setWidth(int(emblem.width() / emblem.devicePixelRatio())), rect.setHeight(int(emblem.height() / emblem.devicePixelRatio())) painter.drawPixmap(rect, emblem) finally: painter.restore()
def paint_embossed_emblem(self, pixmap, painter, orect, right_adjust, left=True): drect = QRect(orect) if left: drect.setLeft(drect.left() + right_adjust) drect.setRight(drect.left() + pixmap.width()) else: drect.setRight(drect.right() - right_adjust) drect.setLeft(drect.right() - pixmap.width() + 1) drect.setBottom(drect.bottom() - self.title_height) drect.setTop(drect.bottom() - pixmap.height()) painter.drawPixmap(drect, pixmap)
def paint_embossed_emblem(self, pixmap, painter, orect, right_adjust, left=True): drect = QRect(orect) pw = int(pixmap.width() / pixmap.devicePixelRatio()) ph = int(pixmap.height() / pixmap.devicePixelRatio()) if left: drect.setLeft(drect.left() + right_adjust) drect.setRight(drect.left() + pw) else: drect.setRight(drect.right() - right_adjust) drect.setLeft(drect.right() - pw + 1) drect.setBottom(drect.bottom() - self.title_height) drect.setTop(drect.bottom() - ph) painter.drawPixmap(drect, pixmap)
def draw_text(self, style, painter, option, widget, index, item): tr = style.subElementRect(style.SE_ItemViewItemText, option, widget) text = index.data(Qt.DisplayRole) hover = option.state & style.State_MouseOver if hover or gprefs['tag_browser_show_counts']: count = unicode_type(index.data(COUNT_ROLE)) width = painter.fontMetrics().boundingRect(count).width() r = QRect(tr) r.setRight(r.right() - 1), r.setLeft(r.right() - width - 4) self.paint_text(painter, r, Qt.AlignCenter | Qt.TextSingleLine, count, hover) tr.setRight(r.left() - 1) else: tr.setRight(tr.right() - 1) is_rating = item.type == TagTreeItem.TAG and not self.rating_pat.sub('', text) if is_rating: painter.setFont(self.rating_font) flags = Qt.AlignVCenter | Qt.AlignLeft | Qt.TextSingleLine lr = QRect(tr) lr.setRight(lr.right() * 2) br = painter.boundingRect(lr, flags, text) if br.width() > tr.width(): g = QLinearGradient(tr.topLeft(), tr.topRight()) c = option.palette.color(QPalette.WindowText) g.setColorAt(0, c), g.setColorAt(0.8, c) c = QColor(c) c.setAlpha(0) g.setColorAt(1, c) pen = QPen() pen.setBrush(QBrush(g)) painter.setPen(pen) self.paint_text(painter, tr, flags, text, hover)
def paintEvent(self, event): ''' @param: event QPaintEvent ''' super().paintEvent(event) # Draw drop indicator if self._dropRow != -1: # BookmarksToolbarButton button = self._buttonAt(self._dropPos) if button: if button.bookmark().isFolder(): return tmpRect = QRect(button.x(), 0, button.width(), self.height()) rect = QRect() if self._dropRow == self._layout.indexOf(button): rect = QRect(max(0, tmpRect.left() - 2), tmpRect.top(), 3, tmpRect.height()) else: rect = QRect(tmpRect.right() + 0, tmpRect.top(), 3, tmpRect.height()) gVar.appTools.paintDropIndicator(self, rect)
def draw_text(self, style, painter, option, widget, index, item): tr = style.subElementRect(style.SE_ItemViewItemText, option, widget) text = index.data(Qt.DisplayRole) hover = option.state & style.State_MouseOver if hover or gprefs['tag_browser_show_counts']: count = unicode_type(index.data(COUNT_ROLE)) width = painter.fontMetrics().boundingRect(count).width() r = QRect(tr) r.setRight(r.right() - 1), r.setLeft(r.right() - width - 4) painter.drawText(r, Qt.AlignCenter | Qt.TextSingleLine, count) tr.setRight(r.left() - 1) else: tr.setRight(tr.right() - 1) is_rating = item.type == TagTreeItem.TAG and not self.rating_pat.sub('', text) if is_rating: painter.setFont(self.rating_font) flags = Qt.AlignVCenter | Qt.AlignLeft | Qt.TextSingleLine lr = QRect(tr) lr.setRight(lr.right() * 2) br = painter.boundingRect(lr, flags, text) if br.width() > tr.width(): g = QLinearGradient(tr.topLeft(), tr.topRight()) c = option.palette.color(QPalette.WindowText) g.setColorAt(0, c), g.setColorAt(0.8, c) c = QColor(c) c.setAlpha(0) g.setColorAt(1, c) pen = QPen() pen.setBrush(QBrush(g)) painter.setPen(pen) painter.drawText(tr, flags, text)
def _paintGroupBox(self, rect, title): painter = QPainter(self) qDrawShadeRect(painter, rect, self.palette(), True) if len(title) > 0: fnt = self.font() fnt.setPointSize(fnt.pointSize() - 1) offset = 5 asterix = ' *' twidth = QFontMetrics(fnt).width(title) awidth = QFontMetrics(fnt).width(asterix) width = twidth + awidth + 2 * offset height = QFontMetrics(fnt).height() rect = QRect(rect.left() + 2 * offset, rect.top() - height / 2 + 1, width, height) painter.fillRect(rect, self.palette().color(self.backgroundRole())) painter.setFont(fnt) painter.drawText( QRect(rect.left() + offset, rect.top(), twidth, rect.height()), Qt.AlignLeft, title) painter.setPen(Qt.red) painter.drawText( QRect(rect.left() + offset + twidth, rect.top(), awidth, rect.height()), Qt.AlignRight, asterix)
def paintEvent(self, event): ''' @param: event QPaintEvent ''' p = QPainter(self) # Just draw separator if self._bookmark.isSeparator(): opt = QStyleOption() opt.initFrom(self) opt.state |= QStyle.State_Horizontal self.style().drawPrimitive(QStyle.PE_IndicatorToolBarSeparator, opt, p) return option = QStyleOptionButton() self.initStyleOption(option) # We are manually drawing the arrow option.features &= ~QStyleOptionButton.HasMenu # Draw button base (only under mouse, this is autoraise button) if self.isDown() or self.hitButton(self.mapFromGlobal(QCursor.pos())): option.state |= QStyle.State_AutoRaise | QStyle.State_Raised self.style().drawPrimitive(QStyle.PE_PanelButtonTool, option, p, self) if self.isDown(): shiftX = self.style().pixelMetric(QStyle.PM_ButtonShiftHorizontal, option, self) shiftY = self.style().pixelMetric(QStyle.PM_ButtonShiftVertical, option, self) else: shiftX = 0 shiftY = 0 height = option.rect.height() center = height / 2 + option.rect.top() + shiftY iconSize = 16 iconYPos = center - iconSize / 2 leftPosition = self.PADDING + shiftX rightPosition = option.rect.right() - self.PADDING # Draw icon if not self._showOnlyText: iconRect = QRect(leftPosition, iconYPos, iconSize, iconSize) p.drawPixmap( QStyle.visualRect(option.direction, option.rect, iconRect), self._bookmark.icon().pixmap(iconSize)) leftPosition = iconRect.right() + self.PADDING # Draw menu arrow if not self._showOnlyIcon and self.menu(): arrowSize = 8 opt = QStyleOption() opt.initFrom(self) rect = QRect(rightPosition - 8, center - arrowSize / 2, arrowSize, arrowSize) opt.rect = QStyle.visualRect(option.direction, option.rect, rect) opt.state &= ~QStyle.State_MouseOver self.style().drawPrimitive(QStyle.PE_IndicatorArrowDown, opt, p, self) rightPosition = rect.left() - self.PADDING # Draw text if not self._showOnlyIcon: textWidth = rightPosition - leftPosition textYPos = center - self.fontMetrics().height() / 2 txt = self.fontMetrics().elidedText(self._bookmark.title(), Qt.ElideRight, textWidth) textRect = QRect(leftPosition, textYPos, textWidth, self.fontMetrics().height()) self.style().drawItemText( p, QStyle.visualRect(option.direction, option.rect, textRect), Qt.TextSingleLine | Qt.AlignCenter, option.palette, True, txt)