CMS 3D CMS Logo

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