From 20b0c2fb20f3c37c2549b7aa59f686a7726a2817 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Tue, 7 Mar 2023 14:12:42 +0100 Subject: map viewer/places examples: Brush up the code - Use modern string literals - Use the variadic template version of QMetaObject::invokeMethod() - Use modern connect syntax - Reorder includes - Trim newlines in QML Fix minor bugs: - Skip empty elements of library path (namely when variable is not set) - Fix the check for osm.useragent to operate on the map - Exit cleanly on QML loading errors Since places uses similar code, apply the changes there as well. Pick-to: 6.5 6.5.0 Change-Id: Ibaeffc6e003c55c458137458c5017584e0c072f1 Reviewed-by: Volker Hilsheimer --- examples/location/mapviewer/forms/Geocode.qml | 2 - .../location/mapviewer/forms/RouteCoordinate.qml | 1 - .../location/mapviewer/forms/RouteListDelegate.qml | 3 - examples/location/mapviewer/main.cpp | 95 +++++++++++----------- examples/location/places/main.cpp | 75 ++++++++--------- 5 files changed, 83 insertions(+), 93 deletions(-) diff --git a/examples/location/mapviewer/forms/Geocode.qml b/examples/location/mapviewer/forms/Geocode.qml index 448441ec..99accad6 100644 --- a/examples/location/mapviewer/forms/Geocode.qml +++ b/examples/location/mapviewer/forms/Geocode.qml @@ -40,5 +40,3 @@ GeocodeForm { postalCode.text = address.postalCode } } - - diff --git a/examples/location/mapviewer/forms/RouteCoordinate.qml b/examples/location/mapviewer/forms/RouteCoordinate.qml index ca289e93..f2eb771f 100644 --- a/examples/location/mapviewer/forms/RouteCoordinate.qml +++ b/examples/location/mapviewer/forms/RouteCoordinate.qml @@ -39,4 +39,3 @@ RouteCoordinateForm { toLongitude.text = "" + toCoordinate.longitude } } - diff --git a/examples/location/mapviewer/forms/RouteListDelegate.qml b/examples/location/mapviewer/forms/RouteListDelegate.qml index 30e43503..dbefbaa3 100644 --- a/examples/location/mapviewer/forms/RouteListDelegate.qml +++ b/examples/location/mapviewer/forms/RouteListDelegate.qml @@ -40,6 +40,3 @@ Item { color: "#46a2da" } } - - - diff --git a/examples/location/mapviewer/main.cpp b/examples/location/mapviewer/main.cpp index b24bb392..8cb804b7 100644 --- a/examples/location/mapviewer/main.cpp +++ b/examples/location/mapviewer/main.cpp @@ -1,70 +1,69 @@ // Copyright (C) 2017 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#include -#include #include -#include -#if QT_CONFIG(ssl) -#include -#endif -#include +#include -static bool parseArgs(QStringList& args, QVariantMap& parameters) -{ +#include - while (!args.isEmpty()) { +#if QT_CONFIG(ssl) +#include +#endif - QString param = args.takeFirst(); +#include - if (param.startsWith("--help")) { - QTextStream out(stdout); - out << "Usage: " << Qt::endl; - out << "--plugin. - Sets parameter = value for plugin" << Qt::endl; - out.flush(); - return true; - } +using namespace Qt::StringLiterals; - if (param.startsWith("--plugin.")) { +static const char *help = R"(Usage: +--plugin. - Sets parameter = value for plugin +)"; +static QVariantMap parseArgs(QStringList args) +{ + QVariantMap parameters; + while (!args.isEmpty()) { + QString param = args.takeFirst(); + if (param.startsWith(u"--plugin.")) { param.remove(0, 9); - - if (args.isEmpty() || args.first().startsWith("--")) { - parameters[param] = true; + if (args.isEmpty() || args.constFirst().startsWith(u"--")) { + parameters.insert(param, QVariant(true)); } else { - QString value = args.takeFirst(); - - if (value == "true" || value == "on" || value == "enabled") { - parameters[param] = true; - } else if (value == "false" || value == "off" - || value == "disable") { - parameters[param] = false; + if (value == u"true" || value == u"on" || value == u"enabled") { + parameters.insert(param, QVariant(true)); + } else if (value == u"false" || value == u"off" + || value == u"disable") { + parameters.insert(param, QVariant(false)); } else { - parameters[param] = value; + parameters.insert(param, QVariant(value)); } } } } - return false; + return parameters; } int main(int argc, char *argv[]) { #if QT_CONFIG(library) - const QByteArray additionalLibraryPaths = qgetenv("QTLOCATION_EXTRA_LIBRARY_PATH"); - for (const QByteArray &p : additionalLibraryPaths.split(':')) - QCoreApplication::addLibraryPath(QString(p)); + const QString additionalLibraryPaths = qEnvironmentVariable("QTLOCATION_EXTRA_LIBRARY_PATH"); + for (const auto &p : additionalLibraryPaths.split(u':', Qt::SkipEmptyParts)) + QCoreApplication::addLibraryPath(p); #endif - QGuiApplication application(argc, argv); - QVariantMap parameters; - QStringList args(QCoreApplication::arguments()); + QGuiApplication application(argc, argv); + QCoreApplication::setApplicationName(u"QtLocation Mapviewer example"_s); - if (parseArgs(args, parameters)) + QStringList args = QCoreApplication::arguments(); + args.removeFirst(); + if (args.contains(u"--help")) { + std::cout << qPrintable(QCoreApplication::applicationName()) << "\n\n" << help; return 0; - if (!args.contains(QStringLiteral("osm.useragent"))) - parameters[QStringLiteral("osm.useragent")] = QStringLiteral("QtLocation Mapviewer example"); + } + + QVariantMap parameters = parseArgs(args); + if (!parameters.contains(u"osm.useragent"_s)) + parameters.insert(u"osm.useragent"_s, QCoreApplication::applicationName()); QQmlApplicationEngine engine; #if QT_CONFIG(ssl) @@ -72,16 +71,16 @@ int main(int argc, char *argv[]) #else engine.rootContext()->setContextProperty("supportsSsl", false); #endif - engine.addImportPath(QStringLiteral(":/imports")); - engine.load(QUrl(QStringLiteral("qrc:///mapviewer.qml"))); - QObject::connect(&engine, SIGNAL(quit()), qApp, SLOT(quit())); - - QObject *item = engine.rootObjects().first(); - Q_ASSERT(item); + engine.addImportPath(u":/imports"_s); + engine.load(QUrl(u"qrc:///mapviewer.qml"_s)); + QObject::connect(&engine, &QQmlApplicationEngine::quit, + qApp, QCoreApplication::quit); - QMetaObject::invokeMethod(item, "initializeProviders", - Q_ARG(QVariant, QVariant::fromValue(parameters))); + auto *item = engine.rootObjects().value(0); + if (item == nullptr) + return -1; + QMetaObject::invokeMethod(item, "initializeProviders", QVariant::fromValue(parameters)); return application.exec(); } diff --git a/examples/location/places/main.cpp b/examples/location/places/main.cpp index 6885d231..32d392c5 100644 --- a/examples/location/places/main.cpp +++ b/examples/location/places/main.cpp @@ -1,71 +1,68 @@ // Copyright (C) 2017 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#include -#include #include #include -#include -static bool parseArgs(QStringList& args, QVariantMap& parameters) -{ +#include - while (!args.isEmpty()) { +#include - QString param = args.takeFirst(); +using namespace Qt::StringLiterals; - if (param.startsWith("--help")) { - QTextStream out(stdout); - out << "Usage: " << Qt::endl; - out << "--plugin. - Sets parameter = value for plugin" << Qt::endl; - out.flush(); - return true; - } - - if (param.startsWith("--plugin.")) { +static const char *help = R"(Usage: +--plugin. - Sets parameter = value for plugin +)"; +static QVariantMap parseArgs(QStringList args) +{ + QVariantMap parameters; + while (!args.isEmpty()) { + QString param = args.takeFirst(); + if (param.startsWith(u"--plugin.")) { param.remove(0, 9); - - if (args.isEmpty() || args.first().startsWith("--")) { - parameters[param] = true; + if (args.isEmpty() || args.constFirst().startsWith(u"--")) { + parameters.insert(param, QVariant(true)); } else { - QString value = args.takeFirst(); - - if (value == "true" || value == "on" || value == "enabled") { - parameters[param] = true; - } else if (value == "false" || value == "off" - || value == "disable") { - parameters[param] = false; + if (value == u"true" || value == u"on" || value == u"enabled") { + parameters.insert(param, QVariant(true)); + } else if (value == u"false" || value == u"off" + || value == u"disable") { + parameters.insert(param, QVariant(false)); } else { - parameters[param] = value; + parameters.insert(param, QVariant(value)); } } } } - return false; + return parameters; } int main(int argc, char *argv[]) { QGuiApplication application(argc, argv); - QVariantMap parameters; - QStringList args(QCoreApplication::arguments()); - - if (parseArgs(args, parameters)) + QStringList args = QCoreApplication::arguments(); + args.removeFirst(); + if (args.contains(u"--help")) { + std::cout << qPrintable(QCoreApplication::applicationName()) << "\n\n" << help; return 0; + } + + QVariantMap parameters = parseArgs(args); QQmlApplicationEngine engine; - engine.addImportPath(QStringLiteral(":/imports")); - engine.load(QUrl(QStringLiteral("qrc:///places.qml"))); - QObject::connect(&engine, SIGNAL(quit()), qApp, SLOT(quit())); + engine.addImportPath(u":/imports"_s); + engine.load(QUrl(u"qrc:///places.qml"_s)); + QObject::connect(&engine, &QQmlApplicationEngine::quit, + qApp, QCoreApplication::quit); - QObject *item = engine.rootObjects().first(); - Q_ASSERT(item); + auto *item = engine.rootObjects().value(0); + if (item == nullptr) + return -1; - QMetaObject::invokeMethod(item, "initializeProviders", - Q_ARG(QVariant, QVariant::fromValue(parameters))); + QMetaObject::invokeMethod(item, "initializeProviders", QVariant::fromValue(parameters)); return application.exec(); } -- cgit v1.2.3