1
# -*- coding: UTF-8 -*-
2
"""Revision history view.
6
__copyright__ = "Copyright © 2005 Canonical Ltd."
7
__author__ = "Daniel Schierbeck <daniel.schierbeck@gmail.com>"
14
from bzrlib.revision import NULL_REVISION
16
from bzrlib.plugins.gtk import lock
17
from bzrlib.plugins.gtk.ui import ProgressPanel
18
from bzrlib.plugins.gtk.branchview import treemodel
19
from bzrlib.plugins.gtk.branchview.linegraph import linegraph, same_branch
20
from bzrlib.plugins.gtk.branchview.graphcell import CellRendererGraph
23
class TreeView(gtk.VBox):
26
'branch': (gobject.TYPE_PYOBJECT,
28
'The Bazaar branch being visualized',
29
gobject.PARAM_CONSTRUCT_ONLY | gobject.PARAM_WRITABLE),
31
'revision': (gobject.TYPE_PYOBJECT,
33
'The currently selected revision',
34
gobject.PARAM_READWRITE),
36
'revision-number': (gobject.TYPE_STRING,
38
'The number of the selected revision',
40
gobject.PARAM_READABLE),
42
'children': (gobject.TYPE_PYOBJECT,
44
'Children of the currently selected revision',
45
gobject.PARAM_READABLE),
47
'parents': (gobject.TYPE_PYOBJECT,
49
'Parents to the currently selected revision',
50
gobject.PARAM_READABLE),
52
'revno-column-visible': (gobject.TYPE_BOOLEAN,
53
'Revision number column',
54
'Show revision number column',
56
gobject.PARAM_READWRITE),
58
'graph-column-visible': (gobject.TYPE_BOOLEAN,
62
gobject.PARAM_READWRITE),
64
'date-column-visible': (gobject.TYPE_BOOLEAN,
68
gobject.PARAM_READWRITE),
70
'compact': (gobject.TYPE_BOOLEAN,
72
'Break ancestry lines to save space',
74
gobject.PARAM_CONSTRUCT | gobject.PARAM_READWRITE),
76
'mainline-only': (gobject.TYPE_BOOLEAN,
78
'Only show the mainline history.',
80
gobject.PARAM_CONSTRUCT | gobject.PARAM_READWRITE),
85
'revision-selected': (gobject.SIGNAL_RUN_FIRST,
88
'revision-activated': (gobject.SIGNAL_RUN_FIRST,
90
(gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT)),
91
'tag-added': (gobject.SIGNAL_RUN_FIRST,
93
(gobject.TYPE_STRING, gobject.TYPE_STRING)),
94
'refreshed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
98
def __init__(self, branch, start, maxnum, compact=True):
99
"""Create a new TreeView.
101
:param branch: Branch object for branch to show.
102
:param start: Revision id of top revision.
103
:param maxnum: Maximum number of revisions to display,
105
:param broken_line_length: After how much lines to break
108
gtk.VBox.__init__(self, spacing=0)
110
self.progress_widget = ProgressPanel()
111
self.pack_start(self.progress_widget, expand=False, fill=True)
112
if getattr(ui.ui_factory, "set_progress_bar_widget", None) is not None:
113
# We'are using our own ui, let's tell it to use our widget.
114
ui.ui_factory.set_progress_bar_widget(self.progress_widget)
116
self.scrolled_window = gtk.ScrolledWindow()
117
self.scrolled_window.set_policy(gtk.POLICY_AUTOMATIC,
118
gtk.POLICY_AUTOMATIC)
119
self.scrolled_window.set_shadow_type(gtk.SHADOW_IN)
120
self.scrolled_window.show()
121
self.pack_start(self.scrolled_window, expand=True, fill=True)
123
self.scrolled_window.add(self.construct_treeview())
132
self.compact = compact
134
gobject.idle_add(self.populate)
136
self.connect("destroy", self._on_destroy)
138
def _on_destroy(self, *ignored):
140
if getattr(ui.ui_factory, "set_progress_bar_widget", None) is not None:
141
# We'are using our own ui, let's tell it to stop using our widget.
142
ui.ui_factory.set_progress_bar_widget(None)
144
def do_get_property(self, property):
145
if property.name == 'revno-column-visible':
146
return self.revno_column.get_visible()
147
elif property.name == 'graph-column-visible':
148
return self.graph_column.get_visible()
149
elif property.name == 'date-column-visible':
150
return self.date_column.get_visible()
151
elif property.name == 'compact':
153
elif property.name == 'mainline-only':
154
return self.mainline_only
155
elif property.name == 'branch':
157
elif property.name == 'revision':
158
return self.model.get_value(self.model.get_iter(self.path),
160
elif property.name == 'revision-number':
161
return self.model.get_value(self.model.get_iter(self.path),
163
elif property.name == 'children':
164
return self.model.get_value(self.model.get_iter(self.path),
166
elif property.name == 'parents':
167
return self.model.get_value(self.model.get_iter(self.path),
170
raise AttributeError, 'unknown property %s' % property.name
172
def do_set_property(self, property, value):
173
if property.name == 'revno-column-visible':
174
self.revno_column.set_visible(value)
175
elif property.name == 'graph-column-visible':
176
self.graph_column.set_visible(value)
177
elif property.name == 'date-column-visible':
178
self.date_column.set_visible(value)
179
elif property.name == 'compact':
181
elif property.name == 'mainline-only':
182
self.mainline_only = value
183
elif property.name == 'branch':
185
elif property.name == 'revision':
186
self.set_revision_id(value.revision_id)
188
raise AttributeError, 'unknown property %s' % property.name
190
def get_revision(self):
191
"""Return revision id of currently selected revision, or None."""
192
return self.get_property('revision')
194
def has_revision_id(self, revision_id):
195
return (revision_id in self.index)
197
def set_revision(self, revision):
198
self.set_property('revision', revision)
200
def set_revision_id(self, revid):
201
"""Change the currently selected revision.
203
:param revid: Revision id of revision to display.
205
self.treeview.set_cursor(self.index[revid])
206
self.treeview.grab_focus()
208
def get_children(self):
209
"""Return the children of the currently selected revision.
211
:return: list of revision ids.
213
return self.get_property('children')
215
def get_parents(self):
216
"""Return the parents of the currently selected revision.
218
:return: list of revision ids.
220
return self.get_property('parents')
222
def add_tag(self, tag, revid=None):
223
if revid is None: revid = self.revision.revision_id
225
if lock.release(self.branch):
227
lock.acquire(self.branch, lock.WRITE)
228
self.model.add_tag(tag, revid)
230
lock.release(self.branch)
232
lock.acquire(self.branch, lock.READ)
234
self.emit('tag-added', tag, revid)
237
gobject.idle_add(self.populate, self.get_revision())
243
self.branch.lock_write()
248
self.branch.lock_read()
251
"""Signal handler for the Back button."""
252
parents = self.get_parents()
256
for parent_id in parents:
257
parent_index = self.index[parent_id]
258
parent = self.model[parent_index][treemodel.REVISION]
259
if same_branch(self.get_revision(), parent):
260
self.set_revision(parent)
263
self.set_revision_id(parents[0])
266
"""Signal handler for the Forward button."""
267
children = self.get_children()
268
if not len(children):
271
for child_id in children:
272
child_index = self.index[child_id]
273
child = self.model[child_index][treemodel.REVISION]
274
if same_branch(child, self.get_revision()):
275
self.set_revision(child)
278
self.set_revision_id(children[0])
280
def populate(self, revision=None):
281
"""Fill the treeview with contents.
283
:param start: Revision id of revision to start with.
284
:param maxnum: Maximum number of revisions to display, or None
286
:param broken_line_length: After how much lines branches \
290
if getattr(ui.ui_factory, "set_progress_bar_widget", None) is not None:
291
# We'are using our own ui, let's tell it to use our widget.
292
ui.ui_factory.set_progress_bar_widget(self.progress_widget)
293
self.progress_bar = ui.ui_factory.nested_progress_bar()
294
self.progress_bar.update("Loading ancestry graph", 0, 5)
298
broken_line_length = 32
300
broken_line_length = None
302
show_graph = self.graph_column.get_visible()
304
self.branch.lock_read()
305
(linegraphdata, index, columns_len) = linegraph(self.branch.repository.get_graph(),
313
self.model = treemodel.TreeModel(self.branch, linegraphdata)
314
self.graph_cell.columns_len = columns_len
315
width = self.graph_cell.get_size(self.treeview)[2]
318
self.graph_column.set_fixed_width(width)
319
self.graph_column.set_max_width(width)
321
self.treeview.set_model(self.model)
323
if not revision or revision == NULL_REVISION:
324
self.treeview.set_cursor(0)
326
self.set_revision(revision)
328
self.emit('refreshed')
331
self.progress_bar.finished()
333
def construct_treeview(self):
334
self.treeview = gtk.TreeView()
336
self.treeview.set_rules_hint(True)
337
# combined revno/summary interactive search
339
# the row in a treemodel is considered "matched" if a REVNO *starts*
340
# from the key (that is the key is found in a REVNO at the offset 0)
341
# or if a MESSAGE *contains* the key anywhere (that is, the key is
342
# found case insensitively in a MESSAGE at any offset)
343
def search_equal_func(model, column, key, iter):
344
return (model.get_value(iter, treemodel.REVNO).find(key) != 0
345
and model.get_value(iter, treemodel.MESSAGE).lower().find(key.lower()) == -1)
347
self.treeview.set_search_equal_func(search_equal_func)
348
self.treeview.set_enable_search(True)
350
# Fix old PyGTK bug - by JAM
351
set_tooltip = getattr(self.treeview, 'set_tooltip_column', None)
352
if set_tooltip is not None:
353
set_tooltip(treemodel.MESSAGE)
355
self._prev_cursor_path = None
356
self.treeview.connect("cursor-changed",
357
self._on_selection_changed)
359
self.treeview.connect("row-activated",
360
self._on_revision_activated)
362
self.treeview.connect("button-release-event",
363
self._on_revision_selected)
365
self.treeview.set_property('fixed-height-mode', True)
369
cell = gtk.CellRendererText()
370
cell.set_property("width-chars", 15)
371
cell.set_property("ellipsize", pango.ELLIPSIZE_END)
372
self.revno_column = gtk.TreeViewColumn("Revision No")
373
self.revno_column.set_resizable(False)
374
self.revno_column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
375
self.revno_column.set_fixed_width(cell.get_size(self.treeview)[2])
376
self.revno_column.pack_start(cell, expand=True)
377
self.revno_column.add_attribute(cell, "text", treemodel.REVNO)
378
self.treeview.append_column(self.revno_column)
380
self.graph_cell = CellRendererGraph()
381
self.graph_column = gtk.TreeViewColumn()
382
self.graph_column.set_resizable(False)
383
self.graph_column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
384
self.graph_column.pack_start(self.graph_cell, expand=True)
385
self.graph_column.add_attribute(self.graph_cell, "node", treemodel.NODE)
386
self.graph_column.add_attribute(self.graph_cell, "tags", treemodel.TAGS)
387
self.graph_column.add_attribute(self.graph_cell, "in-lines", treemodel.LAST_LINES)
388
self.graph_column.add_attribute(self.graph_cell, "out-lines", treemodel.LINES)
389
self.treeview.append_column(self.graph_column)
391
cell = gtk.CellRendererText()
392
cell.set_property("width-chars", 65)
393
cell.set_property("ellipsize", pango.ELLIPSIZE_END)
394
self.summary_column = gtk.TreeViewColumn("Summary")
395
self.summary_column.set_resizable(False)
396
self.summary_column.set_expand(True)
397
self.summary_column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
398
self.summary_column.set_fixed_width(cell.get_size(self.treeview)[2])
399
self.summary_column.pack_start(cell, expand=True)
400
self.summary_column.add_attribute(cell, "markup", treemodel.SUMMARY)
401
self.treeview.append_column(self.summary_column)
403
cell = gtk.CellRendererText()
404
cell.set_property("width-chars", 15)
405
cell.set_property("ellipsize", pango.ELLIPSIZE_END)
406
self.authors_column = gtk.TreeViewColumn("Author(s)")
407
self.authors_column.set_resizable(False)
408
self.authors_column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
409
self.authors_column.set_fixed_width(200)
410
self.authors_column.pack_start(cell, expand=True)
411
self.authors_column.add_attribute(cell, "text", treemodel.AUTHORS)
412
self.treeview.append_column(self.authors_column)
414
cell = gtk.CellRendererText()
415
cell.set_property("width-chars", 20)
416
cell.set_property("ellipsize", pango.ELLIPSIZE_END)
417
self.date_column = gtk.TreeViewColumn("Date")
418
self.date_column.set_visible(False)
419
self.date_column.set_resizable(False)
420
self.date_column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
421
self.date_column.set_fixed_width(130)
422
self.date_column.pack_start(cell, expand=True)
423
self.date_column.add_attribute(cell, "text", treemodel.TIMESTAMP)
424
self.treeview.append_column(self.date_column)
428
def _on_selection_changed(self, treeview):
429
"""callback for when the treeview changes."""
430
(path, focus) = treeview.get_cursor()
431
if (path is not None) and (path != self._prev_cursor_path):
432
self._prev_cursor_path = path # avoid emitting twice per click
434
self.emit('revision-selected')
436
def _on_revision_selected(self, widget, event):
437
from bzrlib.plugins.gtk.revisionmenu import RevisionMenu
438
if event.button == 3:
439
menu = RevisionMenu(self.branch.repository,
440
[self.get_revision().revision_id],
442
menu.connect('tag-added', lambda w, t, r: self.add_tag(t, r))
443
menu.popup(None, None, None, event.button, event.get_time())
445
def _on_revision_activated(self, widget, path, col):
446
self.emit('revision-activated', path, col)