Helpviewer/GreaseMonkey/GreaseMonkeyConfiguration/GreaseMonkeyConfigurationListDelegate.py

changeset 1953
26aa6fd94dc2
child 2077
68a34718a0ce
equal deleted inserted replaced
1952:af4103f0e93f 1953:26aa6fd94dc2
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2012 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a delegate for the special list widget for GreaseMonkey scripts.
8 """
9
10 from PyQt4.QtCore import Qt, QSize, QRect
11 from PyQt4.QtGui import QStyle, QStyledItemDelegate, QStyleOptionViewItemV4, \
12 QApplication, QFontMetrics, QPalette, QFont
13
14 import UI.PixmapCache
15 import Globals
16
17
18 class GreaseMonkeyConfigurationListDelegate(QStyledItemDelegate):
19 """
20 Class implementing a delegate for the special list widget for GreaseMonkey scripts.
21 """
22 IconSize = 32
23 RemoveIconSize = 16
24 CheckBoxSize = 18
25 MinPadding = 5
26 ItemWidth = 200
27
28 def __init__(self, parent=None):
29 """
30 Constructor
31
32 @param parent reference to the parent object (QObject)
33 """
34 super().__init__(parent)
35
36 self.__removePixmap = UI.PixmapCache.getIcon("greaseMonkeyTrash.png").pixmap(
37 GreaseMonkeyConfigurationListDelegate.RemoveIconSize)
38 self.__rowHeight = 0
39 self.__padding = 0
40
41 def padding(self):
42 """
43 Public method to get the padding used.
44
45 @return padding used (integer)
46 """
47 return self.__padding
48
49 def paint(self, painter, option, index):
50 """
51 Public method to paint the specified list item.
52
53 @param painter painter object to paint to (QPainter)
54 @param option style option used for painting (QStyleOptionViewItem)
55 @param index model index of the item (QModelIndex)
56 """
57 opt = QStyleOptionViewItemV4(option)
58 self.initStyleOption(opt, index)
59
60 widget = opt.widget
61 style = widget.style() if widget is not None else QApplication.style()
62 height = opt.rect.height()
63 center = height // 2 + opt.rect.top()
64
65 # Prepare title font
66 titleFont = QFont(opt.font)
67 titleFont.setBold(True)
68 titleFont.setPointSize(titleFont.pointSize() + 1)
69
70 titleMetrics = QFontMetrics(titleFont)
71 if Globals.isWindowsPlatform():
72 colorRole = QPalette.Text
73 else:
74 colorRole = QPalette.HighlightedText \
75 if opt.state & QStyle.State_Selected else QPalette.Text
76
77 leftPos = self.__padding
78 rightPos = opt.rect.right() - self.__padding - \
79 GreaseMonkeyConfigurationListDelegate.RemoveIconSize
80
81 # Draw background
82 style.drawPrimitive(QStyle.PE_PanelItemViewItem, opt, painter, widget)
83
84 # Draw checkbox
85 checkBoxYPos = center - GreaseMonkeyConfigurationListDelegate.CheckBoxSize // 2
86 opt2 = QStyleOptionViewItemV4(opt)
87 if opt2.checkState == Qt.Checked:
88 opt2.state |= QStyle.State_On
89 else:
90 opt2.state |= QStyle.State_Off
91 styleCheckBoxRect = style.subElementRect(
92 QStyle.SE_ViewItemCheckIndicator, opt2, widget)
93 opt2.rect = QRect(leftPos, checkBoxYPos,
94 styleCheckBoxRect.width(), styleCheckBoxRect.height())
95 style.drawPrimitive(QStyle.PE_IndicatorViewItemCheck, opt2, painter, widget)
96 leftPos = opt2.rect.right() + self.__padding
97
98 # Draw icon
99 iconYPos = center - GreaseMonkeyConfigurationListDelegate.IconSize // 2
100 iconRect = QRect(leftPos, iconYPos,
101 GreaseMonkeyConfigurationListDelegate.IconSize,
102 GreaseMonkeyConfigurationListDelegate.IconSize)
103 pixmap = index.data(Qt.DecorationRole).pixmap(
104 GreaseMonkeyConfigurationListDelegate.IconSize)
105 painter.drawPixmap(iconRect, pixmap)
106 leftPos = iconRect.right() + self.__padding
107
108 # Draw script name
109 name = index.data(Qt.DisplayRole)
110 leftTitleEdge = leftPos + 2
111 rightTitleEdge = rightPos - self.__padding
112 leftPosForVersion = titleMetrics.width(name) + self.__padding
113 nameRect = QRect(leftTitleEdge, opt.rect.top() + self.__padding,
114 rightTitleEdge - leftTitleEdge, titleMetrics.height())
115 painter.setFont(titleFont)
116 style.drawItemText(painter, nameRect, Qt.AlignLeft, opt.palette, True,
117 name, colorRole)
118
119 # Draw version
120 version = index.data(Qt.UserRole)
121 versionRect = QRect(nameRect.x() + leftPosForVersion, nameRect.y(),
122 rightTitleEdge - leftTitleEdge, titleMetrics.height())
123 versionFont = titleFont
124 painter.setFont(versionFont)
125 style.drawItemText(painter, versionRect, Qt.AlignLeft, opt.palette, True,
126 version, colorRole)
127
128 # Draw description
129 infoYPos = nameRect.bottom() + opt.fontMetrics.leading()
130 infoRect = QRect(nameRect.x(), infoYPos,
131 nameRect.width(), opt.fontMetrics.height())
132 info = opt.fontMetrics.elidedText(index.data(Qt.UserRole + 1), Qt.ElideRight,
133 infoRect.width())
134 painter.setFont(opt.font)
135 style.drawItemText(painter, infoRect, Qt.AlignLeft | Qt.TextSingleLine,
136 opt.palette, True, info, colorRole)
137
138 # Draw remove button
139 removeIconYPos = center - \
140 GreaseMonkeyConfigurationListDelegate.RemoveIconSize // 2
141 removeIconRect = QRect(rightPos, removeIconYPos,
142 GreaseMonkeyConfigurationListDelegate.RemoveIconSize,
143 GreaseMonkeyConfigurationListDelegate.RemoveIconSize)
144 painter.drawPixmap(removeIconRect, self.__removePixmap)
145
146 def sizeHint(self, option, index):
147 """
148 Public method to get a size hint for the specified list item.
149
150 @param option style option used for painting (QStyleOptionViewItem)
151 @param index model index of the item (QModelIndex)
152 @return size hint (QSize)
153 """
154 if not self.__rowHeight:
155 opt = QStyleOptionViewItemV4(option)
156 self.initStyleOption(opt, index)
157
158 widget = opt.widget
159 style = widget.style() if widget is not None else QApplication.style()
160 padding = style.pixelMetric(QStyle.PM_FocusFrameHMargin) + 1
161
162 titleFont = opt.font
163 titleFont.setBold(True)
164 titleFont.setPointSize(titleFont.pointSize() + 1)
165
166 self.__padding = padding \
167 if padding > GreaseMonkeyConfigurationListDelegate.MinPadding else \
168 GreaseMonkeyConfigurationListDelegate.MinPadding
169
170 titleMetrics = QFontMetrics(titleFont)
171
172 self.__rowHeight = 2 * self.__padding + \
173 opt.fontMetrics.leading() + \
174 opt.fontMetrics.height() + \
175 titleMetrics.height()
176
177 return QSize(GreaseMonkeyConfigurationListDelegate.ItemWidth, self.__rowHeight)

eric ide

mercurial