Пример #1
0
        def paint(self, painter, option, index):
            if option.widget is not None:
                style = option.widget.style()
            else:
                style = QApplication.style()

            option = QStyleOptionViewItem(option)
            option.showDecorationSelected = True

            # option.state &= ~QStyle.State_HasFocus & ~QStyle.State_MouseOver
            if self.isSeparator(index):
                opt = QStyleOption()
                opt.rect = QRect(option.rect)
                if isinstance(option.widget, QAbstractItemView):
                    opt.rect.setWidth(option.widget.viewport().width())
                style.drawPrimitive(QStyle.PE_IndicatorToolBarSeparator,
                                    opt, painter, option.widget)
            else:
                super(CheckComboBox.ComboItemDelegate, self).paint(painter, option, index)
Пример #2
0
    def paint(self, painter, option, index):
        '''
        @param: painter QPainter
        @param: option QStyleOptionViewItem
        @param: index QModelIndex
        '''
        opt = QStyleOptionViewItem(option)
        self.initStyleOption(opt, index)

        w = opt.widget
        # QStyle
        style = None
        # Qt::LayoutDirection
        direction = None
        if w:
            style = w.style()
            direction = w.layoutDirection()
        else:
            style = QApplication.style()
            direction = QApplication.layoutDirection()

        # QPalette.ColorRole
        colorRole = None
        if opt.state & QStyle.State_Selected:
            colorRole = QPalette.HighlightedText
        else:
            colorRole = QPalette.Text

        # QPalette.ColorGroup
        colorGroup = None
        if opt.state & QStyle.State_Enabled:
            if not (opt.state & QStyle.State_Active):
                colorGroup = QPalette.Inactive
            else:
                colorGroup = QPalette.Normal
        else:
            colorGroup = QPalette.Disabled

        if const.OS_WIN:
            opt.palette.setColor(
                QPalette.All, QPalette.HighlightedText,
                opt.palette.color(QPalette.Active, QPalette.Text))
            opt.palette.setColor(QPalette.All, QPalette.Highlight,
                                 opt.palette.base().color().darker(108))

        textPalette = opt.palette
        textPalette.setCurrentColorGroup(colorGroup)

        topPosition = opt.rect.top() + self._padding

        # Draw background
        opt.showDecorationSelected = True
        style.drawPrimitive(QStyle.PE_PanelItemViewItem, opt, painter, w)

        # Draw icon
        iconRect = QRect(
            opt.rect.left() + (opt.rect.width() - self._iconSize) / 2,
            topPosition, self._iconSize, self._iconSize)
        visualIconRect = style.visualRect(direction, opt.rect, iconRect)
        pixmap = index.data(Qt.DecorationRole).pixmap(self._iconSize)
        painter.drawPixmap(visualIconRect, pixmap)
        topPosition += self._iconSize + self._padding

        # Draw title
        title = index.data(Qt.DisplayRole)
        leftTitleEdge = opt.rect.left() + self._padding
        titleRect = QRect(leftTitleEdge, topPosition,
                          opt.rect.width() - 2 * self._padding,
                          opt.fontMetrics.height())
        visualTitleRect = style.visualRect(direction, opt.rect, titleRect)
        style.drawItemText(painter, visualTitleRect, Qt.AlignCenter,
                           textPalette, True, title, colorRole)