示例#1
0
 def standardIcon(cls, icon):
     '''
     @param: icon QStyle::StandardPixmap
     @return: QIcon
     '''
     defIcon = QApplication.style().standardIcon(icon)
     if icon == QStyle.SP_MessageBoxCritical:
         return QIcon.fromTheme('dialog-error', defIcon)
     elif icon == QStyle.SP_MessageBoxInformation:
         return QIcon.fromTheme('dialog-information', defIcon)
     elif icon == QStyle.SP_MessageBoxQuestion:
         return QIcon.fromTheme('dialog-question', defIcon)
     elif icon == QStyle.SP_MessageBoxWarning:
         return QIcon.fromTheme('dialog-warning', defIcon)
     elif icon == QStyle.SP_DialogCloseButton:
         return QIcon.fromTheme('dialog-close', defIcon)
     elif icon == QStyle.SP_BrowserStop:
         return QIcon.fromTheme('progress-stop', defIcon)
     elif icon == QStyle.SP_BrowserReload:
         return QIcon.fromTheme('view-refresh', defIcon)
     elif icon == QStyle.SP_FileDialogToParent:
         return QIcon.fromTheme('go-up', defIcon)
     elif icon == QStyle.SP_ArrowUp:
         return QIcon.fromTheme('go-up', defIcon)
     elif icon == QStyle.SP_ArrowDown:
         return QIcon.fromTheme('go-down', defIcon)
     elif icon == QStyle.SP_ArrowForward:
         if QApplication.layoutDirection() == Qt.RightToLeft:
             return QIcon.fromTheme('go-previous', defIcon)
         else:
             return QIcon.fromTheme('go-next', defIcon)
     elif icon == QStyle.SP_ArrowBack:
         if QApplication.layoutDirection() == Qt.RightToLeft:
             return QIcon.fromTheme('go-next', defIcon)
         else:
             return QIcon.fromTheme('go-previous', defIcon)
     else:
         return defIcon
    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)