Helpviewer/GreaseMonkey/GreaseMonkeyConfiguration/GreaseMonkeyConfigurationListDelegate.py

changeset 3002
6ffc581f00f1
parent 2302
f29e9405c851
child 3022
57179e4cdadd
child 3057
10516539f238
equal deleted inserted replaced
3001:3674ff5fa8f8 3002:6ffc581f00f1
2 2
3 # Copyright (c) 2012 - 2013 Detlev Offenbach <detlev@die-offenbachs.de> 3 # Copyright (c) 2012 - 2013 Detlev Offenbach <detlev@die-offenbachs.de>
4 # 4 #
5 5
6 """ 6 """
7 Module implementing a delegate for the special list widget for GreaseMonkey scripts. 7 Module implementing a delegate for the special list widget for GreaseMonkey
8 scripts.
8 """ 9 """
9 10
10 from PyQt4.QtCore import Qt, QSize, QRect 11 from PyQt4.QtCore import Qt, QSize, QRect
11 from PyQt4.QtGui import QStyle, QStyledItemDelegate, QApplication, QFontMetrics, \ 12 from PyQt4.QtGui import QStyle, QStyledItemDelegate, QApplication, \
12 QPalette, QFont, QStyleOptionViewItemV4 13 QFontMetrics, QPalette, QFont, QStyleOptionViewItemV4
13 14
14 import UI.PixmapCache 15 import UI.PixmapCache
15 import Globals 16 import Globals
16 17
17 18
18 class GreaseMonkeyConfigurationListDelegate(QStyledItemDelegate): 19 class GreaseMonkeyConfigurationListDelegate(QStyledItemDelegate):
19 """ 20 """
20 Class implementing a delegate for the special list widget for GreaseMonkey scripts. 21 Class implementing a delegate for the special list widget for GreaseMonkey
22 scripts.
21 """ 23 """
22 IconSize = 32 24 IconSize = 32
23 RemoveIconSize = 16 25 RemoveIconSize = 16
24 CheckBoxSize = 18 26 CheckBoxSize = 18
25 MinPadding = 5 27 MinPadding = 5
31 33
32 @param parent reference to the parent object (QObject) 34 @param parent reference to the parent object (QObject)
33 """ 35 """
34 super().__init__(parent) 36 super().__init__(parent)
35 37
36 self.__removePixmap = UI.PixmapCache.getIcon("greaseMonkeyTrash.png").pixmap( 38 self.__removePixmap = \
37 GreaseMonkeyConfigurationListDelegate.RemoveIconSize) 39 UI.PixmapCache.getIcon("greaseMonkeyTrash.png").pixmap(
40 GreaseMonkeyConfigurationListDelegate.RemoveIconSize)
38 self.__rowHeight = 0 41 self.__rowHeight = 0
39 self.__padding = 0 42 self.__padding = 0
40 43
41 def padding(self): 44 def padding(self):
42 """ 45 """
80 83
81 # Draw background 84 # Draw background
82 style.drawPrimitive(QStyle.PE_PanelItemViewItem, opt, painter, widget) 85 style.drawPrimitive(QStyle.PE_PanelItemViewItem, opt, painter, widget)
83 86
84 # Draw checkbox 87 # Draw checkbox
85 checkBoxYPos = center - GreaseMonkeyConfigurationListDelegate.CheckBoxSize // 2 88 checkBoxYPos = center - \
89 GreaseMonkeyConfigurationListDelegate.CheckBoxSize // 2
86 opt2 = QStyleOptionViewItemV4(opt) 90 opt2 = QStyleOptionViewItemV4(opt)
87 if opt2.checkState == Qt.Checked: 91 if opt2.checkState == Qt.Checked:
88 opt2.state |= QStyle.State_On 92 opt2.state |= QStyle.State_On
89 else: 93 else:
90 opt2.state |= QStyle.State_Off 94 opt2.state |= QStyle.State_Off
91 styleCheckBoxRect = style.subElementRect( 95 styleCheckBoxRect = style.subElementRect(
92 QStyle.SE_ViewItemCheckIndicator, opt2, widget) 96 QStyle.SE_ViewItemCheckIndicator, opt2, widget)
93 opt2.rect = QRect(leftPos, checkBoxYPos, 97 opt2.rect = QRect(leftPos, checkBoxYPos,
94 styleCheckBoxRect.width(), styleCheckBoxRect.height()) 98 styleCheckBoxRect.width(), styleCheckBoxRect.height())
95 style.drawPrimitive(QStyle.PE_IndicatorViewItemCheck, opt2, painter, widget) 99 style.drawPrimitive(QStyle.PE_IndicatorViewItemCheck, opt2, painter,
100 widget)
96 leftPos = opt2.rect.right() + self.__padding 101 leftPos = opt2.rect.right() + self.__padding
97 102
98 # Draw icon 103 # Draw icon
99 iconYPos = center - GreaseMonkeyConfigurationListDelegate.IconSize // 2 104 iconYPos = center - GreaseMonkeyConfigurationListDelegate.IconSize // 2
100 iconRect = QRect(leftPos, iconYPos, 105 iconRect = QRect(leftPos, iconYPos,
120 version = index.data(Qt.UserRole) 125 version = index.data(Qt.UserRole)
121 versionRect = QRect(nameRect.x() + leftPosForVersion, nameRect.y(), 126 versionRect = QRect(nameRect.x() + leftPosForVersion, nameRect.y(),
122 rightTitleEdge - leftTitleEdge, titleMetrics.height()) 127 rightTitleEdge - leftTitleEdge, titleMetrics.height())
123 versionFont = titleFont 128 versionFont = titleFont
124 painter.setFont(versionFont) 129 painter.setFont(versionFont)
125 style.drawItemText(painter, versionRect, Qt.AlignLeft, opt.palette, True, 130 style.drawItemText(painter, versionRect, Qt.AlignLeft, opt.palette,
126 version, colorRole) 131 True, version, colorRole)
127 132
128 # Draw description 133 # Draw description
129 infoYPos = nameRect.bottom() + opt.fontMetrics.leading() 134 infoYPos = nameRect.bottom() + opt.fontMetrics.leading()
130 infoRect = QRect(nameRect.x(), infoYPos, 135 infoRect = QRect(nameRect.x(), infoYPos,
131 nameRect.width(), opt.fontMetrics.height()) 136 nameRect.width(), opt.fontMetrics.height())
132 info = opt.fontMetrics.elidedText(index.data(Qt.UserRole + 1), Qt.ElideRight, 137 info = opt.fontMetrics.elidedText(
133 infoRect.width()) 138 index.data(Qt.UserRole + 1), Qt.ElideRight, infoRect.width())
134 painter.setFont(opt.font) 139 painter.setFont(opt.font)
135 style.drawItemText(painter, infoRect, Qt.AlignLeft | Qt.TextSingleLine, 140 style.drawItemText(painter, infoRect, Qt.AlignLeft | Qt.TextSingleLine,
136 opt.palette, True, info, colorRole) 141 opt.palette, True, info, colorRole)
137 142
138 # Draw remove button 143 # Draw remove button
154 if not self.__rowHeight: 159 if not self.__rowHeight:
155 opt = QStyleOptionViewItemV4(option) 160 opt = QStyleOptionViewItemV4(option)
156 self.initStyleOption(opt, index) 161 self.initStyleOption(opt, index)
157 162
158 widget = opt.widget 163 widget = opt.widget
159 style = widget.style() if widget is not None else QApplication.style() 164 style = widget.style() if widget is not None \
165 else QApplication.style()
160 padding = style.pixelMetric(QStyle.PM_FocusFrameHMargin) + 1 166 padding = style.pixelMetric(QStyle.PM_FocusFrameHMargin) + 1
161 167
162 titleFont = opt.font 168 titleFont = opt.font
163 titleFont.setBold(True) 169 titleFont.setBold(True)
164 titleFont.setPointSize(titleFont.pointSize() + 1) 170 titleFont.setPointSize(titleFont.pointSize() + 1)
165 171
166 self.__padding = padding \ 172 self.__padding = padding \
167 if padding > GreaseMonkeyConfigurationListDelegate.MinPadding else \ 173 if padding > GreaseMonkeyConfigurationListDelegate.MinPadding \
168 GreaseMonkeyConfigurationListDelegate.MinPadding 174 else GreaseMonkeyConfigurationListDelegate.MinPadding
169 175
170 titleMetrics = QFontMetrics(titleFont) 176 titleMetrics = QFontMetrics(titleFont)
171 177
172 self.__rowHeight = 2 * self.__padding + \ 178 self.__rowHeight = 2 * self.__padding + \
173 opt.fontMetrics.leading() + \ 179 opt.fontMetrics.leading() + \
174 opt.fontMetrics.height() + \ 180 opt.fontMetrics.height() + \
175 titleMetrics.height() 181 titleMetrics.height()
176 182
177 return QSize(GreaseMonkeyConfigurationListDelegate.ItemWidth, self.__rowHeight) 183 return QSize(GreaseMonkeyConfigurationListDelegate.ItemWidth,
184 self.__rowHeight)

eric ide

mercurial