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]
140
self.graph_column.set_fixed_width(width)
141
self.graph_column.set_max_width(width)
143
self.treeview.set_model(self.model)
144
self.treeview.set_cursor(0)
145
self.emit('revisions-loaded')
149
def show_diff(self, branch, revid, parentid):
150
"""Open a new window to show a diff between the given revisions."""
151
from bzrlib.plugins.gtk.diff import DiffWindow
152
window = DiffWindow(parent=self)
153
(parent_tree, rev_tree) = branch.repository.revision_trees([parentid,
155
description = revid + " - " + branch.nick
156
window.set_diff(description, rev_tree, parent_tree)
159
def construct_treeview(self):
160
self.treeview = gtk.TreeView()
162
self.treeview.set_rules_hint(True)
163
self.treeview.set_search_column(4)
165
self.treeview.get_selection().connect("changed",
166
self._on_selection_changed)
168
self.treeview.connect("row-activated",
169
self._on_revision_activated)
171
self.treeview.connect("button-release-event",
172
self._on_revision_selected)
174
self.treeview.set_property('fixed-height-mode', True)
176
self.add(self.treeview)
179
cell = gtk.CellRendererText()
180
cell.set_property("width-chars", 15)
181
cell.set_property("ellipsize", pango.ELLIPSIZE_END)
182
column = gtk.TreeViewColumn("Revision No")
183
column.set_resizable(True)
184
column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
185
column.set_fixed_width(cell.get_size(self.treeview)[2])
186
column.pack_start(cell, expand=True)
187
column.add_attribute(cell, "text", treemodel.REVNO)
188
self.treeview.append_column(column)
190
self.graph_cell = CellRendererGraph()
191
self.graph_column = gtk.TreeViewColumn()
192
self.graph_column.set_resizable(True)
193
self.graph_column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
194
self.graph_column.pack_start(self.graph_cell, expand=False)
195
self.graph_column.add_attribute(self.graph_cell, "node", treemodel.NODE)
196
self.graph_column.add_attribute(self.graph_cell, "in-lines", treemodel.LAST_LINES)
197
self.graph_column.add_attribute(self.graph_cell, "out-lines", treemodel.LINES)
198
self.treeview.append_column(self.graph_column)
200
cell = gtk.CellRendererText()
201
cell.set_property("width-chars", 65)
202
cell.set_property("ellipsize", pango.ELLIPSIZE_END)
203
column = gtk.TreeViewColumn("Message")
204
column.set_resizable(True)
205
column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
206
column.set_fixed_width(cell.get_size(self.treeview)[2])
207
column.pack_start(cell, expand=True)
208
column.add_attribute(cell, "text", treemodel.MESSAGE)
209
self.treeview.append_column(column)
211
cell = gtk.CellRendererText()
212
cell.set_property("width-chars", 15)
213
cell.set_property("ellipsize", pango.ELLIPSIZE_END)
214
column = gtk.TreeViewColumn("Committer")
215
column.set_resizable(True)
216
column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
217
column.set_fixed_width(cell.get_size(self.treeview)[2])
218
column.pack_start(cell, expand=True)
219
column.add_attribute(cell, "text", treemodel.COMMITER)
220
self.treeview.append_column(column)
222
def _on_selection_changed(self, selection, *args):
223
"""callback for when the treeview changes."""
224
(model, selected_rows) = selection.get_selected_rows()
225
if len(selected_rows) > 0:
226
iter = self.model.get_iter(selected_rows[0])
227
self.revision = self.model.get_value(iter, treemodel.REVISION)
228
self.parents = self.model.get_value(iter, treemodel.PARENTS)
229
self.children = self.model.get_value(iter, treemodel.CHILDREN)
231
self.emit('revision-selected')
233
def _on_revision_selected(self, widget, event):
234
from bzrlib.plugins.gtk.revisionmenu import RevisionPopupMenu
235
if event.button == 3:
236
menu = RevisionPopupMenu(self.branch.repository,
237
[self.get_revision().revision_id],
239
menu.popup(None, None, None, event.button, event.get_time())
241
def _on_revision_activated(self, widget, path, col):
242
# TODO: more than one parent
243
"""Callback for when a treeview row gets activated."""
244
revision_id = self.model[path][treemodel.REVID]
245
parents = self.model[path][treemodel.PARENTS]
246
if len(parents) == 0:
247
# Ignore revisions without parent
249
parent_id = parents[0]
250
self.show_diff(self.branch, revision_id, parent_id)
251
self.treeview.grab_focus()