|
2 | 2 | from datetime import datetime |
3 | 3 | import logging |
4 | 4 |
|
5 | | -from ...vendor.Qt import QtWidgets |
| 5 | +from ...vendor.Qt import QtWidgets, QtGui, QtCore |
| 6 | +from ...vendor import Qt |
| 7 | +from ..models import AssetModel |
6 | 8 |
|
7 | 9 | log = logging.getLogger(__name__) |
8 | 10 |
|
@@ -100,3 +102,149 @@ class PrettyTimeDelegate(QtWidgets.QStyledItemDelegate): |
100 | 102 |
|
101 | 103 | def displayText(self, value, locale): |
102 | 104 | return pretty_timestamp(value) |
| 105 | + |
| 106 | + |
| 107 | +class AssetDelegate(QtWidgets.QItemDelegate): |
| 108 | + bar_height = 3 |
| 109 | + |
| 110 | + def sizeHint(self, option, index): |
| 111 | + result = super(AssetDelegate, self).sizeHint(option, index) |
| 112 | + height = result.height() |
| 113 | + result.setHeight(height + self.bar_height) |
| 114 | + |
| 115 | + return result |
| 116 | + |
| 117 | + def paint(self, painter, option, index): |
| 118 | + painter.save() |
| 119 | + |
| 120 | + item_rect = QtCore.QRect(option.rect) |
| 121 | + item_rect.setHeight(option.rect.height() - self.bar_height) |
| 122 | + |
| 123 | + subset_colors = index.data(AssetModel.subsetColorsRole) |
| 124 | + subset_colors_width = 0 |
| 125 | + if subset_colors: |
| 126 | + subset_colors_width = option.rect.width()/len(subset_colors) |
| 127 | + |
| 128 | + subset_rects = [] |
| 129 | + counter = 0 |
| 130 | + for subset_c in subset_colors: |
| 131 | + new_color = None |
| 132 | + new_rect = None |
| 133 | + if subset_c: |
| 134 | + new_color = QtGui.QColor(*subset_c) |
| 135 | + |
| 136 | + new_rect = QtCore.QRect( |
| 137 | + option.rect.left() + (counter * subset_colors_width), |
| 138 | + option.rect.top() + (option.rect.height()-self.bar_height), |
| 139 | + subset_colors_width, |
| 140 | + self.bar_height |
| 141 | + ) |
| 142 | + subset_rects.append((new_color, new_rect)) |
| 143 | + counter += 1 |
| 144 | + |
| 145 | + # Background |
| 146 | + bg_color = QtGui.QColor(60, 60, 60) |
| 147 | + if option.state & QtWidgets.QStyle.State_Selected: |
| 148 | + if len(subset_colors) == 0: |
| 149 | + item_rect.setTop(item_rect.top() + (self.bar_height / 2)) |
| 150 | + if option.state & QtWidgets.QStyle.State_MouseOver: |
| 151 | + bg_color.setRgb(70, 70, 70) |
| 152 | + else: |
| 153 | + item_rect.setTop(item_rect.top() + (self.bar_height / 2)) |
| 154 | + if option.state & QtWidgets.QStyle.State_MouseOver: |
| 155 | + bg_color.setAlpha(100) |
| 156 | + else: |
| 157 | + bg_color.setAlpha(0) |
| 158 | + |
| 159 | + # # -- When not needed to do a rounded corners (easier and without painter restore): |
| 160 | + # painter.fillRect( |
| 161 | + # item_rect, |
| 162 | + # QtGui.QBrush(bg_color) |
| 163 | + # ) |
| 164 | + pen = painter.pen() |
| 165 | + pen.setStyle(QtCore.Qt.NoPen) |
| 166 | + pen.setWidth(0) |
| 167 | + painter.setPen(pen) |
| 168 | + painter.setBrush(QtGui.QBrush(bg_color)) |
| 169 | + painter.drawRoundedRect(option.rect, 3, 3) |
| 170 | + |
| 171 | + if option.state & QtWidgets.QStyle.State_Selected: |
| 172 | + for color, subset_rect in subset_rects: |
| 173 | + if not color or not subset_rect: |
| 174 | + continue |
| 175 | + painter.fillRect(subset_rect, QtGui.QBrush(color)) |
| 176 | + |
| 177 | + painter.restore() |
| 178 | + painter.save() |
| 179 | + |
| 180 | + # Icon |
| 181 | + icon_index = index.model().index( |
| 182 | + index.row(), index.column(), index.parent() |
| 183 | + ) |
| 184 | + # - Default icon_rect if not icon |
| 185 | + icon_rect = QtCore.QRect( |
| 186 | + item_rect.left(), |
| 187 | + item_rect.top(), |
| 188 | + # To make sure it's same size all the time |
| 189 | + option.rect.height() - self.bar_height, |
| 190 | + option.rect.height() - self.bar_height |
| 191 | + ) |
| 192 | + icon = index.model().data(icon_index, QtCore.Qt.DecorationRole) |
| 193 | + |
| 194 | + if icon: |
| 195 | + margin = 0 |
| 196 | + mode = QtGui.QIcon.Normal |
| 197 | + |
| 198 | + if not (option.state & QtWidgets.QStyle.State_Enabled): |
| 199 | + mode = QtGui.QIcon.Disabled |
| 200 | + elif option.state & QtWidgets.QStyle.State_Selected: |
| 201 | + mode = QtGui.QIcon.Selected |
| 202 | + |
| 203 | + if isinstance(icon, QtGui.QPixmap): |
| 204 | + icon = QtGui.QIcon(icon) |
| 205 | + option.decorationSize = icon.size() / icon.devicePixelRatio() |
| 206 | + |
| 207 | + elif isinstance(icon, QtGui.QColor): |
| 208 | + pixmap = QtGui.QPixmap(option.decorationSize) |
| 209 | + pixmap.fill(icon) |
| 210 | + icon = QtGui.QIcon(pixmap) |
| 211 | + |
| 212 | + elif isinstance(icon, QtGui.QImage): |
| 213 | + icon = QtGui.QIcon(QtGui.QPixmap.fromImage(icon)) |
| 214 | + option.decorationSize = icon.size() / icon.devicePixelRatio() |
| 215 | + |
| 216 | + elif isinstance(icon, QtGui.QIcon): |
| 217 | + state = QtGui.QIcon.Off |
| 218 | + if option.state & QtWidgets.QStyle.State_Open: |
| 219 | + state = QtGui.QIcon.On |
| 220 | + actualSize = option.icon.actualSize( |
| 221 | + option.decorationSize, mode, state |
| 222 | + ) |
| 223 | + option.decorationSize = QtCore.QSize( |
| 224 | + min(option.decorationSize.width(), actualSize.width()), |
| 225 | + min(option.decorationSize.height(), actualSize.height()) |
| 226 | + ) |
| 227 | + |
| 228 | + state = QtGui.QIcon.Off |
| 229 | + if option.state & QtWidgets.QStyle.State_Open: |
| 230 | + state = QtGui.QIcon.On |
| 231 | + |
| 232 | + icon.paint( |
| 233 | + painter, icon_rect, |
| 234 | + QtCore.Qt.AlignLeft , mode, state |
| 235 | + ) |
| 236 | + |
| 237 | + # Text |
| 238 | + text_rect = QtCore.QRect( |
| 239 | + icon_rect.left() + icon_rect.width() + 2, |
| 240 | + item_rect.top(), |
| 241 | + item_rect.width(), |
| 242 | + item_rect.height() |
| 243 | + ) |
| 244 | + |
| 245 | + painter.drawText( |
| 246 | + text_rect, QtCore.Qt.AlignVCenter, |
| 247 | + index.data(QtCore.Qt.DisplayRole) |
| 248 | + ) |
| 249 | + |
| 250 | + painter.restore() |
0 commit comments