/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 branchwin.py

  • Committer: Szilveszter Farkas (Phanatic)
  • Date: 2006-07-16 16:27:59 UTC
  • mto: (0.14.1 main) (93.1.1 win32.bialix)
  • mto: This revision was merged to the branch mainline in revision 83.
  • Revision ID: Szilveszter.Farkas@gmail.com-20060716162759-f3e8596921edc374
2006-07-16  Szilveszter Farkas <Szilveszter.Farkas@gmail.com>

    * Done some directory reorganization.
    * setup.py: added some basic install script
    * Began to implement the GTK UI.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
# -*- coding: UTF-8 -*-
3
 
"""Branch window.
4
 
 
5
 
This module contains the code to manage the branch information window,
6
 
which contains both the revision graph and details panes.
7
 
"""
8
 
 
9
 
__copyright__ = "Copyright © 2005 Canonical Ltd."
10
 
__author__    = "Scott James Remnant <scott@ubuntu.com>"
11
 
 
12
 
 
13
 
import os
14
 
 
15
 
import gtk
16
 
import gobject
17
 
import pango
18
 
 
19
 
from bzrlib.osutils import format_date
20
 
 
21
 
from graph import distances, graph, same_branch
22
 
from graphcell import CellRendererGraph
23
 
 
24
 
 
25
 
class BranchWindow(gtk.Window):
26
 
    """Branch window.
27
 
 
28
 
    This object represents and manages a single window containing information
29
 
    for a particular branch.
30
 
    """
31
 
 
32
 
    def __init__(self):
33
 
        gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
34
 
        self.set_border_width(0)
35
 
        self.set_title("bzrk")
36
 
 
37
 
        # Use three-quarters of the screen by default
38
 
        screen = self.get_screen()
39
 
        monitor = screen.get_monitor_geometry(0)
40
 
        width = int(monitor.width * 0.75)
41
 
        height = int(monitor.height * 0.75)
42
 
        self.set_default_size(width, height)
43
 
 
44
 
        # FIXME AndyFitz!
45
 
        icon = self.render_icon(gtk.STOCK_INDEX, gtk.ICON_SIZE_BUTTON)
46
 
        self.set_icon(icon)
47
 
 
48
 
        self.accel_group = gtk.AccelGroup()
49
 
        self.add_accel_group(self.accel_group)
50
 
 
51
 
        self.construct()
52
 
 
53
 
    def construct(self):
54
 
        """Construct the window contents."""
55
 
        paned = gtk.VPaned()
56
 
        paned.pack1(self.construct_top(), resize=True, shrink=False)
57
 
        paned.pack2(self.construct_bottom(), resize=False, shrink=True)
58
 
        self.add(paned)
59
 
        paned.show()
60
 
 
61
 
    def construct_top(self):
62
 
        """Construct the top-half of the window."""
63
 
        vbox = gtk.VBox(spacing=6)
64
 
        vbox.set_border_width(12)
65
 
        vbox.show()
66
 
 
67
 
        scrollwin = gtk.ScrolledWindow()
68
 
        scrollwin.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
69
 
        scrollwin.set_shadow_type(gtk.SHADOW_IN)
70
 
        vbox.pack_start(scrollwin, expand=True, fill=True)
71
 
        scrollwin.show()
72
 
 
73
 
        self.treeview = gtk.TreeView()
74
 
        self.treeview.set_rules_hint(True)
75
 
        self.treeview.set_search_column(4)
76
 
        self.treeview.connect("cursor-changed", self._treeview_cursor_cb)
77
 
        scrollwin.add(self.treeview)
78
 
        self.treeview.show()
79
 
 
80
 
        cell = CellRendererGraph()
81
 
        column = gtk.TreeViewColumn()
82
 
        column.set_resizable(False)
83
 
        column.pack_start(cell, expand=False)
84
 
        column.add_attribute(cell, "node", 1)
85
 
        column.add_attribute(cell, "in-lines", 2)
86
 
        column.add_attribute(cell, "out-lines", 3)
87
 
        self.treeview.append_column(column)
88
 
 
89
 
        cell = gtk.CellRendererText()
90
 
        cell.set_property("width-chars", 40)
91
 
        cell.set_property("ellipsize", pango.ELLIPSIZE_END)
92
 
        column = gtk.TreeViewColumn("Message")
93
 
        column.set_resizable(True)
94
 
        column.pack_start(cell, expand=True)
95
 
        column.add_attribute(cell, "text", 4)
96
 
        self.treeview.append_column(column)
97
 
 
98
 
        cell = gtk.CellRendererText()
99
 
        cell.set_property("width-chars", 40)
100
 
        cell.set_property("ellipsize", pango.ELLIPSIZE_END)
101
 
        column = gtk.TreeViewColumn("Committer")
102
 
        column.set_resizable(True)
103
 
        column.pack_start(cell, expand=True)
104
 
        column.add_attribute(cell, "text", 5)
105
 
        self.treeview.append_column(column)
106
 
 
107
 
        cell = gtk.CellRendererText()
108
 
        cell.set_property("ellipsize", pango.ELLIPSIZE_END)
109
 
        column = gtk.TreeViewColumn("Date")
110
 
        column.set_resizable(True)
111
 
        column.pack_start(cell, expand=True)
112
 
        column.add_attribute(cell, "text", 6)
113
 
        self.treeview.append_column(column)
114
 
 
115
 
        hbox = gtk.HBox(False, spacing=6)
116
 
        vbox.pack_start(hbox, expand=False, fill=False)
117
 
        hbox.show()
118
 
 
119
 
        self.back_button = gtk.Button(stock=gtk.STOCK_GO_BACK)
120
 
        self.back_button.add_accelerator("clicked", self.accel_group, ord('['),
121
 
                                         0, 0)
122
 
        self.back_button.connect("clicked", self._back_clicked_cb)
123
 
        hbox.pack_start(self.back_button, expand=False, fill=True)
124
 
        self.back_button.show()
125
 
 
126
 
        self.fwd_button = gtk.Button(stock=gtk.STOCK_GO_FORWARD)
127
 
        self.fwd_button.add_accelerator("clicked", self.accel_group, ord(']'),
128
 
                                        0, 0)
129
 
        self.fwd_button.connect("clicked", self._fwd_clicked_cb)
130
 
        hbox.pack_start(self.fwd_button, expand=False, fill=True)
131
 
        self.fwd_button.show()
132
 
 
133
 
        return vbox
134
 
 
135
 
    def construct_bottom(self):
136
 
        """Construct the bottom half of the window."""
137
 
        vbox = gtk.VBox(False, spacing=6)
138
 
        vbox.set_border_width(12)
139
 
        (width, height) = self.get_size()
140
 
        vbox.set_size_request(width, int(height / 2.5))
141
 
        vbox.show()
142
 
 
143
 
        self.table = gtk.Table(rows=4, columns=2)
144
 
        self.table.set_row_spacings(6)
145
 
        self.table.set_col_spacings(6)
146
 
        vbox.pack_start(self.table, expand=False, fill=True)
147
 
        self.table.show()
148
 
 
149
 
        align = gtk.Alignment(0.0, 0.5)
150
 
        label = gtk.Label()
151
 
        label.set_markup("<b>Revision:</b>")
152
 
        align.add(label)
153
 
        self.table.attach(align, 0, 1, 0, 1, gtk.FILL, gtk.FILL)
154
 
        label.show()
155
 
        align.show()
156
 
 
157
 
        align = gtk.Alignment(0.0, 0.5)
158
 
        self.revid_label = gtk.Label()
159
 
        self.revid_label.set_selectable(True)
160
 
        align.add(self.revid_label)
161
 
        self.table.attach(align, 1, 2, 0, 1, gtk.EXPAND | gtk.FILL, gtk.FILL)
162
 
        self.revid_label.show()
163
 
        align.show()
164
 
 
165
 
        align = gtk.Alignment(0.0, 0.5)
166
 
        label = gtk.Label()
167
 
        label.set_markup("<b>Committer:</b>")
168
 
        align.add(label)
169
 
        self.table.attach(align, 0, 1, 1, 2, gtk.FILL, gtk.FILL)
170
 
        label.show()
171
 
        align.show()
172
 
 
173
 
        align = gtk.Alignment(0.0, 0.5)
174
 
        self.committer_label = gtk.Label()
175
 
        self.committer_label.set_selectable(True)
176
 
        align.add(self.committer_label)
177
 
        self.table.attach(align, 1, 2, 1, 2, gtk.EXPAND | gtk.FILL, gtk.FILL)
178
 
        self.committer_label.show()
179
 
        align.show()
180
 
 
181
 
        align = gtk.Alignment(0.0, 0.5)
182
 
        label = gtk.Label()
183
 
        label.set_markup("<b>Timestamp:</b>")
184
 
        align.add(label)
185
 
        self.table.attach(align, 0, 1, 2, 3, gtk.FILL, gtk.FILL)
186
 
        label.show()
187
 
        align.show()
188
 
 
189
 
        align = gtk.Alignment(0.0, 0.5)
190
 
        self.timestamp_label = gtk.Label()
191
 
        self.timestamp_label.set_selectable(True)
192
 
        align.add(self.timestamp_label)
193
 
        self.table.attach(align, 1, 2, 2, 3, gtk.EXPAND | gtk.FILL, gtk.FILL)
194
 
        self.timestamp_label.show()
195
 
        align.show()
196
 
 
197
 
        align = gtk.Alignment(0.0, 0.5)
198
 
        label = gtk.Label()
199
 
        label.set_markup("<b>Parents:</b>")
200
 
        align.add(label)
201
 
        self.table.attach(align, 0, 1, 3, 4, gtk.FILL, gtk.FILL)
202
 
        label.show()
203
 
        align.show()
204
 
 
205
 
        self.parents_widgets = []
206
 
 
207
 
        scrollwin = gtk.ScrolledWindow()
208
 
        scrollwin.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
209
 
        scrollwin.set_shadow_type(gtk.SHADOW_IN)
210
 
        vbox.pack_start(scrollwin, expand=True, fill=True)
211
 
        scrollwin.show()
212
 
 
213
 
        self.message_buffer = gtk.TextBuffer()
214
 
        textview = gtk.TextView(self.message_buffer)
215
 
        textview.set_editable(False)
216
 
        textview.set_wrap_mode(gtk.WRAP_WORD)
217
 
        textview.modify_font(pango.FontDescription("Monospace"))
218
 
        scrollwin.add(textview)
219
 
        textview.show()
220
 
 
221
 
        return vbox
222
 
 
223
 
    def set_branch(self, branch, start):
224
 
        """Set the branch and start position for this window.
225
 
 
226
 
        Creates a new TreeModel and populates it with information about
227
 
        the new branch before updating the window title and model of the
228
 
        treeview itself.
229
 
        """
230
 
        # [ revision, node, last_lines, lines, message, committer, timestamp ]
231
 
        self.model = gtk.ListStore(gobject.TYPE_PYOBJECT,
232
 
                                   gobject.TYPE_PYOBJECT,
233
 
                                   gobject.TYPE_PYOBJECT,
234
 
                                   gobject.TYPE_PYOBJECT,
235
 
                                   str, str, str)
236
 
        self.index = {}
237
 
        index = 0
238
 
 
239
 
        last_lines = []
240
 
        (revids, self.revisions, colours, self.children) \
241
 
                 = distances(branch, start)
242
 
        for revision, node, lines in graph(revids, self.revisions, colours):
243
 
            message = revision.message.split("\n")[0]
244
 
            if revision.committer is not None:
245
 
                timestamp = format_date(revision.timestamp, revision.timezone)
246
 
            else:
247
 
                timestamp = None
248
 
 
249
 
            self.model.append([ revision, node, last_lines, lines,
250
 
                                message, revision.committer, timestamp ])
251
 
            self.index[revision] = index
252
 
            index += 1
253
 
 
254
 
            last_lines = lines
255
 
 
256
 
        self.set_title(os.path.basename(branch.base) + " - bzrk")
257
 
        self.treeview.set_model(self.model)
258
 
 
259
 
    def _treeview_cursor_cb(self, *args):
260
 
        """Callback for when the treeview cursor changes."""
261
 
        (path, col) = self.treeview.get_cursor()
262
 
        revision = self.model[path][0]
263
 
 
264
 
        self.back_button.set_sensitive(len(revision.parent_ids) > 0)
265
 
        self.fwd_button.set_sensitive(len(self.children[revision]) > 0)
266
 
 
267
 
        if revision.committer is not None:
268
 
            committer = revision.committer
269
 
            timestamp = format_date(revision.timestamp, revision.timezone)
270
 
            message = revision.message
271
 
        else:
272
 
            committer = ""
273
 
            timestamp = ""
274
 
            message = ""
275
 
 
276
 
        self.revid_label.set_text(revision.revision_id)
277
 
        self.committer_label.set_text(committer)
278
 
        self.timestamp_label.set_text(timestamp)
279
 
        self.message_buffer.set_text(message)
280
 
 
281
 
        for widget in self.parents_widgets:
282
 
            self.table.remove(widget)
283
 
 
284
 
        self.parents_widgets = []
285
 
        self.table.resize(4 + len(revision.parent_ids) - 1, 2)
286
 
        for idx, parent_id in enumerate(revision.parent_ids):
287
 
            self.table.set_row_spacing(idx + 3, 0)
288
 
 
289
 
            align = gtk.Alignment(0.0, 0.0)
290
 
            self.parents_widgets.append(align)
291
 
            self.table.attach(align, 1, 2, idx + 3, idx + 4,
292
 
                              gtk.EXPAND | gtk.FILL, gtk.FILL)
293
 
            align.show()
294
 
 
295
 
            hbox = gtk.HBox(False, 0)
296
 
            align.add(hbox)
297
 
            hbox.show()
298
 
 
299
 
            label = gtk.Label(parent_id)
300
 
            label.set_selectable(True)
301
 
            hbox.pack_start(label, expand=False, fill=True)
302
 
            label.show()
303
 
 
304
 
            image = gtk.Image()
305
 
            image.set_from_stock(gtk.STOCK_JUMP_TO, gtk.ICON_SIZE_MENU)
306
 
            image.show()
307
 
 
308
 
            button = gtk.Button()
309
 
            button.add(image)
310
 
            button.set_relief(gtk.RELIEF_NONE)
311
 
            button.connect("clicked", self._go_clicked_cb, parent_id)
312
 
            hbox.pack_start(button, expand=False, fill=True)
313
 
            button.show()
314
 
 
315
 
            image = gtk.Image()
316
 
            image.set_from_stock(gtk.STOCK_FIND, gtk.ICON_SIZE_MENU)
317
 
            image.show()
318
 
 
319
 
            button = gtk.Button()
320
 
            button.add(image)
321
 
            button.set_relief(gtk.RELIEF_NONE)
322
 
            button.set_sensitive(False)
323
 
            button.connect("clicked", self._show_clicked_cb, parent_id)
324
 
            hbox.pack_start(button, expand=False, fill=True)
325
 
            button.show()
326
 
 
327
 
    def _back_clicked_cb(self, *args):
328
 
        """Callback for when the back button is clicked."""
329
 
        (path, col) = self.treeview.get_cursor()
330
 
        revision = self.model[path][0]
331
 
        if not len(revision.parent_ids):
332
 
            return
333
 
 
334
 
        for parent_id in revision.parent_ids:
335
 
            parent = self.revisions[parent_id]
336
 
            if same_branch(revision, parent):
337
 
                self.treeview.set_cursor(self.index[parent])
338
 
                break
339
 
        else:
340
 
            next = self.revisions[revision.parent_ids[0]]
341
 
            self.treeview.set_cursor(self.index[next])
342
 
 
343
 
    def _fwd_clicked_cb(self, *args):
344
 
        """Callback for when the forward button is clicked."""
345
 
        (path, col) = self.treeview.get_cursor()
346
 
        revision = self.model[path][0]
347
 
        if not len(self.children[revision]):
348
 
            return
349
 
 
350
 
        for child in self.children[revision]:
351
 
            if same_branch(child, revision):
352
 
                self.treeview.set_cursor(self.index[child])
353
 
                break
354
 
        else:
355
 
            prev = list(self.children[revision])[0]
356
 
            self.treeview.set_cursor(self.index[prev])
357
 
 
358
 
    def _go_clicked_cb(self, widget, revid):
359
 
        """Callback for when the go button for a parent is clicked."""
360
 
        self.treeview.set_cursor(self.index[self.revisions[revid]])
361
 
 
362
 
    def _show_clicked_cb(self, widget, revid):
363
 
        """Callback for when the show button for a parent is clicked."""
364
 
        print "SHOW %s" % revid