[qt4] move scintilla code into dedicated class

error while publishing ReST text :related to: #107995
authorAlain Leufroy <alain@leufroy.fr>
changesetac4ddc09c927
branchdefault
phasedraft
hiddenyes
parent revision#ff85afbaabef [qt4] rename actions container of hg-scintilla object
child revision#e83eb0d1e6c6 [qt4] make pylint a little more happy
files modified by this revision
hgviewlib/qt4/hgfileview.py
# HG changeset patch
# User Alain Leufroy <alain@leufroy.fr>
# Date 1368240270 -7200
# Sat May 11 04:44:30 2013 +0200
# Node ID ac4ddc09c927eb9458bc2a1b58206e93c6a2f7ca
# Parent ff85afbaabef9aee456bd3b7a125e2816a244c01
[qt4] move scintilla code into dedicated class

:related to: #107995

diff --git a/hgviewlib/qt4/hgfileview.py b/hgviewlib/qt4/hgfileview.py
@@ -170,10 +170,35 @@
1                  menu.addAction(self.actions[act])
2              else:
3                  menu.addSeparator()
4          menu.exec_(event.globalPos())
5 
6 +    def clear_highlights(self):
7 +        n = self.length()
8 +        self.SendScintilla(qsci.SCI_SETINDICATORCURRENT, 8) # highlight
9 +        self.SendScintilla(qsci.SCI_INDICATORCLEARRANGE, 0, n)
10 +        self.SendScintilla(qsci.SCI_SETINDICATORCURRENT, 9) # current found
11 +                                                            # occurrence
12 +        self.SendScintilla(qsci.SCI_INDICATORCLEARRANGE, 0, n)
13 +
14 +    def highlight_current_search_string(self, pos, text):
15 +        line = self.SendScintilla(qsci.SCI_LINEFROMPOSITION, pos)
16 +        self.ensureLineVisible(line)
17 +        self.SendScintilla(qsci.SCI_SETINDICATORCURRENT, 9)
18 +        self.SendScintilla(qsci.SCI_INDICATORCLEARRANGE, 0, pos)
19 +        self.SendScintilla(qsci.SCI_INDICATORFILLRANGE, pos, len(text))
20 +
21 +    def search_and_highlight_string(self, text):
22 +        data = unicode(self.text())
23 +        self.SendScintilla(qsci.SCI_SETINDICATORCURRENT, 8)
24 +        pos = [data.find(text)]
25 +        n = len(text)
26 +        while pos[-1] > -1:
27 +            self.SendScintilla(qsci.SCI_INDICATORFILLRANGE, pos[-1], n)
28 +            pos.append(data.find(text, pos[-1]+1))
29 +        return [x for x in pos if x > -1]
30 +
31 
32  class HgFileView(QtGui.QFrame):
33 
34      message_logged = pyqtSignal(str, int)
35      rev_for_diff_changed = pyqtSignal(int)
@@ -468,12 +493,10 @@
36      def prevDiff(self):
37          notfirst = self._prevDiff()
38          self.sci.actions['prev'].setEnabled(self.fileMode() and notfirst and self.nDiffs())
39          self.sci.actions['next'].setEnabled(self.fileMode() and self.nDiffs())
40 
41 -
42 -
43      def nextLine(self):
44          x, y = self.sci.getCursorPosition()
45          self.sci.setCursorPosition(x+1, y)
46 
47      def prevLine(self):
@@ -496,53 +519,30 @@
48      def fileMode(self):
49          return self._mode == 'file'
50 
51      def searchString(self, text):
52          self._find_text = text
53 -        self.clearHighlights()
54 +        self.sci.clear_highlights()
55          findpos = self.highlightSearchString(self._find_text)
56          if findpos:
57              def finditer(self, findpos):
58                  if self._find_text:
59                      for pos in findpos:
60 -                        self.highlightCurrentSearchString(pos, self._find_text)
61 +                        self.sci.highlight_current_search_string(pos, self._find_text)
62                          yield self._ctx.rev(), self._filename, pos
63              return finditer(self, findpos)
64 
65 -    def clearHighlights(self):
66 -        n = self.sci.length()
67 -        self.sci.SendScintilla(qsci.SCI_SETINDICATORCURRENT, 8) # highlight
68 -        self.sci.SendScintilla(qsci.SCI_INDICATORCLEARRANGE, 0, n)
69 -        self.sci.SendScintilla(qsci.SCI_SETINDICATORCURRENT, 9) # current found occurrence
70 -        self.sci.SendScintilla(qsci.SCI_INDICATORCLEARRANGE, 0, n)
71 -
72      def highlightSearchString(self, text):
73 -        data = unicode(self.sci.text())
74 -        self.sci.SendScintilla(qsci.SCI_SETINDICATORCURRENT, 8)
75 -        pos = [data.find(text)]
76 -        n = len(text)
77 -        while pos[-1] > -1:
78 -            self.sci.SendScintilla(qsci.SCI_INDICATORFILLRANGE, pos[-1], n)
79 -            pos.append(data.find(text, pos[-1]+1))
80 -        pos = [x for x in pos if x > -1]
81 -        self.message_logged.emit(
82 -                  "Found %d occurrences of '%s' in current file or diff" % (len(pos), text),
83 -                  2000)
84 +        pos = self.sci.search_and_highlight_string(text)
85 +        msg = "Found %d occurrences of %r in current file or diff" % \
86 +              (len(pos), text)
87 +        self.message_logged.emit(msg, 2000)
88          return pos
89 
90 -    def highlightCurrentSearchString(self, pos, text):
91 -        line = self.sci.SendScintilla(qsci.SCI_LINEFROMPOSITION, pos)
92 -        #line, idx = w.lineIndexFromPosition(nextpos)
93 -        self.sci.ensureLineVisible(line)
94 -        self.sci.SendScintilla(qsci.SCI_SETINDICATORCURRENT, 9)
95 -        self.sci.SendScintilla(qsci.SCI_INDICATORCLEARRANGE, 0, pos)
96 -        self.sci.SendScintilla(qsci.SCI_INDICATORFILLRANGE, pos, len(text))
97 -
98      def verticalScrollBar(self):
99          return self.sci.verticalScrollBar()
100 
101 -
102      def idle_fill_files(self):
103          # we make a burst of diff-lines computed at once, but we
104          # disable GUI updates for efficiency reasons, then only
105          # refresh GUI at the end of the burst
106          self.sci.setUpdatesEnabled(False)