Kiln » TortoiseHg » TortoiseHg
Clone URL:  
repoview.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
# 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 PyQt4 import QtCore, QtGui Qt = QtCore.Qt connect = QtCore.QObject.connect SIGNAL = QtCore.SIGNAL from tortoisehg.hgqt import icon as geticon from tortoisehg.hgqt.i18n import _ from tortoisehg.hgqt.quickbar import QuickBar 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'] = QtGui.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 = QtGui.QStringListModel(['tip']) self.completer = QtGui.QCompleter(self.compl_model, self) self.entry = QtGui.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(QtGui.QTableView): """ A QTableView for displaying a HgRepoListModel, with actions, shortcuts, etc. """ def __init__(self, parent=None): QtGui.QTableView.__init__(self, parent) self.init_variables() self.setShowGrid(False) self.verticalHeader().hide() self.verticalHeader().setDefaultSectionSize(20) self.setSelectionMode(QtGui.QAbstractItemView.SingleSelection) self.setSelectionBehavior(QtGui.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 QtGui.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, QtGui.QKeySequence(QtGui.QKeySequence.Back), self.back), ('forward', _('Forward'), 'forward', None, QtGui.QKeySequence(QtGui.QKeySequence.Forward), self.forward), ('manifest', _('Show at rev...'), None, _('Show the manifest at selected revision'), None, self.showAtRev), ('update', _('Update to rev...'), None, None, None, self.updateToRev), ] return a def createActions(self): self._actions = {} for name, desc, icon, tip, key, cb in self._action_defs(): self._actions[name] = QtGui.QAction(desc, self) QtCore.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 contextMenuEvent(self, event): menu = QtGui.QMenu(self) for act in ['update', 'manifest', 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() QtGui.QTableView.setModel(self, model) connect(self.selectionModel(), QtCore.SIGNAL('currentRowChanged (const QModelIndex & , const QModelIndex & )'), self.revisionSelected) self.goto_toolbar.compl_model.setStringList(model.repo.tags().keys()) col = list(model._columns).index('Log') self.horizontalHeader().setResizeMode(col, QtGui.QHeaderView.Stretch) def enableAutoResize(self, *args): self._autoresize = True def disableAutoResize(self, *args): self._autoresize = False QtCore.QTimer.singleShot(100, self.enableAutoResize) def resizeEvent(self, event): # we catch this event to resize smartly tables' columns QtGui.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 = QtGui.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: 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) def nextRev(self): row = self.currentIndex().row() self.setCurrentIndex(self.model().index(min(row+1, self.model().rowCount() - 1), 0)) def prevRev(self): row = self.currentIndex().row() self.setCurrentIndex(self.model().index(max(row - 1, 0), 0))