src/eric7/WebBrowser/GreaseMonkey/GreaseMonkeyConfiguration/GreaseMonkeyConfigurationListDelegate.py

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

eric ide

mercurial