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
20
from bzrlib.revision import NULL_REVISION
22
class TreeView(gtk.ScrolledWindow):
25
'revisions-loaded': (gobject.SIGNAL_RUN_FIRST,
28
'revision-selected': (gobject.SIGNAL_RUN_FIRST,
33
def __init__(self, branch, start, maxnum, broken_line_length=None):
34
"""Create a new TreeView.
36
:param branch: Branch object for branch to show.
37
:param start: Revision id of top revision.
38
:param maxnum: Maximum number of revisions to display,
40
:param broken_line_length: After how much lines to break
43
gtk.ScrolledWindow.__init__(self)
45
self.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
46
self.set_shadow_type(gtk.SHADOW_IN)
48
self.construct_treeview()
51
self.branch.lock_read()
53
gobject.idle_add(self.populate, start, maxnum,
60
self.connect('destroy', lambda w: self.branch.unlock())
62
def get_revision(self):
63
"""Return revision id of currently selected revision, or None."""
66
def set_revision(self, revid):
67
"""Change the currently selected revision.
69
:param revid: Revision id of revision to display.
71
self.treeview.set_cursor(self.index[revid])
72
self.treeview.grab_focus()
74
def get_children(self):
75
"""Return the children of the currently selected revision.
77
:return: list of revision ids.
81
def get_parents(self):
82
"""Return the parents of the currently selected revision.
84
:return: list of revision ids.
89
"""Signal handler for the Back button."""
90
(path, col) = self.treeview.get_cursor()
91
revision = self.model[path][treemodel.REVISION]
92
parents = self.model[path][treemodel.PARENTS]
96
for parent_id in parents:
97
parent_index = self.index[parent_id]
98
parent = self.model[parent_index][treemodel.REVISION]
99
if same_branch(revision, parent):
100
self.treeview.set_cursor(parent_index)
103
self.treeview.set_cursor(self.index[parents[0]])
104
self.treeview.grab_focus()
107
"""Signal handler for the Forward button."""
108
(path, col) = self.treeview.get_cursor()
109
revision = self.model[path][treemodel.REVISION]
110
children = self.model[path][treemodel.CHILDREN]
111
if not len(children):
114
for child_id in children:
115
child_index = self.index[child_id]
116
child = self.model[child_index][treemodel.REVISION]
117
if same_branch(child, revision):
118
self.treeview.set_cursor(child_index)
121
self.treeview.set_cursor(self.index[children[0]])
122
self.treeview.grab_focus()
124
def populate(self, start, maxnum, broken_line_length=None):
125
"""Fill the treeview with contents.
127
:param start: Revision id of revision to start with.
128
:param maxnum: Maximum number of revisions to display, or None
130
:param broken_line_length: After how much lines branches \
133
(linegraphdata, index, columns_len) = linegraph(self.branch.repository,
138
self.model = TreeModel(self.branch.repository, linegraphdata)
139
self.graph_cell.columns_len = columns_len
140
width = self.graph_cell.get_size(self.treeview)[2]
141
self.graph_column.set_fixed_width(width)
142
self.graph_column.set_max_width(width)
144
self.treeview.set_model(self.model)
145
self.treeview.set_cursor(0)
146
self.emit('revisions-loaded')
150
def show_diff(self, branch, revid, parentid=None):
151
"""Open a new window to show a diff between the given revisions."""
152
from bzrlib.plugins.gtk.diff import DiffWindow
153
window = DiffWindow(parent=self)
155
rev_tree = branch.repository.revision_tree(revid)
158
parentid = NULL_REVISION
160
parent_tree = branch.repository.revision_tree(parentid)
162
description = revid + " - " + branch.nick
163
window.set_diff(description, rev_tree, parent_tree)
166
def construct_treeview(self):
167
self.treeview = gtk.TreeView()
169
self.treeview.set_rules_hint(True)
170
self.treeview.set_search_column(treemodel.REVNO)
171
self.treeview.set_tooltip_column(treemodel.MESSAGE)
173
self.treeview.get_selection().connect("changed",
174
self._on_selection_changed)
176
self.treeview.connect("row-activated",
177
self._on_revision_activated)
179
self.treeview.connect("button-release-event",
180
self._on_revision_selected)
182
self.treeview.set_property('fixed-height-mode', True)
184
self.add(self.treeview)
187
cell = gtk.CellRendererText()
188
cell.set_property("width-chars", 15)
189
cell.set_property("ellipsize", pango.ELLIPSIZE_END)
190
column = gtk.TreeViewColumn("Revision No")
191
column.set_resizable(True)
192
column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
193
column.set_fixed_width(cell.get_size(self.treeview)[2])
194
column.pack_start(cell, expand=True)
195
column.add_attribute(cell, "text", treemodel.REVNO)
196
self.treeview.append_column(column)
198
self.graph_cell = CellRendererGraph()
199
self.graph_column = gtk.TreeViewColumn()
200
self.graph_column.set_resizable(True)
201
self.graph_column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
202
self.graph_column.pack_start(self.graph_cell, expand=False)
203
self.graph_column.add_attribute(self.graph_cell, "node", treemodel.NODE)
204
self.graph_column.add_attribute(self.graph_cell, "in-lines", treemodel.LAST_LINES)
205
self.graph_column.add_attribute(self.graph_cell, "out-lines", treemodel.LINES)
206
self.treeview.append_column(self.graph_column)
208
cell = gtk.CellRendererText()
209
cell.set_property("width-chars", 65)
210
cell.set_property("ellipsize", pango.ELLIPSIZE_END)
211
column = gtk.TreeViewColumn("Message")
212
column.set_resizable(True)
213
column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
214
column.set_fixed_width(cell.get_size(self.treeview)[2])
215
column.pack_start(cell, expand=True)
216
column.add_attribute(cell, "text", treemodel.MESSAGE)
217
self.treeview.append_column(column)
219
cell = gtk.CellRendererText()
220
cell.set_property("width-chars", 15)
221
cell.set_property("ellipsize", pango.ELLIPSIZE_END)
222
column = gtk.TreeViewColumn("Committer")
223
column.set_resizable(True)
224
column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
225
column.set_fixed_width(cell.get_size(self.treeview)[2])
226
column.pack_start(cell, expand=True)
227
column.add_attribute(cell, "text", treemodel.COMMITER)
228
self.treeview.append_column(column)
230
def _on_selection_changed(self, selection, *args):
231
"""callback for when the treeview changes."""
232
(model, selected_rows) = selection.get_selected_rows()
233
if len(selected_rows) > 0:
234
iter = self.model.get_iter(selected_rows[0])
235
self.revision = self.model.get_value(iter, treemodel.REVISION)
236
self.parents = self.model.get_value(iter, treemodel.PARENTS)
237
self.children = self.model.get_value(iter, treemodel.CHILDREN)
239
self.emit('revision-selected')
241
def _on_revision_selected(self, widget, event):
242
from bzrlib.plugins.gtk.revisionmenu import RevisionPopupMenu
243
if event.button == 3:
244
menu = RevisionPopupMenu(self.branch.repository,
245
[self.get_revision().revision_id],
247
menu.popup(None, None, None, event.button, event.get_time())
249
def _on_revision_activated(self, widget, path, col):
250
# TODO: more than one parent
251
"""Callback for when a treeview row gets activated."""
252
revision_id = self.model[path][treemodel.REVID]
253
parents = self.model[path][treemodel.PARENTS]
255
if len(parents) == 0:
258
parent_id = parents[0]
260
self.show_diff(self.branch, revision_id, parent_id)
261
self.treeview.grab_focus()