src/eric7/EricWidgets/EricAnimatedLabel.py

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

eric ide

mercurial