1
# -*- coding: UTF-8 -*-
2
"""Revision history view.
6
__copyright__ = "Copyright � 2005 Canonical Ltd."
7
__author__ = "Daniel Schierbeck <daniel.schierbeck@gmail.com>"
17
from linegraph import linegraph, same_branch
18
from graphcell import CellRendererGraph
19
from treemodel import TreeModel
21
class TreeView(gtk.ScrolledWindow):
24
'revisions-loaded': (gobject.SIGNAL_RUN_FIRST,
27
'revision-selected': (gobject.SIGNAL_RUN_FIRST,
32
def __init__(self, branch, start, maxnum, broken_line_length=None):
33
"""Create a new TreeView.
35
:param branch: Branch object for branch to show.
36
:param start: Revision id of top revision.
37
:param maxnum: Maximum number of revisions to display,
39
:param broken_line_length: After how much lines to break
42
gtk.ScrolledWindow.__init__(self)
44
self.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
45
self.set_shadow_type(gtk.SHADOW_IN)
47
self.construct_treeview()
50
self.branch.lock_read()
52
gobject.idle_add(self.populate, start, maxnum,
59
self.connect('destroy', lambda w: self.branch.unlock())
61
def get_revision(self):
62
"""Return revision id of currently selected revision, or None."""
65
def set_revision(self, revid):
66
"""Change the currently selected revision.
68
:param revid: Revision id of revision to display.
70
self.treeview.set_cursor(self.index[revid])
71
self.treeview.grab_focus()
73
def get_children(self):
74
"""Return the children of the currently selected revision.
76
:return: list of revision ids.
80
def get_parents(self):
81
"""Return the parents of the currently selected revision.
83
:return: list of revision ids.
88
"""Signal handler for the Back button."""
89
(path, col) = self.treeview.get_cursor()
90
revision = self.model[path][treemodel.REVISION]
91
parents = self.model[path][treemodel.PARENTS]
95
for parent_id in parents:
96
parent_index = self.index[parent_id]
97
parent = self.model[parent_index][treemodel.REVISION]
98
if same_branch(revision, parent):
99
self.treeview.set_cursor(parent_index)
102
self.treeview.set_cursor(self.index[parents[0]])
103
self.treeview.grab_focus()
106
"""Signal handler for the Forward button."""
107
(path, col) = self.treeview.get_cursor()
108
revision = self.model[path][treemodel.REVISION]
109
children = self.model[path][treemodel.CHILDREN]
110
if not len(children):
113
for child_id in children:
114
child_index = self.index[child_id]
115
child = self.model[child_index][treemodel.REVISION]
116
if same_branch(child, revision):
117
self.treeview.set_cursor(child_index)
120
self.treeview.set_cursor(self.index[children[0]])
121
self.treeview.grab_focus()
123
def populate(self, start, maxnum, broken_line_length=None):
124
"""Fill the treeview with contents.
126
:param start: Revision id of revision to start with.
127
:param maxnum: Maximum number of revisions to display, or None
129
:param broken_line_length: After how much lines branches \
132
(linegraphdata, index, columns_len) = linegraph(self.branch.repository,
137
self.model = TreeModel(self.branch.repository, linegraphdata)
138
self.graph_cell.columns_len = columns_len
139
width = self.graph_cell.get_size(self.treeview)[2]
142
self.graph_column.set_fixed_width(width)
143
self.graph_column.set_max_width(width)
145
self.treeview.set_model(self.model)
146
self.treeview.set_cursor(0)
147
self.emit('revisions-loaded')
151
def show_diff(self, branch, revid, parentid):
152
"""Open a new window to show a diff between the given revisions."""
153
from bzrlib.plugins.gtk.diff import DiffWindow
154
window = DiffWindow(parent=self)
155
(parent_tree, rev_tree) = branch.repository.revision_trees([parentid,
157
description = revid + " - " + branch.nick
158
window.set_diff(description, rev_tree, parent_tree)
161
def construct_treeview(self):
162
self.treeview = gtk.TreeView()
164
self.treeview.set_rules_hint(True)
165
self.treeview.set_search_column(treemodel.REVNO)
167
# Fix old PyGTK bug - by JAM
168
set_tooltip = getattr(self.treeview, 'set_tooltip_column', None)
169
if set_tooltip is not None:
170
set_tooltip(treemodel.MESSAGE)
172
self.treeview.get_selection().connect("changed",
173
self._on_selection_changed)
175
self.treeview.connect("row-activated",
176
self._on_revision_activated)
178
self.treeview.connect("button-release-event",
179
self._on_revision_selected)
181
self.treeview.set_property('fixed-height-mode', True)
183
self.add(self.treeview)
186
cell = gtk.CellRendererText()
187
cell.set_property("width-chars", 15)
188
cell.set_property("ellipsize", pango.ELLIPSIZE_END)
189
column = gtk.TreeViewColumn("Revision No")
190
column.set_resizable(True)
191
column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
192
column.set_fixed_width(cell.get_size(self.treeview)[2])
193
column.pack_start(cell, expand=True)
194
column.add_attribute(cell, "text", treemodel.REVNO)
195
self.treeview.append_column(column)
197
self.graph_cell = CellRendererGraph()
198
self.graph_column = gtk.TreeViewColumn()
199
self.graph_column.set_resizable(True)
200
self.graph_column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
201
self.graph_column.pack_start(self.graph_cell, expand=False)
202
self.graph_column.add_attribute(self.graph_cell, "node", treemodel.NODE)
203
self.graph_column.add_attribute(self.graph_cell, "in-lines", treemodel.LAST_LINES)
204
self.graph_column.add_attribute(self.graph_cell, "out-lines", treemodel.LINES)
205
self.treeview.append_column(self.graph_column)
207
cell = gtk.CellRendererText()
208
cell.set_property("width-chars", 65)
209
cell.set_property("ellipsize", pango.ELLIPSIZE_END)
210
column = gtk.TreeViewColumn("Message")
211
column.set_resizable(True)
212
column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
213
column.set_fixed_width(cell.get_size(self.treeview)[2])
214
column.pack_start(cell, expand=True)
215
column.add_attribute(cell, "text", treemodel.MESSAGE)
216
self.treeview.append_column(column)
218
cell = gtk.CellRendererText()
219
cell.set_property("width-chars", 15)
220
cell.set_property("ellipsize", pango.ELLIPSIZE_END)
221
column = gtk.TreeViewColumn("Committer")
222
column.set_resizable(True)
223
column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
224
column.set_fixed_width(cell.get_size(self.treeview)[2])
225
column.pack_start(cell, expand=True)
226
column.add_attribute(cell, "text", treemodel.COMMITER)
227
self.treeview.append_column(column)
229
def _on_selection_changed(self, selection, *args):
230
"""callback for when the treeview changes."""
231
(model, selected_rows) = selection.get_selected_rows()
232
if len(selected_rows) > 0:
233
iter = self.model.get_iter(selected_rows[0])
234
self.revision = self.model.get_value(iter, treemodel.REVISION)
235
self.parents = self.model.get_value(iter, treemodel.PARENTS)
236
self.children = self.model.get_value(iter, treemodel.CHILDREN)
238
self.emit('revision-selected')
240
def _on_revision_selected(self, widget, event):
241
from bzrlib.plugins.gtk.revisionmenu import RevisionPopupMenu
242
if event.button == 3:
243
menu = RevisionPopupMenu(self.branch.repository,
244
[self.get_revision().revision_id],
246
menu.popup(None, None, None, event.button, event.get_time())
248
def _on_revision_activated(self, widget, path, col):
249
# TODO: more than one parent
250
"""Callback for when a treeview row gets activated."""
251
revision_id = self.model[path][treemodel.REVID]
252
parents = self.model[path][treemodel.PARENTS]
253
if len(parents) == 0:
254
# Ignore revisions without parent
256
parent_id = parents[0]
257
self.show_diff(self.branch, revision_id, parent_id)
258
self.treeview.grab_focus()