def paint(self, painter, option, index): options = QStyleOptionViewItem(option) self.initStyleOption(options, index) style = (QApplication.style() if options.widget is None else options.widget.style()) doc = QTextDocument() text = options.text doc.setHtml(text) doc.setDocumentMargin(0) # This needs to be an empty string to avoid the overlapping the # normal text of the QTreeWidgetItem options.text = "" style.drawControl(QStyle.CE_ItemViewItem, options, painter) ctx = QAbstractTextDocumentLayout.PaintContext() textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options, None) painter.save() painter.translate(textRect.topLeft()) painter.setClipRect(textRect.translated(-textRect.topLeft())) doc.documentLayout().draw(painter, ctx) painter.restore()
def paint(self, painter, option, index): options = QStyleOptionViewItem(option) self.initStyleOption(options, index) style = (QApplication.style() if options.widget is None else options.widget.style()) doc = QTextDocument() doc.setDocumentMargin(self._margin) doc.setHtml(options.text) options.text = "" style.drawControl(QStyle.CE_ItemViewItem, options, painter) ctx = QAbstractTextDocumentLayout.PaintContext() textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options) painter.save() # Adjustments for the file switcher if hasattr(options.widget, 'files_list'): if style.objectName() in ['oxygen', 'qtcurve', 'breeze']: if options.widget.files_list: painter.translate(textRect.topLeft() + QPoint(4, -9)) else: painter.translate(textRect.topLeft()) else: if options.widget.files_list: painter.translate(textRect.topLeft() + QPoint(4, 4)) else: painter.translate(textRect.topLeft() + QPoint(2, 4)) else: painter.translate(textRect.topLeft() + QPoint(0, -3)) doc.documentLayout().draw(painter, ctx) painter.restore()
def paint(self, painter, option, index): options = QStyleOptionViewItem(option) self.initStyleOption(options, index) style = QApplication.style() if options.widget is None else options.widget.style() doc = QTextDocument() doc.setDocumentMargin(self._margin) doc.setHtml(options.text) options.text = "" style.drawControl(QStyle.CE_ItemViewItem, options, painter) ctx = QAbstractTextDocumentLayout.PaintContext() textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options) painter.save() if style.objectName() == "oxygen": painter.translate(textRect.topLeft() + QPoint(5, -9)) else: painter.translate(textRect.topLeft()) painter.setClipRect(textRect.translated(-textRect.topLeft())) doc.documentLayout().draw(painter, ctx) painter.restore()
def paint(self, painter, option, index): options = QStyleOptionViewItem(option) self.initStyleOption(options, index) style = (QApplication.style() if options.widget is None else options.widget.style()) doc = QTextDocument() doc.setDocumentMargin(self._margin) doc.setHtml(options.text) options.text = "" style.drawControl(QStyle.CE_ItemViewItem, options, painter) ctx = QAbstractTextDocumentLayout.PaintContext() textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options) painter.save() if style.objectName() == 'oxygen': painter.translate(textRect.topLeft() + QPoint(5, -9)) else: painter.translate(textRect.topLeft()) painter.setClipRect(textRect.translated(-textRect.topLeft())) doc.documentLayout().draw(painter, ctx) painter.restore()
def _prepare_text_document(self, option, index): # This logic must be shared between paint and sizeHint for consistency options = QStyleOptionViewItem(option) self.initStyleOption(options, index) doc = QTextDocument() doc.setDocumentMargin(self._margin) doc.setHtml(options.text) return options, doc
def _get_height(self): """ Return the expected height of this item's text, including the text margins. """ doc = QTextDocument() doc.setHtml('<hr>') doc.setDocumentMargin(self._PADDING) return doc.size().height()
def _get_height(self): """ Return the expected height of this item's text, including the text margins. """ doc = QTextDocument() try: doc.setHtml('<span style="font-size:{}pt">Title</span>' .format(self._styles['title_font_size'])) except KeyError: doc.setHtml('<span>Title</span>') doc.setDocumentMargin(self._PADDING) return doc.size().height()
def paint(self, painter, option, index): options = QStyleOptionViewItem(option) self.initStyleOption(options, index) style = (QApplication.style() if options.widget is None else options.widget.style()) # Set background color for selected and hovered items. # Inspired by: # - https://stackoverflow.com/a/43253004/438386 # - https://stackoverflow.com/a/27274233/438386 # This is commented for now until we find a way to correctly colorize # the entire line with a single color. # if options.state & QStyle.State_Selected: # # This only applies when the selected item doesn't have focus # if not (options.state & QStyle.State_HasFocus): # options.palette.setBrush( # QPalette.Highlight, # QBrush(self._background_color) # ) if options.state & QStyle.State_MouseOver: painter.fillRect(option.rect, self._background_color) # Set text doc = QTextDocument() text = options.text doc.setHtml(text) doc.setDocumentMargin(0) # This needs to be an empty string to avoid overlapping the # normal text of the QTreeWidgetItem options.text = "" style.drawControl(QStyle.CE_ItemViewItem, options, painter) ctx = QAbstractTextDocumentLayout.PaintContext() textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options, None) painter.save() painter.translate(textRect.topLeft() + QPoint(0, 4)) doc.documentLayout().draw(painter, ctx) painter.restore()
class FrameContentHover(FrameBase): """Frame used in css styling with fade in and fade out effect.""" sig_clicked = Signal() sig_entered = Signal() sig_left = Signal() def __init__(self, *args, **kwargs): """Frame used in css styling with fade in and fade out effect.""" super(FrameContentHover, self).__init__(*args, **kwargs) self.current_opacity = 0 self.max_frame = 100 self.max_opacity = 0.95 self.button_text = None self.label_icon = None self.label_text = None self.summary = '' # Widgets self.text_document = QTextDocument(self) self.timeline = QTimeLine(500) font = QFont() if sys.platform == 'darwin': font.setPointSize(12) elif os.name == 'nt': font.setPointSize(9) else: font.setPointSize(9) self.text_document.setDefaultFont(font) self.text_document.setMaximumBlockCount(5) self.text_document.setDocumentMargin(10) # Setup self.setWindowFlags(Qt.FramelessWindowHint) self.setAutoFillBackground(True) self.setWindowFlags(Qt.FramelessWindowHint) self.setMinimumSize(self.widget_size()) self.timeline.setFrameRange(0, self.max_frame) # Signals self.timeline.frameChanged.connect(self.set_opacity) def set_opacity(self, val): """Set the opacity via timeline.""" self.current_opacity = (val * 1.0 / self.max_frame) * self.max_opacity self.update() def widget_size(self): """Return hover frame size.""" bheight = 40 if self.button_text is None else self.button_text.height() width = SASS_VARIABLES.WIDGET_CONTENT_TOTAL_WIDTH height = SASS_VARIABLES.WIDGET_CONTENT_TOTAL_HEIGHT padding = SASS_VARIABLES.WIDGET_CONTENT_PADDING margin = 0 # SASS_VARIABLES.WIDGET_CONTENT_MARGIN return QSize( width - 2 * (padding + margin), height - 2 * margin - bheight ) def fade_in(self): """Fade in hover card with text.""" self.raise_() self.timeline.stop() self.timeline.setDirection(QTimeLine.Forward) self.timeline.start() def fade_out(self): """Fade out hover card with text.""" self.timeline.stop() self.timeline.setDirection(QTimeLine.Backward) self.timeline.start() def enterEvent(self, event): """Override Qt method.""" super(FrameContentHover, self).enterEvent(event) self.fade_in() self.sig_entered.emit() def leaveEvent(self, event): """Override Qt method.""" super(FrameContentHover, self).leaveEvent(event) self.fade_out() self.sig_left.emit() def mousePressEvent(self, event): """Override Qt method.""" self.sig_clicked.emit() def paintEvent(self, event): """Override Qt method.""" painter = QPainter(self) painter.setOpacity(self.current_opacity) max_width = ( SASS_VARIABLES.WIDGET_CONTENT_TOTAL_WIDTH - 2 * SASS_VARIABLES.WIDGET_CONTENT_PADDING - 2 * SASS_VARIABLES.WIDGET_CONTENT_MARGIN ) # Hover top br = self.label_icon.rect().bottomRight() tl = self.label_icon.rect().topLeft() + QPoint(1, 1) y = br.y() + self.label_text.height() - 2 # br_new = QPoint(br.x() + 2, y) - QPoint(1, 1) br_new = QPoint(max_width - 1, y) - QPoint(1, 1) rect_hover = QRect(tl, br_new) # 2 is the border pen = QPen(Qt.NoPen) brush = QBrush(Qt.SolidPattern) brush.setColor(QColor('#fff')) painter.setBrush(brush) painter.setPen(pen) painter.drawRect(rect_hover) font = self.font() font.setPointSize(10) painter.setFont(font) pen = QPen() pen.setColor(QColor('black')) painter.setPen(pen) td = self.text_document td.setPageSize(QSizeF(rect_hover.size())) td.setHtml(self.summary) td.drawContents(painter) self.raise_()