[qt4] add an intuitive link to display heavy files

Related to #109862

Actually one could display heavy file diffs using the dedicated button in the toolbar or the corresponding entry in the context menu. These actions toggle the file size checking.

There is 2 improvements in this changeset:

  • I've added a more explicite message that appears near the file path with a link that helps the user displaying the file data.
  • Just display the file content without "permanently" modifying the file size checking.

Note

This is the simplest implementation I've found. Next changesets are dedicated to make the message more visible and to add more information.

How to test

hg --config hgview.maxfilesize=1 qv -I qt

You shall see the message in the filepath widget with a link that bypasses the file size check and displays the diff content.

authorAlain Leufroy <alain@leufroy.fr>
changeset3e69744780c3
branchdefault
phasedraft
hiddenyes
parent revision#e4dadbf7a05a [qt] fix typo ``to_unicode`` -> ``tounicode``
child revision#18efa4102cd2 [qt4] hide text area on heavy file
files modified by this revision
hgviewlib/hggraph.py
hgviewlib/qt4/hgfileview.py
hgviewlib/qt4/hgqv.qrc
hgviewlib/qt4/icons/heavy_small.png
# HG changeset patch
# User Alain Leufroy <alain@leufroy.fr>
# Date 1370648671 -7200
# Sat Jun 08 01:44:31 2013 +0200
# Node ID 3e69744780c3d18e6f016adb20e82f05bf947798
# Parent e4dadbf7a05a49cd99fc6901e0f6e98ed8b32c08
[qt4] add an intuitive link to display heavy files

Related to #109862

Actually one could display heavy file diffs using the dedicated button
in the toolbar or the corresponding entry in the context menu.
These actions toggle the file size checking.


There is 2 improvements in this changeset:

- I've added a more explicite message that appears near the file path
with a link that helps the user displaying the file data.

- Just display the file content without "permanently" modifying the
file size checking.

.. note::

This is the simplest implementation I've found. Next changesets are
dedicated to make the message more visible and to add more
information.

.. admonition:: How to test

::

hg --config hgview.maxfilesize=1 qv -I qt

You shall see the message in the filepath widget with a link that
bypasses the file size check and displays the diff content.

diff --git a/hgviewlib/hggraph.py b/hgviewlib/hggraph.py
@@ -478,16 +478,18 @@
1          return self.fileflags(filename, rev)[0]
2 
3      def filename(self, rev):
4          return self.nodesdict[rev].extra[0]
5 
6 -    def filedata(self, filename, rev, mode='diff', flag=None):
7 +    def filedata(self, filename, rev, mode='diff', flag=None, maxfilesize=None):
8          """XXX written under dubious encoding assumptions
9 
10          The modification flag is computed using *fileflag* if ``flag`` is None.
11          """
12          # XXX This really begins to be a dirty mess...
13 +        if maxfilesize is None:
14 +            maxfilesize = self.maxfilesize
15          data = ""
16          if flag is None:
17              flag = self.fileflag(filename, rev)
18          ctx = self.repo.changectx(rev)
19          filesize = 0
@@ -503,20 +505,20 @@
20                  data += u"footprint: %s\n" % tounicode(data)
21              return "+", data
22          if flag not in ('-', '?'):
23              if fctx is None:# or fctx.node() is None:
24                  return '', None
25 -            if self.maxfilesize >= 0 and filesize > self.maxfilesize:
26 +            if maxfilesize >= 0 and filesize > maxfilesize:
27                  try:
28                      div = int(filesize).bit_length() // 10
29                      sym = ('', 'K', 'M', 'G', 'T', 'E')[div] # more, really ???
30                      val = int(filesize / (2 ** (div * 10)))
31                  except AttributeError: # py<2.7
32                      val = filesize
33                      sym = ''
34                  data = u"File too big ! (~%i%so)" % (val, sym)
35 -                return flag, data
36 +                return 'file too big', data
37              if flag == "+" or mode == 'file':
38                  flag = '+'
39                  # return the whole file
40                  data = fctx.data()
41                  if util.binary(data):
diff --git a/hgviewlib/qt4/hgfileview.py b/hgviewlib/qt4/hgfileview.py
@@ -217,11 +217,14 @@
42          self.topLayout = QtGui.QVBoxLayout()
43          self.filenamelabel = QtGui.QLabel()
44          self.filenamelabel.setWordWrap(True)
45          self.filenamelabel.setTextInteractionFlags(
46              QtCore.Qt.TextSelectableByKeyboard|
47 -            QtCore.Qt.TextSelectableByMouse)
48 +            QtCore.Qt.TextSelectableByMouse|
49 +            QtCore.Qt.LinksAccessibleByMouse)
50 +        self.filenamelabel.linkActivated.connect(
51 +            lambda link: self.displayFile(show_big_file=True))
52          self.execflaglabel = QtGui.QLabel()
53          self.execflaglabel.setWordWrap(True)
54          self.topLayout.addWidget(self.filenamelabel)
55          self.topLayout.addWidget(self.execflaglabel)
56          self.execflaglabel.hide()
@@ -335,11 +338,11 @@
57 
58      def displayDiff(self, rev):
59          if rev != self._p_rev:
60              self.displayFile(rev=rev)
61 
62 -    def displayFile(self, filename=None, rev=None):
63 +    def displayFile(self, filename=None, rev=None, show_big_file=None):
64          if filename is None:
65              filename = self._filename
66 
67          self._realfilename = filename
68          if isbfile(filename):
@@ -363,11 +366,19 @@
69              return
70          if self._mode == 'diff' and self._p_rev is not None:
71              mode = self._p_rev
72          else:
73              mode = self._mode
74 -        flag, data = self._model.graph.filedata(filename, self._ctx.rev(), mode)
75 +        if show_big_file:
76 +            flag, data = self._model.graph.filedata(filename, self._ctx.rev(), mode, maxfilesize=-1)
77 +        else:
78 +            flag, data = self._model.graph.filedata(filename, self._ctx.rev(), mode)
79 +        if flag == 'file too big':
80 +            self.filenamelabel.setText(
81 +                '%s %s' %
82 +                (data, '<a href="show-big-file">Click to display anyway <img src=":/icons/heavy_small.png" width="16" height="16" title="Display heavy file">.</a>'))
83 +            return
84          if flag == '-':
85              return
86          if flag == '':
87              return
88 
diff --git a/hgviewlib/qt4/hgqv.qrc b/hgviewlib/qt4/hgqv.qrc
@@ -22,9 +22,10 @@
89      <file>icons/showhide.png</file>
90      <file>icons/content.png</file>
91      <file>icons/unfilter.png</file>
92      <file>icons/diffmode.png</file>
93      <file>icons/heavy.png</file>
94 +    <file>icons/heavy_small.png</file>
95      <file>icons/valid.png</file>
96      <file>icons/loading.png</file>
97    </qresource>
98  </RCC>
diff --git a/hgviewlib/qt4/icons/heavy_small.png b/hgviewlib/qt4/icons/heavy_small.png