diff --git a/src/calibre/ebooks/epub/iterator.py b/src/calibre/ebooks/epub/iterator.py index e953cbda51..82b9aa09ef 100644 --- a/src/calibre/ebooks/epub/iterator.py +++ b/src/calibre/ebooks/epub/iterator.py @@ -195,5 +195,8 @@ class EbookIterator(object): self.bookmarks.append(bm) self.save_bookmarks() + def set_bookmarks(self, bookmarks): + self.bookmarks = bookmarks + def __exit__(self, *args): - self._tdir.__exit__(*args) \ No newline at end of file + self._tdir.__exit__(*args) diff --git a/src/calibre/gui2/viewer/bookmarkmanager.py b/src/calibre/gui2/viewer/bookmarkmanager.py new file mode 100644 index 0000000000..6dcd662754 --- /dev/null +++ b/src/calibre/gui2/viewer/bookmarkmanager.py @@ -0,0 +1,93 @@ +__license__ = 'GPL v3' +__copyright__ = '2009, John Schember ' + + +from PyQt4.Qt import Qt, QDialog, QAbstractTableModel, QVariant, SIGNAL, \ + QModelIndex, QInputDialog, QLineEdit + +from calibre.gui2.viewer.bookmarkmanager_ui import Ui_BookmarkManager +from calibre.gui2 import NONE, qstring_to_unicode + +class BookmarkManager(QDialog, Ui_BookmarkManager): + def __init__(self, parent, bookmarks): + QDialog.__init__(self, parent) + + self.setupUi(self) + + self.bookmarks = bookmarks[:] + self.set_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) + + def set_bookmarks(self): + self._model = BookmarkTableModel(self, self.bookmarks) + self.bookmarks_table.setModel(self._model) + + def delete_bookmark(self): + indexes = self.bookmarks_table.selectionModel().selectedIndexes() + if indexes != []: + self._model.remove_row(indexes[0].row()) + + 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 + + +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()] = (qstring_to_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]) + else: + return QVariant(section+1) + + def remove_row(self, row): + self.beginRemoveRows(QModelIndex(), row, row) + del self.bookmarks[row] + self.endRemoveRows() + diff --git a/src/calibre/gui2/viewer/bookmarkmanager.ui b/src/calibre/gui2/viewer/bookmarkmanager.ui new file mode 100644 index 0000000000..44e044b52a --- /dev/null +++ b/src/calibre/gui2/viewer/bookmarkmanager.ui @@ -0,0 +1,57 @@ + + BookmarkManager + + + + 0 + 0 + 400 + 300 + + + + Dialog + + + + + + false + + + true + + + QAbstractItemView::SingleSelection + + + false + + + + + + + Revert + + + + + + + Delete + + + + + + + Edit + + + + + + + + diff --git a/src/calibre/gui2/viewer/main.py b/src/calibre/gui2/viewer/main.py index ec0d878ae8..990281a471 100644 --- a/src/calibre/gui2/viewer/main.py +++ b/src/calibre/gui2/viewer/main.py @@ -13,6 +13,7 @@ from PyQt4.Qt import QMovie, QApplication, Qt, QIcon, QTimer, QWidget, SIGNAL, \ from calibre.gui2.viewer.main_ui import Ui_EbookViewer from calibre.gui2.viewer.printing import Printing +from calibre.gui2.viewer.bookmarkmanager import BookmarkManager from calibre.gui2.main_window import MainWindow from calibre.gui2 import Application, ORG_NAME, APP_UID, choose_files, \ info_dialog, error_dialog @@ -263,7 +264,11 @@ class EbookViewer(MainWindow, Ui_EbookViewer): self.connect(self.toc, SIGNAL('clicked(QModelIndex)'), self.toc_clicked) self.connect(self.reference, SIGNAL('goto(PyQt_PyObject)'), self.goto) + + self.bookmarks_menu = QMenu() + self.action_bookmark.setMenu(self.bookmarks_menu) self.set_bookmarks([]) + if pathtoebook is not None: f = functools.partial(self.load_ebook, pathtoebook) QTimer.singleShot(50, f) @@ -488,17 +493,28 @@ class EbookViewer(MainWindow, Ui_EbookViewer): self.setCursor(Qt.BusyCursor) def set_bookmarks(self, bookmarks): - menu = QMenu() + self.bookmarks_menu.clear() + self.bookmarks_menu.addAction(_("Manage Bookmarks"), self.manage_bookmarks) + self.bookmarks_menu.addSeparator() current_page = None for bm in bookmarks: if bm[0] == 'calibre_current_page_bookmark': current_page = bm else: - menu.addAction(bm[0], partial(self.goto_bookmark, bm)) - self.action_bookmark.setMenu(menu) - self._menu = menu + self.bookmarks_menu.addAction(bm[0], partial(self.goto_bookmark, bm)) return current_page + def manage_bookmarks(self): + bmm = BookmarkManager(self, self.iterator.bookmarks) + bmm.exec_() + + bookmarks = bmm.get_bookmarks() + + if bookmarks != self.iterator.bookmarks: + self.iterator.set_bookmarks(bookmarks) + self.iterator.save_bookmarks() + self.set_bookmarks(bookmarks) + def save_current_position(self): try: pos = self.view.bookmark() diff --git a/src/calibre/gui2/viewer/printing.py b/src/calibre/gui2/viewer/printing.py index 8644cd311b..e948360338 100644 --- a/src/calibre/gui2/viewer/printing.py +++ b/src/calibre/gui2/viewer/printing.py @@ -1,5 +1,9 @@ #!/usr/bin/env python +__license__ = 'GPL v3' +__copyright__ = '2009, John Schember ' + + import os, sys, traceback, urlparse from BeautifulSoup import BeautifulSoup, Tag