| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 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 |
|
| 54 |
|
| 55 |
|
| 56 |
if isinstance(model, QtCore.QAbstractProxyModel): |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 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 |
|
| 97 |
|
| 98 |
bottom_index = self.indexAt(self.viewport().rect().bottomLeft()) |
| 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 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 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 |
|
| 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 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
if win32util.is_vista_or_win7(): |
| 164 |
AERO_ENABLED = win32util.is_aero_enabled() |
| 165 |
|
| 166 |
|
| 167 |
def get_text_color(option, style): |
| 168 |
|
| 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 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
return option.palette.color(cg, QtGui.QPalette.Text) |
| 184 |
elif AERO_ENABLED: |
| 185 |
|
| 186 |
|
| 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 |
|
| 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) |