diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2020-05-26 07:40:56 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2020-05-26 07:41:36 +0200 |
commit | 49c4d1f31014605c9355b1f6901e28e5762301d1 (patch) | |
tree | 8293343bf138ef2d53ab118b667fe1205d2b0bd7 /sources/pyside2/PySide2/glue/qtuitools.cpp | |
parent | 10390cc16084c5f386163664f6e42d9e4d28b129 (diff) | |
parent | 6717d3540fac74c91d9381958a08e60f6532d402 (diff) |
Merge remote-tracking branch 'origin/5.14' into 5.15
Change-Id: I9c7163094bc934f481002adcea78ef4928ed26be
Diffstat (limited to 'sources/pyside2/PySide2/glue/qtuitools.cpp')
-rw-r--r-- | sources/pyside2/PySide2/glue/qtuitools.cpp | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/sources/pyside2/PySide2/glue/qtuitools.cpp b/sources/pyside2/PySide2/glue/qtuitools.cpp index 00fc8e44a..668b512e4 100644 --- a/sources/pyside2/PySide2/glue/qtuitools.cpp +++ b/sources/pyside2/PySide2/glue/qtuitools.cpp @@ -109,3 +109,141 @@ registerCustomWidget(%PYARG_1); // Avoid calling the original function: %CPPSELF.%FUNCTION_NAME() %PYARG_0 = QUiLoaderLoadUiFromFileName(%CPPSELF, %1, %2); // @snippet quiloader-load-2 + +// @snippet loaduitype +/* +Arguments: + %PYARG_1 (uifile) +*/ +// 1. Generate the Python code from the UI file +#ifdef IS_PY3K +PyObject *strObj = PyUnicode_AsUTF8String(%PYARG_1); +char *arg1 = PyBytes_AsString(strObj); +QByteArray uiFileName(arg1); +Py_DECREF(strObj); +#else +QByteArray uiFileName(PyBytes_AsString(%PYARG_1)); +#endif + +QFile uiFile(uiFileName); + +if (!uiFile.exists()) { + qCritical().noquote() << "File" << uiFileName << "does not exists"; + Py_RETURN_NONE; +} + +if (uiFileName.isEmpty()) { + qCritical() << "Error converting the UI filename to QByteArray"; + Py_RETURN_NONE; +} + +QString uicBin("uic"); +QStringList uicArgs = {"-g", "python", QString::fromUtf8(uiFileName)}; + +QProcess uicProcess; +uicProcess.start(uicBin, uicArgs); +if (!uicProcess.waitForFinished()) { + qCritical() << "Cannot run 'uic': " << uicProcess.errorString() << " - " + << "Exit status " << uicProcess.exitStatus() + << " (" << uicProcess.exitCode() << ")\n" + << "Check if 'uic' is in PATH"; + Py_RETURN_NONE; +} +QByteArray uiFileContent = uicProcess.readAllStandardOutput(); +QByteArray errorOutput = uicProcess.readAllStandardError(); + +if (!errorOutput.isEmpty()) { + qCritical().noquote() << errorOutput; + Py_RETURN_NONE; +} + +// 2. Obtain the 'classname' and the Qt base class. +QByteArray className; +QByteArray baseClassName; + +// Problem +// The generated Python file doesn't have the Qt Base class information. + +// Solution +// Use the XML file +if (!uiFile.open(QIODevice::ReadOnly)) + Py_RETURN_NONE; + +// This will look for the first <widget> tag, e.g.: +// <widget class="QWidget" name="ThemeWidgetForm"> +// and then extract the information from "class", and "name", +// to get the baseClassName and className respectively +QXmlStreamReader reader(&uiFile); +while (!reader.atEnd() && baseClassName.isEmpty() && className.isEmpty()) { + auto token = reader.readNext(); + if (token == QXmlStreamReader::StartElement && reader.name() == "widget") { + baseClassName = reader.attributes().value(QLatin1String("class")).toUtf8(); + className = reader.attributes().value(QLatin1String("name")).toUtf8(); + } +} + +uiFile.close(); + +if (className.isEmpty() || baseClassName.isEmpty() || reader.hasError()) { + qCritical() << "An error occurred when parsing the UI file while looking for the class info " + << reader.errorString(); + Py_RETURN_NONE; +} + +QByteArray pyClassName("Ui_"+className); + +PyObject *module = PyImport_ImportModule("__main__"); +PyObject *loc = PyModule_GetDict(module); + +// 3. exec() the code so the class exists in the context: exec(uiFileContent) +// The context of PyRun_SimpleString is __main__. +// 'Py_file_input' is the equivalent to using exec(), since it will execute +// the code, without returning anything. +Shiboken::AutoDecRef codeUi(Py_CompileString(uiFileContent.constData(), "<stdin>", Py_file_input)); +if (codeUi.isNull()) { + qCritical() << "Error while compiling the generated Python file"; + Py_RETURN_NONE; +} +PyObject *uiObj = nullptr; +#ifdef IS_PY3K +uiObj = PyEval_EvalCode(codeUi, loc, loc); +#else +uiObj = PyEval_EvalCode(reinterpret_cast<PyCodeObject *>(codeUi.object()), loc, loc); +#endif + +if (uiObj == nullptr) { + qCritical() << "Error while running exec() on the generated code"; + Py_RETURN_NONE; +} + +// 4. eval() the name of the class on a variable to return +// 'Py_eval_input' is the equivalent to using eval(), since it will just +// evaluate an expression. +Shiboken::AutoDecRef codeClass(Py_CompileString(pyClassName.constData(),"<stdin>", Py_eval_input)); +if (codeClass.isNull()) { + qCritical() << "Error while compiling the Python class"; + Py_RETURN_NONE; +} + +Shiboken::AutoDecRef codeBaseClass(Py_CompileString(baseClassName.constData(), "<stdin>", Py_eval_input)); +if (codeBaseClass.isNull()) { + qCritical() << "Error while compiling the base class"; + Py_RETURN_NONE; +} + +#ifdef IS_PY3K +PyObject *classObj = PyEval_EvalCode(codeClass, loc, loc); +PyObject *baseClassObj = PyEval_EvalCode(codeBaseClass, loc, loc); +#else +PyObject *classObj = PyEval_EvalCode(reinterpret_cast<PyCodeObject *>(codeClass.object()), loc, loc); +PyObject *baseClassObj = PyEval_EvalCode(reinterpret_cast<PyCodeObject *>(codeBaseClass.object()), loc, loc); +#endif + +%PYARG_0 = PyTuple_New(2); +if (%PYARG_0 == nullptr) { + qCritical() << "Error while creating the return Tuple"; + Py_RETURN_NONE; +} +PyTuple_SET_ITEM(%PYARG_0, 0, classObj); +PyTuple_SET_ITEM(%PYARG_0, 1, baseClassObj); +// @snippet loaduitype |