|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2010 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog starting a process and showing its output. |
|
8 """ |
|
9 |
|
10 from PyQt6.QtCore import Qt, QCoreApplication |
|
11 from PyQt6.QtWidgets import QDialog, QDialogButtonBox |
|
12 |
|
13 from .Ui_HgDialog import Ui_HgDialog |
|
14 |
|
15 import Preferences |
|
16 import Utilities |
|
17 |
|
18 |
|
19 class HgDialog(QDialog, Ui_HgDialog): |
|
20 """ |
|
21 Class implementing a dialog starting a process and showing its output. |
|
22 |
|
23 It starts a QProcess and displays a dialog that |
|
24 shows the output of the process. The dialog is modal, |
|
25 which causes a synchronized execution of the process. |
|
26 """ |
|
27 def __init__(self, text, hg=None, useClient=True, parent=None): |
|
28 """ |
|
29 Constructor |
|
30 |
|
31 @param text text to be shown by the label (string) |
|
32 @param hg reference to the Mercurial interface object (Hg) |
|
33 @param useClient flag indicating to use the command server client |
|
34 if possible (boolean) |
|
35 @param parent parent widget (QWidget) |
|
36 """ |
|
37 super().__init__(parent) |
|
38 self.setupUi(self) |
|
39 self.setWindowFlags(Qt.WindowType.Window) |
|
40 |
|
41 self.buttonBox.button( |
|
42 QDialogButtonBox.StandardButton.Close).setEnabled(False) |
|
43 self.buttonBox.button( |
|
44 QDialogButtonBox.StandardButton.Cancel).setDefault(True) |
|
45 |
|
46 self.username = '' |
|
47 self.password = '' |
|
48 self.vcs = hg |
|
49 |
|
50 self.outputGroup.setTitle(text) |
|
51 |
|
52 self.show() |
|
53 QCoreApplication.processEvents() |
|
54 |
|
55 def __finish(self): |
|
56 """ |
|
57 Private slot called when the process finished or the user pressed |
|
58 the button. |
|
59 """ |
|
60 self.buttonBox.button( |
|
61 QDialogButtonBox.StandardButton.Close).setEnabled(True) |
|
62 self.buttonBox.button( |
|
63 QDialogButtonBox.StandardButton.Cancel).setEnabled(False) |
|
64 self.buttonBox.button( |
|
65 QDialogButtonBox.StandardButton.Close).setDefault(True) |
|
66 self.buttonBox.button( |
|
67 QDialogButtonBox.StandardButton.Close).setFocus( |
|
68 Qt.FocusReason.OtherFocusReason) |
|
69 |
|
70 if ( |
|
71 Preferences.getVCS("AutoClose") and |
|
72 self.normal and |
|
73 self.errors.toPlainText() == "" |
|
74 ): |
|
75 self.accept() |
|
76 |
|
77 def on_buttonBox_clicked(self, button): |
|
78 """ |
|
79 Private slot called by a button of the button box clicked. |
|
80 |
|
81 @param button button that was clicked (QAbstractButton) |
|
82 """ |
|
83 if button == self.buttonBox.button( |
|
84 QDialogButtonBox.StandardButton.Close |
|
85 ): |
|
86 self.close() |
|
87 elif button == self.buttonBox.button( |
|
88 QDialogButtonBox.StandardButton.Cancel |
|
89 ): |
|
90 self.vcs.getClient().cancel() |
|
91 |
|
92 def startProcess(self, args, showArgs=True, environment=None, client=None): |
|
93 """ |
|
94 Public slot used to start the process. |
|
95 |
|
96 @param args list of arguments for the process |
|
97 @type list of str |
|
98 @param showArgs flag indicating to show the arguments |
|
99 @type bool |
|
100 @param environment dictionary of environment settings to add |
|
101 or change for the git process |
|
102 @type dict |
|
103 @param client reference to a non-standard command client |
|
104 @type HgClient |
|
105 @return flag indicating a successful start of the process |
|
106 @rtype bool |
|
107 """ |
|
108 self.errorGroup.hide() |
|
109 self.inputGroup.hide() |
|
110 self.normal = False |
|
111 |
|
112 self.__hasAddOrDelete = False |
|
113 if ( |
|
114 args[0] in ["qpush", "qpop", "qgoto", "rebase", |
|
115 "update", "import", "revert", "graft", "shelve", |
|
116 "unshelve", "strip", "histedit"] or |
|
117 (args[0] in ["pull", "unbundle"] and |
|
118 ("--update" in args[1:] or "--rebase" in args[1:])) |
|
119 ): |
|
120 self.__updateCommand = True |
|
121 else: |
|
122 self.__updateCommand = False |
|
123 |
|
124 if showArgs: |
|
125 self.resultbox.append(' '.join(args)) |
|
126 self.resultbox.append('') |
|
127 |
|
128 if client is None: |
|
129 client = self.vcs.getClient() |
|
130 out, err = client.runcommand( |
|
131 args, |
|
132 prompt=self.__getInput, |
|
133 output=self.__showOutput, |
|
134 error=self.__showError |
|
135 ) |
|
136 |
|
137 if err: |
|
138 self.__showError(err) |
|
139 if out: |
|
140 self.__showOutput(out) |
|
141 |
|
142 self.normal = True |
|
143 |
|
144 self.__finish() |
|
145 |
|
146 return True |
|
147 |
|
148 def normalExit(self): |
|
149 """ |
|
150 Public method to check for a normal process termination. |
|
151 |
|
152 @return flag indicating normal process termination (boolean) |
|
153 """ |
|
154 return self.normal |
|
155 |
|
156 def normalExitWithoutErrors(self): |
|
157 """ |
|
158 Public method to check for a normal process termination without |
|
159 error messages. |
|
160 |
|
161 @return flag indicating normal process termination (boolean) |
|
162 """ |
|
163 return self.normal and self.errors.toPlainText() == "" |
|
164 |
|
165 def __showOutput(self, out): |
|
166 """ |
|
167 Private slot to show some output. |
|
168 |
|
169 @param out output to be shown (string) |
|
170 """ |
|
171 self.resultbox.insertPlainText(Utilities.filterAnsiSequences(out)) |
|
172 self.resultbox.ensureCursorVisible() |
|
173 |
|
174 # check for a changed project file |
|
175 if self.__updateCommand: |
|
176 for line in out.splitlines(): |
|
177 if ( |
|
178 '.epj' in line or |
|
179 '.e4p' in line |
|
180 ): |
|
181 self.__hasAddOrDelete = True |
|
182 break |
|
183 |
|
184 QCoreApplication.processEvents() |
|
185 |
|
186 def __showError(self, out): |
|
187 """ |
|
188 Private slot to show some error. |
|
189 |
|
190 @param out error to be shown (string) |
|
191 """ |
|
192 self.errorGroup.show() |
|
193 self.errors.insertPlainText(Utilities.filterAnsiSequences(out)) |
|
194 self.errors.ensureCursorVisible() |
|
195 |
|
196 QCoreApplication.processEvents() |
|
197 |
|
198 def hasAddOrDelete(self): |
|
199 """ |
|
200 Public method to check, if the last action contained an add or delete. |
|
201 |
|
202 @return flag indicating the presence of an add or delete (boolean) |
|
203 """ |
|
204 return self.__hasAddOrDelete |
|
205 |
|
206 def __getInput(self, size, message): |
|
207 """ |
|
208 Private method to get some input from the user. |
|
209 |
|
210 @param size maximum length of the requested input |
|
211 @type int |
|
212 @param message message sent by the server |
|
213 @type str |
|
214 @return tuple containing data entered by the user and |
|
215 a flag indicating a password input |
|
216 @rtype tuple of (str, bool) |
|
217 """ |
|
218 self.inputGroup.show() |
|
219 self.input.setMaxLength(size) |
|
220 self.input.setFocus(Qt.FocusReason.OtherFocusReason) |
|
221 |
|
222 self.resultbox.ensureCursorVisible() |
|
223 self.errors.ensureCursorVisible() |
|
224 |
|
225 from PyQt6.QtCore import QEventLoop |
|
226 loop = QEventLoop(self) |
|
227 self.sendButton.clicked[bool].connect(loop.quit) |
|
228 self.input.returnPressed.connect(loop.quit) |
|
229 loop.exec() |
|
230 message = self.input.text() + "\n" |
|
231 isPassword = self.passwordCheckBox.isChecked() |
|
232 |
|
233 self.input.clear() |
|
234 self.inputGroup.hide() |
|
235 |
|
236 return message, isPassword |