uic: Add an option to modify the generated resource import

Historically, resource imports were generated as "import file_rc",
however, pyside6-project generates files by prepending "rc_". Add an
option to flip this.

Change-Id: Iee0f161cb2101c8080bd131a6401bbaf4682186d
Reviewed-by: Adrian Herrmann <adrian.herrmann@qt.io>
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
(cherry picked from commit 3696c99a9ab74113557578401d467d845171e543)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Friedemann Kleint 2022-09-08 10:24:07 +02:00 committed by Qt Cherry-pick Bot
parent 1ce75ea7de
commit 5a84483bbf
3 changed files with 17 additions and 3 deletions

View File

@ -89,6 +89,11 @@ int runUic(int argc, char *argv[])
fromImportsOption.setDescription(QStringLiteral("Python: generate imports relative to '.'"));
parser.addOption(fromImportsOption);
// FIXME Qt 7: Flip the default?
QCommandLineOption rcPrefixOption(QStringLiteral("rc-prefix"));
rcPrefixOption.setDescription(QStringLiteral("Python: Generate \"rc_file\" instead of \"file_rc\" import"));
parser.addOption(rcPrefixOption);
// FIXME Qt 7: Remove?
QCommandLineOption useStarImportsOption(QStringLiteral("star-imports"));
useStarImportsOption.setDescription(QStringLiteral("Python: Use * imports"));
@ -117,6 +122,9 @@ int runUic(int argc, char *argv[])
driver.option().forceStringConnectionSyntax = 1;
}
if (parser.isSet(rcPrefixOption))
driver.option().rcPrefix = 1;
Language language = Language::Cpp;
if (parser.isSet(generatorOption)) {
if (parser.value(generatorOption).compare("python"_L1) == 0)

View File

@ -24,6 +24,7 @@ struct Option
unsigned int forceMemberFnPtrConnectionSyntax: 1;
unsigned int forceStringConnectionSyntax: 1;
unsigned int useStarImports: 1;
unsigned int rcPrefix: 1; // Python: Generate "rc_file" instead of "file_rc" import
QString inputFile;
QString outputFile;
@ -48,6 +49,7 @@ struct Option
forceMemberFnPtrConnectionSyntax(0),
forceStringConnectionSyntax(0),
useStarImports(0),
rcPrefix(0),
prefix(QLatin1StringView("Ui_"))
{ indent.fill(u' ', 4); }

View File

@ -56,14 +56,17 @@ static WriteImports::ClassesPerModule defaultClasses()
// 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)
static QString pythonResource(QString resource, bool prefix)
{
const qsizetype lastSlash = resource.lastIndexOf(u'/');
if (lastSlash != -1)
resource.remove(0, lastSlash + 1);
if (resource.endsWith(".qrc"_L1)) {
resource.chop(4);
resource.append("_rc"_L1);
if (prefix)
resource.prepend("rc_"_L1);
else
resource.append("_rc"_L1);
}
return resource;
}
@ -140,7 +143,8 @@ void WriteImports::acceptUI(DomUI *node)
const auto includes = resources->elementInclude();
for (auto include : includes) {
if (include->hasAttributeLocation())
writeImport(pythonResource(include->attributeLocation()));
writeImport(pythonResource(include->attributeLocation(),
uic()->option().rcPrefix));
}
output << '\n';
}