From a1c6108b7228f1d82b0df946ec616a9e9d19f60d Mon Sep 17 00:00:00 2001 From: John Schember Date: Wed, 25 Feb 2009 15:01:42 -0500 Subject: [PATCH] ebook-viewer: Bookmark Manager: Import/Export, new UI layout. --- src/calibre/gui2/viewer/bookmarkmanager.py | 47 ++++++++- src/calibre/gui2/viewer/bookmarkmanager.ui | 109 ++++++++++++++++----- src/calibre/gui2/viewer/main.py | 3 +- 3 files changed, 132 insertions(+), 27 deletions(-) diff --git a/src/calibre/gui2/viewer/bookmarkmanager.py b/src/calibre/gui2/viewer/bookmarkmanager.py index 6dcd662754..60709a2ac1 100644 --- a/src/calibre/gui2/viewer/bookmarkmanager.py +++ b/src/calibre/gui2/viewer/bookmarkmanager.py @@ -1,9 +1,12 @@ +from __future__ import with_statement + __license__ = 'GPL v3' __copyright__ = '2009, John Schember ' +import cPickle, os from PyQt4.Qt import Qt, QDialog, QAbstractTableModel, QVariant, SIGNAL, \ - QModelIndex, QInputDialog, QLineEdit + QModelIndex, QInputDialog, QLineEdit, QFileDialog from calibre.gui2.viewer.bookmarkmanager_ui import Ui_BookmarkManager from calibre.gui2 import NONE, qstring_to_unicode @@ -20,9 +23,13 @@ class BookmarkManager(QDialog, Ui_BookmarkManager): 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): - self._model = BookmarkTableModel(self, self.bookmarks) + def set_bookmarks(self, bookmarks=None): + if bookmarks == None: + bookmarks = self.bookmarks[:] + self._model = BookmarkTableModel(self, bookmarks) self.bookmarks_table.setModel(self._model) def delete_bookmark(self): @@ -41,6 +48,40 @@ class BookmarkManager(QDialog, Ui_BookmarkManager): 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), _("Pickled Bookmarks (*.pickle)")) + if filename == '': + return + + 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 == '': + return + + imported = None + with open(filename, 'r') as fileobj: + imported = cPickle.load(fileobj) + + if imported != None: + bad = False + try: + for bm in imported: + if len(bm) != 2: + bad = True + break + except: + pass + + if not bad: + bookmarks = self._model.bookmarks[:] + for bm in imported: + if bm not in bookmarks and bm[0] != 'calibre_current_page_bookmark': + bookmarks.append(bm) + self.set_bookmarks(bookmarks) + class BookmarkTableModel(QAbstractTableModel): headers = [_("Name")] diff --git a/src/calibre/gui2/viewer/bookmarkmanager.ui b/src/calibre/gui2/viewer/bookmarkmanager.ui index 44e044b52a..110b5db841 100644 --- a/src/calibre/gui2/viewer/bookmarkmanager.ui +++ b/src/calibre/gui2/viewer/bookmarkmanager.ui @@ -5,15 +5,59 @@ 0 0 - 400 - 300 + 451 + 363 - Dialog + Bookmark Manager - + + + + Actions + + + + + + Edit + + + + + + + Delete + + + + + + + Reset + + + + + + + Export + + + + + + + Import + + + + + + + false @@ -29,29 +73,48 @@ - - - - Revert - - - - - - - Delete - - - - - - - Edit + + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok - + + + buttonBox + accepted() + BookmarkManager + accept() + + + 225 + 337 + + + 225 + 181 + + + + + buttonBox + rejected() + BookmarkManager + reject() + + + 225 + 337 + + + 225 + 181 + + + + diff --git a/src/calibre/gui2/viewer/main.py b/src/calibre/gui2/viewer/main.py index 990281a471..79c42c2a81 100644 --- a/src/calibre/gui2/viewer/main.py +++ b/src/calibre/gui2/viewer/main.py @@ -506,7 +506,8 @@ class EbookViewer(MainWindow, Ui_EbookViewer): def manage_bookmarks(self): bmm = BookmarkManager(self, self.iterator.bookmarks) - bmm.exec_() + if bmm.exec_() != BookmarkManager.Accepted: + return bookmarks = bmm.get_bookmarks()