diff options
Diffstat (limited to 'src/tools/uic/main.cpp')
-rw-r--r-- | src/tools/uic/main.cpp | 191 |
1 files changed, 126 insertions, 65 deletions
diff --git a/src/tools/uic/main.cpp b/src/tools/uic/main.cpp index 853538479f..d46b788419 100644 --- a/src/tools/uic/main.cpp +++ b/src/tools/uic/main.cpp @@ -1,30 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the tools applications of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 #include "uic.h" #include "option.h" @@ -35,19 +10,52 @@ #include <qdir.h> #include <qhashfunctions.h> #include <qtextstream.h> -#include <qtextcodec.h> #include <qcoreapplication.h> #include <qcommandlineoption.h> #include <qcommandlineparser.h> +#include <qfileinfo.h> QT_BEGIN_NAMESPACE +using namespace Qt::StringLiterals; + +static const char pythonPathVar[] = "PYTHONPATH"; + +// From the Python paths, find the component the UI file is under +static QString pythonRoot(const QString &pythonPath, const QString &uiFileIn) +{ +#ifdef Q_OS_WIN + static const Qt::CaseSensitivity fsSensitivity = Qt::CaseInsensitive; +#else + static const Qt::CaseSensitivity fsSensitivity = Qt::CaseSensitive; +#endif + + if (pythonPath.isEmpty() || uiFileIn.isEmpty()) + return {}; + const QString uiFile = QFileInfo(uiFileIn).canonicalFilePath(); + if (uiFile.isEmpty()) + return {}; + const auto uiFileSize = uiFile.size(); + const auto paths = pythonPath.split(QDir::listSeparator(), Qt::SkipEmptyParts); + for (const auto &path : paths) { + const QString canonicalPath = QFileInfo(path).canonicalFilePath(); + const auto canonicalPathSize = canonicalPath.size(); + if (uiFileSize > canonicalPathSize + && uiFile.at(canonicalPathSize) == u'/' + && uiFile.startsWith(canonicalPath, fsSensitivity)) { + return canonicalPath; + } + } + return {}; +} + int runUic(int argc, char *argv[]) { - qSetGlobalQHashSeed(0); // set the hash seed to 0 + QHashSeed::setDeterministicGlobalSeed(); QCoreApplication app(argc, argv); - QCoreApplication::setApplicationVersion(QString::fromLatin1(QT_VERSION_STR)); + const QString version = QString::fromLatin1(qVersion()); + QCoreApplication::setApplicationVersion(version); Driver driver; @@ -55,56 +63,89 @@ int runUic(int argc, char *argv[]) // If you use this code as an example for a translated app, make sure to translate the strings. QCommandLineParser parser; parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions); - parser.setApplicationDescription(QStringLiteral("Qt User Interface Compiler version %1").arg(QString::fromLatin1(QT_VERSION_STR))); + parser.setApplicationDescription(u"Qt User Interface Compiler version %1"_s.arg(version)); parser.addHelpOption(); parser.addVersionOption(); - QCommandLineOption dependenciesOption(QStringList() << QStringLiteral("d") << QStringLiteral("dependencies")); - dependenciesOption.setDescription(QStringLiteral("Display the dependencies.")); + QCommandLineOption dependenciesOption(QStringList{u"d"_s, u"dependencies"_s}); + dependenciesOption.setDescription(u"Display the dependencies."_s); parser.addOption(dependenciesOption); - QCommandLineOption outputOption(QStringList() << QStringLiteral("o") << QStringLiteral("output")); - outputOption.setDescription(QStringLiteral("Place the output into <file>")); - outputOption.setValueName(QStringLiteral("file")); + QCommandLineOption outputOption(QStringList{u"o"_s, u"output"_s}); + outputOption.setDescription(u"Place the output into <file>"_s); + outputOption.setValueName(u"file"_s); parser.addOption(outputOption); - QCommandLineOption noAutoConnectionOption(QStringList() << QStringLiteral("a") << QStringLiteral("no-autoconnection")); - noAutoConnectionOption.setDescription(QStringLiteral("Do not generate a call to QObject::connectSlotsByName().")); + QCommandLineOption noAutoConnectionOption(QStringList{u"a"_s, u"no-autoconnection"_s}); + noAutoConnectionOption.setDescription(u"Do not generate a call to QObject::connectSlotsByName()."_s); parser.addOption(noAutoConnectionOption); - QCommandLineOption noProtOption(QStringList() << QStringLiteral("p") << QStringLiteral("no-protection")); - noProtOption.setDescription(QStringLiteral("Disable header protection.")); + QCommandLineOption noProtOption(QStringList{u"p"_s, u"no-protection"_s}); + noProtOption.setDescription(u"Disable header protection."_s); parser.addOption(noProtOption); - QCommandLineOption noImplicitIncludesOption(QStringList() << QStringLiteral("n") << QStringLiteral("no-implicit-includes")); - noImplicitIncludesOption.setDescription(QStringLiteral("Disable generation of #include-directives.")); + QCommandLineOption noImplicitIncludesOption(QStringList{u"n"_s, u"no-implicit-includes"_s}); + noImplicitIncludesOption.setDescription(u"Disable generation of #include-directives."_s); parser.addOption(noImplicitIncludesOption); - QCommandLineOption postfixOption(QStringLiteral("postfix")); - postfixOption.setDescription(QStringLiteral("Postfix to add to all generated classnames.")); - postfixOption.setValueName(QStringLiteral("postfix")); + QCommandLineOption postfixOption(u"postfix"_s); + postfixOption.setDescription(u"Postfix to add to all generated classnames."_s); + postfixOption.setValueName(u"postfix"_s); parser.addOption(postfixOption); - QCommandLineOption translateOption(QStringList() << QStringLiteral("tr") << QStringLiteral("translate")); - translateOption.setDescription(QStringLiteral("Use <function> for i18n.")); - translateOption.setValueName(QStringLiteral("function")); + QCommandLineOption noQtNamespaceOption(u"no-qt-namespace"_s); + noQtNamespaceOption.setDescription( + u"Disable wrapping the definition of the generated class in QT_{BEGIN,END}_NAMESPACE."_s); + parser.addOption(noQtNamespaceOption); + + QCommandLineOption translateOption(QStringList{u"tr"_s, u"translate"_s}); + translateOption.setDescription(u"Use <function> for i18n."_s); + translateOption.setValueName(u"function"_s); parser.addOption(translateOption); - QCommandLineOption includeOption(QStringList() << QStringLiteral("include")); - includeOption.setDescription(QStringLiteral("Add #include <include-file> to <file>.")); - includeOption.setValueName(QStringLiteral("include-file")); + QCommandLineOption includeOption(QStringList{u"include"_s}); + includeOption.setDescription(u"Add #include <include-file> to <file>."_s); + includeOption.setValueName(u"include-file"_s); parser.addOption(includeOption); - QCommandLineOption generatorOption(QStringList() << QStringLiteral("g") << QStringLiteral("generator")); - generatorOption.setDescription(QStringLiteral("Select generator.")); - generatorOption.setValueName(QStringLiteral("python|cpp")); + QCommandLineOption generatorOption(QStringList{u"g"_s, u"generator"_s}); + generatorOption.setDescription(u"Select generator."_s); + generatorOption.setValueName(u"python|cpp"_s); parser.addOption(generatorOption); - QCommandLineOption idBasedOption(QStringLiteral("idbased")); - idBasedOption.setDescription(QStringLiteral("Use id based function for i18n")); + QCommandLineOption connectionsOption(QStringList{u"c"_s, u"connections"_s}); + connectionsOption.setDescription(u"Connection syntax."_s); + connectionsOption.setValueName(u"pmf|string"_s); + parser.addOption(connectionsOption); + + QCommandLineOption idBasedOption(u"idbased"_s); + idBasedOption.setDescription(u"Use id based function for i18n"_s); parser.addOption(idBasedOption); - parser.addPositionalArgument(QStringLiteral("[uifile]"), QStringLiteral("Input file (*.ui), otherwise stdin.")); + QCommandLineOption fromImportsOption(u"from-imports"_s); + fromImportsOption.setDescription(u"Python: generate imports relative to '.'"_s); + parser.addOption(fromImportsOption); + + QCommandLineOption absoluteImportsOption(u"absolute-imports"_s); + absoluteImportsOption.setDescription(u"Python: generate absolute imports"_s); + parser.addOption(absoluteImportsOption); + + // FIXME Qt 7: Flip the default? + QCommandLineOption rcPrefixOption(u"rc-prefix"_s); + rcPrefixOption.setDescription(uR"(Python: Generate "rc_file" instead of "file_rc" import)"_s); + parser.addOption(rcPrefixOption); + + // FIXME Qt 7: Remove? + QCommandLineOption useStarImportsOption(u"star-imports"_s); + useStarImportsOption.setDescription(u"Python: Use * imports"_s); + parser.addOption(useStarImportsOption); + + QCommandLineOption pythonPathOption(u"python-paths"_s); + pythonPathOption.setDescription(u"Python paths for --absolute-imports."_s); + pythonPathOption.setValueName(u"pathlist"_s); + parser.addOption(pythonPathOption); + + parser.addPositionalArgument(u"[uifile]"_s, u"Input file (*.ui), otherwise stdin."_s); parser.process(app); @@ -113,22 +154,44 @@ int runUic(int argc, char *argv[]) driver.option().autoConnection = !parser.isSet(noAutoConnectionOption); driver.option().headerProtection = !parser.isSet(noProtOption); driver.option().implicitIncludes = !parser.isSet(noImplicitIncludesOption); + driver.option().qtNamespace = !parser.isSet(noQtNamespaceOption); driver.option().idBased = parser.isSet(idBasedOption); driver.option().postfix = parser.value(postfixOption); driver.option().translateFunction = parser.value(translateOption); driver.option().includeFile = parser.value(includeOption); + if (parser.isSet(connectionsOption)) { + const auto value = parser.value(connectionsOption); + if (value == "pmf"_L1) + driver.option().forceMemberFnPtrConnectionSyntax = 1; + else if (value == "string"_L1) + driver.option().forceStringConnectionSyntax = 1; + } + + const QString inputFile = parser.positionalArguments().value(0); Language language = Language::Cpp; if (parser.isSet(generatorOption)) { - if (parser.value(generatorOption).compare(QLatin1String("python")) == 0) + if (parser.value(generatorOption).compare("python"_L1) == 0) language = Language::Python; } language::setLanguage(language); + if (language == Language::Python) { + if (parser.isSet(fromImportsOption)) + driver.option().pythonResourceImport = Option::PythonResourceImport::FromDot; + else if (parser.isSet(absoluteImportsOption)) + driver.option().pythonResourceImport = Option::PythonResourceImport::Absolute; + driver.option().useStarImports = parser.isSet(useStarImportsOption); + if (parser.isSet(rcPrefixOption)) + driver.option().rcPrefix = 1; + QString pythonPaths; + if (parser.isSet(pythonPathOption)) + pythonPaths = parser.value(pythonPathOption); + else if (qEnvironmentVariableIsSet(pythonPathVar)) + pythonPaths = QString::fromUtf8(qgetenv(pythonPathVar)); + driver.option().pythonRoot = pythonRoot(pythonPaths, inputFile); + } - QString inputFile; - if (!parser.positionalArguments().isEmpty()) - inputFile = parser.positionalArguments().at(0); - else // reading from stdin + if (inputFile.isEmpty()) // reading from stdin driver.option().headerProtection = false; if (driver.option().dependencies) { @@ -144,9 +207,7 @@ int runUic(int argc, char *argv[]) return 1; } out = new QTextStream(&f); -#if QT_CONFIG(textcodec) - out->setCodec(QTextCodec::codecForName("UTF-8")); -#endif + out->setEncoding(QStringConverter::Utf8); } bool rtn = driver.uic(inputFile, out); |