Logo Search packages:      
Sourcecode: calibre version File versions  Download package

from __future__ import with_statement

__license__   = 'GPL v3'
__copyright__ = '2009, John Schember <john@nachtimwald.com>'

import cPickle, os

from PyQt4.Qt import Qt, QDialog, QAbstractTableModel, QVariant, SIGNAL, \
    QModelIndex, QInputDialog, QLineEdit, QFileDialog

from calibre.gui2.viewer.bookmarkmanager_ui import Ui_BookmarkManager
from calibre.gui2 import NONE

00014 class BookmarkManager(QDialog, Ui_BookmarkManager):
    def __init__(self, parent, bookmarks):
        QDialog.__init__(self, parent)


        self.bookmarks = bookmarks[:]

        self.connect(self.button_revert, SIGNAL('clicked()'), self.set_bookmarks)
        self.connect(self.button_delete, SIGNAL('clicked()'), self.delete_bookmark)
        self.connect(self.button_edit, SIGNAL('clicked()'), self.edit_bookmark)
        self.connect(self.button_export, SIGNAL('clicked()'), self.export_bookmarks)
        self.connect(self.button_import, SIGNAL('clicked()'), self.import_bookmarks)

    def set_bookmarks(self, bookmarks=None):
        if bookmarks == None:
            bookmarks = self.bookmarks[:]
        self._model = BookmarkTableModel(self, bookmarks)

    def delete_bookmark(self):
        indexes = self.bookmarks_table.selectionModel().selectedIndexes()
        if indexes != []:

    def edit_bookmark(self):
        indexes = self.bookmarks_table.selectionModel().selectedIndexes()
        if indexes != []:
            title, ok = QInputDialog.getText(self, _('Edit bookmark'), _('New title for bookmark:'), QLineEdit.Normal, self._model.data(indexes[0], Qt.DisplayRole).toString())
            title = QVariant(unicode(title).strip())
            if ok and title:
                self._model.setData(indexes[0], title, Qt.EditRole)

    def get_bookmarks(self):
        return self._model.bookmarks

    def export_bookmarks(self):
        filename = QFileDialog.getSaveFileName(self, _("Export Bookmarks"),
                '%s%suntitled.pickle' % (os.getcwdu(), os.sep),
                _("Saved Bookmarks (*.pickle)"))
        if filename == '':

        with open(filename, 'w') as fileobj:
            cPickle.dump(self._model.bookmarks, fileobj)

    def import_bookmarks(self):
        filename = QFileDialog.getOpenFileName(self, _("Import Bookmarks"), '%s' % os.getcwdu(), _("Pickled Bookmarks (*.pickle)"))
        if filename == '':

        imported = None
        with open(filename, 'r') as fileobj:
            imported = cPickle.load(fileobj)

        if imported != None:
            bad = False
                for bm in imported:
                    if len(bm) != 2:
                        bad = True

            if not bad:
                bookmarks = self._model.bookmarks[:]
                for bm in imported:
                    if bm not in bookmarks and bm[0] != 'calibre_current_page_bookmark':

00088 class BookmarkTableModel(QAbstractTableModel):
    headers = [_("Name")]

    def __init__(self, parent, bookmarks):
        QAbstractTableModel.__init__(self, parent)

        self.bookmarks = bookmarks[:]

    def rowCount(self, parent):
        if parent and parent.isValid():
            return 0
        return len(self.bookmarks)

    def columnCount(self, parent):
        if parent and parent.isValid():
            return 0
        return len(self.headers)

    def data(self, index, role):
        if role in (Qt.DisplayRole, Qt.EditRole):
            ans = self.bookmarks[index.row()][0]
            return NONE if ans is None else QVariant(ans)
        return NONE

    def setData(self, index, value, role):
        if role == Qt.EditRole:
            self.bookmarks[index.row()] = (unicode(value.toString()).strip(), self.bookmarks[index.row()][1])
            self.emit(SIGNAL("dataChanged(QModelIndex, QModelIndex)"), index, index)
            return True
        return False

    def flags(self, index):
        flags = QAbstractTableModel.flags(self, index)
        flags |= Qt.ItemIsEditable
        return flags

    def headerData(self, section, orientation, role):
        if role != Qt.DisplayRole:
            return NONE
        if orientation == Qt.Horizontal:
            return QVariant(self.headers[section])
            return QVariant(section+1)

    def remove_row(self, row):
        self.beginRemoveRows(QModelIndex(), row, row)
        del self.bookmarks[row]

Generated by  Doxygen 1.6.0   Back to index