Project/CreateDialogCodeDialog.py

changeset 6590
8cad6f50ff4c
parent 6548
21f8260753b5
child 6645
ad476851d7e0
equal deleted inserted replaced
6589:613426e62983 6590:8cad6f50ff4c
16 import os 16 import os
17 import json 17 import json
18 import xml.etree.ElementTree 18 import xml.etree.ElementTree
19 19
20 from PyQt5.QtCore import QMetaObject, QByteArray, QRegExp, Qt, pyqtSlot, \ 20 from PyQt5.QtCore import QMetaObject, QByteArray, QRegExp, Qt, pyqtSlot, \
21 QMetaMethod, QSortFilterProxyModel, QProcess 21 QMetaMethod, QSortFilterProxyModel, QProcess, QProcessEnvironment
22 from PyQt5.QtGui import QStandardItemModel, QBrush, QStandardItem 22 from PyQt5.QtGui import QStandardItemModel, QBrush, QStandardItem
23 from PyQt5.QtWidgets import QWidget, QDialog, QDialogButtonBox, QAction 23 from PyQt5.QtWidgets import QWidget, QDialog, QDialogButtonBox, QAction
24 from PyQt5 import uic 24 from PyQt5 import uic
25 25
26 26
144 Public method to determine, if there was an initialzation error. 144 Public method to determine, if there was an initialzation error.
145 145
146 @return flag indicating an initialzation error (boolean) 146 @return flag indicating an initialzation error (boolean)
147 """ 147 """
148 return self.__initError 148 return self.__initError
149 149
150 def __objectNameExternal(self): 150 def __runUicLoadUi(self, command):
151 """ 151 """
152 Private method to get the object name of a form via an external 152 Private method to run the UicLoadUi.py script with the given command
153 interpreter. 153 and return the output.
154 154
155 @return object name 155 @param command uic command to be run
156 @rtype str 156 @type str
157 """ 157 @return tuple of process output and error flag
158 interpreter = self.project.getDebugProperty("INTERPRETER") 158 @rtype tuple of (str, bool)
159 objectName = "" 159 """
160 venvName = self.project.getDebugProperty("VIRTUALENV")
161 venvManager = e5App().getObject("VirtualEnvManager")
162 interpreter = venvManager.getVirtualenvInterpreter(venvName)
163 execPath = venvManager.getVirtualenvExecPath(venvName)
164
165 env = QProcessEnvironment.systemEnvironment()
166 if execPath:
167 if env.contains("PATH"):
168 env.insert(
169 "PATH", os.pathsep.join([execPath, env.value("PATH")])
170 )
171 else:
172 env.insert("PATH", execPath)
160 173
161 loadUi = os.path.join(os.path.dirname(__file__), "UicLoadUi.py") 174 loadUi = os.path.join(os.path.dirname(__file__), "UicLoadUi.py")
162 args = [ 175 args = [
163 loadUi, 176 loadUi,
164 "object_name", 177 command,
165 self.formFile, 178 self.formFile,
166 self.project.getProjectPath(), 179 self.project.getProjectPath(),
167 ] 180 ]
168 181
182 uicText = ""
183 ok = False
184
169 proc = QProcess() 185 proc = QProcess()
186 proc.setWorkingDirectory(self.project.getProjectPath())
187 proc.setProcessEnvironment(env)
170 proc.start(interpreter, args) 188 proc.start(interpreter, args)
189 started = proc.waitForStarted(5000)
171 finished = proc.waitForFinished(30000) 190 finished = proc.waitForFinished(30000)
172 if finished: 191 if started and finished:
173 text = proc.readAllStandardOutput() 192 output = proc.readAllStandardOutput()
193 outText = str(output, "utf-8", "replace")
174 if proc.exitCode() == 0: 194 if proc.exitCode() == 0:
175 objectName = str(text, "utf-8", "replace").strip() 195 ok = True
196 uicText = outText.strip()
176 else: 197 else:
177 errorText = str(text, "utf-8", "replace")
178 E5MessageBox.critical( 198 E5MessageBox.critical(
179 self, 199 self,
180 self.tr("uic error"), 200 self.tr("uic error"),
181 self.tr( 201 self.tr(
182 """<p>There was an error loading the form <b>{0}</b>""" 202 """<p>There was an error loading the form <b>{0}</b>"""
183 """.</p><p>{1}</p>""").format( 203 """.</p><p>{1}</p>""").format(
184 self.formFile, errorText)) 204 self.formFile, outText)
205 )
206 else:
207 E5MessageBox.critical(
208 self,
209 self.tr("uic error"),
210 self.tr(
211 """<p>The project specific Python interpreter <b>{0}</b>"""
212 """ could not be started or did not finish within 30"""
213 """ seconds.</p>""").format(interpreter)
214 )
215
216 return uicText, ok
217
218 def __objectNameExternal(self):
219 """
220 Private method to get the object name of a form via an external
221 interpreter.
222
223 @return object name
224 @rtype str
225 """
226 objectName = ""
227
228 output, ok = self.__runUicLoadUi("object_name")
229 if ok and output:
230 objectName = output
185 231
186 return objectName 232 return objectName
187 233
188 def __objectName(self): 234 def __objectName(self):
189 """ 235 """
190 Private method to get the object name of a form. 236 Private method to get the object name of a form.
191 237
192 @return object name 238 @return object name
193 @rtype str 239 @rtype str
194 """ 240 """
195 if self.project.getDebugProperty("INTERPRETER"): 241 if self.project.getDebugProperty("VIRTUALENV"):
196 return self.__objectNameExternal() 242 return self.__objectNameExternal()
197 else: 243 else:
198 try: 244 try:
199 dlg = uic.loadUi( 245 dlg = uic.loadUi(
200 self.formFile, package=self.project.getProjectPath()) 246 self.formFile, package=self.project.getProjectPath())
215 interpreter. 261 interpreter.
216 262
217 @return class name 263 @return class name
218 @rtype str 264 @rtype str
219 """ 265 """
220 interpreter = self.project.getDebugProperty("INTERPRETER")
221 className = "" 266 className = ""
222 267
223 loadUi = os.path.join(os.path.dirname(__file__), "UicLoadUi.py") 268 output, ok = self.__runUicLoadUi("class_name")
224 args = [ 269 if ok and output:
225 loadUi, 270 className = output
226 "class_name",
227 self.formFile,
228 self.project.getProjectPath(),
229 ]
230
231 proc = QProcess()
232 proc.start(interpreter, args)
233 finished = proc.waitForFinished(30000)
234 if finished:
235 text = proc.readAllStandardOutput()
236 if proc.exitCode() == 0:
237 className = str(text, "utf-8", "replace").strip()
238 else:
239 errorText = str(text, "utf-8", "replace")
240 E5MessageBox.critical(
241 self,
242 self.tr("uic error"),
243 self.tr(
244 """<p>There was an error loading the form <b>{0}</b>"""
245 """.</p><p>{1}</p>""").format(
246 self.formFile, errorText))
247 271
248 return className 272 return className
249 273
250 def __className(self): 274 def __className(self):
251 """ 275 """
252 Private method to get the class name of a form. 276 Private method to get the class name of a form.
253 277
254 @return class name 278 @return class name
255 @rtype str 279 @rtype str
256 """ 280 """
257 if self.project.getDebugProperty("INTERPRETER"): 281 if self.project.getDebugProperty("VIRTUALENV"):
258 return self.__objectNameExternal() 282 return self.__classNameExternal()
259 else: 283 else:
260 try: 284 try:
261 dlg = uic.loadUi( 285 dlg = uic.loadUi(
262 self.formFile, package=self.project.getProjectPath()) 286 self.formFile, package=self.project.getProjectPath())
263 return dlg.metaObject().className() 287 return dlg.metaObject().className()
325 def __updateSlotsModelExternal(self): 349 def __updateSlotsModelExternal(self):
326 """ 350 """
327 Private slot to update the slots tree display getting the data via an 351 Private slot to update the slots tree display getting the data via an
328 external interpreter. 352 external interpreter.
329 """ 353 """
330 interpreter = self.project.getDebugProperty("INTERPRETER") 354 output, ok = self.__runUicLoadUi("signatures")
331 objectsList = [] 355 if ok and output:
332 356 objectsList = json.loads(output.strip())
333 loadUi = os.path.join(os.path.dirname(__file__), "UicLoadUi.py")
334 args = [
335 loadUi,
336 "signatures",
337 self.formFile,
338 self.project.getProjectPath(),
339 ]
340
341 proc = QProcess()
342 proc.start(interpreter, args)
343 finished = proc.waitForFinished(30000)
344 if not finished:
345 return
346
347 text = proc.readAllStandardOutput()
348 if proc.exitCode() != 0:
349 errorText = str(text, "utf-8", "replace")
350 E5MessageBox.critical(
351 self,
352 self.tr("uic error"),
353 self.tr(
354 """<p>There was an error loading the form <b>{0}</b>"""
355 """.</p><p>{1}</p>""").format(
356 self.formFile, errorText))
357 else:
358 objectsListStr = str(text, "utf-8", "replace").strip()
359 objectsList = json.loads(objectsListStr)
360 357
361 signatureList = self.__signatures() 358 signatureList = self.__signatures()
362 359
363 self.slotsModel.clear() 360 self.slotsModel.clear()
364 self.slotsModel.setHorizontalHeaderLabels([""]) 361 self.slotsModel.setHorizontalHeaderLabels([""])
404 """ 401 """
405 Private slot to update the slots tree display. 402 Private slot to update the slots tree display.
406 """ 403 """
407 self.filterEdit.clear() 404 self.filterEdit.clear()
408 405
409 if self.project.getDebugProperty("INTERPRETER"): 406 if self.project.getDebugProperty("VIRTUALENV"):
410 self.__updateSlotsModelExternal() 407 self.__updateSlotsModelExternal()
411 else: 408 else:
412 try: 409 try:
413 dlg = uic.loadUi( 410 dlg = uic.loadUi(
414 self.formFile, package=self.project.getProjectPath()) 411 self.formFile, package=self.project.getProjectPath())

eric ide

mercurial