|
7 | 7 | @site: https://pyqt5.com https://github.com/892768447 |
8 | 8 | @email: 892768447@qq.com |
9 | 9 | @file: Widgets.Skins.ColourfulWidget |
10 | | -@description: |
| 10 | +@description: 多彩控件 |
11 | 11 | """ |
12 | | -from PyQt5.QtCore import QSize, Qt |
13 | | -from PyQt5.QtGui import QPainter, QBrush, QColor |
14 | | -from PyQt5.QtWidgets import QWidget, QPushButton |
15 | 12 |
|
16 | | -from UiFiles.Ui_ScrollArea import Ui_FormScrollArea |
| 13 | +from PyQt5.QtWidgets import QPushButton |
| 14 | + |
17 | 15 | from Utils.CommonUtil import Signals |
18 | 16 | from Utils.ThemeManager import ThemeManager |
19 | 17 | from Utils.ThemeThread import ColourfulThread |
| 18 | +from Widgets.Skins.SkinBaseWidget import SkinBaseWidget, SkinBaseItemWidget,\ |
| 19 | + PixmapWidth, PixmapHeight |
20 | 20 |
|
21 | 21 |
|
22 | 22 | __Author__ = "Irony" |
23 | 23 | __Copyright__ = "Copyright (c) 2019" |
24 | 24 |
|
25 | | -PixmapWidth = 158 |
26 | | -PixmapHeight = 152 # 图片大小 |
27 | | -MarginBottom = 26 # 底部文字 |
28 | | - |
29 | | - |
30 | | -class ColourfulItemWidget(QWidget): |
31 | | - |
32 | | - def __init__(self, name, color, *args, **kwargs): |
33 | | - super(ColourfulItemWidget, self).__init__(*args, **kwargs) |
34 | | - # 加载鼠标样式 |
35 | | - ThemeManager.loadCursor(self, ThemeManager.CursorPointer) |
36 | | - self.name = name |
37 | | - self.color = color |
38 | | - self.hovered = False |
39 | | - self.colorHover = QColor(0, 0, 0, 40) |
40 | | - self.textColor = QColor(102, 102, 102) |
41 | | - |
42 | | - def mousePressEvent(self, event): |
43 | | - super(ColourfulItemWidget, self).mousePressEvent(event) |
44 | | - self.hovered = True |
45 | | - self.textColor = QColor(18, 183, 245) |
46 | | - self.update() |
47 | | - |
48 | | - def mouseReleaseEvent(self, event): |
49 | | - super(ColourfulItemWidget, self).mouseReleaseEvent(event) |
50 | | - self.hovered = False |
51 | | - self.textColor = QColor(102, 102, 102) |
52 | | - self.update() |
53 | | - Signals.colourfulItemClicked.emit(self.name, self.color) |
54 | | - |
55 | | - def enterEvent(self, event): |
56 | | - super(ColourfulItemWidget, self).enterEvent(event) |
57 | | - self.hovered = True |
58 | | - self.textColor = QColor(Qt.black) |
59 | | - self.update() |
60 | | - |
61 | | - def leaveEvent(self, event): |
62 | | - super(ColourfulItemWidget, self).leaveEvent(event) |
63 | | - self.hovered = False |
64 | | - self.textColor = QColor(102, 102, 102) |
65 | | - self.update() |
66 | | - |
67 | | - def paintEvent(self, event): |
68 | | - super(ColourfulItemWidget, self).paintEvent(event) |
69 | | - painter = QPainter(self) |
70 | | - painter.setRenderHint(QPainter.SmoothPixmapTransform) |
71 | | - # 绘制颜色方块 |
72 | | - painter.save() |
73 | | - painter.setPen(Qt.NoPen) |
74 | | - painter.setBrush(QBrush(self.color)) |
75 | | - painter.drawRoundedRect( |
76 | | - 0, 0, PixmapWidth, PixmapHeight, 2, 2) |
77 | | - if self.hovered: |
78 | | - # 绘制一层灰色 |
79 | | - painter.setBrush(QBrush(self.colorHover)) |
80 | | - painter.drawRoundedRect( |
81 | | - 0, 0, PixmapWidth, PixmapHeight, 2, 2) |
82 | | - painter.restore() |
83 | | - # 绘制文字 |
84 | | - painter.setPen(self.textColor) |
85 | | - painter.drawText(0, 0, PixmapWidth, PixmapHeight + MarginBottom, |
86 | | - Qt.AlignHCenter | Qt.AlignBottom, self.name) |
87 | | - painter.end() |
88 | | - |
89 | | - def sizeHint(self): |
90 | | - return QSize(PixmapWidth, PixmapHeight + MarginBottom) |
91 | | - |
92 | 25 |
|
93 | | -class ColourfulWidget(QWidget, Ui_FormScrollArea): |
| 26 | +class ColourfulWidget(SkinBaseWidget): |
94 | 27 |
|
95 | 28 | def __init__(self, *args, **kwargs): |
96 | 29 | super(ColourfulWidget, self).__init__(*args, **kwargs) |
97 | | - self.setupUi(self) |
98 | | - self.setAttribute(Qt.WA_StyledBackground, True) |
99 | 30 | Signals.colourfulItemAdded.connect(self.onColourfulItemAdded) |
100 | 31 | Signals.colourfulItemAddFinished.connect( |
101 | 32 | self.onColourfulItemAddFinished) |
@@ -137,4 +68,4 @@ def onColourfulItemAdded(self, row, col, name, color): |
137 | 68 | self.lastRow = row |
138 | 69 | self.lastCol = col |
139 | 70 | self.gridLayout.addWidget( |
140 | | - ColourfulItemWidget(name, color, self), row, col) |
| 71 | + SkinBaseItemWidget(name, color, Signals.colourfulItemClicked, self), row, col) |
0 commit comments