From 014631a8e3f3f3178041a39475798d13a6c0d4e7 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 24 Feb 2016 10:00:59 +0530 Subject: [PATCH] E-book viewer: Allow copying the Table of Contents to the clipboard by right clicking on it. Fixes #1548791 [[Enhancement] Copy book index to clipboard](https://bugs.launchpad.net/calibre/+bug/1548791) --- src/calibre/gui2/viewer/toc.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/calibre/gui2/viewer/toc.py b/src/calibre/gui2/viewer/toc.py index e1036586fe..e994aefd45 100644 --- a/src/calibre/gui2/viewer/toc.py +++ b/src/calibre/gui2/viewer/toc.py @@ -13,7 +13,7 @@ from functools import partial from PyQt5.Qt import ( QStandardItem, QStandardItemModel, Qt, QFont, QTreeView, QWidget, QHBoxLayout, QToolButton, QIcon, QModelIndex, pyqtSignal, QMenu, - QStyledItemDelegate, QToolTip) + QStyledItemDelegate, QToolTip, QApplication) from calibre.ebooks.metadata.toc import TOC as MTOC from calibre.gui2 import error_dialog @@ -93,6 +93,8 @@ class TOCView(QTreeView): m.addSeparator() m.addAction(_('Expand all items'), self.expandAll) m.addAction(_('Collapse all items'), self.collapseAll) + m.addSeparator() + m.addAction(_('Copy table of contents to clipboard'), self.copy_to_clipboard) m.exec_(self.mapToGlobal(pos)) def keyPressEvent(self, event): @@ -103,6 +105,10 @@ class TOCView(QTreeView): pass return QTreeView.keyPressEvent(self, event) + def copy_to_clipboard(self): + m = self.model() + QApplication.clipboard().setText(getattr(m, 'as_plain_text', '')) + class TOCSearch(QWidget): def __init__(self, toc_view, parent=None): @@ -383,3 +389,10 @@ class TOC(QStandardItemModel): index = self.indexFromItem(item) return index return QModelIndex() + + @property + def as_plain_text(self): + lines = [] + for item in self.all_items: + lines.append(' ' * (4 * item.depth) + (item.title or '')) + return '\n'.join(lines)