|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2020 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a label widget showing an animated pixmap. |
|
8 """ |
|
9 |
|
10 from PyQt6.QtCore import pyqtSlot, QTimer |
|
11 from PyQt6.QtGui import QPixmap |
|
12 from PyQt6.QtWidgets import QLabel |
|
13 |
|
14 import UI.PixmapCache |
|
15 |
|
16 |
|
17 class EricAnimatedLabel(QLabel): |
|
18 """ |
|
19 Class implementing a label widget showing an animated pixmap. |
|
20 """ |
|
21 def __init__(self, parent=None, *, animationFile="", interval=100): |
|
22 """ |
|
23 Constructor |
|
24 |
|
25 @param parent reference to the parent window |
|
26 @type QWidget |
|
27 @keyparam animationFile path to the file containing the animation data |
|
28 @type str |
|
29 @keyparam interval interval in milliseconds between animation frames |
|
30 @type int |
|
31 """ |
|
32 super().__init__(parent) |
|
33 |
|
34 self.__timer = QTimer(self) |
|
35 self.__timer.setInterval(interval) |
|
36 self.__timer.timeout.connect(self.__animate) |
|
37 |
|
38 self.__currentFrame = 0 |
|
39 self.__frames = 0 |
|
40 self.__pixmap = None |
|
41 self.__pixmapHeight = 0 |
|
42 self.__animationFile = "" |
|
43 self.__animationFileLoaded = False |
|
44 |
|
45 self.__loadAnimationFile(animationFile) |
|
46 |
|
47 def __loadAnimationFile(self, animationFile): |
|
48 """ |
|
49 Private method to load an animation file. |
|
50 |
|
51 @param animationFile path to the file containing the animation data |
|
52 @type str |
|
53 """ |
|
54 self.__animationFile = animationFile |
|
55 |
|
56 pixmap = UI.PixmapCache.getPixmap(animationFile) |
|
57 if not pixmap.isNull(): |
|
58 self.__pixmap = pixmap |
|
59 self.__pixmapHeight = pixmap.height() |
|
60 self.__frames = pixmap.width() // pixmap.height() |
|
61 # assume quadratic animation frames |
|
62 self.__animationFileLoaded = True |
|
63 else: |
|
64 self.__pixmap = QPixmap() |
|
65 self.__pixmapHeight = 0 |
|
66 self.__frames = 0 |
|
67 self.__animationFileLoaded = False |
|
68 |
|
69 self.reset() |
|
70 |
|
71 @pyqtSlot() |
|
72 def __animate(self): |
|
73 """ |
|
74 Private slot to animate the pixmap. |
|
75 """ |
|
76 if self.__animationFileLoaded: |
|
77 self.__currentFrame = (self.__currentFrame + 1) % self.__frames |
|
78 super().setPixmap(self.__pixmap.copy( |
|
79 self.__currentFrame * self.__pixmapHeight, |
|
80 0, |
|
81 self.__pixmapHeight, |
|
82 self.__pixmapHeight |
|
83 )) |
|
84 else: |
|
85 self.clear() |
|
86 |
|
87 @pyqtSlot() |
|
88 def reset(self): |
|
89 """ |
|
90 Public slot to reset the animation. |
|
91 """ |
|
92 self.__currentFrame = -1 |
|
93 self.__animate() |
|
94 |
|
95 @pyqtSlot() |
|
96 def start(self): |
|
97 """ |
|
98 Public slot to start the animation. |
|
99 """ |
|
100 if self.__animationFileLoaded: |
|
101 self.__timer.start() |
|
102 |
|
103 @pyqtSlot() |
|
104 def stop(self): |
|
105 """ |
|
106 Public slot to stop the animation. |
|
107 """ |
|
108 self.__timer.stop() |
|
109 |
|
110 def isActive(self): |
|
111 """ |
|
112 Public method to check, if the animation is active. |
|
113 |
|
114 @return flag indicating an active animation |
|
115 @rtype bool |
|
116 """ |
|
117 return self.__timer.isActive() and self.__animationFileLoaded |
|
118 |
|
119 def setAnimationFile(self, animationFile): |
|
120 """ |
|
121 Public method to set the name of the animation file. |
|
122 |
|
123 @param animationFile path to the file containing the animation data |
|
124 @type str |
|
125 """ |
|
126 active = self.__timer.isActive() |
|
127 self.__timer.stop() |
|
128 self.__loadAnimationFile(animationFile) |
|
129 if active and self.__animationFileLoaded: |
|
130 self.__timer.start() |
|
131 |
|
132 def getAnimationFile(self): |
|
133 """ |
|
134 Public method to get the name of the animation file. |
|
135 |
|
136 @return path to the file containing the animation data |
|
137 @rtype str |
|
138 """ |
|
139 return self.__animationFile |
|
140 |
|
141 def isAnimationFileLoaded(self): |
|
142 """ |
|
143 Public method to check, if the animation file was loaded. |
|
144 |
|
145 @return flag indicating a successfully loaded animation file |
|
146 @rtype bool |
|
147 """ |
|
148 return self.__animationFileLoaded |
|
149 |
|
150 def setInterval(self, interval): |
|
151 """ |
|
152 Public method to set the interval between the animated frames. |
|
153 |
|
154 @param interval interval in milliseconds between animation frames |
|
155 @type int |
|
156 """ |
|
157 self.__timer.setInterval(interval) |
|
158 |
|
159 def getInterval(self): |
|
160 """ |
|
161 Public method to get the interval between the animated frames. |
|
162 |
|
163 @return interval in milliseconds between animation frames |
|
164 @rtype int |
|
165 """ |
|
166 return self.__timer.interval() |
|
167 |
|
168 def setPixmap(self, pixmap): |
|
169 """ |
|
170 Public slot to set the pixmap of the label. |
|
171 |
|
172 Setting a standard pixmap will stop the animation and set the given |
|
173 pixmap without animating it. Thereafter the animation has to be |
|
174 restarted with the start() method. |
|
175 |
|
176 @param pixmap pixmap to be set |
|
177 @type QPixmap |
|
178 """ |
|
179 self.stop() |
|
180 super().setPixmap(pixmap) |