home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1217 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  10.3 KB  |  273 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
  6. __docformat__ = 'restructuredtext en'
  7. import sys
  8. import os
  9. import time
  10. from PyQt4.Qt import QImage, QSizePolicy, QTimer, QDialog, Qt, QSize, QStackedLayout, QLabel
  11. from calibre import plugins
  12. from calibre.gui2 import config, available_height, available_width
  13. (pictureflow, pictureflowerror) = plugins['pictureflow']
  14. if pictureflow is not None:
  15.     
  16.     class EmptyImageList(pictureflow.FlowImages):
  17.         
  18.         def __init__(self):
  19.             pictureflow.FlowImages.__init__(self)
  20.  
  21.  
  22.     
  23.     class FileSystemImages(pictureflow.FlowImages):
  24.         
  25.         def __init__(self, dirpath):
  26.             pictureflow.FlowImages.__init__(self)
  27.             self.images = []
  28.             self.captions = []
  29.             for f in os.listdir(dirpath):
  30.                 f = os.path.join(dirpath, f)
  31.                 img = QImage(f)
  32.                 if not img.isNull():
  33.                     self.images.append(img)
  34.                     self.captions.append(os.path.basename(f))
  35.                     continue
  36.             
  37.  
  38.         
  39.         def count(self):
  40.             return len(self.images)
  41.  
  42.         
  43.         def image(self, index):
  44.             return self.images[index]
  45.  
  46.         
  47.         def caption(self, index):
  48.             return self.captions[index]
  49.  
  50.         
  51.         def currentChanged(self, index):
  52.             print 'current changed:', index
  53.  
  54.  
  55.     
  56.     class DatabaseImages(pictureflow.FlowImages):
  57.         
  58.         def __init__(self, model, buffer = 20):
  59.             pictureflow.FlowImages.__init__(self)
  60.             self.model = model
  61.             self.model.modelReset.connect(self.reset)
  62.  
  63.         
  64.         def count(self):
  65.             return self.model.count()
  66.  
  67.         
  68.         def caption(self, index):
  69.             
  70.             try:
  71.                 ans = self.model.title(index)
  72.                 if not ans:
  73.                     ans = ''
  74.             except:
  75.                 ans = ''
  76.  
  77.             return ans
  78.  
  79.         
  80.         def subtitle(self, index):
  81.             
  82.             try:
  83.                 return u'Γÿà' * self.model.rating(index)
  84.             except:
  85.                 pass
  86.  
  87.             return ''
  88.  
  89.         
  90.         def reset(self):
  91.             self.dataChanged.emit()
  92.  
  93.         
  94.         def image(self, index):
  95.             return self.model.cover(index)
  96.  
  97.  
  98.     
  99.     class CoverFlow(pictureflow.PictureFlow):
  100.         
  101.         def __init__(self, parent = None):
  102.             pictureflow.PictureFlow.__init__(self, parent, config['cover_flow_queue_length'] + 1)
  103.             self.setMinimumSize(QSize(300, 150))
  104.             self.setFocusPolicy(Qt.WheelFocus)
  105.             self.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding))
  106.  
  107.         
  108.         def sizeHint(self):
  109.             return self.minimumSize()
  110.  
  111.         
  112.         def wheelEvent(self, ev):
  113.             ev.accept()
  114.             if ev.delta() < 0:
  115.                 self.showNext()
  116.             elif ev.delta() > 0:
  117.                 self.showPrevious()
  118.             
  119.  
  120.  
  121. else:
  122.     CoverFlow = None
  123.     DatabaseImages = None
  124.     FileSystemImages = None
  125.  
  126. class CoverFlowMixin(object):
  127.     
  128.     def __init__(self):
  129.         self.cover_flow = None
  130.         if CoverFlow is not None:
  131.             self.cf_last_updated_at = None
  132.             self.cover_flow_sync_timer = QTimer(self)
  133.             self.cover_flow_sync_timer.timeout.connect(self.cover_flow_do_sync)
  134.             self.cover_flow_sync_flag = True
  135.             self.cover_flow = CoverFlow(parent = self)
  136.             self.cover_flow.currentChanged.connect(self.sync_listview_to_cf)
  137.             self.library_view.selectionModel().currentRowChanged.connect(self.sync_cf_to_listview)
  138.             self.db_images = DatabaseImages(self.library_view.model())
  139.             self.cover_flow.setImages(self.db_images)
  140.             self.cover_flow.itemActivated.connect(self.view_specific_book)
  141.         else:
  142.             self.cover_flow = QLabel('<p>' + _('Cover browser could not be loaded') + '<br>' + pictureflowerror)
  143.             self.cover_flow.setWordWrap(True)
  144.         if config['separate_cover_flow']:
  145.             self.cb_splitter.button.clicked.connect(self.toggle_cover_browser)
  146.             if CoverFlow is not None:
  147.                 self.cover_flow.stop.connect(self.hide_cover_browser)
  148.             
  149.         else:
  150.             self.cb_splitter.insertWidget(self.cb_splitter.side_index, self.cover_flow)
  151.             if CoverFlow is not None:
  152.                 self.cover_flow.stop.connect(self.cb_splitter.hide_side_pane)
  153.             
  154.         self.cb_splitter.button.toggled.connect(self.cover_browser_toggled)
  155.  
  156.     
  157.     def toggle_cover_browser(self):
  158.         cbd = getattr(self, 'cb_dialog', None)
  159.         if cbd is not None:
  160.             self.hide_cover_browser()
  161.         else:
  162.             self.show_cover_browser()
  163.  
  164.     
  165.     def cover_browser_toggled(self, *args):
  166.         if self.cb_splitter.button.isChecked():
  167.             self.cover_browser_shown()
  168.         else:
  169.             self.cover_browser_hidden()
  170.  
  171.     
  172.     def cover_browser_shown(self):
  173.         self.cover_flow.setFocus(Qt.OtherFocusReason)
  174.         if CoverFlow is not None:
  175.             self.cover_flow.setCurrentSlide(self.library_view.currentIndex().row())
  176.             self.cover_flow_sync_timer.start(500)
  177.         
  178.         self.library_view.setCurrentIndex(self.library_view.currentIndex())
  179.         self.library_view.scroll_to_row(self.library_view.currentIndex().row())
  180.  
  181.     
  182.     def cover_browser_hidden(self):
  183.         if CoverFlow is not None:
  184.             self.cover_flow_sync_timer.stop()
  185.             idx = self.library_view.model().index(self.cover_flow.currentSlide(), 0)
  186.             if idx.isValid():
  187.                 sm = self.library_view.selectionModel()
  188.                 sm.select(idx, sm.ClearAndSelect | sm.Rows)
  189.                 self.library_view.setCurrentIndex(idx)
  190.                 self.library_view.scroll_to_row(idx.row())
  191.             
  192.         
  193.  
  194.     
  195.     def show_cover_browser(self):
  196.         d = QDialog(self)
  197.         ah = available_height()
  198.         aw = available_width()
  199.         d.resize(int(aw / 1.5), ah - 60)
  200.         d._layout = QStackedLayout()
  201.         d.setLayout(d._layout)
  202.         d.setWindowTitle(_('Browse by covers'))
  203.         d.layout().addWidget(self.cover_flow)
  204.         self.cover_flow.setVisible(True)
  205.         self.cover_flow.setFocus(Qt.OtherFocusReason)
  206.         d.show()
  207.         self.cb_splitter.button.set_state_to_hide()
  208.         d.finished.connect(self.cb_splitter.button.set_state_to_show)
  209.         self.cb_dialog = d
  210.  
  211.     
  212.     def hide_cover_browser(self):
  213.         cbd = getattr(self, 'cb_dialog', None)
  214.         if cbd is not None:
  215.             cbd.accept()
  216.             self.cb_dialog = None
  217.         
  218.  
  219.     
  220.     def sync_cf_to_listview(self, current, previous):
  221.         if self.cover_flow_sync_flag and self.cover_flow.isVisible() and self.cover_flow.currentSlide() != current.row():
  222.             self.cover_flow.setCurrentSlide(current.row())
  223.         
  224.         self.cover_flow_sync_flag = True
  225.  
  226.     
  227.     def cover_flow_do_sync(self):
  228.         self.cover_flow_sync_flag = True
  229.         
  230.         try:
  231.             if self.cover_flow.isVisible() and self.cf_last_updated_at is not None and time.time() - self.cf_last_updated_at > 0.5:
  232.                 self.cf_last_updated_at = None
  233.                 row = self.cover_flow.currentSlide()
  234.                 m = self.library_view.model()
  235.                 index = m.index(row, 0)
  236.                 if self.library_view.currentIndex().row() != row and index.isValid():
  237.                     self.cover_flow_sync_flag = False
  238.                     self.library_view.scroll_to_row(index.row())
  239.                     sm = self.library_view.selectionModel()
  240.                     sm.select(index, sm.ClearAndSelect | sm.Rows)
  241.                     self.library_view.setCurrentIndex(index)
  242.                 
  243.         except:
  244.             import traceback
  245.             traceback.print_exc()
  246.  
  247.  
  248.     
  249.     def sync_listview_to_cf(self, row):
  250.         self.cf_last_updated_at = time.time()
  251.  
  252.  
  253.  
  254. def main(args = sys.argv):
  255.     return 0
  256.  
  257. if __name__ == '__main__':
  258.     from PyQt4.QtGui import QApplication, QMainWindow
  259.     app = QApplication([])
  260.     w = QMainWindow()
  261.     cf = CoverFlow()
  262.     cf.resize(int(available_width() / 1.5), available_height() - 60)
  263.     w.resize(cf.size() + QSize(30, 20))
  264.     path = sys.argv[1]
  265.     model = FileSystemImages(sys.argv[1])
  266.     cf.currentChanged[int].connect(model.currentChanged)
  267.     cf.setImages(model)
  268.     w.setCentralWidget(cf)
  269.     w.show()
  270.     cf.setFocus(Qt.OtherFocusReason)
  271.     sys.exit(app.exec_())
  272.  
  273.