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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from __future__ import with_statement
  5. __license__ = 'GPL v3'
  6. __copyright__ = '2009, John Schember <john@nachtimwald.com>'
  7. import cPickle
  8. import os
  9. from PyQt4.Qt import Qt, QDialog, QAbstractTableModel, QVariant, SIGNAL, QModelIndex, QInputDialog, QLineEdit, QFileDialog
  10. from calibre.gui2.viewer.bookmarkmanager_ui import Ui_BookmarkManager
  11. from calibre.gui2 import NONE
  12.  
  13. class BookmarkManager(QDialog, Ui_BookmarkManager):
  14.     
  15.     def __init__(self, parent, bookmarks):
  16.         QDialog.__init__(self, parent)
  17.         self.setupUi(self)
  18.         self.bookmarks = bookmarks[:]
  19.         self.set_bookmarks()
  20.         self.connect(self.button_revert, SIGNAL('clicked()'), self.set_bookmarks)
  21.         self.connect(self.button_delete, SIGNAL('clicked()'), self.delete_bookmark)
  22.         self.connect(self.button_edit, SIGNAL('clicked()'), self.edit_bookmark)
  23.         self.connect(self.button_export, SIGNAL('clicked()'), self.export_bookmarks)
  24.         self.connect(self.button_import, SIGNAL('clicked()'), self.import_bookmarks)
  25.  
  26.     
  27.     def set_bookmarks(self, bookmarks = None):
  28.         if bookmarks == None:
  29.             bookmarks = self.bookmarks[:]
  30.         
  31.         self._model = BookmarkTableModel(self, bookmarks)
  32.         self.bookmarks_table.setModel(self._model)
  33.  
  34.     
  35.     def delete_bookmark(self):
  36.         indexes = self.bookmarks_table.selectionModel().selectedIndexes()
  37.         if indexes != []:
  38.             self._model.remove_row(indexes[0].row())
  39.         
  40.  
  41.     
  42.     def edit_bookmark(self):
  43.         indexes = self.bookmarks_table.selectionModel().selectedIndexes()
  44.         if indexes != []:
  45.             (title, ok) = QInputDialog.getText(self, _('Edit bookmark'), _('New title for bookmark:'), QLineEdit.Normal, self._model.data(indexes[0], Qt.DisplayRole).toString())
  46.             title = QVariant(unicode(title).strip())
  47.             if ok and title:
  48.                 self._model.setData(indexes[0], title, Qt.EditRole)
  49.             
  50.         
  51.  
  52.     
  53.     def get_bookmarks(self):
  54.         return self._model.bookmarks
  55.  
  56.     
  57.     def export_bookmarks(self):
  58.         filename = QFileDialog.getSaveFileName(self, _('Export Bookmarks'), '%s%suntitled.pickle' % (os.getcwdu(), os.sep), _('Saved Bookmarks (*.pickle)'))
  59.         if filename == '':
  60.             return None
  61.         
  62.         try:
  63.             fileobj = _[1]
  64.             cPickle.dump(self._model.bookmarks, fileobj)
  65.         finally:
  66.             pass
  67.  
  68.  
  69.     
  70.     def import_bookmarks(self):
  71.         filename = QFileDialog.getOpenFileName(self, _('Import Bookmarks'), '%s' % os.getcwdu(), _('Pickled Bookmarks (*.pickle)'))
  72.         if filename == '':
  73.             return None
  74.         imported = None
  75.         
  76.         try:
  77.             fileobj = _[1]
  78.             imported = cPickle.load(fileobj)
  79.         finally:
  80.             pass
  81.  
  82.  
  83.  
  84.  
  85. class BookmarkTableModel(QAbstractTableModel):
  86.     headers = [
  87.         _('Name')]
  88.     
  89.     def __init__(self, parent, bookmarks):
  90.         QAbstractTableModel.__init__(self, parent)
  91.         self.bookmarks = bookmarks[:]
  92.  
  93.     
  94.     def rowCount(self, parent):
  95.         if parent and parent.isValid():
  96.             return 0
  97.         return len(self.bookmarks)
  98.  
  99.     
  100.     def columnCount(self, parent):
  101.         if parent and parent.isValid():
  102.             return 0
  103.         return len(self.headers)
  104.  
  105.     
  106.     def data(self, index, role):
  107.         if role in (Qt.DisplayRole, Qt.EditRole):
  108.             ans = self.bookmarks[index.row()][0]
  109.             if ans is None:
  110.                 return NONE
  111.             return QVariant(ans)
  112.         return NONE
  113.  
  114.     
  115.     def setData(self, index, value, role):
  116.         if role == Qt.EditRole:
  117.             self.bookmarks[index.row()] = (unicode(value.toString()).strip(), self.bookmarks[index.row()][1])
  118.             self.emit(SIGNAL('dataChanged(QModelIndex, QModelIndex)'), index, index)
  119.             return True
  120.         return False
  121.  
  122.     
  123.     def flags(self, index):
  124.         flags = QAbstractTableModel.flags(self, index)
  125.         flags |= Qt.ItemIsEditable
  126.         return flags
  127.  
  128.     
  129.     def headerData(self, section, orientation, role):
  130.         if role != Qt.DisplayRole:
  131.             return NONE
  132.         if orientation == Qt.Horizontal:
  133.             return QVariant(self.headers[section])
  134.         return QVariant(section + 1)
  135.  
  136.     
  137.     def remove_row(self, row):
  138.         self.beginRemoveRows(QModelIndex(), row, row)
  139.         del self.bookmarks[row]
  140.         self.endRemoveRows()
  141.  
  142.  
  143.