From 04b0930625b907f051dcfe8456558aec3d2eaaf1 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 6 May 2014 21:03:41 +0530 Subject: [PATCH] Edit book: Allow copying the list of errors from the Check Book tool to the clipboard by right clicking on the list of errors --- src/calibre/gui2/tweak_book/check.py | 33 +++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/calibre/gui2/tweak_book/check.py b/src/calibre/gui2/tweak_book/check.py index fba761dbee..22a8afaeaa 100644 --- a/src/calibre/gui2/tweak_book/check.py +++ b/src/calibre/gui2/tweak_book/check.py @@ -9,7 +9,7 @@ __copyright__ = '2013, Kovid Goyal ' import sys from PyQt4.Qt import ( - QIcon, Qt, QSplitter, QListWidget, QTextBrowser, QPalette, + QIcon, Qt, QSplitter, QListWidget, QTextBrowser, QPalette, QMenu, QListWidgetItem, pyqtSignal, QApplication, QStyledItemDelegate) from calibre.ebooks.oeb.polish.check.base import WARN, INFO, DEBUG, ERROR, CRITICAL @@ -28,6 +28,19 @@ def icon_for_level(level): icon = None return QIcon(I(icon)) if icon else QIcon() +def prefix_for_level(level): + if level > WARN: + text = _('ERROR') + elif level == WARN: + text = _('WARNING') + elif level == INFO: + text = _('INFO') + else: + text = '' + if text: + text += ': ' + return text + class Delegate(QStyledItemDelegate): def initStyleOption(self, option, index): @@ -47,6 +60,8 @@ class Check(QSplitter): self.setChildrenCollapsible(False) self.items = i = QListWidget(self) + i.setContextMenuPolicy(Qt.CustomContextMenu) + i.customContextMenuRequested.connect(self.context_menu) self.items.setSpacing(3) self.items.itemDoubleClicked.connect(self.current_item_activated) self.items.currentItemChanged.connect(self.current_item_changed) @@ -66,6 +81,22 @@ class Check(QSplitter): if state is not None: self.restoreState(state) + def context_menu(self, pos): + m = QMenu() + if self.items.count() > 0: + m.addAction(QIcon(I('edit-copy.png')), _('Copy list of errors to clipboard'), self.copy_to_clipboard) + if list(m.actions()): + m.exec_(self.mapToGlobal(pos)) + + def copy_to_clipboard(self): + items = [] + for item in (self.items.item(i) for i in xrange(self.items.count())): + msg = unicode(item.text()) + msg = prefix_for_level(item.data(Qt.UserRole).toPyObject().level) + msg + items.append(msg) + if items: + QApplication.clipboard().setText('\n'.join(items)) + def save_state(self): tprefs.set('check-book-splitter-state', bytearray(self.saveState()))