/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz

« back to all changes in this revision

Viewing changes to viz/branchwin.py

  • Committer: Daniel Schierbeck
  • Date: 2007-10-14 01:02:38 UTC
  • Revision ID: daniel.schierbeck@gmail.com-20071014010238-6axx7nks1zxds7s8
Made the diff viewer close on Ctrl-W and Ctrl-Q.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: UTF-8 -*-
 
2
"""Branch window.
 
3
 
 
4
This module contains the code to manage the branch information window,
 
5
which contains both the revision graph and details panes.
 
6
"""
 
7
 
 
8
__copyright__ = "Copyright © 2005 Canonical Ltd."
 
9
__author__    = "Scott James Remnant <scott@ubuntu.com>"
 
10
 
 
11
 
 
12
import gtk
 
13
import gobject
 
14
import pango
 
15
import treemodel
 
16
 
 
17
from bzrlib.osutils import format_date
 
18
 
 
19
from linegraph import linegraph, same_branch
 
20
from graphcell import CellRendererGraph
 
21
from treemodel import TreeModel
 
22
 
 
23
 
 
24
class BranchWindow(gtk.Window):
 
25
    """Branch window.
 
26
 
 
27
    This object represents and manages a single window containing information
 
28
    for a particular branch.
 
29
    """
 
30
 
 
31
    def __init__(self, parent=None):
 
32
        gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
 
33
        self.set_border_width(0)
 
34
        self.set_title("Revision history")
 
35
 
 
36
        self._parent = parent
 
37
 
 
38
        self.connect('key-press-event', self._on_key_pressed)
 
39
 
 
40
        # Use three-quarters of the screen by default
 
41
        screen = self.get_screen()
 
42
        monitor = screen.get_monitor_geometry(0)
 
43
        width = int(monitor.width * 0.75)
 
44
        height = int(monitor.height * 0.75)
 
45
        self.set_default_size(width, height)
 
46
 
 
47
        # FIXME AndyFitz!
 
48
        icon = self.render_icon(gtk.STOCK_INDEX, gtk.ICON_SIZE_BUTTON)
 
49
        self.set_icon(icon)
 
50
 
 
51
        self.accel_group = gtk.AccelGroup()
 
52
        self.add_accel_group(self.accel_group)
 
53
 
 
54
        self.construct()
 
55
 
 
56
    def construct(self):
 
57
        """Construct the window contents."""
 
58
        vbox = gtk.VBox(spacing=0)
 
59
        self.add(vbox)
 
60
 
 
61
        vbox.pack_start(self.construct_navigation(), expand=False, fill=True)
 
62
        vbox.pack_start(self.construct_loading_msg(), expand=False, fill=True)
 
63
        
 
64
        paned = gtk.VPaned()
 
65
        paned.pack1(self.construct_top(), resize=True, shrink=False)
 
66
        paned.pack2(self.construct_bottom(), resize=False, shrink=True)
 
67
        paned.show()
 
68
        vbox.pack_start(paned, expand=True, fill=True)
 
69
        vbox.set_focus_child(paned)
 
70
 
 
71
        vbox.show()
 
72
    
 
73
    def construct_loading_msg(self):
 
74
        image_loading = gtk.image_new_from_stock(gtk.STOCK_REFRESH,
 
75
                                                 gtk.ICON_SIZE_BUTTON)
 
76
        image_loading.show()
 
77
        
 
78
        label_loading = gtk.Label(_("Please wait, loading ancestral graph..."))
 
79
        label_loading.set_alignment(0.0, 0.5)        
 
80
        label_loading.show()
 
81
        
 
82
        self.loading_msg_box = gtk.HBox()
 
83
        self.loading_msg_box.set_spacing(5)
 
84
        self.loading_msg_box.set_border_width(5)        
 
85
        self.loading_msg_box.pack_start(image_loading, False, False)
 
86
        self.loading_msg_box.pack_start(label_loading, True, True)
 
87
        self.loading_msg_box.show()
 
88
        
 
89
        return self.loading_msg_box
 
90
 
 
91
    def construct_top(self):
 
92
        """Construct the top-half of the window."""
 
93
        scrollwin = gtk.ScrolledWindow()
 
94
        scrollwin.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
 
95
        scrollwin.set_shadow_type(gtk.SHADOW_IN)
 
96
        scrollwin.show()
 
97
 
 
98
        self.treeview = gtk.TreeView()
 
99
        self.treeview.set_rules_hint(True)
 
100
        self.treeview.set_search_column(4)
 
101
        self.treeview.get_selection().connect("changed", self._treeselection_changed_cb)
 
102
        self.treeview.connect("row-activated", self._treeview_row_activated_cb)
 
103
        self.treeview.connect("button-release-event", 
 
104
                self._treeview_row_mouseclick)
 
105
        self.treeview.set_property('fixed-height-mode', True)
 
106
        scrollwin.add(self.treeview)
 
107
        self.treeview.show()
 
108
 
 
109
        cell = gtk.CellRendererText()
 
110
        cell.set_property("width-chars", 15)
 
111
        cell.set_property("ellipsize", pango.ELLIPSIZE_END)
 
112
        column = gtk.TreeViewColumn("Revision No")
 
113
        column.set_resizable(True)
 
114
        column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
 
115
        column.set_fixed_width(cell.get_size(self.treeview)[2])
 
116
        column.pack_start(cell, expand=True)
 
117
        column.add_attribute(cell, "text", treemodel.REVNO)
 
118
        self.treeview.append_column(column)
 
119
 
 
120
        self.graph_cell = CellRendererGraph()
 
121
        self.graph_column = gtk.TreeViewColumn()
 
122
        self.graph_column.set_resizable(True)
 
123
        self.graph_column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
 
124
        self.graph_column.pack_start(self.graph_cell, expand=False)
 
125
        self.graph_column.add_attribute(self.graph_cell, "node", treemodel.NODE)
 
126
        self.graph_column.add_attribute(self.graph_cell, "in-lines", treemodel.LAST_LINES)
 
127
        self.graph_column.add_attribute(self.graph_cell, "out-lines", treemodel.LINES)
 
128
        self.treeview.append_column(self.graph_column)
 
129
 
 
130
        cell = gtk.CellRendererText()
 
131
        cell.set_property("width-chars", 65)
 
132
        cell.set_property("ellipsize", pango.ELLIPSIZE_END)
 
133
        column = gtk.TreeViewColumn("Message")
 
134
        column.set_resizable(True)
 
135
        column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
 
136
        column.set_fixed_width(cell.get_size(self.treeview)[2])
 
137
        column.pack_start(cell, expand=True)
 
138
        column.add_attribute(cell, "text", treemodel.MESSAGE)
 
139
        self.treeview.append_column(column)
 
140
 
 
141
        cell = gtk.CellRendererText()
 
142
        cell.set_property("width-chars", 15)
 
143
        cell.set_property("ellipsize", pango.ELLIPSIZE_END)
 
144
        column = gtk.TreeViewColumn("Committer")
 
145
        column.set_resizable(True)
 
146
        column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
 
147
        column.set_fixed_width(cell.get_size(self.treeview)[2])
 
148
        column.pack_start(cell, expand=True)
 
149
        column.add_attribute(cell, "text", treemodel.COMMITER)
 
150
        self.treeview.append_column(column)
 
151
 
 
152
        return scrollwin
 
153
 
 
154
    def construct_navigation(self):
 
155
        """Construct the navigation buttons."""
 
156
        frame = gtk.Frame()
 
157
        frame.set_shadow_type(gtk.SHADOW_OUT)
 
158
        frame.show()
 
159
        
 
160
        hbox = gtk.HBox(spacing=12)
 
161
        frame.add(hbox)
 
162
        hbox.show()
 
163
 
 
164
        self.back_button = gtk.Button(stock=gtk.STOCK_GO_BACK)
 
165
        self.back_button.set_relief(gtk.RELIEF_NONE)
 
166
        self.back_button.add_accelerator("clicked", self.accel_group, ord('['),
 
167
                                         0, 0)
 
168
        self.back_button.connect("clicked", self._back_clicked_cb)
 
169
        hbox.pack_start(self.back_button, expand=False, fill=True)
 
170
        self.back_button.show()
 
171
 
 
172
        self.fwd_button = gtk.Button(stock=gtk.STOCK_GO_FORWARD)
 
173
        self.fwd_button.set_relief(gtk.RELIEF_NONE)
 
174
        self.fwd_button.add_accelerator("clicked", self.accel_group, ord(']'),
 
175
                                        0, 0)
 
176
        self.fwd_button.connect("clicked", self._fwd_clicked_cb)
 
177
        hbox.pack_start(self.fwd_button, expand=False, fill=True)
 
178
        self.fwd_button.show()
 
179
 
 
180
        return frame
 
181
 
 
182
    def construct_bottom(self):
 
183
        """Construct the bottom half of the window."""
 
184
        from bzrlib.plugins.gtk.logview import LogView
 
185
        self.logview = LogView(None, True, [], True)
 
186
        (width, height) = self.get_size()
 
187
        self.logview.set_size_request(width, int(height / 2.5))
 
188
        self.logview.show()
 
189
        self.logview.set_show_callback(self._show_clicked_cb)
 
190
        self.logview.set_go_callback(self._go_clicked_cb)
 
191
        return self.logview
 
192
 
 
193
    def set_branch(self, branch, start, maxnum):
 
194
        """Set the branch and start position for this window.
 
195
 
 
196
        Creates a new TreeModel and populates it with information about
 
197
        the new branch before updating the window title and model of the
 
198
        treeview itself.
 
199
        """
 
200
        self.branch = branch
 
201
        self.set_title(branch.nick + " - revision history")
 
202
        gobject.idle_add(self.populate_model, start, maxnum)
 
203
 
 
204
    def populate_model(self, start, maxnum):
 
205
        (linegraphdata, index, columns_len) = linegraph(self.branch,
 
206
                                                        start,
 
207
                                                        maxnum)
 
208
        self.model = TreeModel(self.branch, linegraphdata)
 
209
        self.graph_cell.columns_len = columns_len
 
210
        width = self.graph_cell.get_size(self.treeview)[2]
 
211
        self.graph_column.set_fixed_width(width)
 
212
        self.graph_column.set_max_width(width)
 
213
        self.index = index
 
214
        self.treeview.set_model(self.model)
 
215
        self.treeview.get_selection().select_path(0)
 
216
        self.loading_msg_box.hide()
 
217
        return False
 
218
    
 
219
    def _on_key_pressed(self, widget, event):
 
220
        """ Key press event handler. """
 
221
        keyname = gtk.gdk.keyval_name(event.keyval)
 
222
        func = getattr(self, '_on_key_press_' + keyname, None)
 
223
        if func:
 
224
            return func(event)
 
225
 
 
226
    def _on_key_press_w(self, event):
 
227
        if event.state & gtk.gdk.CONTROL_MASK:
 
228
            self.destroy()
 
229
            if self._parent is None:
 
230
                gtk.main_quit()
 
231
 
 
232
    def _on_key_press_q(self, event):
 
233
        if event.state & gtk.gdk.CONTROL_MASK:
 
234
            gtk.main_quit()
 
235
    
 
236
    def _treeselection_changed_cb(self, selection, *args):
 
237
        """Callback for when the treeview changes."""
 
238
        (model, selected_rows) = selection.get_selected_rows()
 
239
        if len(selected_rows) > 0:
 
240
            iter = self.model.get_iter(selected_rows[0])
 
241
            revision = self.model.get_value(iter, treemodel.REVISION)
 
242
            parents = self.model.get_value(iter, treemodel.PARENTS)
 
243
            children = self.model.get_value(iter, treemodel.CHILDREN)
 
244
            
 
245
            self.back_button.set_sensitive(len(parents) > 0)
 
246
            self.fwd_button.set_sensitive(len(children) > 0)
 
247
            tags = []
 
248
            if self.branch.supports_tags():
 
249
                tagdict = self.branch.tags.get_reverse_tag_dict()
 
250
                if tagdict.has_key(revision.revision_id):
 
251
                    tags = tagdict[revision.revision_id]
 
252
            self.logview.set_revision(revision, tags, children)
 
253
 
 
254
    def _back_clicked_cb(self, *args):
 
255
        """Callback for when the back button is clicked."""
 
256
        (path, col) = self.treeview.get_cursor()
 
257
        revision = self.model[path][0]
 
258
        parents = self.model[path][4]
 
259
        if not len(parents):
 
260
            return
 
261
 
 
262
        for parent_id in parents:
 
263
            parent = self.revisions[self.index[parent_id]]
 
264
            if same_branch(revision, parent):
 
265
                self.treeview.set_cursor(self.index[parent_id])
 
266
                break
 
267
        else:
 
268
            self.treeview.set_cursor(self.index[parents[0]])
 
269
        self.treeview.grab_focus()
 
270
 
 
271
    def _fwd_clicked_cb(self, *args):
 
272
        """Callback for when the forward button is clicked."""
 
273
        (path, col) = self.treeview.get_cursor()
 
274
        revision = self.model[path][0]
 
275
        children = self.model[path][5]
 
276
        if not len(children):
 
277
            return
 
278
 
 
279
        for child_id in children:
 
280
            child = self.revisions[self.index[child_id]]
 
281
            if same_branch(child, revision):
 
282
                self.treeview.set_cursor(self.index[child_id])
 
283
                break
 
284
        else:
 
285
            self.treeview.set_cursor(self.index[children[0]])
 
286
        self.treeview.grab_focus()
 
287
 
 
288
    def _go_clicked_cb(self, revid):
 
289
        """Callback for when the go button for a parent is clicked."""
 
290
        self.treeview.set_cursor(self.index[revid])
 
291
        self.treeview.grab_focus()
 
292
 
 
293
    def show_diff(self, branch, revid, parentid):
 
294
        """Open a new window to show a diff between the given revisions."""
 
295
        from bzrlib.plugins.gtk.diff import DiffWindow
 
296
        window = DiffWindow(parent=self)
 
297
        (parent_tree, rev_tree) = branch.repository.revision_trees([parentid, 
 
298
                                                                   revid])
 
299
        description = revid + " - " + branch.nick
 
300
        window.set_diff(description, rev_tree, parent_tree)
 
301
        window.show()
 
302
 
 
303
    def _show_clicked_cb(self, revid, parentid):
 
304
        """Callback for when the show button for a parent is clicked."""
 
305
        self.show_diff(self.branch, revid, parentid)
 
306
        self.treeview.grab_focus()
 
307
 
 
308
    def _treeview_row_mouseclick(self, widget, event):
 
309
        from bzrlib.plugins.gtk.revisionmenu import RevisionPopupMenu
 
310
        if event.button == 3:
 
311
            menu = RevisionPopupMenu(self.branch.repository, 
 
312
                [x.revision_id for x in self.selected_revisions()],
 
313
                self.branch)
 
314
            menu.popup(None, None, None, event.button, event.get_time())
 
315
 
 
316
    def selected_revision(self, path):
 
317
        return self.model[path][treemodel.REVISION]
 
318
 
 
319
    def selected_revisions(self):
 
320
        return [self.selected_revision(path) for path in \
 
321
                self.treeview.get_selection().get_selected_rows()[1]]
 
322
 
 
323
    def _treeview_row_activated_cb(self, widget, path, col):
 
324
        # TODO: more than one parent
 
325
        """Callback for when a treeview row gets activated."""
 
326
        revision_id = self.model[path][treemodel.REVID]
 
327
        parents = self.model[path][treemodel.PARENTS]
 
328
        if len(parents) == 0:
 
329
            # Ignore revisions without parent
 
330
            return
 
331
        parent_id = parents[0]
 
332
        self.show_diff(self.branch, revision_id, parent_id)
 
333
        self.treeview.grab_focus()