[qt] add open editor to the file list view

Related to #137719

How to test

hg --config hgview.editor=emacs qv -I qt

Select an "old" revision, then left-click on a file name in the file list. You shall now have the "open in editor" action.

authorAlain Leufroy <alain.leufroy@logilab.fr>
changesetc20945e19931
branchdefault
phasedraft
hiddenyes
parent revision#f1200d18f974 [qt] add open in editor action
child revision#7df82e710164 [qt4] open in editor even if the file is "too big"
files modified by this revision
hgviewlib/qt4/hgfileview.py
# HG changeset patch
# User Alain Leufroy <alain.leufroy@logilab.fr>
# Date 1370651447 -7200
# Sat Jun 08 02:30:47 2013 +0200
# Node ID c20945e1993117c81e0e53be87aec4cc1b6ea96b
# Parent f1200d18f974ced937a6f7ec46172597d0ba3f8e
[qt] add open editor to the file list view

Related to #137719

.. admonition:: How to test

::

hg --config hgview.editor=emacs qv -I qt

Select an "old" revision, then left-click on a file name in the
file list. You shall now have the "open in editor" action.

diff --git a/hgviewlib/qt4/hgfileview.py b/hgviewlib/qt4/hgfileview.py
@@ -423,19 +423,13 @@
1          return True
2 
3      def openEditor(self):
4          """Open the editor with the content of the selected file at
5          the selected revision"""
6 -        filename = self._filename
7 -        _, data = self._model.graph.filedata(self._filename, self._ctx.rev(), 'file')
8 -        fid, filepath = tempfile.mkstemp(suffix=os.path.basename(self._filename))
9 -        os.close(fid)
10 -        with open(filepath, 'w') as fid:
11 -            fid.write(data)
12 -        process = QtCore.QProcess(self)
13 -        process.start(self.cfg.getEditor(), [filepath])
14 -        process.finished.connect(lambda rt, st, path=str(filepath): os.remove(path))
15 +        _, content = self._model.graph.filedata(self._filename, self._ctx.rev(), 'file')
16 +        open_tempfile_in_editor(self, self.cfg.getEditor(), content,
17 +                                suffix=os.path.basename(self._filename))
18 
19      def updateDiffDecorations(self):
20          """
21          Recompute the diff and starts the timer
22          responsible for filling diff decoration markers
@@ -654,10 +648,20 @@
23              self.diffNavigate(sel_file)
24 
25      def toggleFullFileList(self, *args):
26          self.model().toggleFullFileList()
27 
28 +    def openEditor(self):
29 +        """Open the editor with the content of the selected file at
30 +        the selected revision"""
31 +        index = self.currentIndex()
32 +        sel_file = self.model().fileFromIndex(index)
33 +        from_rev = self.model().current_ctx.rev()
34 +        content = self.model().repo[from_rev].filectx(sel_file).data()
35 +        open_tempfile_in_editor(self, HgConfig(self.model().repo.ui).getEditor(),
36 +                                content, suffix=os.path.basename(sel_file))
37 +
38      def navigate(self, filename=None):
39          self._navigate(filename, FileViewer, self._nav_dialogs)
40 
41      def diffNavigate(self, filename=None):
42          self._navigate(filename, FileDiffViewer, self._diff_dialogs)
@@ -682,10 +686,12 @@
43      def _action_defs(self):
44          a = [("navigate", self.tr("Navigate"), None ,
45                self.tr('Navigate the revision tree of this file'), None, self.navigate),
46               ("diffnavigate", self.tr("Diff-mode navigate"), None,
47                self.tr('Navigate the revision tree of this file in diff mode'), None, self.diffNavigate),
48 +             ("openeditor", self.tr("Open in Editor"), None,
49 +              self.tr("Open file in Editor at the current revision"), None, self.openEditor),
50               ]
51          return a
52 
53      def createActions(self):
54          self._actions = {}
@@ -702,11 +708,11 @@
55              self._actions[name] = act
56              self.addAction(act)
57 
58      def contextMenuEvent(self, event):
59          menu = QtGui.QMenu(self)
60 -        for act in ['navigate', 'diffnavigate']:
61 +        for act in ['navigate', 'diffnavigate', 'openeditor']:
62              if act:
63                  menu.addAction(self._actions[act])
64              else:
65                  menu.addSeparator()
66          menu.exec_(event.globalPos())
@@ -729,5 +735,16 @@
67          self.setCurrentIndex(self.model().index(min(row+1,
68                               self.model().rowCount() - 1), 0))
69      def prevFile(self):
70          row = self.currentIndex().row()
71          self.setCurrentIndex(self.model().index(max(row - 1, 0), 0))
72 +
73 +
74 +def open_tempfile_in_editor(parent, editor, content, suffix=None):
75 +    """Open the editor with the given content in a temporary file."""
76 +    fid, filepath = tempfile.mkstemp(suffix=suffix)
77 +    os.close(fid)
78 +    with open(filepath, 'w') as fid:
79 +        fid.write(content)
80 +    process = QtCore.QProcess(parent)
81 +    process.start(editor, [filepath])
82 +    process.finished.connect(lambda rt, st: os.remove(filepath))