6
__copyright__ = "Copyright © 2005 Canonical Ltd."
6
__copyright__ = "Copyright � 2005 Canonical Ltd."
7
7
__author__ = "Daniel Schierbeck <daniel.schierbeck@gmail.com>"
9
from gi.repository import Gtk
10
from gi.repository import GObject
11
from gi.repository import Pango
17
from linegraph import linegraph, same_branch
18
from graphcell import CellRendererGraph
19
from treemodel import TreeModel
14
20
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):
22
class TreeView(gtk.ScrolledWindow):
25
24
__gproperties__ = {
26
'branch': (GObject.TYPE_PYOBJECT,
25
'branch': (gobject.TYPE_PYOBJECT,
28
27
'The Bazaar branch being visualized',
29
GObject.PARAM_CONSTRUCT_ONLY | GObject.PARAM_WRITABLE),
28
gobject.PARAM_CONSTRUCT_ONLY | gobject.PARAM_WRITABLE),
31
'revision': (GObject.TYPE_PYOBJECT,
30
'revision': (gobject.TYPE_PYOBJECT,
33
32
'The currently selected revision',
34
GObject.PARAM_READWRITE),
33
gobject.PARAM_READWRITE),
36
'revision-number': (GObject.TYPE_STRING,
35
'revision-number': (gobject.TYPE_STRING,
38
37
'The number of the selected revision',
40
GObject.PARAM_READABLE),
39
gobject.PARAM_READABLE),
42
'children': (GObject.TYPE_PYOBJECT,
41
'children': (gobject.TYPE_PYOBJECT,
44
43
'Children of the currently selected revision',
45
GObject.PARAM_READABLE),
44
gobject.PARAM_READABLE),
47
'parents': (GObject.TYPE_PYOBJECT,
46
'parents': (gobject.TYPE_PYOBJECT,
48
47
'Parent revisions',
49
48
'Parents to the currently selected revision',
50
GObject.PARAM_READABLE),
49
gobject.PARAM_READABLE),
52
'revno-column-visible': (GObject.TYPE_BOOLEAN,
53
'Revision number column',
51
'revno-column-visible': (gobject.TYPE_BOOLEAN,
54
53
'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,
55
gobject.PARAM_READWRITE),
57
'date-column-visible': (gobject.TYPE_BOOLEAN,
66
59
'Show date column',
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),
61
gobject.PARAM_READWRITE)
85
'revision-selected': (GObject.SignalFlags.RUN_FIRST,
88
'revision-activated': (GObject.SignalFlags.RUN_FIRST,
90
(GObject.TYPE_PYOBJECT, GObject.TYPE_PYOBJECT)),
91
'tag-added': (GObject.SignalFlags.RUN_FIRST,
93
(GObject.TYPE_STRING, GObject.TYPE_STRING)),
94
'refreshed': (GObject.SignalFlags.RUN_FIRST, None,
66
'revisions-loaded': (gobject.SIGNAL_RUN_FIRST,
69
'revision-selected': (gobject.SIGNAL_RUN_FIRST,
98
def __init__(self, branch, start, maxnum, compact=True):
74
def __init__(self, branch, start, maxnum, broken_line_length=None):
99
75
"""Create a new TreeView.
101
77
:param branch: Branch object for branch to show.
105
81
:param broken_line_length: After how much lines to break
108
super(TreeView, self).__init__(homogeneous=False, spacing=0)
110
self.progress_widget = ProgressPanel()
111
self.pack_start(self.progress_widget, False, True, 0)
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.PolicyType.AUTOMATIC,
118
Gtk.PolicyType.AUTOMATIC)
119
self.scrolled_window.set_shadow_type(Gtk.ShadowType.IN)
120
self.scrolled_window.show()
121
self.pack_start(self.scrolled_window, True, True, 0)
123
self.scrolled_window.add(self.construct_treeview())
84
gtk.ScrolledWindow.__init__(self)
86
self.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
87
self.set_shadow_type(gtk.SHADOW_IN)
89
self.construct_treeview()
126
92
self.branch = branch
132
self.compact = compact
134
self.model = treemodel.BranchTreeModel(self.branch, [])
135
GObject.idle_add(self.populate)
137
self.connect("destroy", self._on_destroy)
139
def _on_destroy(self, *ignored):
141
if getattr(ui.ui_factory, "set_progress_bar_widget", None) is not None:
142
# We'are using our own ui, let's tell it to stop using our widget.
143
ui.ui_factory.set_progress_bar_widget(None)
94
gobject.idle_add(self.populate, start, maxnum,
97
self.connect("destroy", lambda x: self.branch.unlock())
145
99
def do_get_property(self, property):
146
100
if property.name == 'revno-column-visible':
147
101
return self.revno_column.get_visible()
148
elif property.name == 'graph-column-visible':
149
return self.graph_column.get_visible()
150
102
elif property.name == 'date-column-visible':
151
103
return self.date_column.get_visible()
152
elif property.name == 'compact':
154
elif property.name == 'mainline-only':
155
return self.mainline_only
156
104
elif property.name == 'branch':
157
105
return self.branch
158
106
elif property.name == 'revision':
159
if self.path is None:
161
return self.model.get_value(self.model.get_iter(self.path),
107
return self.model.get_value(self.iter, treemodel.REVISION)
163
108
elif property.name == 'revision-number':
164
if self.path is None:
166
return self.model.get_value(self.model.get_iter(self.path),
109
return self.model.get_value(self.iter, treemodel.REVNO)
168
110
elif property.name == 'children':
169
if self.path is None:
171
return self.model.get_value(self.model.get_iter(self.path),
111
return self.model.get_value(self.iter, treemodel.CHILDREN)
173
112
elif property.name == 'parents':
174
if self.path is None:
176
return self.model.get_value(self.model.get_iter(self.path),
113
return self.model.get_value(self.iter, treemodel.PARENTS)
179
115
raise AttributeError, 'unknown property %s' % property.name
181
117
def do_set_property(self, property, value):
182
118
if property.name == 'revno-column-visible':
183
119
self.revno_column.set_visible(value)
184
elif property.name == 'graph-column-visible':
185
self.graph_column.set_visible(value)
186
120
elif property.name == 'date-column-visible':
187
121
self.date_column.set_visible(value)
188
elif property.name == 'compact':
190
elif property.name == 'mainline-only':
191
self.mainline_only = value
192
122
elif property.name == 'branch':
193
123
self.branch = value
194
124
elif property.name == 'revision':
296
194
:param broken_line_length: After how much lines branches \
297
195
should be broken.
300
if getattr(ui.ui_factory, "set_progress_bar_widget", None) is not None:
301
# We'are using our own ui, let's tell it to use our widget.
302
ui.ui_factory.set_progress_bar_widget(self.progress_widget)
303
self.progress_bar = ui.ui_factory.nested_progress_bar()
304
self.progress_bar.update("Loading ancestry graph", 0, 5)
308
broken_line_length = 32
310
broken_line_length = None
312
show_graph = self.graph_column.get_visible()
314
self.branch.lock_read()
315
(linegraphdata, index, columns_len) = linegraph(
316
self.branch.repository.get_graph(),
324
self.model.set_line_graph_data(linegraphdata)
325
self.graph_cell.columns_len = columns_len
326
width = self.graph_cell.get_preferred_width(self.treeview)[1]
330
# The get_preferred_width() call got an insane value.
332
self.graph_column.set_fixed_width(width)
333
self.graph_column.set_max_width(width)
335
self.treeview.set_model(self.model)
337
if not revision or revision == NULL_REVISION:
338
self.treeview.set_cursor(Gtk.TreePath(path=0), None, False)
340
self.set_revision(revision)
342
self.emit('refreshed')
345
self.progress_bar.finished()
197
self.branch.lock_read()
198
(linegraphdata, index, columns_len) = linegraph(self.branch.repository,
203
self.model = TreeModel(self.branch.repository, linegraphdata)
204
self.graph_cell.columns_len = columns_len
205
width = self.graph_cell.get_size(self.treeview)[2]
206
self.graph_column.set_fixed_width(width)
207
self.graph_column.set_max_width(width)
209
self.treeview.set_model(self.model)
210
self.treeview.set_cursor(0)
211
self.emit('revisions-loaded')
215
def show_diff(self, revid=None, parentid=None):
216
"""Open a new window to show a diff between the given revisions."""
217
from bzrlib.plugins.gtk.diff import DiffWindow
218
window = DiffWindow(parent=self)
220
parents = self.get_parents()
223
revid = self.get_revision().revision_id
225
if parentid is None and len(parents) > 0:
226
parentid = parents[0]
229
parentid = NULL_REVISION
231
rev_tree = self.branch.repository.revision_tree(revid)
232
parent_tree = self.branch.repository.revision_tree(parentid)
234
description = revid + " - " + self.branch.nick
235
window.set_diff(description, rev_tree, parent_tree)
347
238
def construct_treeview(self):
348
self.treeview = Gtk.TreeView()
239
self.treeview = gtk.TreeView()
350
241
self.treeview.set_rules_hint(True)
351
# combined revno/summary interactive search
353
# the row in a treemodel is considered "matched" if a REVNO *starts*
354
# from the key (that is the key is found in a REVNO at the offset 0)
355
# or if a MESSAGE *contains* the key anywhere (that is, the key is
356
# found case insensitively in a MESSAGE at any offset)
357
def search_equal_func(model, column, key, iter, ignored):
358
return (model.get_value(iter, treemodel.REVNO).find(key) != 0
359
and model.get_value(iter, treemodel.MESSAGE).lower().find(key.lower()) == -1)
361
self.treeview.set_search_equal_func(search_equal_func, None)
362
self.treeview.set_enable_search(True)
364
self.treeview.set_tooltip_column(treemodel.MESSAGE)
365
self.treeview.set_headers_visible(True)
367
self._prev_cursor_path = None
242
self.treeview.set_search_column(treemodel.REVNO)
244
# Fix old PyGTK bug - by JAM
245
set_tooltip = getattr(self.treeview, 'set_tooltip_column', None)
246
if set_tooltip is not None:
247
set_tooltip(treemodel.MESSAGE)
368
249
self.treeview.connect("cursor-changed",
369
250
self._on_selection_changed)
377
258
self.treeview.set_property('fixed-height-mode', True)
260
self.add(self.treeview)
379
261
self.treeview.show()
381
cell = Gtk.CellRendererText()
263
cell = gtk.CellRendererText()
382
264
cell.set_property("width-chars", 15)
383
cell.set_property("ellipsize", Pango.EllipsizeMode.END)
384
self.revno_column = Gtk.TreeViewColumn("Revision No")
265
cell.set_property("ellipsize", pango.ELLIPSIZE_END)
266
self.revno_column = gtk.TreeViewColumn("Revision No")
385
267
self.revno_column.set_resizable(True)
386
self.revno_column.set_sizing(Gtk.TreeViewColumnSizing.FIXED)
387
self.revno_column.set_fixed_width(
388
cell.get_preferred_width(self.treeview)[1])
389
self.revno_column.pack_start(cell, True)
268
self.revno_column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
269
self.revno_column.set_fixed_width(cell.get_size(self.treeview)[2])
270
self.revno_column.pack_start(cell, expand=True)
390
271
self.revno_column.add_attribute(cell, "text", treemodel.REVNO)
391
272
self.treeview.append_column(self.revno_column)
393
274
self.graph_cell = CellRendererGraph()
394
self.graph_column = Gtk.TreeViewColumn()
275
self.graph_column = gtk.TreeViewColumn()
395
276
self.graph_column.set_resizable(True)
396
self.graph_column.set_sizing(Gtk.TreeViewColumnSizing.FIXED)
397
self.graph_column.pack_start(self.graph_cell, True)
398
self.graph_column.add_attribute(
399
self.graph_cell, "node", treemodel.NODE)
400
self.graph_column.add_attribute(
401
self.graph_cell, "tags", treemodel.TAGS)
402
self.graph_column.add_attribute(
403
self.graph_cell, "in-lines", treemodel.LAST_LINES)
404
self.graph_column.add_attribute(
405
self.graph_cell, "out-lines", treemodel.LINES)
277
self.graph_column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
278
self.graph_column.pack_start(self.graph_cell, expand=False)
279
self.graph_column.add_attribute(self.graph_cell, "node", treemodel.NODE)
280
self.graph_column.add_attribute(self.graph_cell, "in-lines", treemodel.LAST_LINES)
281
self.graph_column.add_attribute(self.graph_cell, "out-lines", treemodel.LINES)
406
282
self.treeview.append_column(self.graph_column)
408
cell = Gtk.CellRendererText()
284
cell = gtk.CellRendererText()
409
285
cell.set_property("width-chars", 65)
410
cell.set_property("ellipsize", Pango.EllipsizeMode.END)
411
self.summary_column = Gtk.TreeViewColumn("Summary")
412
self.summary_column.set_resizable(True)
413
self.summary_column.set_expand(True)
414
self.summary_column.set_sizing(Gtk.TreeViewColumnSizing.FIXED)
415
self.summary_column.set_fixed_width(
416
cell.get_preferred_width(self.treeview)[1])
417
self.summary_column.pack_start(cell, True)
418
self.summary_column.add_attribute(cell, "markup", treemodel.SUMMARY)
419
self.treeview.append_column(self.summary_column)
286
cell.set_property("ellipsize", pango.ELLIPSIZE_END)
287
self.msg_column = gtk.TreeViewColumn("Message")
288
self.msg_column.set_resizable(True)
289
self.msg_column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
290
self.msg_column.set_fixed_width(cell.get_size(self.treeview)[2])
291
self.msg_column.pack_start(cell, expand=True)
292
self.msg_column.add_attribute(cell, "text", treemodel.MESSAGE)
293
self.treeview.append_column(self.msg_column)
421
cell = Gtk.CellRendererText()
295
cell = gtk.CellRendererText()
422
296
cell.set_property("width-chars", 15)
423
cell.set_property("ellipsize", Pango.EllipsizeMode.END)
424
self.authors_column = Gtk.TreeViewColumn("Author(s)")
425
self.authors_column.set_resizable(False)
426
self.authors_column.set_sizing(Gtk.TreeViewColumnSizing.FIXED)
427
self.authors_column.set_fixed_width(200)
428
self.authors_column.pack_start(cell, True)
429
self.authors_column.add_attribute(cell, "text", treemodel.AUTHORS)
430
self.treeview.append_column(self.authors_column)
297
cell.set_property("ellipsize", pango.ELLIPSIZE_END)
298
self.committer_column = gtk.TreeViewColumn("Committer")
299
self.committer_column.set_resizable(True)
300
self.committer_column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
301
self.committer_column.set_fixed_width(cell.get_size(self.treeview)[2])
302
self.committer_column.pack_start(cell, expand=True)
303
self.committer_column.add_attribute(cell, "text", treemodel.COMMITER)
304
self.treeview.append_column(self.committer_column)
432
cell = Gtk.CellRendererText()
306
cell = gtk.CellRendererText()
433
307
cell.set_property("width-chars", 20)
434
cell.set_property("ellipsize", Pango.EllipsizeMode.END)
435
self.date_column = Gtk.TreeViewColumn("Date")
308
cell.set_property("ellipsize", pango.ELLIPSIZE_END)
309
self.date_column = gtk.TreeViewColumn("Date")
436
310
self.date_column.set_visible(False)
437
311
self.date_column.set_resizable(True)
438
self.date_column.set_sizing(Gtk.TreeViewColumnSizing.FIXED)
439
self.date_column.set_fixed_width(130)
440
self.date_column.pack_start(cell, True)
312
self.date_column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
313
self.date_column.set_fixed_width(cell.get_size(self.treeview)[2])
314
self.date_column.pack_start(cell, expand=True)
441
315
self.date_column.add_attribute(cell, "text", treemodel.TIMESTAMP)
442
316
self.treeview.append_column(self.date_column)
446
318
def _on_selection_changed(self, treeview):
447
319
"""callback for when the treeview changes."""
448
320
(path, focus) = treeview.get_cursor()
449
if (path is not None) and (path != self._prev_cursor_path):
450
self._prev_cursor_path = path # avoid emitting twice per click
322
self.iter = self.model.get_iter(path)
452
323
self.emit('revision-selected')
454
325
def _on_revision_selected(self, widget, event):
455
from bzrlib.plugins.gtk.revisionmenu import RevisionMenu
326
from bzrlib.plugins.gtk.revisionmenu import RevisionPopupMenu
456
327
if event.button == 3:
458
rev = self.get_revision()
460
revs.append(rev.revision_id)
461
menu = RevisionMenu(self.branch.repository, revs, self.branch)
462
menu.connect('tag-added', lambda w, t, r: self.add_tag(t, r))
463
menu.popup(None, None, None, None, event.button, event.get_time())
328
menu = RevisionPopupMenu(self.branch.repository,
329
[self.get_revision().revision_id],
331
menu.popup(None, None, None, event.button, event.get_time())
465
333
def _on_revision_activated(self, widget, path, col):
466
self.emit('revision-activated', path, col)
334
# TODO: more than one parent
335
"""Callback for when a treeview row gets activated."""
336
revision_id = self.model[path][treemodel.REVID]
337
parents = self.model[path][treemodel.PARENTS]
339
if len(parents) == 0:
342
parent_id = parents[0]
344
self.show_diff(revision_id, parent_id)
345
self.treeview.grab_focus()