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