uic: Add handling of resources imports for Python
The Python-bases pyside2-uic would write an import for each resource file encountered, adding a "_rc" suffix. Add this handling. Task-number: PYSIDE-1171 Task-number: PYSIDE-1170 Change-Id: I870d61ca7262c0684de5359a5f990d23a4171032 Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
This commit is contained in:
parent
8b0b2057d0
commit
01f0df1cf5
@ -108,6 +108,10 @@ int runUic(int argc, char *argv[])
|
||||
idBasedOption.setDescription(QStringLiteral("Use id based function for i18n"));
|
||||
parser.addOption(idBasedOption);
|
||||
|
||||
QCommandLineOption fromImportsOption(QStringLiteral("from-imports"));
|
||||
fromImportsOption.setDescription(QStringLiteral("Python: generate imports relative to '.'"));
|
||||
parser.addOption(fromImportsOption);
|
||||
|
||||
parser.addPositionalArgument(QStringLiteral("[uifile]"), QStringLiteral("Input file (*.ui), otherwise stdin."));
|
||||
|
||||
parser.process(app);
|
||||
@ -118,6 +122,7 @@ int runUic(int argc, char *argv[])
|
||||
driver.option().headerProtection = !parser.isSet(noProtOption);
|
||||
driver.option().implicitIncludes = !parser.isSet(noImplicitIncludesOption);
|
||||
driver.option().idBased = parser.isSet(idBasedOption);
|
||||
driver.option().fromImports = parser.isSet(fromImportsOption);
|
||||
driver.option().postfix = parser.value(postfixOption);
|
||||
driver.option().translateFunction = parser.value(translateOption);
|
||||
driver.option().includeFile = parser.value(includeOption);
|
||||
|
@ -45,6 +45,7 @@ struct Option
|
||||
unsigned int limitXPM_LineLength : 1;
|
||||
unsigned int implicitIncludes: 1;
|
||||
unsigned int idBased: 1;
|
||||
unsigned int fromImports: 1;
|
||||
|
||||
QString inputFile;
|
||||
QString outputFile;
|
||||
@ -65,6 +66,7 @@ struct Option
|
||||
limitXPM_LineLength(0),
|
||||
implicitIncludes(1),
|
||||
idBased(0),
|
||||
fromImports(0),
|
||||
prefix(QLatin1String("Ui_"))
|
||||
{ indent.fill(QLatin1Char(' '), 4); }
|
||||
|
||||
|
@ -29,6 +29,7 @@
|
||||
#include "pythonwriteimports.h"
|
||||
|
||||
#include <customwidgetsinfo.h>
|
||||
#include <option.h>
|
||||
#include <uic.h>
|
||||
|
||||
#include <ui4.h>
|
||||
@ -46,6 +47,20 @@ from PySide2.QtGui import (QBrush, QColor, QConicalGradient, QFont,
|
||||
from PySide2.QtWidgets import *
|
||||
)I";
|
||||
|
||||
// Change the name of a qrc file "dir/foo.qrc" file to the Python
|
||||
// module name "foo_rc" according to project conventions.
|
||||
static QString pythonResource(QString resource)
|
||||
{
|
||||
const int lastSlash = resource.lastIndexOf(QLatin1Char('/'));
|
||||
if (lastSlash != -1)
|
||||
resource.remove(0, lastSlash + 1);
|
||||
if (resource.endsWith(QLatin1String(".qrc"))) {
|
||||
resource.chop(4);
|
||||
resource.append(QLatin1String("_rc"));
|
||||
}
|
||||
return resource;
|
||||
}
|
||||
|
||||
namespace Python {
|
||||
|
||||
WriteImports::WriteImports(Uic *uic) : m_uic(uic)
|
||||
@ -60,6 +75,23 @@ void WriteImports::acceptUI(DomUI *node)
|
||||
TreeWalker::acceptCustomWidgets(customWidgets);
|
||||
output << '\n';
|
||||
}
|
||||
|
||||
if (auto resources = node->elementResources()) {
|
||||
const auto includes = resources->elementInclude();
|
||||
for (auto include : includes) {
|
||||
if (include->hasAttributeLocation())
|
||||
writeImport(pythonResource(include->attributeLocation()));
|
||||
}
|
||||
output << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
void WriteImports::writeImport(const QString &module)
|
||||
{
|
||||
|
||||
if (m_uic->option().fromImports)
|
||||
m_uic->output() << "from . ";
|
||||
m_uic->output() << "import " << module << '\n';
|
||||
}
|
||||
|
||||
QString WriteImports::qtModuleOf(const DomCustomWidget *node) const
|
||||
|
@ -46,6 +46,7 @@ public:
|
||||
void acceptCustomWidget(DomCustomWidget *node) override;
|
||||
|
||||
private:
|
||||
void writeImport(const QString &module);
|
||||
QString qtModuleOf(const DomCustomWidget *node) const;
|
||||
|
||||
Uic *const m_uic;
|
||||
|
Loading…
x
Reference in New Issue
Block a user