mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Add a right click context menu action to copy selected text
This commit is contained in:
parent
e61ee4b510
commit
183048d01a
@ -18,7 +18,7 @@ from PyQt4.Qt import (
|
|||||||
QTextCursor, QTextCharFormat, Qt, QRect, QPainter, QPalette, QPen,
|
QTextCursor, QTextCharFormat, Qt, QRect, QPainter, QPalette, QPen,
|
||||||
QBrush, QColor, QTextLayout, QCursor, QFont, QSplitterHandle, QStyle,
|
QBrush, QColor, QTextLayout, QCursor, QFont, QSplitterHandle, QStyle,
|
||||||
QPainterPath, QHBoxLayout, QWidget, QScrollBar, QEventLoop, pyqtSignal,
|
QPainterPath, QHBoxLayout, QWidget, QScrollBar, QEventLoop, pyqtSignal,
|
||||||
QImage, QPixmap)
|
QImage, QPixmap, QMenu, QIcon)
|
||||||
|
|
||||||
from calibre import human_readable, fit_image
|
from calibre import human_readable, fit_image
|
||||||
from calibre.ebooks.oeb.polish.utils import guess_type
|
from calibre.ebooks.oeb.polish.utils import guess_type
|
||||||
@ -51,6 +51,8 @@ class TextBrowser(PlainTextEdit): # {{{
|
|||||||
|
|
||||||
def __init__(self, right=False, parent=None):
|
def __init__(self, right=False, parent=None):
|
||||||
PlainTextEdit.__init__(self, parent)
|
PlainTextEdit.__init__(self, parent)
|
||||||
|
self.setContextMenuPolicy(Qt.CustomContextMenu)
|
||||||
|
self.customContextMenuRequested.connect(self.show_context_menu)
|
||||||
self.setFocusPolicy(Qt.NoFocus)
|
self.setFocusPolicy(Qt.NoFocus)
|
||||||
self.right = right
|
self.right = right
|
||||||
self.setReadOnly(True)
|
self.setReadOnly(True)
|
||||||
@ -106,6 +108,15 @@ class TextBrowser(PlainTextEdit): # {{{
|
|||||||
f.setBackground(self.diff_backgrounds[x])
|
f.setBackground(self.diff_backgrounds[x])
|
||||||
setattr(self, '%s_format' % x, f)
|
setattr(self, '%s_format' % x, f)
|
||||||
|
|
||||||
|
def show_context_menu(self, pos):
|
||||||
|
m = QMenu(self)
|
||||||
|
a = m.addAction
|
||||||
|
i = unicode(self.textCursor().selectedText())
|
||||||
|
if i:
|
||||||
|
a(QIcon(I('edit-copy.png')), _('Copy to clipboard'), self.copy)
|
||||||
|
if len(m.actions()) > 0:
|
||||||
|
m.exec_(self.mapToGlobal(pos))
|
||||||
|
|
||||||
def clear(self):
|
def clear(self):
|
||||||
PlainTextEdit.clear(self)
|
PlainTextEdit.clear(self)
|
||||||
self.line_number_map.clear()
|
self.line_number_map.clear()
|
||||||
@ -425,6 +436,7 @@ class DiffSplit(QSplitter): # {{{
|
|||||||
left_text, right_text = text % human_readable(len(left_text)), text % human_readable(len(right_text))
|
left_text, right_text = text % human_readable(len(left_text)), text % human_readable(len(right_text))
|
||||||
self.add_text_diff(left_text, right_text, None, None)
|
self.add_text_diff(left_text, right_text, None, None)
|
||||||
|
|
||||||
|
# image diffs {{{
|
||||||
@property
|
@property
|
||||||
def failed_img(self):
|
def failed_img(self):
|
||||||
if self._failed_img is None:
|
if self._failed_img is None:
|
||||||
@ -455,7 +467,7 @@ class DiffSplit(QSplitter): # {{{
|
|||||||
left_img, right_img = load(left_data), load(right_data)
|
left_img, right_img = load(left_data), load(right_data)
|
||||||
change = []
|
change = []
|
||||||
# Let any initial resizing of the window finish in case this is the
|
# Let any initial resizing of the window finish in case this is the
|
||||||
# first diff, to avoid expensize resize calculation later
|
# first diff, to avoid the expensive resize calculation later
|
||||||
QApplication.processEvents(QEventLoop.ExcludeUserInputEvents | QEventLoop.ExcludeSocketNotifiers)
|
QApplication.processEvents(QEventLoop.ExcludeUserInputEvents | QEventLoop.ExcludeSocketNotifiers)
|
||||||
for v, img, size in ((self.left, left_img, len(left_data)), (self.right, right_img, len(right_data))):
|
for v, img, size in ((self.left, left_img, len(left_data)), (self.right, right_img, len(right_data))):
|
||||||
c = v.textCursor()
|
c = v.textCursor()
|
||||||
@ -516,7 +528,9 @@ class DiffSplit(QSplitter): # {{{
|
|||||||
scaled, w, h = fit_image(w, h, view.width() - 5, int(0.9 * view.height()))
|
scaled, w, h = fit_image(w, h, view.width() - 5, int(0.9 * view.height()))
|
||||||
line_height = view.blockBoundingRect(view.document().begin()).height()
|
line_height = view.blockBoundingRect(view.document().begin()).height()
|
||||||
return int(ceil(h / line_height)) + 1, w
|
return int(ceil(h / line_height)) + 1, w
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
# text diffs {{{
|
||||||
def add_text_diff(self, left_text, right_text, context, syntax):
|
def add_text_diff(self, left_text, right_text, context, syntax):
|
||||||
left_text = unicodedata.normalize('NFC', left_text)
|
left_text = unicodedata.normalize('NFC', left_text)
|
||||||
right_text = unicodedata.normalize('NFC', right_text)
|
right_text = unicodedata.normalize('NFC', right_text)
|
||||||
@ -712,6 +726,7 @@ class DiffSplit(QSplitter): # {{{
|
|||||||
for block, fmts in ((lsb, lfmts), (rsb, rfmts)):
|
for block, fmts in ((lsb, lfmts), (rsb, rfmts)):
|
||||||
if fmts:
|
if fmts:
|
||||||
block.layout().setAdditionalFormats(fmts)
|
block.layout().setAdditionalFormats(fmts)
|
||||||
|
# }}}
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
class DiffView(QWidget): # {{{
|
class DiffView(QWidget): # {{{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user