CMS 3D CMS Logo

RotatingIcon.py
Go to the documentation of this file.
1 from builtins import range
2 from PyQt4.QtCore import QTimeLine,SIGNAL,Qt
3 from PyQt4.QtGui import QLabel,QPixmap,QMatrix,QPainter
4 
5 class RotatingIcon(QLabel):
6  def __init__(self,resource,parent=None,steps=20,width=15,height=15):
7  QLabel.__init__(self,parent)
8  self._resource=resource
9  self._steps=steps
10  self._width=width
11  self._height=height
12  self._progressTimeLine = QTimeLine(1000, self)
13  self._progressTimeLine.setFrameRange(0, self._steps)
14  self._progressTimeLine.setLoopCount(0)
15  self.connect(self._progressTimeLine, SIGNAL("frameChanged(int)"), self.setProgress)
16  self._renderPixmaps()
17  self.setProgress(0)
18 
19  def _renderPixmaps(self):
20  self._pixmaps=[]
21  for i in range(self._steps+1):
22  angle = int(i * 360.0 / self._steps)
23  pixmap = QPixmap(self._resource)
24  # if problem with loading png
25  if pixmap.size().width()==0:
26  self._pixmaps=None
27  return
28  rotate_matrix = QMatrix()
29  rotate_matrix.rotate(angle)
30  pixmap_rotated = pixmap.transformed(rotate_matrix)
31  pixmap_moved = QPixmap(pixmap.size())
32  pixmap_moved.fill(Qt.transparent)
33  painter = QPainter()
34  painter.begin(pixmap_moved)
35  painter.drawPixmap((pixmap_moved.width() - pixmap_rotated.width()) / 2.0, (pixmap_moved.height() - pixmap_rotated.height()) / 2.0, pixmap_rotated)
36  painter.end()
37  self._pixmaps+=[pixmap_moved.scaled(self._width, self._height)]
38 
39  def setProgress(self, progress):
40  if self._pixmaps!=None:
41  self.setPixmap(self._pixmaps[progress])
42 
43  def start(self):
44  self.setProgress(0)
45  self._progressTimeLine.start()
46 
47  def stop(self):
48  self._progressTimeLine.stop()
def __init__(self, resource, parent=None, steps=20, width=15, height=15)
Definition: RotatingIcon.py:6