Kiln » TortoiseHg » TortoiseHg
Clone URL:  
treeview.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
# treeview.py - changelog viewer implementation # # Copyright 2008 Steve Borho <steve@borho.org> # # This software may be used and distributed according to the terms of the # GNU General Public License version 2, incorporated herein by reference. ''' Mercurial revision DAG visualization library Implements a gtk.TreeModel which visualizes a Mercurial repository revision history. Portions of this code stolen mercilessly from bzr-gtk visualization dialog. Other portions stolen from graphlog extension. ''' import gtk import gobject import pango import os import time from tortoisehg.util.i18n import _ from tortoisehg.util import hglib from tortoisehg.hgtk.logview import treemodel from tortoisehg.hgtk.logview.graphcell import CellRendererGraph from tortoisehg.hgtk.logview.revgraph import * COLS = 'graph rev id revhex branch changes msg user date utc age tag' class TreeView(gtk.ScrolledWindow): __gproperties__ = { 'repo': (gobject.TYPE_PYOBJECT, 'Repository', 'The Mercurial repository being visualized', gobject.PARAM_CONSTRUCT_ONLY | gobject.PARAM_WRITABLE), 'limit': (gobject.TYPE_PYOBJECT, 'Revision Display Limit', 'The maximum number of revisions to display', gobject.PARAM_READWRITE), 'original-tip-revision': (gobject.TYPE_PYOBJECT, 'Tip revision when application opened', 'Revisions above this number will be drawn green', gobject.PARAM_READWRITE), 'msg-column-visible': (gobject.TYPE_BOOLEAN, 'Summary', 'Show summary column', True, gobject.PARAM_READWRITE), 'user-column-visible': (gobject.TYPE_BOOLEAN, 'User', 'Show user column', True, gobject.PARAM_READWRITE), 'date-column-visible': (gobject.TYPE_BOOLEAN, 'Date', 'Show date column', False, gobject.PARAM_READWRITE), 'utc-column-visible': (gobject.TYPE_BOOLEAN, 'UTC', 'Show UTC/GMT date column', False, gobject.PARAM_READWRITE), 'age-column-visible': (gobject.TYPE_BOOLEAN, 'Age', 'Show age column', False, gobject.PARAM_READWRITE), 'rev-column-visible': (gobject.TYPE_BOOLEAN, 'Rev', 'Show revision number column', False, gobject.PARAM_READWRITE), 'id-column-visible': (gobject.TYPE_BOOLEAN, 'ID', 'Show revision ID column', False, gobject.PARAM_READWRITE), 'revhex-column-visible': (gobject.TYPE_BOOLEAN, 'Rev/ID', 'Show revision number/ID column', False, gobject.PARAM_READWRITE), 'branch-column-visible': (gobject.TYPE_BOOLEAN, 'Branch', 'Show branch', False, gobject.PARAM_READWRITE), 'changes-column-visible': (gobject.TYPE_BOOLEAN, 'Changes', 'Show changes column', False, gobject.PARAM_READWRITE), 'tag-column-visible': (gobject.TYPE_BOOLEAN, 'Tags', 'Show tag column', False, gobject.PARAM_READWRITE), 'branch-color': (gobject.TYPE_BOOLEAN, 'Branch color', 'Color by branch', False, gobject.PARAM_READWRITE) } __gsignals__ = { 'revisions-loaded': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ()), 'batch-loaded': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ()), 'revision-selected': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ()) } def __init__(self, repo, limit=500, stbar=None): """Create a new TreeView. :param repo: Repository object to show """ gtk.ScrolledWindow.__init__(self) self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) self.set_shadow_type(gtk.SHADOW_IN) self.batchsize = limit self.repo = repo self.currevid = None self.stbar = stbar self.grapher = None self.graphdata = [] self.index = {} self.opts = { 'outgoing':[], 'orig-tip':None, 'npreviews':0, 'branch-color':False, 'show-graph':True } self.construct_treeview() def set_repo(self, repo, stbar=None): self.repo = repo self.stbar = stbar def search_in_tree(self, model, column, key, iter, data): """Searches all fields shown in the tree when the user hits crtr+f, not just the ones that are set via tree.set_search_column. Case insensitive """ key = key.lower() row = model[iter] if row[treemodel.HEXID].startswith(key): return False for col in (treemodel.REVID, treemodel.COMMITER, treemodel.MESSAGE): if key in str(row[col]).lower(): return False return True def create_log_generator(self, graphcol, pats, opts): if self.repo is None: self.grapher = None return only_branch = opts.get('branch', None) if opts.get('filehist') is not None: self.grapher = filelog_grapher(self.repo, opts['filehist']) elif graphcol: end = 0 if only_branch is not None: b = self.repo.branchtags() if only_branch in b: node = b[only_branch] start = self.repo.changelog.rev(node) else: start = len(self.repo.changelog) - 1 elif opts.get('revrange'): if len(opts['revrange']) >= 2: start, end = opts['revrange'] else: start = opts['revrange'][0] end = start else: start = len(self.repo.changelog) - 1 noheads = opts.get('noheads', False) if opts.get('branch-view', False): self.grapher = branch_grapher(self.repo, start, end, only_branch, self.opts.get('branch-color')) else: self.grapher = revision_grapher(self.repo, start, end, only_branch, noheads, self.opts.get('branch-color')) elif opts.get('revlist', None): self.grapher = dumb_log_generator(self.repo, opts['revlist']) else: self.grapher = filtered_log_generator(self.repo, pats, opts) self.opts['show-graph'] = graphcol self.graphdata = [] self.index = {} self.max_cols = 1 self.model = None self.limit = self.batchsize def populate(self, revid=None): 'Fill the treeview with contents' stopped = False if self.repo is None: stopped = True return False if os.name == "nt": timer = time.clock else: timer = time.time startsec = timer() try: while (not self.limit) or len(self.graphdata) < self.limit: (rev, node, lines, wfile) = self.grapher.next() self.max_cols = max(self.max_cols, len(lines)) self.index[rev] = len(self.graphdata) self.graphdata.append( (rev, node, lines, wfile) ) if self.model: rowref = self.model.get_iter(len(self.graphdata)-1) path = self.model.get_path(rowref) self.model.row_inserted(path, rowref) cursec = timer() if cursec < startsec or cursec > startsec + 0.1: break except StopIteration: stopped = True if stopped: pass elif self.limit is None: return True elif len(self.graphdata) < self.limit: return True if not len(self.graphdata): self.treeview.set_model(None) if self.stbar is not None: self.stbar.end() self.emit('revisions-loaded') return False self.graph_cell.columns_len = self.max_cols width = self.graph_cell.get_size(self.treeview)[2] if width > 500: width = 500 gcol = self.tvcolumns['graph'] gcol.set_fixed_width(width) gcol.set_visible(self.opts.get('show-graph')) if not self.model: model = treemodel.TreeModel(self.repo, self.graphdata, self.opts) self.treeview.set_model(model) self.model = model self.emit('batch-loaded') if stopped: self.emit('revisions-loaded') if revid is not None: self.set_revision_id(revid) if self.stbar is not None: self.stbar.end() revision_text = _('%(count)d of %(total)d Revisions') % { 'count': len(self.model), 'total': len(self.repo) } self.stbar.set_text(revision_text, name='rev') return False def do_get_property(self, property): pn = property.name cv = '-column-visible' if pn.endswith(cv): colname = pn[:-len(cv)] return self.tvcolumns[colname].get_visible() elif pn == 'branch-color': return self.opts.get('branch-color') elif pn == 'repo': return self.repo elif pn == 'limit': return self.limit else: raise AttributeError, 'unknown property %s' % pn def do_set_property(self, property, value): pn = property.name cv = '-column-visible' if pn.endswith(cv): colname = pn[:-len(cv)] self.tvcolumns[colname].set_visible(value) elif pn == 'branch-color': self.opts['branch-color'] = value elif pn == 'repo': self.repo = value elif pn == 'limit': self.batchsize = value else: raise AttributeError, 'unknown property %s' % pn def get_revid_at_path(self, path): return self.model[path][treemodel.REVID] def get_path_at_revid(self, revid): if revid in self.index: row_index = self.index[revid] iter = self.model.get_iter(row_index) path = self.model.get_path(iter) return path else: return None def get_wfile_at_path(self, path): if self.model: return self.model[path][treemodel.WFILE] else: return None def next_revision_batch(self, size): if not self.grapher: self.emit('revisions-loaded') return self.batchsize = size self.limit += self.batchsize if self.stbar is not None: self.stbar.begin() gobject.idle_add(self.populate) def load_all_revisions(self): if not self.grapher: self.emit('revisions-loaded') return self.limit = None if self.stbar is not None: self.stbar.begin() gobject.idle_add(self.populate) def scroll_to_revision(self, revid): if revid in self.index: row = self.index[revid] self.treeview.scroll_to_cell(row, use_align=True, row_align=0.5) def set_revision_id(self, revid, load=False): """Change the currently selected revision. :param revid: Revision id of revision to display. """ if revid in self.index: row = self.index[revid] self.treeview.set_cursor(row) self.treeview.grab_focus() elif load: handler = None def loaded(dummy): if revid in self.index: if handler is not None: self.disconnect(handler) self.set_revision_id(revid) self.scroll_to_revision(revid) else: self.next_revision_batch(self.batchsize) handler = self.connect('batch-loaded', loaded) self.next_revision_batch(self.batchsize) def refresh(self, graphcol, pats, opts): self.opts.update(opts) if self.repo is not None: hglib.invalidaterepo(self.repo) if len(self.repo) > 0: self.create_log_generator(graphcol, pats, opts) if self.stbar is not None: self.stbar.begin() gobject.idle_add(self.populate, self.currevid) else: self.treeview.set_model(None) self.stbar.set_text(_('Repository is empty')) def construct_treeview(self): self.treeview = gtk.TreeView() self.treeview.set_rules_hint(True) self.treeview.set_reorderable(False) self.treeview.set_enable_search(True) self.treeview.set_search_equal_func(self.search_in_tree, None) self.treeview.get_selection().set_mode(gtk.SELECTION_SINGLE) self.treeview.connect("cursor-changed", self._on_selection_changed) self.treeview.set_property('fixed-height-mode', True) self.treeview.show() self.add(self.treeview) self.tvcolumns = {} self.graph_cell = CellRendererGraph() col = self.tvcolumns['graph'] = gtk.TreeViewColumn(_('Graph')) col.set_resizable(True) col.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED) col.pack_start(self.graph_cell, expand=False) col.add_attribute(self.graph_cell, "node", treemodel.GRAPHNODE) col.add_attribute(self.graph_cell, "in-lines", treemodel.LAST_LINES) col.add_attribute(self.graph_cell, "out-lines", treemodel.LINES) cell = gtk.CellRendererText() cell.set_property("width-chars", 8) cell.set_property("ellipsize", pango.ELLIPSIZE_END) cell.set_property("xalign", 1.0) col = self.tvcolumns['rev'] = gtk.TreeViewColumn(_('Rev')) col.set_visible(False) col.set_resizable(True) col.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED) col.set_fixed_width(cell.get_size(self.treeview)[2]) col.pack_start(cell, expand=True) col.add_attribute(cell, "text", treemodel.REVID) col.add_attribute(cell, "foreground", treemodel.FGCOLOR) cell = gtk.CellRendererText() cell.set_property("width-chars", 15) cell.set_property("ellipsize", pango.ELLIPSIZE_END) cell.set_property("family", "Monospace") col = self.tvcolumns['id'] = gtk.TreeViewColumn(_('ID')) col.set_visible(False) col.set_resizable(True) col.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED) col.set_fixed_width(cell.get_size(self.treeview)[2]) col.pack_start(cell, expand=True) col.add_attribute(cell, "text", treemodel.HEXID) col.add_attribute(cell, "foreground", treemodel.FGCOLOR) cell = gtk.CellRendererText() cell.set_property("width-chars", 22) cell.set_property("ellipsize", pango.ELLIPSIZE_END) col = self.tvcolumns['revhex'] = gtk.TreeViewColumn(_('Rev/ID')) col.set_visible(False) col.set_resizable(True) col.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED) col.set_fixed_width(cell.get_size(self.treeview)[2]) col.pack_start(cell, expand=True) col.add_attribute(cell, "foreground", treemodel.FGCOLOR) col.add_attribute(cell, "markup", treemodel.REVHEX) cell = gtk.CellRendererText() cell.set_property("width-chars", 15) cell.set_property("ellipsize", pango.ELLIPSIZE_END) col = self.tvcolumns['branch'] = gtk.TreeViewColumn(_('Branch')) col.set_visible(False) col.set_resizable(True) col.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED) col.set_fixed_width(cell.get_size(self.treeview)[2]) col.pack_start(cell, expand=True) col.add_attribute(cell, "text", treemodel.BRANCH) col.add_attribute(cell, "foreground", treemodel.FGCOLOR) cell = gtk.CellRendererText() cell.set_property("width-chars", 10) cell.set_property("ellipsize", pango.ELLIPSIZE_END) col = self.tvcolumns['changes'] = gtk.TreeViewColumn(_('Changes')) col.set_visible(False) col.set_resizable(True) col.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED) col.set_fixed_width(cell.get_size(self.treeview)[2]) col.pack_start(cell, expand=True) col.add_attribute(cell, "markup", treemodel.CHANGES) cell = gtk.CellRendererText() cell.set_property("width-chars", 80) cell.set_property("ellipsize", pango.ELLIPSIZE_END) col = self.tvcolumns['msg'] = gtk.TreeViewColumn(_('Summary')) col.set_resizable(True) col.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED) col.set_fixed_width(cell.get_size(self.treeview)[2]) col.pack_end(cell, expand=True) col.add_attribute(cell, "foreground", treemodel.FGCOLOR) col.add_attribute(cell, "markup", treemodel.MESSAGE) cell = gtk.CellRendererText() cell.set_property("width-chars", 20) cell.set_property("ellipsize", pango.ELLIPSIZE_END) col = self.tvcolumns['user'] = gtk.TreeViewColumn(_('User')) col.set_resizable(True) col.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED) col.set_fixed_width(cell.get_size(self.treeview)[2]) col.pack_start(cell, expand=True) col.add_attribute(cell, "text", treemodel.COMMITER) col.add_attribute(cell, "foreground", treemodel.FGCOLOR) cell = gtk.CellRendererText() cell.set_property("width-chars", 20) cell.set_property("ellipsize", pango.ELLIPSIZE_END) col = self.tvcolumns['date'] = gtk.TreeViewColumn(_('Local Date')) col.set_visible(False) col.set_resizable(True) col.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED) col.set_fixed_width(cell.get_size(self.treeview)[2]) col.pack_start(cell, expand=True) col.add_attribute(cell, "text", treemodel.LOCALTIME) col.add_attribute(cell, "foreground", treemodel.FGCOLOR) cell = gtk.CellRendererText() cell.set_property("width-chars", 20) cell.set_property("ellipsize", pango.ELLIPSIZE_END) col = self.tvcolumns['utc'] = gtk.TreeViewColumn(_('Universal Date')) col.set_visible(False) col.set_resizable(True) col.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED) col.set_fixed_width(cell.get_size(self.treeview)[2]) col.pack_start(cell, expand=True) col.add_attribute(cell, "text", treemodel.UTC) col.add_attribute(cell, "foreground", treemodel.FGCOLOR) cell = gtk.CellRendererText() cell.set_property("width-chars", 16) cell.set_property("ellipsize", pango.ELLIPSIZE_END) col = self.tvcolumns['age'] = gtk.TreeViewColumn(_('Age')) col.set_visible(True) col.set_resizable(True) col.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED) col.set_fixed_width(cell.get_size(self.treeview)[2]) col.pack_start(cell, expand=True) col.add_attribute(cell, "text", treemodel.AGE) col.add_attribute(cell, "foreground", treemodel.FGCOLOR) cell = gtk.CellRendererText() cell.set_property("width-chars", 10) cell.set_property("ellipsize", pango.ELLIPSIZE_END) col = self.tvcolumns['tag'] = gtk.TreeViewColumn(_('Tags')) col.set_visible(False) col.set_resizable(True) col.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED) col.set_fixed_width(cell.get_size(self.treeview)[2]) col.pack_start(cell, expand=True) col.add_attribute(cell, "text", treemodel.TAGS) col.add_attribute(cell, "foreground", treemodel.FGCOLOR) self.columns = COLS.split() def set_columns(self, columns): if ' '.join(columns) != ' '.join(self.columns): cols = self.treeview.get_columns() for cn in self.columns: c = self.tvcolumns[cn] if c in cols: self.treeview.remove_column(c) for cn in columns: try: c = self.tvcolumns[cn] self.treeview.append_column(c) except KeyError: continue self.columns = columns def get_columns(self): return self.columns def _on_selection_changed(self, treeview): """callback for when the treeview changes.""" (path, focus) = treeview.get_cursor() if path and self.model: self.currevid = self.model[path][treemodel.REVID] self.emit('revision-selected')