-
Notifications
You must be signed in to change notification settings - Fork 9
/
GIFWidget.py
229 lines (196 loc) · 7.71 KB
/
GIFWidget.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# -*- coding: utf-8 -*-
import math
from PySide2.QtWidgets import *
from PySide2.QtGui import *
from PySide2.QtCore import *
class OutlinedLabel(QLabel):
def __init__(self, text='', fontColor='#FFFFFF', outColor='#FFFFFF'):
super().__init__()
self.setText(text)
self.w = 1 / 10
self.mode = True
self.setBrush(QColor(fontColor))
self.setPen(outColor)
def scaledOutlineMode(self):
return self.mode
def setScaledOutlineMode(self, state):
self.mode = state
def outlineThickness(self):
return self.w * self.font().pointSize() if self.mode else self.w
def setOutlineThickness(self, value):
self.w = value
def setBrush(self, brush):
if not isinstance(brush, QBrush):
brush = QBrush(brush)
self.brush = brush
def setPen(self, pen):
if not isinstance(pen, QPen):
pen = QPen(pen)
pen.setJoinStyle(Qt.RoundJoin)
self.pen = pen
def sizeHint(self):
w = math.ceil(self.outlineThickness() * 2)
return super().sizeHint() + QSize(w, w)
def minimumSizeHint(self):
w = math.ceil(self.outlineThickness() * 2)
return super().minimumSizeHint() + QSize(w, w)
def paintEvent(self, event):
w = self.outlineThickness()
rect = self.rect()
metrics = QFontMetrics(self.font())
tr = metrics.boundingRect(self.text()).adjusted(0, 0, w, w)
if self.indent() == -1:
if self.frameWidth():
indent = (metrics.boundingRect('x').width() + w * 2) / 2
else:
indent = w
else:
indent = self.indent()
if self.alignment() & Qt.AlignLeft:
x = rect.left() + indent - min(metrics.leftBearing(self.text()['0']), 0)
elif self.alignment() & Qt.AlignRight:
x = rect.x() + rect.width() - indent - tr.width()
else:
x = (rect.width() - tr.width()) / 2
if self.alignment() & Qt.AlignTop:
y = rect.top() + indent + metrics.ascent()
elif self.alignment() & Qt.AlignBottom:
y = rect.y() + rect.height() - indent - metrics.descent()
else:
y = (rect.height() + metrics.ascent() - metrics.descent()) / 2
path = QPainterPath()
path.addText(x, y, self.font(), self.text())
qp = QPainter(self)
qp.setRenderHint(QPainter.Antialiasing)
self.pen.setWidthF(w * 2)
qp.strokePath(path, self.pen)
if 1 < self.brush.style() < 15:
qp.fillPath(path, self.palette().window())
qp.fillPath(path, self.brush)
class GIFWidget(QWidget):
finish = Signal()
moveDelta = Signal(QPoint)
def __init__(self, qmovie, opacity=False, top=True, frame=180,
fontColor='#000000', outColor='#FFFFFF', parent=None):
super().__init__(parent)
self.mousePressToken = False
self.executeToken = False
self.ID = 'DD'
self.number = '100'
self.gift = '小心心'
self.setWindowTitle('答谢特效')
if opacity:
self.setAttribute(Qt.WA_TranslucentBackground)
self.setWindowFlag(Qt.FramelessWindowHint)
if top:
self.setWindowFlag(Qt.WindowStaysOnTopHint)
self.show()
self.layout = QGridLayout()
self.setLayout(self.layout)
self.text = ' 感谢 DD 投喂的100个小心心 '
self.showText = OutlinedLabel(self.text, fontColor, outColor)
self.showText.setAlignment(Qt.AlignCenter)
self.showText.setAttribute(Qt.WA_TranslucentBackground)
self.layout.addWidget(self.showText, 1, 0, 1, 1)
self.textOpacity = QGraphicsOpacityEffect()
self.showText.setGraphicsEffect(self.textOpacity)
self.textOpacity.setOpacity(1)
self.showGIF = QLabel()
self.showGIF.setAttribute(Qt.WA_TranslucentBackground)
self.showGIF.setAlignment(Qt.AlignCenter)
self.layout.addWidget(self.showGIF, 0, 0, 1, 1)
# if gifPath:
# self.movie = QMovie(gifPath)
# self.showGIF.setMovie(self.movie)
# self.movie.start()
self.showGIF.setMovie(qmovie)
self.gifOpacity = QGraphicsOpacityEffect()
self.showGIF.setGraphicsEffect(self.gifOpacity)
self.gifOpacity.setOpacity(1)
self.totalFrame = frame
self.frame = self.totalFrame
self.animationTimer = QTimer()
self.animationTimer.setInterval(16)
self.animationTimer.timeout.connect(self.playAnimate)
def setGIFPath(self, gifPath):
self.movie = QMovie(gifPath)
self.showGIF.setMovie(self.movie)
self.movie.start()
def setText(self, text, mode, default=False):
if default:
self.ID = 'DD'
self.number = '100'
self.gift = '小心心'
if mode == 'gift':
text = ' %s ' % text.replace('{用户}', self.ID).replace('{数量}', self.number).replace('{礼物}', self.gift)
self.showText.setText(text)
elif mode == 'captain':
text = ' %s ' % text.replace('{用户}', self.ID)
self.showText.setText(text)
def setFont(self, font):
metrics = QFontMetrics(font)
path = QPainterPath()
pen = QPen(Qt.red)
penwidth = 2
pen.setWidth(penwidth)
l = metrics.width(self.text)
w = self.showText.width()
px = (l - w) / 2
if px < 0:
px = -px
py = (self.showText.height() - metrics.height()) / 2 + metrics.ascent()
if py < 0:
py = -py
path.addText(px, py, font, self.text)
painter = QPainter()
painter.strokePath(path, pen)
painter.drawPath(path)
painter.fillPath(path, QBrush(Qt.red))
self.showText.setFont(font)
def setColor(self, color):
self.showText.setStyleSheet('color:' + color)
def setBackgroundColor(self, color):
self.setStyleSheet('background-color:%s' % color)
def setGiftInfo(self, giftInfo):
self.text = ' 感谢 %s 投喂的%s个%s ' % tuple(giftInfo)
self.showText.setText(self.text)
def setGuard(self, guardInfo):
self.text = '%s %s %s' % tuple(guardInfo)
self.showText.setText(self.text)
def setSecond(self, seconds):
self.frame = seconds * 60
self.totalFrame = seconds * 60
def mousePressEvent(self, QEvent):
self.mousePressToken = True
self.startPos = QEvent.pos()
def mouseReleaseEvent(self, QEvent):
self.mousePressToken = False
def mouseMoveEvent(self, QEvent):
if self.mousePressToken:
self.move(self.pos() + (QEvent.pos() - self.startPos))
self.moveDelta.emit(QEvent.pos() - self.startPos)
def playAnimate(self):
if self.frame >= self.totalFrame - 5:
self.gifOpacity.setOpacity(0)
self.textOpacity.setOpacity(0)
self.frame -= 1
elif self.frame >= self.totalFrame - 15:
self.gifOpacity.setOpacity((self.totalFrame - self.frame - 5) / 10)
self.textOpacity.setOpacity((self.totalFrame - self.frame - 5) / 10)
self.frame -= 1
elif self.frame > 40:
self.frame -= 1
elif self.frame > 0:
self.gifOpacity.setOpacity(self.frame / 40)
self.textOpacity.setOpacity(self.frame / 40)
self.frame -= 1
else:
self.frame = self.totalFrame
self.animationTimer.stop()
if not self.executeToken:
self.gifOpacity.setOpacity(1)
self.textOpacity.setOpacity(1)
else:
self.gifOpacity.setOpacity(0)
self.textOpacity.setOpacity(0)
self.finish.emit()