forked from Fluorite-Apps/Fluorite-Store
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state_tooltip.py
137 lines (112 loc) · 4.43 KB
/
state_tooltip.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# coding:utf-8
from PyQt5.QtCore import QEasingCurve, QPropertyAnimation, Qt, QTimer, pyqtSignal
from PyQt5.QtGui import QPainter, QPixmap
from PyQt5.QtWidgets import QLabel, QWidget, QToolButton
class StateTooltip(QWidget):
""" State tooltip """
closedSignal = pyqtSignal()
def __init__(self, title, content, parent=None):
"""
Parameters
----------
title: str
title of tooltip
content: str
content of tooltip
parant:
parent window
"""
super().__init__(parent)
self.title = title
self.content = content
self.titleLabel = QLabel(self.title, self)
self.contentLabel = QLabel(self.content, self)
self.rotateTimer = QTimer(self)
self.closeTimer = QTimer(self)
self.animation = QPropertyAnimation(self, b"windowOpacity")
self.busyImage = QPixmap("resource/statetooltip/images/running.png")
self.doneImage = QPixmap("resource/statetooltip/images/completed.png")
self.closeButton = QToolButton(self)
self.isDone = False
self.rotateAngle = 0
self.deltaAngle = 20
self.__initWidget()
def __initWidget(self):
""" initialize widgets """
self.setAttribute(Qt.WA_StyledBackground)
self.rotateTimer.setInterval(50)
self.closeTimer.setInterval(1000)
self.contentLabel.setMinimumWidth(200)
# connect signal to slot
self.closeButton.clicked.connect(self.__onCloseButtonClicked)
self.rotateTimer.timeout.connect(self.__rotateTimerFlowSlot)
self.closeTimer.timeout.connect(self.__slowlyClose)
self.__setQss()
self.__initLayout()
self.rotateTimer.start()
def __initLayout(self):
""" initialize layout """
self.setFixedSize(max(self.titleLabel.width(),
self.contentLabel.width()) + 70, 64)
self.titleLabel.move(40, 11)
self.contentLabel.move(15, 34)
self.closeButton.move(self.width() - 30, 23)
def __setQss(self):
""" set style sheet """
self.titleLabel.setObjectName("titleLabel")
self.contentLabel.setObjectName("contentLabel")
with open("resource/statetooltip/style/state_tooltip.qss", encoding="utf-8") as f:
self.setStyleSheet(f.read())
self.titleLabel.adjustSize()
self.contentLabel.adjustSize()
def setTitle(self, title: str):
""" set the title of tooltip """
self.title = title
self.titleLabel.setText(title)
self.titleLabel.adjustSize()
def setContent(self, content: str):
""" set the content of tooltip """
self.content = content
self.contentLabel.setText(content)
# adjustSize() will mask spinner get stuck
self.contentLabel.adjustSize()
def setState(self, isDone=False):
""" set the state of tooltip """
self.isDone = isDone
self.update()
if self.isDone:
self.closeTimer.start()
def __onCloseButtonClicked(self):
""" close button clicked slot """
self.closedSignal.emit()
self.hide()
def __slowlyClose(self):
""" fade out """
self.rotateTimer.stop()
self.animation.setEasingCurve(QEasingCurve.Linear)
self.animation.setDuration(500)
self.animation.setStartValue(1)
self.animation.setEndValue(0)
self.animation.finished.connect(self.deleteLater)
self.animation.start()
def __rotateTimerFlowSlot(self):
""" rotate timer time out slot """
self.rotateAngle = (self.rotateAngle + self.deltaAngle) % 360
self.update()
def paintEvent(self, e):
""" paint state tooltip """
super().paintEvent(e)
painter = QPainter(self)
painter.setRenderHints(QPainter.SmoothPixmapTransform)
painter.setPen(Qt.NoPen)
if not self.isDone:
painter.translate(24, 23)
painter.rotate(self.rotateAngle)
painter.drawPixmap(
-int(self.busyImage.width() / 2),
-int(self.busyImage.height() / 2),
self.busyImage,
)
else:
painter.drawPixmap(14, 13, self.doneImage.width(),
self.doneImage.height(), self.doneImage)