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