Attachment 'movie_delegate.py'
Download
Toggle line numbers
1 #!/usr/bin/env python
2
3 import random, sys
4 from PyQt4.QtCore import pyqtSignal, QObject, Qt, QTimer, QVariant
5 from PyQt4.QtGui import *
6
7 class Delegate(QItemDelegate):
8
9 needsRedraw = pyqtSignal()
10
11 def __init__(self, movie, parent = None):
12
13 QItemDelegate.__init__(self, parent)
14 self.movie = movie
15 self.movie.frameChanged.connect(self.needsRedraw)
16 self.playing = False
17
18 def startMovie(self):
19 self.movie.start()
20 self.playing = True
21
22 def stopMovie(self):
23 self.movie.stop()
24 self.playing = False
25
26 def paint(self, painter, option, index):
27
28 waiting = index.data(Qt.UserRole).toBool()
29 if waiting:
30 option = option.__class__(option)
31 pixmap = self.movie.currentPixmap()
32 painter.drawPixmap(option.rect.topLeft(), pixmap)
33 option.rect = option.rect.translated(pixmap.width(), 0)
34
35 QItemDelegate.paint(self, painter, option, index)
36
37
38 class Model(QStandardItemModel):
39
40 finished = pyqtSignal()
41
42 def __init__(self, parent = None):
43
44 QStandardItemModel.__init__(self, parent)
45 self.pendingItems = {}
46
47 def appendRow(self, item):
48
49 if item.data(Qt.UserRole).toBool():
50
51 timer = QTimer()
52 timer.timeout.connect(self.checkPending)
53 timer.setSingleShot(True)
54 self.pendingItems[timer] = item
55 timer.start(2000 + random.randrange(0, 2000))
56
57 QStandardItemModel.appendRow(self, item)
58
59 def checkPending(self):
60
61 # Check when items are updated so that we can emit the finished()
62 # signal when the list is cleared.
63 item = self.pendingItems[self.sender()]
64 del self.pendingItems[self.sender()]
65 item.setData(QVariant(False), Qt.UserRole)
66 if not self.pendingItems:
67 self.finished.emit()
68
69
70 if __name__ == "__main__":
71
72 random.seed()
73
74 app = QApplication(sys.argv)
75 view = QListView()
76 model = Model()
77 waiting = True
78
79 for i in range(5):
80
81 item = QStandardItem("Test %i" % i)
82 item.setData(QVariant(waiting), Qt.UserRole)
83 waiting = not waiting
84 model.appendRow(item)
85
86 view.setModel(model)
87
88 delegate = Delegate(QMovie("animation.mng"))
89 view.setItemDelegate(delegate)
90 delegate.needsRedraw.connect(view.viewport().update)
91 delegate.startMovie()
92
93 model.finished.connect(delegate.stopMovie)
94 model.finished.connect(view.viewport().update)
95
96 view.show()
97 sys.exit(app.exec_())
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.