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

cmdui: add Runner class

Changeset 26fabf247f40

Parent 1c99c73d7520

by Yuki KODAMA

Changes to one file · Browse files at 26fabf247f40 Showing diff from parent 1c99c73d7520 Diff from another changeset...

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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
 # cmdui.py - A widget to execute Mercurial command for TortoiseHg  #  # Copyright 2010 Yuki KODAMA <endflow.net@gmail.com>  #  # This software may be used and distributed according to the terms of the  # GNU General Public License version 2, incorporated herein by reference.    from PyQt4.QtCore import *  from PyQt4.QtGui import *    from tortoisehg.util import hglib  from tortoisehg.hgqt.i18n import _, localgettext  from tortoisehg.hgqt import qtlib, thread    local = localgettext()    class ProgressMonitor(QWidget):     def __init__(self):   super(ProgressMonitor, self).__init__()     # main layout box   vbox = QVBoxLayout()   vbox.setContentsMargins(*(0,)*4)   self.setLayout(vbox)     ## status layout box   hbox = QHBoxLayout()   hbox.setContentsMargins(*(0,)*4)   vbox.addLayout(hbox)     self.status_label = QLabel()   hbox.addWidget(self.status_label, 1)     self.prog_label = QLabel()   hbox.addWidget(self.prog_label, 0, Qt.AlignRight)     ## progressbar   self.pbar = QProgressBar()   self.pbar.setTextVisible(False)   self.pbar.setMinimum(0)   vbox.addWidget(self.pbar)     # prepare to show   self.clear_progress()     ### Public Methods ###     def clear_progress(self):   self.pbar.setMaximum(100)   self.pbar.reset()   self.status_label.setText('')   self.prog_label.setText('')   self.inprogress = False     def fillup_progress(self):   """stick progress to full"""   self.clear_progress()   self.pbar.setValue(self.pbar.maximum())     def unknown_progress(self):   self.pbar.setMinimum(0)   self.pbar.setMaximum(0)     def set_text(self, text):   self.status_label.setText(text)    class Core(QObject):   """Core functionality for running Mercurial command.   Do not attempt to instantiate and use this directly.   """     commandStarted = pyqtSignal()   commandFinished = pyqtSignal(thread.DataWrapper)   commandCanceling = pyqtSignal()     def __init__(self):   super(Core, self).__init__()     self.thread = None   self.output_text = QTextBrowser()   self.output_text.document().setDefaultStyleSheet(qtlib.thgstylesheet)   self.pmon = None   self.queue = []     ### Public Methods ###     def run(self, cmdline, *cmdlines):   '''Execute or queue Mercurial command'''   self.queue.append(cmdline)   if len(cmdlines):   self.queue.extend(cmdlines)   if not self.is_running():   self.run_next()     def cancel(self):   '''Cancel running Mercurial command'''   if self.is_running():   self.thread.abort()   self.commandCanceling.emit()     def set_pmon(self, pmon):   self.pmon = pmon     def is_running(self):   return bool(self.thread and self.thread.isRunning())     ### Private Method ###     def run_next(self):   try:   cmdline = self.queue.pop(0)   self.thread = thread.CmdThread(cmdline)   except IndexError:   return False     self.thread.started.connect(self.command_started)   self.thread.commandFinished.connect(self.command_finished)   self.thread.outputReceived.connect(self.output_received)   self.thread.errorReceived.connect(self.error_received)   if self.pmon:   self.thread.progressReceived.connect(self.progress_received)   self.thread.start()     return True     def append_output(self, msg, style=''):   msg = msg.replace('\n', '<br />')   self.output_text.insertHtml('<font style="%s">%s</font>' % (style, msg))   max = self.output_text.verticalScrollBar().maximum()   self.output_text.verticalScrollBar().setSliderPosition(max)     def clear_output(self):   self.output_text.clear()     ### Signal Handlers ###     def command_started(self):   if self.pmon:   self.pmon.set_text(_('Running...'))   self.pmon.unknown_progress()     self.commandStarted.emit()     def command_finished(self, wrapper):   ret = wrapper.data     if self.pmon:   if ret is None:   self.pmon.clear_progress()   if self.pmon.pbar.maximum() == 0: # busy indicator   if ret == 0: # finished successfully   self.pmon.fillup_progress()   else:   self.pmon.clear_progress()   if ret is None:   if self.thread.abortbyuser:   status = _('Terminated by user')   else:   status = _('Terminated')   else:   status = _('Finished')   self.pmon.set_text(status)     if ret == 0 and self.run_next():   return # run next command     self.commandFinished.emit(wrapper)     def command_canceling(self):   if self.pmon:   self.pmon.set_text(_('Canceling...'))   self.pmon.unknown_progress()     self.commandCanceling.emit()     def output_received(self, wrapper):   msg, label = wrapper.data   msg = hglib.tounicode(msg)   msg = Qt.escape(msg)   style = qtlib.geteffect(label)   self.append_output(msg, style)     def error_received(self, wrapper):   msg, label = wrapper.data   msg = hglib.tounicode(msg)   msg = Qt.escape(msg)   style = qtlib.geteffect(label)   self.append_output(msg, style)     def progress_received(self, wrapper):   if self.thread.isFinished():   self.pmon.clear_progress()   return     counting = False   topic, item, pos, total, unit = wrapper.data   if pos is None:   self.pmon.clear_progress()   return   if total is None:   count = '%d' % pos   counting = True   else:   self.pmon.pbar.setMaximum(total)   self.pmon.pbar.setValue(pos)   count = '%d / %d' % (pos, total)   if unit:   count += ' ' + unit   self.pmon.prog_label.setText(hglib.tounicode(count))   if item:   status = '%s: %s' % (topic, item)   else:   status = local._('Status: %s') % topic   self.pmon.status_label.setText(hglib.tounicode(status))   self.pmon.inprogress = True     if not self.pmon.inprogress or counting:   # use indeterminate mode   self.pmon.pbar.setMinimum(0)    class Widget(QWidget):   """An embeddable widget for running Mercurial command"""     commandStarted = pyqtSignal()   commandFinished = pyqtSignal(thread.DataWrapper)   commandCanceling = pyqtSignal()     def __init__(self):   super(Widget, self).__init__()     self.core = Core()   self.core.commandStarted.connect(lambda: self.commandStarted.emit())   self.core.commandFinished.connect(lambda w: self.commandFinished.emit(w))   self.core.commandCanceling.connect(lambda: self.commandCanceling.emit())     # main layout grid   grid = QGridLayout()   grid.setSpacing(4)   grid.setContentsMargins(*(1,)*4)     ## status and progress labels   self.pmon = ProgressMonitor()   self.core.set_pmon(self.pmon)   grid.addWidget(self.pmon, 0, 0)     # command output area   grid.addWidget(self.core.output_text, 1, 0, 5, 0)   grid.setRowStretch(1, 1)     # widget setting   self.setLayout(grid)     # prepare to show   self.core.output_text.setHidden(True)     ### Public Methods ###     def run(self, cmdline, *args):   self.core.run(cmdline, *args)     def cancel(self):   self.core.cancel()     def show_output(self, visible):   self.core.output_text.setShown(visible)     def is_show_output(self):   return self.core.output_text.isVisible()    class Dialog(QDialog):   """A dialog for running random Mercurial command"""     commandStarted = pyqtSignal()   commandFinished = pyqtSignal(thread.DataWrapper)   commandCanceling = pyqtSignal()     def __init__(self, cmdline, parent=None, finishfunc=None):   super(Dialog, self).__init__(parent)   self.setWindowFlags(self.windowFlags() & ~Qt.WindowContextHelpButtonHint)     self.finishfunc = finishfunc     self.core = Core()   self.core.commandFinished.connect(self.command_finished)   self.core.commandCanceling.connect(lambda: self.commandCanceling.emit())     # main layout grid   grid = QGridLayout()   grid.setSpacing(6)   grid.setContentsMargins(*(7,)*4)     ## status and progress labels   self.pmon = ProgressMonitor()   self.core.set_pmon(self.pmon)   grid.addWidget(self.pmon, 0, 0)     # command output area   grid.addWidget(self.core.output_text, 1, 0, 5, 0)   grid.setRowStretch(1, 1)     # bottom buttons   buttons = QDialogButtonBox()   self.cancel_btn = buttons.addButton(QDialogButtonBox.Cancel)   self.cancel_btn.clicked.connect(self.cancel_clicked)   self.close_btn = buttons.addButton(QDialogButtonBox.Close)   self.close_btn.clicked.connect(self.reject)   self.detail_btn = buttons.addButton(_('Detail'),   QDialogButtonBox.ResetRole)   self.detail_btn.setAutoDefault(False)   self.detail_btn.setCheckable(True)   self.detail_btn.setChecked(True)   self.detail_btn.toggled.connect(self.show_output)   grid.addWidget(buttons, 7, 0)     self.setLayout(grid)   self.setWindowTitle(_('TortoiseHg Command Dialog'))   self.resize(540, 420)     # prepare to show   self.close_btn.setHidden(True)     # start command   self.core.run(cmdline)     def show_output(self, visible):   """show/hide command output"""   self.core.output_text.setVisible(visible)   self.detail_btn.setChecked(visible)     # workaround to adjust only window height   self.setMinimumWidth(self.width())   self.adjustSize()   self.setMinimumWidth(0)     ### Private Method ###     def reject(self):   if self.core.is_running():   ret = QMessageBox.question(self, _('Confirm Exit'), _('Mercurial'   ' command is still running.\nAre you sure you want'   ' to terminate?'), QMessageBox.Yes | QMessageBox.No,   QMessageBox.No)   if ret == QMessageBox.Yes:   self.cancel_clicked()     # don't close dialog   return     # close dialog   if self.core.thread.ret == 0:   self.accept() # means command successfully finished   else:   super(Dialog, self).reject()     ### Signal Handlers ###     def cancel_clicked(self):   self.core.cancel()     def command_finished(self, wrapper):   self.cancel_btn.setHidden(True)   self.close_btn.setShown(True)   self.close_btn.setFocus()   if self.finishfunc:   self.finishfunc()     def command_canceling(self):   self.cancel_btn.setDisabled(True) + +class Runner(QObject): + """A component for running Mercurial command without UI + + This command runner doesn't show any UI element unless it gets a warning + or an error while the command is running. Once an error or a warning is + received, it pops-up a small dialog which contains the command log. + """ + + commandStarted = pyqtSignal() + commandFinished = pyqtSignal(thread.DataWrapper) + commandCanceling = pyqtSignal() + + def __init__(self, title=_('TortoiseHg'), parent=None): + super(Runner, self).__init__() + + self.title = title + self.parent = parent + + self.core = Core() + self.core.commandStarted.connect(lambda: self.commandStarted.emit()) + self.core.commandFinished.connect(self.command_finished) + self.core.commandCanceling.connect(lambda: self.commandCanceling.emit()) + + self.core.output_text.setMinimumSize(460, 320) + + ### Public Methods ### + + def run(self, cmdline, *args): + self.core.run(cmdline, *args) + + def cancel(self): + self.core.cancel() + + def show_output(self, visible=True): + if not hasattr(self, 'dlg'): + self.dlg = QDialog(self.parent) + self.dlg.setWindowTitle(self.title) + flags = self.dlg.windowFlags() & ~Qt.WindowContextHelpButtonHint + self.dlg.setWindowFlags(flags) + box = QVBoxLayout() + box.setContentsMargins(*(0,)*4) + box.addWidget(self.core.output_text) + self.dlg.setLayout(box) + self.dlg.setVisible(visible) + + ### Signal Handler ### + + def command_finished(self, wrapper): + if wrapper.data != 0: + self.show_output() + self.commandFinished.emit(wrapper)