Kiln » TortoiseHg » TortoiseHg
Clone URL:  
Pushed to one repository · View In Graph Contained in 1.9, 1.9.1, and 1.9.2

repomodel: simplify maxWidthValueForColumn, give graph width

This function is not called often enough to justify a dictionary
of lambda functions. Supply a real width for the revision graph.

Changeset 263c67a126cf

Parent 1cf89f23fd4d

by Steve Borho

Changes to 2 files · Browse files at 263c67a126cf Showing diff from parent 1cf89f23fd4d Diff from another changeset...

 
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
 
252
253
254
255
256
257
258
259
260
261
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
262
263
264
 
86
87
88
 
 
 
 
 
 
 
 
 
 
 
89
90
91
 
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
@@ -86,17 +86,6 @@
  'Filename': lambda ctx, gnode: gnode.extra[0],   }   -# in following lambdas, r is a hg repo -_maxwidth = {'ID': lambda self, r: str(len(r)), - 'Date': lambda self, r: cvrt_date(r[None].date()), - 'Tags': lambda self, r: sorted(r.tags().keys(), - key=lambda x: len(x))[-1][:10], - 'Branch': lambda self, r: sorted(r.branchtags().keys(), - key=lambda x: len(x))[-1], - 'Author': lambda self, r: 'author name', # TODO get actual max - 'Filename': lambda self, r: self.filename, - } -  def datacached(meth):   """   decorator used to cache 'data' method of Qt models. It will *not* @@ -252,13 +241,29 @@
  def columnCount(self, parent=None):   return len(self._columns)   - def maxWidthValueForColumn(self, column): - column = self._columns[column] - try: - if column in _maxwidth: - return _maxwidth[column](self, self.repo) - except IndexError: - pass + def maxWidthValueForColumn(self, col): + column = self._columns[col] + if column == 'ID': + return str(len(self.repo)) + if column == 'Date': + return cvrt_date(self.repo[None].date()) + if column == 'Tags': + try: + return sorted(r.tags().keys(), key=lambda x: len(x))[-1][:10] + except IndexError: + pass + if column == 'Branch': + try: + return sorted(r.branchtags().keys(), key=lambda x: len(x))[-1] + except IndexError: + pass + if column == 'Author': + return 'author name' # TODO get actual max + if column == 'Filename': + return self.filename + if column == 'Graph': + return self.col2x(self.graph.max_cols) + # Fall through for Log   return None     def user_color(self, user):
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
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
 # Copyright (c) 2009-2010 LOGILAB S.A. (Paris, FRANCE).  # http://www.logilab.fr/ -- mailto:contact@logilab.fr  #  # This program is free software; you can redistribute it and/or modify it under  # the terms of the GNU General Public License as published by the Free Software  # Foundation; either version 2 of the License, or (at your option) any later  # version.  #  # This program is distributed in the hope that it will be useful, but WITHOUT  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS  # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.  #  # You should have received a copy of the GNU General Public License along with  # this program; if not, write to the Free Software Foundation, Inc.,  # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.    from mercurial.error import RepoError    from tortoisehg.hgqt.qtlib import geticon  from tortoisehg.hgqt.i18n import _  from tortoisehg.hgqt.quickbar import QuickBar    from PyQt4.QtCore import *  from PyQt4.QtGui import *    connect = QObject.connect      class GotoQuickBar(QuickBar):   def __init__(self, parent):   QuickBar.__init__(self, "Goto", "Ctrl+G", "Goto", parent)     def createActions(self, openkey, desc):   QuickBar.createActions(self, openkey, desc)   self._actions['go'] = QAction("Go", self)   connect(self._actions['go'], SIGNAL('triggered()'),   self.goto)     def goto(self):   self.emit(SIGNAL('goto'), unicode(self.entry.text()))     def createContent(self):   QuickBar.createContent(self)   self.compl_model = QStringListModel(['tip'])   self.completer = QCompleter(self.compl_model, self)   self.entry = QLineEdit(self)   self.entry.setCompleter(self.completer)   self.addWidget(self.entry)   self.addAction(self._actions['go'])     connect(self.entry, SIGNAL('returnPressed()'),   self._actions['go'].trigger)     def setVisible(self, visible=True):   QuickBar.setVisible(self, visible)   if visible:   self.entry.setFocus()   self.entry.selectAll()     def __del__(self):   # prevent a warning in the console:   # QObject::startTimer: QTimer can only be used with threads started with QThread   self.entry.setCompleter(None)    class HgRepoView(QTableView):   """   A QTableView for displaying a HgRepoListModel,   with actions, shortcuts, etc.   """   def __init__(self, parent=None):   QTableView.__init__(self, parent)   self.init_variables()   self.setShowGrid(False)   self.verticalHeader().hide()   self.verticalHeader().setDefaultSectionSize(20)   self.horizontalHeader().setHighlightSections(False)   self.setSelectionMode(QAbstractItemView.SingleSelection)   self.setSelectionBehavior(QAbstractItemView.SelectRows)     self.createActions()   self.createToolbars()   connect(self,   SIGNAL('doubleClicked (const QModelIndex &)'),   self.revisionActivated)     self._autoresize = True   connect(self.horizontalHeader(),   SIGNAL('sectionResized(int, int, int)'),   self.disableAutoResize)     def mousePressEvent(self, event):   index = self.indexAt(event.pos())   if not index.isValid():   return   if event.button() == Qt.MidButton:   self.gotoAncestor(index)   return   QTableView.mousePressEvent(self, event)     def createToolbars(self):   self.goto_toolbar = tb = GotoQuickBar(self)   tb.setObjectName("goto_toolbar")   connect(tb, SIGNAL('goto'), self.goto)     def _action_defs(self):   a = [('back', _('Back'), 'back', None,   QKeySequence(QKeySequence.Back),   self.back),   ('forward', _('Forward'), 'forward', None,   QKeySequence(QKeySequence.Forward),   self.forward),   ('manifest', _('Show at rev...'), None,   _('Show the manifest at selected revision'), None,   self.showAtRev),   ('update', _('Update...'), 'update', None, None,   self.updateToRev),   ('merge', _('Merge with...'), 'merge', None, None,   self.mergeWithRev),   ('tag', _('Tag...'), 'tag', None, None,   self.tagToRev),   ('backout', _('Backout...'), None, None, None,   self.backoutToRev),   ]   return a     def createActions(self):   self._actions = {}   for name, desc, icon, tip, key, cb in self._action_defs():   self._actions[name] = QAction(desc, self)   QTimer.singleShot(0, self.configureActions)     def configureActions(self):   for name, desc, icon, tip, key, cb in self._action_defs():   act = self._actions[name]   if icon:   act.setIcon(geticon(icon))   if tip:   act.setStatusTip(tip)   if key:   act.setShortcut(key)   if cb:   connect(act, SIGNAL('triggered()'), cb)   self.addAction(act)     def showAtRev(self):   self.emit(SIGNAL('revisionActivated'), self.current_rev)     def updateToRev(self):   self.emit(SIGNAL('updateToRevision'), self.current_rev)     def mergeWithRev(self):   self.emit(SIGNAL('mergeWithRevision'), self.current_rev)     def tagToRev(self):   self.emit(SIGNAL('tagToRevision'), self.current_rev)     def backoutToRev(self):   self.emit(SIGNAL('backoutToRevision'), self.current_rev)     def contextMenuEvent(self, event):   menu = QMenu(self)   for act in ['update', 'manifest', 'merge', 'tag', 'backout',   None, 'back', 'forward']:   if act:   menu.addAction(self._actions[act])   else:   menu.addSeparator()   menu.exec_(event.globalPos())     def init_variables(self):   # member variables   self.current_rev = None   # rev navigation history (manage 'back' action)   self._rev_history = []   self._rev_pos = -1   self._in_history = False # flag set when we are "in" the   # history. It is required cause we cannot known, in   # "revision_selected", if we are crating a new branch in the   # history navigation or if we are navigating the history     def setModel(self, model):   self.init_variables()   QTableView.setModel(self, model)   connect(self.selectionModel(),   SIGNAL('currentRowChanged (const QModelIndex & , const QModelIndex & )'),   self.revisionSelected)   self.goto_toolbar.compl_model.setStringList(model.repo.tags().keys())     def enableAutoResize(self, *args):   self._autoresize = True     def disableAutoResize(self, *args):   self._autoresize = False   QTimer.singleShot(100, self.enableAutoResize)     def resizeEvent(self, event):   # we catch this event to resize smartly tables' columns   QTableView.resizeEvent(self, event)   if self._autoresize:   self.resizeColumns()     def resizeColumns(self, *args):   # resize columns the smart way: the column holding Log   # is resized according to the total widget size.   model = self.model()   if not model:   return   col1_width = self.viewport().width()   fontm = QFontMetrics(self.font())   tot_stretch = 0.0   for c in range(model.columnCount()):   if model._columns[c] in model._stretchs:   tot_stretch += model._stretchs[model._columns[c]]   continue   w = model.maxWidthValueForColumn(c) - if w is not None: + if isinstance(w, int): + self.setColumnWidth(c, w) + elif w is not None:   w = fontm.width(unicode(w) + 'w')   self.setColumnWidth(c, w)   else:   w = self.sizeHintForColumn(c)   self.setColumnWidth(c, w)   col1_width -= self.columnWidth(c)   col1_width = max(col1_width, 100)   for c in range(model.columnCount()):   if model._columns[c] in model._stretchs:   w = model._stretchs[model._columns[c]] / tot_stretch   self.setColumnWidth(c, col1_width * w)     def revFromindex(self, index):   if not index.isValid():   return   model = self.model()   if model and model.graph:   row = index.row()   gnode = model.graph[row]   return gnode.rev     def revisionActivated(self, index):   rev = self.revFromindex(index)   if rev is not None:   self.emit(SIGNAL('revisionActivated'), rev)     def revisionSelected(self, index, index_from):   """   Callback called when a revision is selected in the revisions table   """   rev = self.revFromindex(index)   if True:#rev is not None:   model = self.model()   if self.current_rev is not None and self.current_rev == rev:   return   if not self._in_history:   del self._rev_history[self._rev_pos+1:]   self._rev_history.append(rev)   self._rev_pos = len(self._rev_history)-1     self._in_history = False   self.current_rev = rev     self.emit(SIGNAL('revisionSelected'), rev)   self.set_navigation_button_state()     def gotoAncestor(self, index):   rev = self.revFromindex(index)   if rev is not None and self.current_rev is not None:   repo = self.model().repo   ctx = repo[self.current_rev]   ctx2 = repo[rev]   ancestor = ctx.ancestor(ctx2)   self.emit(SIGNAL('showMessage'),   "Goto ancestor of %s and %s"%(ctx.rev(), ctx2.rev()), 2000)   self.goto(ancestor.rev())     def set_navigation_button_state(self):   if len(self._rev_history) > 0:   back = self._rev_pos > 0   forw = self._rev_pos < len(self._rev_history)-1   else:   back = False   forw = False   self._actions['back'].setEnabled(back)   self._actions['forward'].setEnabled(forw)     def back(self):   if self._rev_history and self._rev_pos>0:   self._rev_pos -= 1   idx = self.model().indexFromRev(self._rev_history[self._rev_pos])   if idx is not None:   self._in_history = True   self.setCurrentIndex(idx)   self.set_navigation_button_state()     def forward(self):   if self._rev_history and self._rev_pos<(len(self._rev_history)-1):   self._rev_pos += 1   idx = self.model().indexFromRev(self._rev_history[self._rev_pos])   if idx is not None:   self._in_history = True   self.setCurrentIndex(idx)   self.set_navigation_button_state()     def goto(self, rev):   """   Select revision 'rev' (can be anything understood by repo.changectx())   """   rev = str(rev) # might be a QString   repo = self.model().repo   try:   rev = repo.changectx(rev).rev()   except RepoError:   self.emit(SIGNAL('showMessage'), "Can't find revision '%s'"%rev, 2000)   else:   idx = self.model().indexFromRev(rev)   if idx is not None:   self.goto_toolbar.setVisible(False)   self.setCurrentIndex(idx)