2 |
2 |
3 # Copyright (c) 2012 - 2013 Detlev Offenbach <detlev@die-offenbachs.de> |
3 # Copyright (c) 2012 - 2013 Detlev Offenbach <detlev@die-offenbachs.de> |
4 # |
4 # |
5 |
5 |
6 """ |
6 """ |
7 Module implementing a delegate for the special list widget for GreaseMonkey scripts. |
7 Module implementing a delegate for the special list widget for GreaseMonkey |
|
8 scripts. |
8 """ |
9 """ |
9 |
10 |
10 from __future__ import unicode_literals # __IGNORE_WARNING__ |
11 from __future__ import unicode_literals # __IGNORE_WARNING__ |
11 |
12 |
12 from PyQt4.QtCore import Qt, QSize, QRect |
13 from PyQt4.QtCore import Qt, QSize, QRect |
13 from PyQt4.QtGui import QStyle, QStyledItemDelegate, QApplication, QFontMetrics, \ |
14 from PyQt4.QtGui import QStyle, QStyledItemDelegate, QApplication, \ |
14 QPalette, QFont, QStyleOptionViewItemV4 |
15 QFontMetrics, QPalette, QFont, QStyleOptionViewItemV4 |
15 |
16 |
16 import UI.PixmapCache |
17 import UI.PixmapCache |
17 import Globals |
18 import Globals |
18 |
19 |
19 |
20 |
20 class GreaseMonkeyConfigurationListDelegate(QStyledItemDelegate): |
21 class GreaseMonkeyConfigurationListDelegate(QStyledItemDelegate): |
21 """ |
22 """ |
22 Class implementing a delegate for the special list widget for GreaseMonkey scripts. |
23 Class implementing a delegate for the special list widget for GreaseMonkey |
|
24 scripts. |
23 """ |
25 """ |
24 IconSize = 32 |
26 IconSize = 32 |
25 RemoveIconSize = 16 |
27 RemoveIconSize = 16 |
26 CheckBoxSize = 18 |
28 CheckBoxSize = 18 |
27 MinPadding = 5 |
29 MinPadding = 5 |
33 |
35 |
34 @param parent reference to the parent object (QObject) |
36 @param parent reference to the parent object (QObject) |
35 """ |
37 """ |
36 super(GreaseMonkeyConfigurationListDelegate, self).__init__(parent) |
38 super(GreaseMonkeyConfigurationListDelegate, self).__init__(parent) |
37 |
39 |
38 self.__removePixmap = UI.PixmapCache.getIcon("greaseMonkeyTrash.png").pixmap( |
40 self.__removePixmap = \ |
39 GreaseMonkeyConfigurationListDelegate.RemoveIconSize) |
41 UI.PixmapCache.getIcon("greaseMonkeyTrash.png").pixmap( |
|
42 GreaseMonkeyConfigurationListDelegate.RemoveIconSize) |
40 self.__rowHeight = 0 |
43 self.__rowHeight = 0 |
41 self.__padding = 0 |
44 self.__padding = 0 |
42 |
45 |
43 def padding(self): |
46 def padding(self): |
44 """ |
47 """ |
82 |
85 |
83 # Draw background |
86 # Draw background |
84 style.drawPrimitive(QStyle.PE_PanelItemViewItem, opt, painter, widget) |
87 style.drawPrimitive(QStyle.PE_PanelItemViewItem, opt, painter, widget) |
85 |
88 |
86 # Draw checkbox |
89 # Draw checkbox |
87 checkBoxYPos = center - GreaseMonkeyConfigurationListDelegate.CheckBoxSize // 2 |
90 checkBoxYPos = center - \ |
|
91 GreaseMonkeyConfigurationListDelegate.CheckBoxSize // 2 |
88 opt2 = QStyleOptionViewItemV4(opt) |
92 opt2 = QStyleOptionViewItemV4(opt) |
89 if opt2.checkState == Qt.Checked: |
93 if opt2.checkState == Qt.Checked: |
90 opt2.state |= QStyle.State_On |
94 opt2.state |= QStyle.State_On |
91 else: |
95 else: |
92 opt2.state |= QStyle.State_Off |
96 opt2.state |= QStyle.State_Off |
93 styleCheckBoxRect = style.subElementRect( |
97 styleCheckBoxRect = style.subElementRect( |
94 QStyle.SE_ViewItemCheckIndicator, opt2, widget) |
98 QStyle.SE_ViewItemCheckIndicator, opt2, widget) |
95 opt2.rect = QRect(leftPos, checkBoxYPos, |
99 opt2.rect = QRect(leftPos, checkBoxYPos, |
96 styleCheckBoxRect.width(), styleCheckBoxRect.height()) |
100 styleCheckBoxRect.width(), styleCheckBoxRect.height()) |
97 style.drawPrimitive(QStyle.PE_IndicatorViewItemCheck, opt2, painter, widget) |
101 style.drawPrimitive(QStyle.PE_IndicatorViewItemCheck, opt2, painter, |
|
102 widget) |
98 leftPos = opt2.rect.right() + self.__padding |
103 leftPos = opt2.rect.right() + self.__padding |
99 |
104 |
100 # Draw icon |
105 # Draw icon |
101 iconYPos = center - GreaseMonkeyConfigurationListDelegate.IconSize // 2 |
106 iconYPos = center - GreaseMonkeyConfigurationListDelegate.IconSize // 2 |
102 iconRect = QRect(leftPos, iconYPos, |
107 iconRect = QRect(leftPos, iconYPos, |
122 version = index.data(Qt.UserRole) |
127 version = index.data(Qt.UserRole) |
123 versionRect = QRect(nameRect.x() + leftPosForVersion, nameRect.y(), |
128 versionRect = QRect(nameRect.x() + leftPosForVersion, nameRect.y(), |
124 rightTitleEdge - leftTitleEdge, titleMetrics.height()) |
129 rightTitleEdge - leftTitleEdge, titleMetrics.height()) |
125 versionFont = titleFont |
130 versionFont = titleFont |
126 painter.setFont(versionFont) |
131 painter.setFont(versionFont) |
127 style.drawItemText(painter, versionRect, Qt.AlignLeft, opt.palette, True, |
132 style.drawItemText(painter, versionRect, Qt.AlignLeft, opt.palette, |
128 version, colorRole) |
133 True, version, colorRole) |
129 |
134 |
130 # Draw description |
135 # Draw description |
131 infoYPos = nameRect.bottom() + opt.fontMetrics.leading() |
136 infoYPos = nameRect.bottom() + opt.fontMetrics.leading() |
132 infoRect = QRect(nameRect.x(), infoYPos, |
137 infoRect = QRect(nameRect.x(), infoYPos, |
133 nameRect.width(), opt.fontMetrics.height()) |
138 nameRect.width(), opt.fontMetrics.height()) |
134 info = opt.fontMetrics.elidedText(index.data(Qt.UserRole + 1), Qt.ElideRight, |
139 info = opt.fontMetrics.elidedText( |
135 infoRect.width()) |
140 index.data(Qt.UserRole + 1), Qt.ElideRight, infoRect.width()) |
136 painter.setFont(opt.font) |
141 painter.setFont(opt.font) |
137 style.drawItemText(painter, infoRect, Qt.AlignLeft | Qt.TextSingleLine, |
142 style.drawItemText(painter, infoRect, Qt.AlignLeft | Qt.TextSingleLine, |
138 opt.palette, True, info, colorRole) |
143 opt.palette, True, info, colorRole) |
139 |
144 |
140 # Draw remove button |
145 # Draw remove button |
156 if not self.__rowHeight: |
161 if not self.__rowHeight: |
157 opt = QStyleOptionViewItemV4(option) |
162 opt = QStyleOptionViewItemV4(option) |
158 self.initStyleOption(opt, index) |
163 self.initStyleOption(opt, index) |
159 |
164 |
160 widget = opt.widget |
165 widget = opt.widget |
161 style = widget.style() if widget is not None else QApplication.style() |
166 style = widget.style() if widget is not None \ |
|
167 else QApplication.style() |
162 padding = style.pixelMetric(QStyle.PM_FocusFrameHMargin) + 1 |
168 padding = style.pixelMetric(QStyle.PM_FocusFrameHMargin) + 1 |
163 |
169 |
164 titleFont = opt.font |
170 titleFont = opt.font |
165 titleFont.setBold(True) |
171 titleFont.setBold(True) |
166 titleFont.setPointSize(titleFont.pointSize() + 1) |
172 titleFont.setPointSize(titleFont.pointSize() + 1) |
167 |
173 |
168 self.__padding = padding \ |
174 self.__padding = padding \ |
169 if padding > GreaseMonkeyConfigurationListDelegate.MinPadding else \ |
175 if padding > GreaseMonkeyConfigurationListDelegate.MinPadding \ |
170 GreaseMonkeyConfigurationListDelegate.MinPadding |
176 else GreaseMonkeyConfigurationListDelegate.MinPadding |
171 |
177 |
172 titleMetrics = QFontMetrics(titleFont) |
178 titleMetrics = QFontMetrics(titleFont) |
173 |
179 |
174 self.__rowHeight = 2 * self.__padding + \ |
180 self.__rowHeight = 2 * self.__padding + \ |
175 opt.fontMetrics.leading() + \ |
181 opt.fontMetrics.leading() + \ |
176 opt.fontMetrics.height() + \ |
182 opt.fontMetrics.height() + \ |
177 titleMetrics.height() |
183 titleMetrics.height() |
178 |
184 |
179 return QSize(GreaseMonkeyConfigurationListDelegate.ItemWidth, self.__rowHeight) |
185 return QSize(GreaseMonkeyConfigurationListDelegate.ItemWidth, |
|
186 self.__rowHeight) |