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