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)

This commit is contained in:
Kovid Goyal 2016-02-24 10:00:59 +05:30
parent 8626437fe1
commit 014631a8e3

View File

@ -13,7 +13,7 @@ from functools import partial
from PyQt5.Qt import ( from PyQt5.Qt import (
QStandardItem, QStandardItemModel, Qt, QFont, QTreeView, QWidget, QStandardItem, QStandardItemModel, Qt, QFont, QTreeView, QWidget,
QHBoxLayout, QToolButton, QIcon, QModelIndex, pyqtSignal, QMenu, QHBoxLayout, QToolButton, QIcon, QModelIndex, pyqtSignal, QMenu,
QStyledItemDelegate, QToolTip) QStyledItemDelegate, QToolTip, QApplication)
from calibre.ebooks.metadata.toc import TOC as MTOC from calibre.ebooks.metadata.toc import TOC as MTOC
from calibre.gui2 import error_dialog from calibre.gui2 import error_dialog
@ -93,6 +93,8 @@ class TOCView(QTreeView):
m.addSeparator() m.addSeparator()
m.addAction(_('Expand all items'), self.expandAll) m.addAction(_('Expand all items'), self.expandAll)
m.addAction(_('Collapse all items'), self.collapseAll) 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)) m.exec_(self.mapToGlobal(pos))
def keyPressEvent(self, event): def keyPressEvent(self, event):
@ -103,6 +105,10 @@ class TOCView(QTreeView):
pass pass
return QTreeView.keyPressEvent(self, event) return QTreeView.keyPressEvent(self, event)
def copy_to_clipboard(self):
m = self.model()
QApplication.clipboard().setText(getattr(m, 'as_plain_text', ''))
class TOCSearch(QWidget): class TOCSearch(QWidget):
def __init__(self, toc_view, parent=None): def __init__(self, toc_view, parent=None):
@ -383,3 +389,10 @@ class TOC(QStandardItemModel):
index = self.indexFromItem(item) index = self.indexFromItem(item)
return index return index
return QModelIndex() 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)