Debian Codegraph @

lib/revtreeview.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 #
3 # QBrz - Qt frontend to Breezy commands
4 # Copyright (C) 2006-2007 Gary van der Merwe <[email protected]>
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20 from PyQt5 import QtCore, QtGui, QtWidgets
21 from breezy.plugins.qbrz.lib import MS_WINDOWS
22 from breezy.plugins.qbrz.lib.util import run_in_loading_queue
23 from breezy.plugins.qbrz.lib.lazycachedrevloader import load_revisions
24 from breezy.plugins.qbrz.lib.diff import ExtDiffContext
25 from breezy.transport.local import LocalTransport
26
27 RevIdRole = QtCore.Qt.UserRole + 1
28
29
30 class RevisionTreeView(QtWidgets.QTreeView):
31 """TreeView widget to shows revisions.
32
33 Only revisions that are visible on screen are loaded.
34
35 The model for this tree view must have the following methods:
36 def on_revisions_loaded(self, revisions, last_call)
37 def get_revid(self, index)
38 def get_repo(self)
39 """
40
41 def __init__(self, parent=None):
42 QtWidgets.QTreeView.__init__(self, parent)
43 self.verticalScrollBar().valueChanged[int].connect(self.scroll_changed)
44 self.collapsed[QtCore.QModelIndex].connect(self.collapsed_expanded)
45 self.expanded[QtCore.QModelIndex].connect(self.collapsed_expanded)
46
47 self.load_revisions_call_count = 0
48 self.load_revisions_throbber_shown = False
49 self.revision_loading_disabled = False
50 self.diff_context = ExtDiffContext(self)
51
52 def setModel(self, model):
53 # QtWidgets.QTreeView.setModel(self, model)
54
55 # Do NOT be tempted to simplify this back to the old version
56 if isinstance(model, QtCore.QAbstractProxyModel):
57 # Connecting the below signal has funny results when we connect to to a ProxyModel, so connect to the source model.
58 # RJLRJL: This is because, if you are doing things on the proxy rather than the source, you need to update the
59 # source using sourceModel(). You might also need to use sourceModel().layoutChanged.emit() too. Doing
60 # model = model.sourceModel() is not quite the same thing.
61 QtWidgets.QTreeView.setModel(self, model.sourceModel())
62 model.sourceModel().dataChanged[QtCore.QModelIndex, QtCore.QModelIndex].connect(self.data_changed)
63 model.sourceModel().layoutChanged.connect(self.layout_changed)
64 else:
65 QtWidgets.QTreeView.setModel(self, model)
66 model.dataChanged[QtCore.QModelIndex, QtCore.QModelIndex].connect(self.data_changed)
67 model.layoutChanged.connect(self.layout_changed)
68
69 def scroll_changed(self, value):
70 self.load_visible_revisions()
71
72 def data_changed(self, start_index, end_index):
73 self.load_visible_revisions()
74
75 def layout_changed(self):
76 self.load_visible_revisions()
77
78 def collapsed_expanded(self, index):
79 self.load_visible_revisions()
80
81 def resizeEvent(self, e):
82 self.load_visible_revisions()
83 QtWidgets.QTreeView.resizeEvent(self, e)
84
85 def load_visible_revisions(self):
86 if not self.revision_loading_disabled:
87 run_in_loading_queue(self._load_visible_revisions)
88
89 def _load_visible_revisions(self):
90 model = self.model()
91
92 index = self.indexAt(self.viewport().rect().topLeft())
93 if not index.isValid():
94 return
95
96 # if self.throbber is not None:
97 # throbber_height = self.throbber. etc...
98 bottom_index = self.indexAt(self.viewport().rect().bottomLeft()) # + throbber_height
99
100 revids = []
101 while True:
102 revid = index.data(RevIdRole)
103 if revid is not None:
104 if revid not in revids:
105 revids.append(revid)
106 if index == bottom_index:
107 break
108 index = self.indexBelow(index)
109 if not index.isValid():
110 break
111
112 revids = list(revids)
113 if len(revids) == 0:
114 return
115
116 self.load_revisions_call_count += 1
117 current_call_count = self.load_revisions_call_count
118
119 def before_batch_load(repo, revids):
120 if current_call_count < self.load_revisions_call_count:
121 return True
122
123 repo_is_local = isinstance(repo.controldir.transport, LocalTransport)
124 if not repo_is_local:
125 # Disable this until we have thobber that does not irratate
126 # users when we show and hide quickly.
127 # if not self.load_revisions_throbber_shown \
128 # and hasattr(self, "throbber"):
129 # self.throbber.show()
130 # self.load_revisions_throbber_shown = True
131 # Allow for more scrolling to happen.
132 self.delay(500)
133
134 return False
135
136 try:
137 load_revisions(revids, model.get_repo(), revisions_loaded=model.on_revisions_loaded, before_batch_load=before_batch_load)
138 finally:
139 self.load_revisions_call_count -= 1
140 if self.load_revisions_call_count == 0:
141 # This is the last running method
142 if self.load_revisions_throbber_shown:
143 self.load_revisions_throbber_shown = False
144 self.throbber.hide()
145
146 def delay(self, timeout):
147
148 def null():
149 pass
150
151 QtCore.QTimer.singleShot(timeout, null)
152 QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.WaitForMoreEvents)
153
154
155 has_vista_style = hasattr(QtGui, "QWindowsVistaStyle")
156 AERO_ENABLED = False
157 if MS_WINDOWS:
158 from breezy.plugins.qbrz.lib import win32util
159 # it seems that latest available bzr distributives for Windows
160 # contain a bit outdated version of PyQt which does not support Win8 styles well.
161 # But it does support Vista/Win7 good enough.
162 # So we enable our hack with selected text color only on Vista/Win7
163 if win32util.is_vista_or_win7():
164 AERO_ENABLED = win32util.is_aero_enabled()
165
166
167 def get_text_color(option, style):
168 # cg == ColorGroup
169 if option.state & QtWidgets.QStyle.State_Enabled:
170 if option.state & QtWidgets.QStyle.State_Active:
171 cg = QtGui.QPalette.Active
172 else:
173 cg = QtGui.QPalette.Inactive
174 else:
175 cg = QtGui.QPalette.Disabled
176
177 if option.state & QtWidgets.QStyle.State_Selected:
178 if has_vista_style and isinstance(style, QtGui.QWindowsVistaStyle):
179 # QWindowsVistaStyle normally modifies it palette,
180 # but as we can't reuse that code, we have to reproduce
181 # what it does here.
182 # https://bugs.edge.launchpad.net/qbrz/+bug/457895
183 return option.palette.color(cg, QtGui.QPalette.Text)
184 elif AERO_ENABLED:
185 # hack-hack-hack for Vista/Win7: we need to use the black text
186 # when aero selection of light blue with gradient is used
187 return option.palette.color(cg, QtGui.QPalette.Text)
188
189 return option.palette.color(cg, QtGui.QPalette.HighlightedText)
190 else:
191 return option.palette.color(cg, QtGui.QPalette.Text)
192
193
194 class RevNoItemDelegate(QtWidgets.QStyledItemDelegate):
195
196 def __init__(self, max_mainline_digits=4, parent=None):
197 QtWidgets.QItemDelegate.__init__(self, parent)
198 self.max_mainline_digits = max_mainline_digits
199
200 def paint(self, painter, option, index):
201 # option = QtGui.QStyleOptionViewItemV4(option)
202 option = QtWidgets.QStyleOptionViewItem(option)
203 self.initStyleOption(option, index)
204 widget = self.parent()
205 style = widget.style()
206
207 painter.save()
208 painter.setClipRect(option.rect)
209 style.drawPrimitive(QtWidgets.QStyle.PE_PanelItemViewItem, option, painter, widget)
210
211 text_margin = style.pixelMetric(QtWidgets.QStyle.PM_FocusFrameHMargin, None, widget) + 1
212 text_rect = option.rect.adjusted(text_margin, 0, -text_margin, 0)
213
214 painter.setPen(get_text_color(option, style))
215
216 if option.text:
217 text = option.text
218 paint_revno(painter, text_rect, text, self.max_mainline_digits)
219
220 painter.restore()
221
222 def set_max_revno(self, revno):
223 """Update max_mainline_digits based on max revno.
224 Return the new value of max_mainline_digits to caller.
225 """
226 mainline_digits = len("%d" % revno)
227 self.max_mainline_digits = max(mainline_digits, 4)
228 return self.max_mainline_digits
229
230
231 def paint_revno(painter, rect, revno, max_mainline_digits):
232 splitpoint = revno.find(".")
233 if splitpoint == -1:
234 splitpoint = len(revno)
235 mainline, therest = revno[:splitpoint], revno[splitpoint:]
236
237 if mainline.endswith(" ?"):
238 mainline = mainline[:-2]
239 therest = " ?"
240
241 fm = painter.fontMetrics()
242 mainline_width = fm.width("8"*max_mainline_digits)
243 therest_width = fm.width(therest)
244
245 if mainline_width + therest_width > rect.width():
246 text = revno
247 if fm.width(text) > rect.width():
248 text = fm.elidedText(text, QtCore.Qt.ElideRight, rect.width())
249 painter.drawText(rect, QtCore.Qt.AlignRight, text)
250 else:
251 mainline_rect = QtCore.QRect(rect.x(), rect.y(), mainline_width, rect.height())
252 therest_rect = QtCore.QRect(rect.x() + mainline_width, rect.y(), rect.width() - mainline_width, rect.height())
253 painter.drawText(mainline_rect, QtCore.Qt.AlignRight, mainline)
254 painter.drawText(therest_rect, QtCore.Qt.AlignLeft, therest)