def paint(self, painter, option, index): optionV4 = QStyleOptionViewItemV4(option) self.initStyleOption(optionV4, index) style = optionV4.widget.style() if optionV4.widget else QApplication.style() doc = QTextDocument() doc.setHtml(optionV4.text) # painting item without text optionV4.text = QString() style.drawControl(QStyle.CE_ItemViewItem, optionV4, painter) ctx = QAbstractTextDocumentLayout.PaintContext() # Hilight text if item is selected if optionV4.state & QStyle.State_Selected: ctx.palette.setColor(QPalette.Text, optionV4.palette.color(QPalette.Active, QPalette.HighlightedText)) textRect = style.subElementRect(QStyle.SE_ItemViewItemText, optionV4) 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): """Render the delegate for the item.""" if index.column() != self._html_column: return QStyledItemDelegate.paint(self, painter, option, index) options = QStyleOptionViewItemV4(option) self.initStyleOption(options, index) if options.widget is None: style = QApplication.style() else: style = options.widget.style() doc = QTextDocument() 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() painter.translate(textRect.topLeft()) painter.setClipRect(textRect.translated(-textRect.topLeft())) doc.documentLayout().draw(painter, ctx) painter.restore()
def paint(self, painter, option, index): """QStyledItemDelegate.paint implementation """ option.state &= ~QStyle.State_HasFocus # never draw focus rect options = QStyleOptionViewItemV4(option) self.initStyleOption(options, index) style = QApplication.style( ) if options.widget is None else options.widget.style() doc = QTextDocument() doc.setDocumentMargin(1) doc.setHtml(options.text) # bad long (multiline) strings processing doc.setTextWidth(options.rect.width()) options.text = "" style.drawControl(QStyle.CE_ItemViewItem, options, painter) ctx = QAbstractTextDocumentLayout.PaintContext() # Highlighting text if item is selected if option.state & QStyle.State_Selected: ctx.palette.setColor( QPalette.Text, option.palette.color(QPalette.Active, QPalette.HighlightedText)) textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options) painter.save() painter.translate(textRect.topLeft()) painter.setClipRect(textRect.translated(-textRect.topLeft())) doc.documentLayout().draw(painter, ctx) painter.restore()
def sizeHint(self, option, index): options = QStyleOptionViewItemV4(option) self.initStyleOption(options, index) doc = QTextDocument() doc.setHtml(options.text) doc.setTextWidth(options.rect.width()) return QtCore.QSize(doc.idealWidth(), doc.size().height())
def paint(self, painter, option, index): script = index.data(Qt.DisplayRole).toPyObject() if script.flags & Script.Modified: option = QStyleOptionViewItemV4(option) option.palette.setColor(QPalette.Text, QColor(Qt.red)) option.palette.setColor(QPalette.Highlight, QColor(Qt.darkRed)) QStyledItemDelegate.paint(self, painter, option, index)
def sizeHint(self, option, index): option = QStyleOptionViewItemV4(option) self.initStyleOption(option, index) size = QStyledItemDelegate.sizeHint(self, option, index) # TODO: get the default QMenu item height from the current style. size.setHeight(max(size.height(), 25)) return size
def sizeHint(self, option, index): optionV4 = QStyleOptionViewItemV4(option) self.initStyleOption(optionV4, index) doc = QTextDocument() doc.setHtml(optionV4.text) doc.setTextWidth(optionV4.rect.width()) return QSize(doc.idealWidth(), max(doc.size().height(), optionV4.decorationSize.height()))
def sizeHint(self, option, index): """Calculate the needed size.""" options = QStyleOptionViewItemV4(option) self.initStyleOption(options, index) doc = QTextDocument() doc.setHtml(options.text) doc.setTextWidth(options.rect.width()) return QSize(doc.idealWidth(), doc.size().height())
def sizeHint(self, option, index): """QStyledItemDelegate.sizeHint implementation """ options = QStyleOptionViewItemV4(option) self.initStyleOption(options, index) doc = QTextDocument() doc.setDocumentMargin(1) # bad long (multiline) strings processing doc.setTextWidth(options.rect.width()) doc.setHtml(options.text) return QSize(doc.idealWidth(), doc.size().height())
def paintCheckBoxColumn(self, painter, option, index): opt = QStyleOptionViewItemV4(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): """paint right aligned checkbox""" viewItemOption = QStyleOptionViewItemV4(option) if self.cellFilter(index): textMargin = self.__textMargin() newRect = QStyle.alignedRect( option.direction, Qt.AlignRight, QSize(option.decorationSize.width() + 5, option.decorationSize.height()), QRect(option.rect.x() + textMargin, option.rect.y(), option.rect.width() - (2 * textMargin), option.rect.height())) viewItemOption.rect = newRect QStyledItemDelegate.paint(self, painter, viewItemOption, index)
def paint(self, painter, option, index): options = QStyleOptionViewItemV4(option) self.initStyleOption(options, index) style = QApplication.style( ) if options.widget is None else options.widget.style() doc = QTextDocument() doc.setHtml(options.text) options.text = "" style.drawControl(QStyle.CE_ItemViewItem, options, painter) ctx = QAbstractTextDocumentLayout.PaintContext() textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options) # Highlighting text if item is selected if options.state & QStyle.State_Selected: # ctx.palette.setColor( # QPalette.Text, options.palette.color(QPalette.Active, QPalette.HighlightedText) # ) # painter.setBrush( options.palette.color( QPalette.Active, QPalette.Highlight ) ) # painter.setBrush( QColor( '#c1f48b' ) ) painter.setBrush(QColor(0, 180, 0, 30)) painter.setPen(Qt.NoPen) painter.drawRect(textRect) elif options.state & QStyle.State_MouseOver: # painter.setBrush( QColor( '#faffb8' ) ) # painter.setPen( Qt.NoPen ) # painter.drawRect( textRect ) painter.setBrush(QColor(0, 255, 0, 10)) painter.setPen(Qt.NoPen) painter.drawRect(textRect) else: painter.setPen(QColor(0, 0, 0, 10)) painter.drawLine(textRect.bottomLeft(), textRect.bottomRight()) 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): self.initStyleOption(option, index) # No idea why, but this cast is required if we want to have access to the V4 valuess option = QStyleOptionViewItemV4(option) if (index.column() == 1) and (option.state & QStyle.State_Selected): cboption = QStyleOptionComboBox() cboption.rect = option.rect # On OS X (with Qt4.6.0), adding State_Enabled to the flags causes the whole drawing to # fail (draw nothing), but it's an OS X only glitch. On Windows, it works alright. cboption.state |= QStyle.State_Enabled QApplication.style().drawComplexControl(QStyle.CC_ComboBox, cboption, painter) painter.setBrush(option.palette.text()) rect = QRect(option.rect) rect.setLeft(rect.left() + 4) painter.drawText(rect, Qt.AlignLeft, option.text) else: QStyledItemDelegate.paint(self, painter, option, index)
def editorEvent(self, event, model, option, index): flags = model.flags(index) if not flags & Qt.ItemIsUserCheckable or \ not option.state & QStyle.State_Enabled or \ not flags & Qt.ItemIsEnabled: return False checkstate = model.data(index, Qt.CheckStateRole) if checkstate is None: return False widget = option.widget style = widget.style() if widget else QApplication.style() if event.type() in { QEvent.MouseButtonPress, QEvent.MouseButtonRelease, QEvent.MouseButtonDblClick }: pos = event.pos() opt = QStyleOptionViewItemV4(option) self.initStyleOption(opt, index) rect = style.subElementRect(QStyle.SE_ItemViewItemCheckIndicator, opt, widget) if event.button() != Qt.LeftButton or not rect.contains(pos): return False if event.type() in { QEvent.MouseButtonPress, QEvent.MouseButtonDblClick }: return True elif event.type() == QEvent.KeyPress: if event.key() != Qt.Key_Space and event.key() != Qt.Key_Select: return False else: return False if model.flags(index) & Qt.ItemIsTristate: checkstate = (checkstate + 1) % 3 else: checkstate = \ Qt.Unchecked if checkstate == Qt.Checked else Qt.Checked return model.setData(index, checkstate, Qt.CheckStateRole)
def paint(self, painter, option, index): painter.save() if index.isValid(): style = self.parent().style() styleOption = QStyleOptionViewItemV4(option) node = index.internalPointer() val = index.model().data(index).toPyObject() if node.typeInfo == 'CODE': if node.valueFormat == 'percent': text = '%.2f %% ' % (val * 100) elif node.valueFormat == 'integer': text = '%d ' % val else: text = '%.2f ' % val styleOption.text = text styleOption.displayAlignment = Qt.AlignRight style.drawControl(QStyle.CE_ItemViewItem, styleOption, painter) elif node.typeInfo == 'BAREME' and index.column() == 2: styleOption = QStyleOptionButton() styleOption.rect = option.rect styleOption.text = QString('Editer') styleOption.textVisible = True styleOption.state = styleOption.state | QStyle.State_Enabled style.drawControl(QStyle.CE_PushButton, styleOption, painter) else: QStyledItemDelegate.paint(self, painter, styleOption, index) else: QStyledItemDelegate.paint(painter, option, index) painter.restore()
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 = index.model().data(index, NameRole).toString() summary = index.model().data(index, SummaryRole).toString() ptype = str(index.model().data(index, TypeRole).toString()) rate = int(index.model().data(index, RateRole).toInt()[0]) installed = index.model().data(index, InstalledRole).toBool() # We need to request update if its not possible to get meta data about the package try: # Get Package Icon if exists _icon = index.model().data(index, Qt.DecorationRole).toString() 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(index.model().data(index, ComponentRole).toString()) 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.drawRoundRect(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(index.model().data(index, IsaRole).toString()) 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.drawRoundRect(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.drawRoundRect(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 = index.model().data(index, DescriptionRole).toString() size = index.model().data(index, SizeRole).toString() homepage = index.model().data(index, HomepageRole).toString() installedVersion = str(index.model().data( index, InstalledVersionRole).toString()) version = 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.toString()) p.drawText(left + self._titleFM['release'], position, width, rect.height(), Qt.TextWordWrap, version.toString()) if not installedVersion == '': 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 = index.model().data(index, RepositoryRole).toString() if not repository == '': repository = i18n( '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 = QStyleOptionViewItemV4(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 = i18n("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