diff options
| author | Friedemann Kleint <[email protected]> | 2023-01-13 13:43:44 +0100 |
|---|---|---|
| committer | Friedemann Kleint <[email protected]> | 2023-02-01 21:41:53 +0100 |
| commit | 814d66d55860a1eb204b804871d579da95eabd8f (patch) | |
| tree | 151cc90aa2d552d7b3a17b292608e5ad8ea5a30b /src/tools/uic/python/pythonwriteimports.cpp | |
| parent | d4f72b4de63fe0622908c7657f9724bad359574e (diff) | |
uic: Add option for absolute Python resource imports
Add option that generates an absolute Python import.
import resources.rc_resources
from a path like
../resources/resources.qrc
assuming the project root is .. .
Add an additional option to specify the import paths, from which
the project root can be determined.
Pick-to: 6.5
Task-number: PYSIDE-2191
Change-Id: Ib444eb666217b8c010dba0079b0ffe9ddbaa3414
Reviewed-by: Cristian Maureira-Fredes <[email protected]>
Reviewed-by: Shyamnath Premnadh <[email protected]>
Diffstat (limited to 'src/tools/uic/python/pythonwriteimports.cpp')
| -rw-r--r-- | src/tools/uic/python/pythonwriteimports.cpp | 70 |
1 files changed, 47 insertions, 23 deletions
diff --git a/src/tools/uic/python/pythonwriteimports.cpp b/src/tools/uic/python/pythonwriteimports.cpp index a268f87bb48..b122c0f895b 100644 --- a/src/tools/uic/python/pythonwriteimports.cpp +++ b/src/tools/uic/python/pythonwriteimports.cpp @@ -10,6 +10,8 @@ #include <ui4.h> +#include <QtCore/qdir.h> +#include <QtCore/qfileinfo.h> #include <QtCore/qtextstream.h> #include <algorithm> @@ -54,23 +56,6 @@ 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, bool prefix) -{ - const qsizetype lastSlash = resource.lastIndexOf(u'/'); - if (lastSlash != -1) - resource.remove(0, lastSlash + 1); - if (resource.endsWith(".qrc"_L1)) { - resource.chop(4); - if (prefix) - resource.prepend("rc_"_L1); - else - resource.append("_rc"_L1); - } - return resource; -} - // Helpers for WriteImports::ClassesPerModule maps static void insertClass(const QString &module, const QString &className, WriteImports::ClassesPerModule *c) @@ -143,18 +128,57 @@ void WriteImports::acceptUI(DomUI *node) const auto includes = resources->elementInclude(); for (auto include : includes) { if (include->hasAttributeLocation()) - writeImport(pythonResource(include->attributeLocation(), - uic()->option().rcPrefix)); + writeResourceImport(include->attributeLocation()); } output << '\n'; } } -void WriteImports::writeImport(const QString &module) +QString WriteImports::resourceAbsolutePath(QString resource) const +{ + // If we know the project root, generate an absolute Python import + // to the resource. options. pythonRoot is the Python path component + // under which the UI file is. + const auto &options = uic()->option(); + if (!options.inputFile.isEmpty() && !options.pythonRoot.isEmpty()) { + resource = QDir::cleanPath(QFileInfo(options.inputFile).canonicalPath() + u'/' + resource); + if (resource.size() > options.pythonRoot.size()) + resource.remove(0, options.pythonRoot.size() + 1); + } + // If nothing is known, we assume the directory pointed by "../" is the root + while (resource.startsWith(u"../")) + resource.remove(0, 3); + resource.replace(u'/', u'.'); + return resource; +} + +void WriteImports::writeResourceImport(const QString &module) { - if (uic()->option().fromImports) - uic()->output() << "from . "; - uic()->output() << "import " << module << '\n'; + const auto &options = uic()->option(); + auto &str = uic()->output(); + + QString resource = QDir::cleanPath(module); + if (resource.endsWith(u".qrc")) + resource.chop(4); + const qsizetype basePos = resource.lastIndexOf(u'/') + 1; + // Change the name of a qrc file "dir/foo.qrc" file to the Python + // module name "foo_rc" according to project conventions. + if (options.rcPrefix) + resource.insert(basePos, u"rc_"); + else + resource.append(u"_rc"); + + switch (options.pythonResourceImport) { + case Option::PythonResourceImport::Default: + str << "import " << QStringView{resource}.sliced(basePos) << '\n'; + break; + case Option::PythonResourceImport::FromDot: + str << "from . import " << QStringView{resource}.sliced(basePos) << '\n'; + break; + case Option::PythonResourceImport::Absolute: + str << "import " << resourceAbsolutePath(resource) << '\n'; + break; + } } void WriteImports::doAdd(const QString &className, const DomCustomWidget *dcw) |
