mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Implement double clicking in the diff view opening the editor when viewing a diff against the current container
This commit is contained in:
parent
209880df6c
commit
5eb1745526
@ -396,10 +396,10 @@ class Boss(QObject):
|
|||||||
d.resize(600, 400)
|
d.resize(600, 400)
|
||||||
d.exec_()
|
d.exec_()
|
||||||
|
|
||||||
def create_diff_dialog(self, revert_msg=_('&Revert changes')):
|
def create_diff_dialog(self, revert_msg=_('&Revert changes'), show_open_in_editor=True):
|
||||||
global _diff_dialogs
|
global _diff_dialogs
|
||||||
from calibre.gui2.tweak_book.diff.main import Diff
|
from calibre.gui2.tweak_book.diff.main import Diff
|
||||||
d = Diff(revert_button_msg=revert_msg, parent=self.gui)
|
d = Diff(revert_button_msg=revert_msg, parent=self.gui, show_open_in_editor=show_open_in_editor)
|
||||||
[x.break_cycles() for x in _diff_dialogs if not x.isVisible()]
|
[x.break_cycles() for x in _diff_dialogs if not x.isVisible()]
|
||||||
_diff_dialogs = [x for x in _diff_dialogs if x.isVisible()] + [d]
|
_diff_dialogs = [x for x in _diff_dialogs if x.isVisible()] + [d]
|
||||||
d.show(), d.raise_(), d.setFocus(Qt.OtherFocusReason)
|
d.show(), d.raise_(), d.setFocus(Qt.OtherFocusReason)
|
||||||
@ -409,6 +409,15 @@ class Boss(QObject):
|
|||||||
self.commit_all_editors_to_container()
|
self.commit_all_editors_to_container()
|
||||||
d = self.create_diff_dialog()
|
d = self.create_diff_dialog()
|
||||||
d.revert_requested.connect(partial(self.revert_requested, self.global_undo.previous_container))
|
d.revert_requested.connect(partial(self.revert_requested, self.global_undo.previous_container))
|
||||||
|
def line_activated(name, lnum, right):
|
||||||
|
if right:
|
||||||
|
self.edit_file_requested(name, None, guess_type(name))
|
||||||
|
if name in editors:
|
||||||
|
editor = editors[name]
|
||||||
|
editor.go_to_line(lnum)
|
||||||
|
editor.setFocus(Qt.OtherFocusReason)
|
||||||
|
self.gui.raise_()
|
||||||
|
d.line_activated.connect(line_activated)
|
||||||
d.container_diff(self.global_undo.previous_container, self.global_undo.current_container)
|
d.container_diff(self.global_undo.previous_container, self.global_undo.current_container)
|
||||||
|
|
||||||
def revert_requested(self, container):
|
def revert_requested(self, container):
|
||||||
|
@ -129,12 +129,15 @@ def ebook_diff(path1, path2):
|
|||||||
class Diff(Dialog):
|
class Diff(Dialog):
|
||||||
|
|
||||||
revert_requested = pyqtSignal()
|
revert_requested = pyqtSignal()
|
||||||
|
line_activated = pyqtSignal(object, object, object)
|
||||||
|
|
||||||
def __init__(self, revert_button_msg=None, parent=None):
|
def __init__(self, revert_button_msg=None, parent=None, show_open_in_editor=False):
|
||||||
self.context = 3
|
self.context = 3
|
||||||
self.apply_diff_calls = []
|
self.apply_diff_calls = []
|
||||||
|
self.show_open_in_editor = show_open_in_editor
|
||||||
self.revert_button_msg = revert_button_msg
|
self.revert_button_msg = revert_button_msg
|
||||||
Dialog.__init__(self, _('Differences between books'), 'diff-dialog', parent=parent)
|
Dialog.__init__(self, _('Differences between books'), 'diff-dialog', parent=parent)
|
||||||
|
self.view.line_activated.connect(self.line_activated)
|
||||||
|
|
||||||
def sizeHint(self):
|
def sizeHint(self):
|
||||||
geom = QApplication.instance().desktop().availableGeometry(self)
|
geom = QApplication.instance().desktop().availableGeometry(self)
|
||||||
@ -151,7 +154,7 @@ class Diff(Dialog):
|
|||||||
self.l = l = QGridLayout()
|
self.l = l = QGridLayout()
|
||||||
self.w.setLayout(l)
|
self.w.setLayout(l)
|
||||||
|
|
||||||
self.view = v = DiffView(self)
|
self.view = v = DiffView(self, show_open_in_editor=self.show_open_in_editor)
|
||||||
l.addWidget(v, l.rowCount(), 0, 1, -1)
|
l.addWidget(v, l.rowCount(), 0, 1, -1)
|
||||||
|
|
||||||
r = l.rowCount()
|
r = l.rowCount()
|
||||||
@ -217,7 +220,7 @@ class Diff(Dialog):
|
|||||||
|
|
||||||
def break_cycles(self):
|
def break_cycles(self):
|
||||||
self.view = None
|
self.view = None
|
||||||
for x in ('revert_requested',):
|
for x in ('revert_requested', 'line_activated'):
|
||||||
try:
|
try:
|
||||||
getattr(self, x).disconnect()
|
getattr(self, x).disconnect()
|
||||||
except:
|
except:
|
||||||
|
@ -70,10 +70,12 @@ class TextBrowser(PlainTextEdit): # {{{
|
|||||||
wheel_event = pyqtSignal(object)
|
wheel_event = pyqtSignal(object)
|
||||||
next_change = pyqtSignal(object)
|
next_change = pyqtSignal(object)
|
||||||
scrolled = pyqtSignal()
|
scrolled = pyqtSignal()
|
||||||
|
line_activated = pyqtSignal(object, object, object)
|
||||||
|
|
||||||
def __init__(self, right=False, parent=None):
|
def __init__(self, right=False, parent=None, show_open_in_editor=False):
|
||||||
PlainTextEdit.__init__(self, parent)
|
PlainTextEdit.__init__(self, parent)
|
||||||
self.setFrameStyle(0)
|
self.setFrameStyle(0)
|
||||||
|
self.show_open_in_editor = show_open_in_editor
|
||||||
self.side_margin = 0
|
self.side_margin = 0
|
||||||
self.setContextMenuPolicy(Qt.CustomContextMenu)
|
self.setContextMenuPolicy(Qt.CustomContextMenu)
|
||||||
self.customContextMenuRequested.connect(self.show_context_menu)
|
self.customContextMenuRequested.connect(self.show_context_menu)
|
||||||
@ -144,9 +146,35 @@ class TextBrowser(PlainTextEdit): # {{{
|
|||||||
a(QIcon(I('arrow-up.png')), _('Previous change'), partial(self.next_change.emit, -1))
|
a(QIcon(I('arrow-up.png')), _('Previous change'), partial(self.next_change.emit, -1))
|
||||||
a(QIcon(I('arrow-down.png')), _('Next change'), partial(self.next_change.emit, 1))
|
a(QIcon(I('arrow-down.png')), _('Next change'), partial(self.next_change.emit, 1))
|
||||||
|
|
||||||
|
if self.show_open_in_editor:
|
||||||
|
b = self.cursorForPosition(pos).block()
|
||||||
|
if b.isValid():
|
||||||
|
a(QIcon(I('tweak.png')), _('Open file in the editor'), partial(self.generate_sync_request, b.blockNumber()))
|
||||||
|
|
||||||
if len(m.actions()) > 0:
|
if len(m.actions()) > 0:
|
||||||
m.exec_(self.mapToGlobal(pos))
|
m.exec_(self.mapToGlobal(pos))
|
||||||
|
|
||||||
|
def mouseDoubleClickEvent(self, ev):
|
||||||
|
if ev.button() == 1:
|
||||||
|
b = self.cursorForPosition(ev.pos()).block()
|
||||||
|
if b.isValid():
|
||||||
|
self.generate_sync_request(b.blockNumber())
|
||||||
|
return PlainTextEdit.mouseDoubleClickEvent(self, ev)
|
||||||
|
|
||||||
|
def generate_sync_request(self, block_number):
|
||||||
|
if not self.headers:
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
lnum = int(self.line_number_map.get(block_number, ''))
|
||||||
|
except:
|
||||||
|
lnum = 1
|
||||||
|
for i, (num, text) in enumerate(self.headers):
|
||||||
|
if num > block_number:
|
||||||
|
name = text if i == 0 else self.headers[i - 1][1]
|
||||||
|
else:
|
||||||
|
name = self.headers[0][1]
|
||||||
|
self.line_activated.emit(name, lnum, bool(self.right))
|
||||||
|
|
||||||
def search(self, query, reverse=False):
|
def search(self, query, reverse=False):
|
||||||
''' Search for query, also searching the headers. Matches in headers
|
''' Search for query, also searching the headers. Matches in headers
|
||||||
are not highlighted as managing the highlight is too much of a pain.'''
|
are not highlighted as managing the highlight is too much of a pain.'''
|
||||||
@ -468,11 +496,11 @@ class DiffSplitHandle(QSplitterHandle): # {{{
|
|||||||
|
|
||||||
class DiffSplit(QSplitter): # {{{
|
class DiffSplit(QSplitter): # {{{
|
||||||
|
|
||||||
def __init__(self, parent=None):
|
def __init__(self, parent=None, show_open_in_editor=False):
|
||||||
QSplitter.__init__(self, parent)
|
QSplitter.__init__(self, parent)
|
||||||
self._failed_img = None
|
self._failed_img = None
|
||||||
|
|
||||||
self.left, self.right = TextBrowser(parent=self), TextBrowser(right=True, parent=self)
|
self.left, self.right = TextBrowser(parent=self), TextBrowser(right=True, parent=self, show_open_in_editor=show_open_in_editor)
|
||||||
self.addWidget(self.left), self.addWidget(self.right)
|
self.addWidget(self.left), self.addWidget(self.right)
|
||||||
self.split_words = re.compile(r"\w+|\W", re.UNICODE)
|
self.split_words = re.compile(r"\w+|\W", re.UNICODE)
|
||||||
self.clear()
|
self.clear()
|
||||||
@ -832,8 +860,9 @@ class DiffSplit(QSplitter): # {{{
|
|||||||
class DiffView(QWidget): # {{{
|
class DiffView(QWidget): # {{{
|
||||||
|
|
||||||
SYNC_POSITION = 0.4
|
SYNC_POSITION = 0.4
|
||||||
|
line_activated = pyqtSignal(object, object, object)
|
||||||
|
|
||||||
def __init__(self, parent=None):
|
def __init__(self, parent=None, show_open_in_editor=False):
|
||||||
QWidget.__init__(self, parent)
|
QWidget.__init__(self, parent)
|
||||||
self.changes = [[], [], []]
|
self.changes = [[], [], []]
|
||||||
self.delta = 0
|
self.delta = 0
|
||||||
@ -841,7 +870,7 @@ class DiffView(QWidget): # {{{
|
|||||||
self.setLayout(l)
|
self.setLayout(l)
|
||||||
self.syncpos = 0
|
self.syncpos = 0
|
||||||
l.setMargin(0), l.setSpacing(0)
|
l.setMargin(0), l.setSpacing(0)
|
||||||
self.view = DiffSplit(self)
|
self.view = DiffSplit(self, show_open_in_editor=show_open_in_editor)
|
||||||
l.addWidget(self.view)
|
l.addWidget(self.view)
|
||||||
self.add_diff = self.view.add_diff
|
self.add_diff = self.view.add_diff
|
||||||
self.scrollbar = QScrollBar(self)
|
self.scrollbar = QScrollBar(self)
|
||||||
@ -859,6 +888,7 @@ class DiffView(QWidget): # {{{
|
|||||||
v.wheel_event.connect(self.scrollbar.wheelEvent)
|
v.wheel_event.connect(self.scrollbar.wheelEvent)
|
||||||
if i < 2:
|
if i < 2:
|
||||||
v.next_change.connect(self.next_change)
|
v.next_change.connect(self.next_change)
|
||||||
|
v.line_activated.connect(self.line_activated)
|
||||||
v.scrolled.connect(partial(self.scrolled, i + 1))
|
v.scrolled.connect(partial(self.scrolled, i + 1))
|
||||||
|
|
||||||
def next_change(self, delta):
|
def next_change(self, delta):
|
||||||
|
@ -155,6 +155,9 @@ class Editor(QMainWindow):
|
|||||||
def apply_settings(self, prefs=None):
|
def apply_settings(self, prefs=None):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def go_to_line(self, *args, **kwargs):
|
||||||
|
pass
|
||||||
|
|
||||||
def set_focus(self):
|
def set_focus(self):
|
||||||
self.canvas.setFocus(Qt.OtherFocusReason)
|
self.canvas.setFocus(Qt.OtherFocusReason)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user