From abb608a22605dc5feb2f1700f8253eeeabee2bec Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 27 Apr 2010 07:42:01 -0600 Subject: [PATCH] Add A go to context menu to the ebook viewer. Fixes #1230 (Feature Suggestions:) --- src/calibre/gui2/status.py | 2 + src/calibre/gui2/viewer/documentview.py | 73 ++++++++++++++++++------- 2 files changed, 54 insertions(+), 21 deletions(-) diff --git a/src/calibre/gui2/status.py b/src/calibre/gui2/status.py index 57febb5d8e..28a1bbea6b 100644 --- a/src/calibre/gui2/status.py +++ b/src/calibre/gui2/status.py @@ -127,6 +127,8 @@ class BookInfoDisplay(QWidget): keys.sort(cmp=lambda x, y: cmp(self.WEIGHTS[x], self.WEIGHTS[y])) for key in keys: txt = data[key] + if not txt or not txt.strip() or txt == 'None': + continue if isinstance(key, str): key = key.decode(preferred_encoding, 'replace') if isinstance(txt, str): diff --git a/src/calibre/gui2/viewer/documentview.py b/src/calibre/gui2/viewer/documentview.py index 5ad22506b6..7ca9b1bd95 100644 --- a/src/calibre/gui2/viewer/documentview.py +++ b/src/calibre/gui2/viewer/documentview.py @@ -7,10 +7,12 @@ __docformat__ = 'restructuredtext en' ''' import os, math, re, glob, sys from base64 import b64encode +from functools import partial + from PyQt4.Qt import QSize, QSizePolicy, QUrl, SIGNAL, Qt, QTimer, \ QPainter, QPalette, QBrush, QFontDatabase, QDialog, \ QColor, QPoint, QImage, QRegion, QVariant, QIcon, \ - QFont, pyqtSignature, QAction, QByteArray + QFont, pyqtSignature, QAction, QByteArray, QMenu from PyQt4.QtWebKit import QWebPage, QWebView, QWebSettings from calibre.utils.config import Config, StringConfig @@ -449,6 +451,50 @@ class DocumentView(QWebView): _('&Lookup in dictionary'), self) self.dictionary_action.setShortcut(Qt.CTRL+Qt.Key_L) self.dictionary_action.triggered.connect(self.lookup) + self.goto_location_action = QAction(_('Go to...'), self) + self.goto_location_menu = m = QMenu(self) + self.goto_location_actions = a = { + 'Next Page': self.next_page, + 'Previous Page': self.previous_page, + 'Section Top' : partial(self.scroll_to, 0), + 'Document Top': self.goto_document_start, + 'Section Bottom':partial(self.scroll_to, 1), + 'Document Bottom': self.goto_document_end, + 'Next Section': self.goto_next_section, + 'Previous Section': self.goto_previous_section, + } + for name, key in [(_('Next Section'), 'Next Section'), + (_('Previous Section'), 'Previous Section'), + (None, None), + (_('Document Start'), 'Document Top'), + (_('Document End'), 'Document Bottom'), + (None, None), + (_('Section Start'), 'Section Top'), + (_('Section End'), 'Section Bottom'), + (None, None), + (_('Next Page'), 'Next Page'), + (_('Previous Page'), 'Previous Page')]: + if key is None: + m.addSeparator() + else: + m.addAction(name, a[key], self.shortcuts.get_sequences(key)[0]) + self.goto_location_action.setMenu(self.goto_location_menu) + + def goto_next_section(self, *args): + if self.manager is not None: + self.manager.goto_next_section() + + def goto_previous_section(self, *args): + if self.manager is not None: + self.manager.goto_previous_section() + + def goto_document_start(self, *args): + if self.manager is not None: + self.manager.goto_start() + + def goto_document_end(self, *args): + if self.manager is not None: + self.manager.goto_end() @property def copy_action(self): @@ -488,6 +534,8 @@ class DocumentView(QWebView): text = unicode(self.selectedText()) if text: menu.insertAction(list(menu.actions())[0], self.dictionary_action) + menu.addSeparator() + menu.addAction(self.goto_location_action) menu.exec_(ev.globalPos()) def lookup(self, *args): @@ -763,20 +811,9 @@ class DocumentView(QWebView): def keyPressEvent(self, event): key = self.shortcuts.get_match(event) - if key == 'Next Page': - self.next_page() - elif key == 'Previous Page': - self.previous_page() - elif key == 'Section Top': - self.scroll_to(0) - elif key == 'Document Top': - if self.manager is not None: - self.manager.goto_start() - elif key == 'Section Bottom': - self.scroll_to(1) - elif key == 'Document Bottom': - if self.manager is not None: - self.manager.goto_end() + func = self.goto_location_actions.get(key, None) + if func is not None: + func() elif key == 'Down': self.scroll_by(y=15) elif key == 'Up': @@ -785,12 +822,6 @@ class DocumentView(QWebView): self.scroll_by(x=-15) elif key == 'Right': self.scroll_by(x=15) - elif key == 'Next Section': - if self.manager is not None: - self.manager.goto_next_section() - elif key == 'Previous Section': - if self.manager is not None: - self.manager.goto_previous_section() else: return QWebView.keyPressEvent(self, event)