mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
eBook-viewer: Basic bookmark manager
This commit is contained in:
parent
d473fcf95e
commit
e496f88ed0
@ -195,5 +195,8 @@ class EbookIterator(object):
|
|||||||
self.bookmarks.append(bm)
|
self.bookmarks.append(bm)
|
||||||
self.save_bookmarks()
|
self.save_bookmarks()
|
||||||
|
|
||||||
|
def set_bookmarks(self, bookmarks):
|
||||||
|
self.bookmarks = bookmarks
|
||||||
|
|
||||||
def __exit__(self, *args):
|
def __exit__(self, *args):
|
||||||
self._tdir.__exit__(*args)
|
self._tdir.__exit__(*args)
|
93
src/calibre/gui2/viewer/bookmarkmanager.py
Normal file
93
src/calibre/gui2/viewer/bookmarkmanager.py
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
__license__ = 'GPL v3'
|
||||||
|
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
|
||||||
|
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
57
src/calibre/gui2/viewer/bookmarkmanager.ui
Normal file
57
src/calibre/gui2/viewer/bookmarkmanager.ui
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
<ui version="4.0" >
|
||||||
|
<class>BookmarkManager</class>
|
||||||
|
<widget class="QDialog" name="BookmarkManager" >
|
||||||
|
<property name="geometry" >
|
||||||
|
<rect>
|
||||||
|
<x>0</x>
|
||||||
|
<y>0</y>
|
||||||
|
<width>400</width>
|
||||||
|
<height>300</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<property name="windowTitle" >
|
||||||
|
<string>Dialog</string>
|
||||||
|
</property>
|
||||||
|
<layout class="QGridLayout" name="gridLayout" >
|
||||||
|
<item row="0" column="0" colspan="3" >
|
||||||
|
<widget class="QTableView" name="bookmarks_table" >
|
||||||
|
<property name="showDropIndicator" stdset="0" >
|
||||||
|
<bool>false</bool>
|
||||||
|
</property>
|
||||||
|
<property name="alternatingRowColors" >
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
<property name="selectionMode" >
|
||||||
|
<enum>QAbstractItemView::SingleSelection</enum>
|
||||||
|
</property>
|
||||||
|
<property name="sortingEnabled" >
|
||||||
|
<bool>false</bool>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="1" column="0" >
|
||||||
|
<widget class="QPushButton" name="button_revert" >
|
||||||
|
<property name="text" >
|
||||||
|
<string>Revert</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="1" column="1" >
|
||||||
|
<widget class="QPushButton" name="button_delete" >
|
||||||
|
<property name="text" >
|
||||||
|
<string>Delete</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="1" column="2" >
|
||||||
|
<widget class="QPushButton" name="button_edit" >
|
||||||
|
<property name="text" >
|
||||||
|
<string>Edit</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</widget>
|
||||||
|
<resources/>
|
||||||
|
<connections/>
|
||||||
|
</ui>
|
@ -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.main_ui import Ui_EbookViewer
|
||||||
from calibre.gui2.viewer.printing import Printing
|
from calibre.gui2.viewer.printing import Printing
|
||||||
|
from calibre.gui2.viewer.bookmarkmanager import BookmarkManager
|
||||||
from calibre.gui2.main_window import MainWindow
|
from calibre.gui2.main_window import MainWindow
|
||||||
from calibre.gui2 import Application, ORG_NAME, APP_UID, choose_files, \
|
from calibre.gui2 import Application, ORG_NAME, APP_UID, choose_files, \
|
||||||
info_dialog, error_dialog
|
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.toc, SIGNAL('clicked(QModelIndex)'), self.toc_clicked)
|
||||||
self.connect(self.reference, SIGNAL('goto(PyQt_PyObject)'), self.goto)
|
self.connect(self.reference, SIGNAL('goto(PyQt_PyObject)'), self.goto)
|
||||||
|
|
||||||
|
|
||||||
|
self.bookmarks_menu = QMenu()
|
||||||
|
self.action_bookmark.setMenu(self.bookmarks_menu)
|
||||||
self.set_bookmarks([])
|
self.set_bookmarks([])
|
||||||
|
|
||||||
if pathtoebook is not None:
|
if pathtoebook is not None:
|
||||||
f = functools.partial(self.load_ebook, pathtoebook)
|
f = functools.partial(self.load_ebook, pathtoebook)
|
||||||
QTimer.singleShot(50, f)
|
QTimer.singleShot(50, f)
|
||||||
@ -488,17 +493,28 @@ class EbookViewer(MainWindow, Ui_EbookViewer):
|
|||||||
self.setCursor(Qt.BusyCursor)
|
self.setCursor(Qt.BusyCursor)
|
||||||
|
|
||||||
def set_bookmarks(self, bookmarks):
|
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
|
current_page = None
|
||||||
for bm in bookmarks:
|
for bm in bookmarks:
|
||||||
if bm[0] == 'calibre_current_page_bookmark':
|
if bm[0] == 'calibre_current_page_bookmark':
|
||||||
current_page = bm
|
current_page = bm
|
||||||
else:
|
else:
|
||||||
menu.addAction(bm[0], partial(self.goto_bookmark, bm))
|
self.bookmarks_menu.addAction(bm[0], partial(self.goto_bookmark, bm))
|
||||||
self.action_bookmark.setMenu(menu)
|
|
||||||
self._menu = menu
|
|
||||||
return current_page
|
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):
|
def save_current_position(self):
|
||||||
try:
|
try:
|
||||||
pos = self.view.bookmark()
|
pos = self.view.bookmark()
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
__license__ = 'GPL v3'
|
||||||
|
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
|
||||||
|
|
||||||
|
|
||||||
import os, sys, traceback, urlparse
|
import os, sys, traceback, urlparse
|
||||||
|
|
||||||
from BeautifulSoup import BeautifulSoup, Tag
|
from BeautifulSoup import BeautifulSoup, Tag
|
||||||
|
Loading…
x
Reference in New Issue
Block a user