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