~ubuntu-branches/ubuntu/jaunty/calibre/jaunty-backports

« back to all changes in this revision

Viewing changes to src/calibre/gui2/cover_flow.py

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-01-20 17:14:02 UTC
  • Revision ID: james.westby@ubuntu.com-20090120171402-8y3znf6nokwqe80k
Tags: upstream-0.4.125+dfsg
ImportĀ upstreamĀ versionĀ 0.4.125+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env  python
 
2
__license__   = 'GPL v3'
 
3
__copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
 
4
__docformat__ = 'restructuredtext en'
 
5
 
 
6
'''
 
7
Module to implement the Cover Flow feature
 
8
'''
 
9
 
 
10
import sys, os
 
11
 
 
12
from PyQt4.QtGui import QImage, QSizePolicy
 
13
from PyQt4.QtCore import Qt, QSize, SIGNAL, QObject
 
14
 
 
15
from calibre import plugins
 
16
from calibre.gui2 import config
 
17
pictureflow, pictureflowerror = plugins['pictureflow']
 
18
 
 
19
if pictureflow is not None:
 
20
    class EmptyImageList(pictureflow.FlowImages):
 
21
        def __init__(self):
 
22
            pictureflow.FlowImages.__init__(self)
 
23
            
 
24
    class FileSystemImages(pictureflow.FlowImages):
 
25
        
 
26
        def __init__(self, dirpath):
 
27
            pictureflow.FlowImages.__init__(self)
 
28
            self.images = []
 
29
            self.captions = []
 
30
            for f in os.listdir(dirpath):
 
31
                f = os.path.join(dirpath, f)
 
32
                img = QImage(f)
 
33
                if not img.isNull():
 
34
                    self.images.append(img)
 
35
                    self.captions.append(os.path.basename(f))
 
36
                
 
37
        def count(self):
 
38
            return len(self.images)
 
39
        
 
40
        def image(self, index):
 
41
            return self.images[index]
 
42
        
 
43
        def caption(self, index):
 
44
            return self.captions[index]
 
45
        
 
46
        def currentChanged(self, index):
 
47
            print 'current changed:', index
 
48
        
 
49
    class DatabaseImages(pictureflow.FlowImages):
 
50
        
 
51
        def __init__(self, model, buffer=20):
 
52
            pictureflow.FlowImages.__init__(self)
 
53
            self.model = model
 
54
            QObject.connect(self.model, SIGNAL('modelReset()'), self.reset)
 
55
            
 
56
        def count(self):
 
57
            return self.model.count()
 
58
        
 
59
        def caption(self, index):
 
60
            return self.model.title(index)
 
61
        
 
62
        def reset(self):
 
63
            self.emit(SIGNAL('dataChanged()'))
 
64
            
 
65
        def image(self, index):
 
66
            return self.model.cover(index)
 
67
        
 
68
                    
 
69
            
 
70
    class CoverFlow(pictureflow.PictureFlow):
 
71
        
 
72
        def __init__(self, height=300, parent=None, text_height=25):
 
73
            pictureflow.PictureFlow.__init__(self, parent, 
 
74
                                config['cover_flow_queue_length']+1)
 
75
            self.setSlideSize(QSize(int(2/3. * height), height))
 
76
            self.setMinimumSize(QSize(int(2.35*0.67*height), (5/3.)*height+text_height))
 
77
            self.setFocusPolicy(Qt.WheelFocus)
 
78
            self.setSizePolicy(QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum))
 
79
            
 
80
        def wheelEvent(self, ev):
 
81
            ev.accept()
 
82
            if ev.delta() < 0:
 
83
                self.showNext()
 
84
            elif ev.delta() > 0:
 
85
                self.showPrevious()
 
86
            
 
87
        
 
88
else:
 
89
    CoverFlow = None
 
90
    DatabaseImages = None
 
91
    FileSystemImages = None
 
92
 
 
93
def main(args=sys.argv):
 
94
    return 0
 
95
 
 
96
if __name__ == '__main__':
 
97
    from PyQt4.QtGui import QApplication, QMainWindow
 
98
    app = QApplication([])
 
99
    w = QMainWindow()
 
100
    cf = CoverFlow()
 
101
    cf.resize(cf.minimumSize())
 
102
    w.resize(cf.minimumSize()+QSize(30, 20))
 
103
    path = sys.argv[1]
 
104
    model = FileSystemImages(sys.argv[1])
 
105
    cf.setImages(model)
 
106
    cf.connect(cf, SIGNAL('currentChanged(int)'), model.currentChanged)
 
107
    w.setCentralWidget(cf)
 
108
    
 
109
    w.show()
 
110
    cf.setFocus(Qt.OtherFocusReason)
 
111
    sys.exit(app.exec_())