home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1255 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  6.2 KB  |  151 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 textwrap
  8. import os
  9. import re
  10. from PyQt4.QtCore import QCoreApplication, SIGNAL, QModelIndex, QTimer, Qt
  11. from PyQt4.QtGui import QDialog, QPixmap, QGraphicsScene, QIcon
  12. from calibre.gui2.dialogs.book_info_ui import Ui_BookInfo
  13. from calibre.gui2 import dynamic, open_local_file
  14. from calibre import fit_image
  15. from calibre.library.comments import comments_to_html
  16.  
  17. class BookInfo(QDialog, Ui_BookInfo):
  18.     
  19.     def __init__(self, parent, view, row):
  20.         QDialog.__init__(self, parent)
  21.         Ui_BookInfo.__init__(self)
  22.         self.setupUi(self)
  23.         self.cover_pixmap = None
  24.         desktop = QCoreApplication.instance().desktop()
  25.         screen_height = desktop.availableGeometry().height() - 100
  26.         self.resize(self.size().width(), screen_height)
  27.         self.view = view
  28.         self.current_row = None
  29.         self.fit_cover.setChecked(dynamic.get('book_info_dialog_fit_cover', True))
  30.         self.refresh(row)
  31.         self.connect(self.view.selectionModel(), SIGNAL('currentChanged(QModelIndex,QModelIndex)'), self.slave)
  32.         self.connect(self.next_button, SIGNAL('clicked()'), self.next)
  33.         self.connect(self.previous_button, SIGNAL('clicked()'), self.previous)
  34.         self.connect(self.text, SIGNAL('linkActivated(QString)'), self.open_book_path)
  35.         self.fit_cover.stateChanged.connect(self.toggle_cover_fit)
  36.         self.cover.resizeEvent = self.cover_view_resized
  37.  
  38.     
  39.     def toggle_cover_fit(self, state):
  40.         dynamic.set('book_info_dialog_fit_cover', self.fit_cover.isChecked())
  41.         self.resize_cover()
  42.  
  43.     
  44.     def cover_view_resized(self, event):
  45.         QTimer.singleShot(1, self.resize_cover)
  46.  
  47.     
  48.     def slave(self, current, previous):
  49.         row = current.row()
  50.         self.refresh(row)
  51.  
  52.     
  53.     def open_book_path(self, path):
  54.         path = unicode(path)
  55.         if os.sep in path:
  56.             open_local_file(path)
  57.         else:
  58.             path = self.view.model().db.format_abspath(self.current_row, path)
  59.             if path is not None:
  60.                 open_local_file(path)
  61.             
  62.  
  63.     
  64.     def next(self):
  65.         row = self.view.currentIndex().row()
  66.         ni = self.view.model().index(row + 1, 0)
  67.         if ni.isValid():
  68.             self.view.setCurrentIndex(ni)
  69.         
  70.  
  71.     
  72.     def previous(self):
  73.         row = self.view.currentIndex().row()
  74.         ni = self.view.model().index(row - 1, 0)
  75.         if ni.isValid():
  76.             self.view.setCurrentIndex(ni)
  77.         
  78.  
  79.     
  80.     def resize_cover(self):
  81.         if self.cover_pixmap is None:
  82.             return None
  83.         self.setWindowIcon(QIcon(self.cover_pixmap))
  84.         self.scene = QGraphicsScene()
  85.         pixmap = self.cover_pixmap
  86.         if self.fit_cover.isChecked():
  87.             (scaled, new_width, new_height) = fit_image(pixmap.width(), pixmap.height(), self.cover.size().width() - 10, self.cover.size().height() - 10)
  88.             if scaled:
  89.                 pixmap = pixmap.scaled(new_width, new_height, Qt.KeepAspectRatio, Qt.SmoothTransformation)
  90.             
  91.         
  92.         self.scene.addPixmap(pixmap)
  93.         self.cover.setScene(self.scene)
  94.  
  95.     
  96.     def refresh(self, row):
  97.         if isinstance(row, QModelIndex):
  98.             row = row.row()
  99.         
  100.         if row == self.current_row:
  101.             return None
  102.         info = self.view.model().get_book_info(row)
  103.         if info is None:
  104.             return None
  105.         info is None(self.previous_button.setEnabled if row == 0 else True)
  106.         row == self.current_row(self.next_button.setEnabled if row == self.view.model().rowCount(QModelIndex()) - 1 else True)
  107.         self.current_row = row
  108.         self.setWindowTitle(info[_('Title')])
  109.         self.title.setText('<b>' + info.pop(_('Title')))
  110.         comments = info.pop(_('Comments'), '')
  111.         if comments:
  112.             comments = comments_to_html(comments)
  113.         
  114.         if re.search('<[a-zA-Z]+>', comments) is None:
  115.             lines = comments.splitlines()
  116.             lines = [ _[1] if x.strip() else '<br><br>' for x in lines ]
  117.             comments = '\n'.join(lines)
  118.         
  119.         self.comments.setText('<div>%s</div>' % comments)
  120.         cdata = info.pop('cover', '')
  121.         self.cover_pixmap = QPixmap.fromImage(cdata)
  122.         self.resize_cover()
  123.         rows = u''
  124.         self.text.setText('')
  125.         self.data = info
  126.         if _('Path') in info.keys():
  127.             p = info[_('Path')]
  128.             info[_('Path')] = '<a href="%s">%s</a>' % (p, p)
  129.         
  130.         if _('Formats') in info.keys():
  131.             formats = info[_('Formats')].split(',')
  132.             info[_('Formats')] = ''
  133.             for f in formats:
  134.                 f = f.strip()
  135.                 info[_('Formats')] += '<a href="%s">%s</a>, ' % (f, f)
  136.             
  137.         
  138.         for key in info.keys():
  139.             if key == 'id':
  140.                 continue
  141.             
  142.             txt = info[key]
  143.             if key != _('Path'):
  144.                 txt = u'<br />\n'.join(textwrap.wrap(txt, 120))
  145.             
  146.             rows += u'<tr><td><b>%s:</b></td><td>%s</td></tr>' % (key, txt)
  147.         
  148.         self.text.setText(u'<table>' + rows + '</table>')
  149.  
  150.  
  151.