7 Module implementing a dialog starting a process and showing its output. |
7 Module implementing a dialog starting a process and showing its output. |
8 """ |
8 """ |
9 |
9 |
10 |
10 |
11 import os |
11 import os |
|
12 import re |
12 |
13 |
13 from PyQt5.QtCore import ( |
14 from PyQt5.QtCore import ( |
14 QProcess, QTimer, pyqtSlot, Qt, QCoreApplication, QProcessEnvironment |
15 QProcess, QTimer, pyqtSlot, Qt, QCoreApplication, QProcessEnvironment |
15 ) |
16 ) |
16 from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QLineEdit |
17 from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QLineEdit |
29 |
30 |
30 It starts a QProcess and displays a dialog that |
31 It starts a QProcess and displays a dialog that |
31 shows the output of the process. The dialog is modal, |
32 shows the output of the process. The dialog is modal, |
32 which causes a synchronized execution of the process. |
33 which causes a synchronized execution of the process. |
33 """ |
34 """ |
34 def __init__(self, outputTitle="", windowTitle="", parent=None): |
35 def __init__(self, outputTitle="", windowTitle="", showProgress=False, |
|
36 parent=None): |
35 """ |
37 """ |
36 Constructor |
38 Constructor |
37 |
39 |
38 @param outputTitle title for the output group |
40 @param outputTitle title for the output group |
39 @type str |
41 @type str |
40 @param windowTitle title of the dialog |
42 @param windowTitle title of the dialog |
41 @type str |
43 @type str |
|
44 @param showProgress flag indicating to show a progress bar |
|
45 @type bool |
42 @param parent reference to the parent widget |
46 @param parent reference to the parent widget |
43 @type QWidget |
47 @type QWidget |
44 """ |
48 """ |
45 super(E5ProcessDialog, self).__init__(parent) |
49 super(E5ProcessDialog, self).__init__(parent) |
46 self.setupUi(self) |
50 self.setupUi(self) |
50 |
54 |
51 if windowTitle: |
55 if windowTitle: |
52 self.setWindowTitle(windowTitle) |
56 self.setWindowTitle(windowTitle) |
53 if outputTitle: |
57 if outputTitle: |
54 self.outputGroup.setTitle(outputTitle) |
58 self.outputGroup.setTitle(outputTitle) |
|
59 self.__showProgress = showProgress |
|
60 self.progressBar.setVisible(self.__showProgress) |
55 |
61 |
56 self.__process = None |
62 self.__process = None |
|
63 self.__progressRe = re.compile(r"""(\d{1,3})\s*%""") |
57 |
64 |
58 self.show() |
65 self.show() |
59 QCoreApplication.processEvents() |
66 QCoreApplication.processEvents() |
60 |
67 |
61 def __finish(self): |
68 def __finish(self): |
204 """ |
211 """ |
205 if self.__process is not None: |
212 if self.__process is not None: |
206 s = str(self.__process.readAllStandardOutput(), |
213 s = str(self.__process.readAllStandardOutput(), |
207 Preferences.getSystem("IOEncoding"), |
214 Preferences.getSystem("IOEncoding"), |
208 'replace') |
215 'replace') |
|
216 if self.__showProgress: |
|
217 match = self.__progressRe.search(s) |
|
218 if match: |
|
219 progress = int(match.group(1)) |
|
220 self.progressBar.setValue(progress) |
|
221 if not s.endswith("\n"): |
|
222 s = s + "\n" |
209 self.resultbox.insertPlainText(s) |
223 self.resultbox.insertPlainText(s) |
210 self.resultbox.ensureCursorVisible() |
224 self.resultbox.ensureCursorVisible() |
211 |
225 |
212 QCoreApplication.processEvents() |
226 QCoreApplication.processEvents() |
213 |
227 |