From 8fa0c2c390cc8dd3afae1bc4e939a653423a6449 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sivert=20Kr=C3=B8vel?= Date: Mon, 31 Oct 2022 21:09:19 +0100 Subject: [PATCH] McuSupport: Implement support for parsing path type from json The json kit files have an entry named "type" which is intended to specify whether a specific path points to a file or a directory (or something else). Until now, this entry has not been handled and all lineEdits expect a path, thus appearing red in the UI if supplied with a file. With this patch, support for the type "file" is added, with the possibility to support further types in parseLineEditType. Currently, only File and ExistingDirectory are supported, with the latter being used by default whenever no "type" entry is specified. Task-number: UL-6610 Change-Id: I252d4eff76d4a11b92ce55a0c0964446072e48c1 Reviewed-by: Daniele Bortolotti Reviewed-by: Reviewed-by: Leena Miettinen Reviewed-by: Eike Ziller --- src/plugins/mcusupport/mcupackage.cpp | 5 ++- src/plugins/mcusupport/mcupackage.h | 27 ++++++++------ src/plugins/mcusupport/mcusupportsdk.cpp | 37 ++++++++++++++++++- src/plugins/mcusupport/mcutargetdescription.h | 3 ++ src/plugins/mcusupport/mcutargetfactory.cpp | 3 +- src/plugins/mcusupport/test/unittest.cpp | 30 +++++++++++---- src/plugins/mcusupport/test/unittest.h | 2 + 7 files changed, 85 insertions(+), 22 deletions(-) diff --git a/src/plugins/mcusupport/mcupackage.cpp b/src/plugins/mcusupport/mcupackage.cpp index 63e0fb2aadc..f306873f40a 100644 --- a/src/plugins/mcusupport/mcupackage.cpp +++ b/src/plugins/mcusupport/mcupackage.cpp @@ -37,7 +37,8 @@ McuPackage::McuPackage(const SettingsHandler::Ptr &settingsHandler, const QStringList &versions, const QString &downloadUrl, const McuPackageVersionDetector *versionDetector, - const bool addToSystemPath) + const bool addToSystemPath, + const Utils::PathChooser::Kind &valueType) : settingsHandler(settingsHandler) , m_label(label) , m_defaultPath(settingsHandler->getPath(settingsKey, QSettings::SystemScope, defaultPath)) @@ -49,6 +50,7 @@ McuPackage::McuPackage(const SettingsHandler::Ptr &settingsHandler, , m_environmentVariableName(envVarName) , m_downloadUrl(downloadUrl) , m_addToSystemPath(addToSystemPath) + , m_valueType(valueType) { m_path = FilePath::fromUserInput(qtcEnvironmentVariable(m_environmentVariableName)); if (!m_path.exists()) { @@ -245,6 +247,7 @@ QWidget *McuPackage::widget() { auto *widget = new QWidget; m_fileChooser = new PathChooser(widget); + m_fileChooser->setExpectedKind(m_valueType); m_fileChooser->lineEdit()->setButtonIcon(FancyLineEdit::Right, Icons::RESET.icon()); m_fileChooser->lineEdit()->setButtonVisible(FancyLineEdit::Right, true); connect(m_fileChooser->lineEdit(), &FancyLineEdit::rightButtonClicked, this, [&] { diff --git a/src/plugins/mcusupport/mcupackage.h b/src/plugins/mcusupport/mcupackage.h index 7528bba91de..e30364df7a4 100644 --- a/src/plugins/mcusupport/mcupackage.h +++ b/src/plugins/mcusupport/mcupackage.h @@ -8,6 +8,7 @@ #include "settingshandler.h" #include +#include #include @@ -18,7 +19,6 @@ class ToolChain; } namespace Utils { -class PathChooser; class InfoLabel; class Id; } // namespace Utils @@ -30,17 +30,19 @@ class McuPackage : public McuAbstractPackage Q_OBJECT public: - McuPackage(const SettingsHandler::Ptr &settingsHandler, - const QString &label, - const Utils::FilePath &defaultPath, - const Utils::FilePath &detectionPath, - const QString &settingsKey, - const QString &cmakeVarName, - const QString &envVarName, - const QStringList &versions = {}, - const QString &downloadUrl = {}, - const McuPackageVersionDetector *versionDetector = nullptr, - const bool addToPath = false); + McuPackage( + const SettingsHandler::Ptr &settingsHandler, + const QString &label, + const Utils::FilePath &defaultPath, + const Utils::FilePath &detectionPath, + const QString &settingsKey, + const QString &cmakeVarName, + const QString &envVarName, + const QStringList &versions = {}, + const QString &downloadUrl = {}, + const McuPackageVersionDetector *versionDetector = nullptr, + const bool addToPath = false, + const Utils::PathChooser::Kind &valueType = Utils::PathChooser::Kind::ExistingDirectory); ~McuPackage() override = default; @@ -90,6 +92,7 @@ private: const QString m_environmentVariableName; const QString m_downloadUrl; const bool m_addToSystemPath; + const Utils::PathChooser::Kind m_valueType; Status m_status = Status::InvalidPath; }; // class McuPackage diff --git a/src/plugins/mcusupport/mcusupportsdk.cpp b/src/plugins/mcusupport/mcusupportsdk.cpp index 043aad927f3..4ddca854abc 100644 --- a/src/plugins/mcusupport/mcusupportsdk.cpp +++ b/src/plugins/mcusupport/mcusupportsdk.cpp @@ -9,6 +9,7 @@ #include "mcusupportconstants.h" #include "mcusupportoptions.h" #include "mcusupportplugin.h" +#include "mcusupporttr.h" #include "mcusupportversiondetection.h" #include "mcutarget.h" #include "mcutargetdescription.h" @@ -630,6 +631,39 @@ static VersionDetection parseVersionDetection(const QJsonObject &packageEntry) }; } +static Utils::PathChooser::Kind parseLineEditType(const QJsonValue &type) +{ + //Utility function to handle the different kinds of PathChooser + //Default is ExistingDirectory, see pathchooser.h for more options + const auto defaultValue = Utils::PathChooser::Kind::ExistingDirectory; + if (type.isUndefined()) { + //No "type" entry in the json file, this is not an error + return defaultValue; + } + + const QString typeString = type.toString(); + if (typeString.isNull()) { + printMessage(Tr::tr("Parsing error: the type entry in JSON kit files must be a string, " + "defaulting to \"path\"") + .arg(typeString), + true); + + return defaultValue; + + } else if (typeString.compare("file", Qt::CaseInsensitive) == 0) { + return Utils::PathChooser::File; + } else if (typeString.compare("path", Qt::CaseInsensitive) == 0) { + return Utils::PathChooser::ExistingDirectory; + } else { + printMessage(Tr::tr( + "Parsing error: the type entry \"%2\" in JSON kit files is not supported, " + "defaulting to \"path\"") + .arg(typeString), + true); + + return defaultValue; + } +} static PackageDescription parsePackage(const QJsonObject &cmakeEntry) { @@ -652,7 +686,8 @@ static PackageDescription parsePackage(const QJsonObject &cmakeEntry) FilePath::fromUserInput(detectionPathString), versions, parseVersionDetection(cmakeEntry), - cmakeEntry["addToSystemPath"].toBool()}; + cmakeEntry["addToSystemPath"].toBool(), + parseLineEditType(cmakeEntry["type"])}; } static QList parsePackages(const QJsonArray &cmakeEntries) diff --git a/src/plugins/mcusupport/mcutargetdescription.h b/src/plugins/mcusupport/mcutargetdescription.h index 3c8600b6604..c1cc87d6d92 100644 --- a/src/plugins/mcusupport/mcutargetdescription.h +++ b/src/plugins/mcusupport/mcutargetdescription.h @@ -4,6 +4,8 @@ #pragma once #include +#include + #include #include #include @@ -32,6 +34,7 @@ struct PackageDescription QStringList versions; VersionDetection versionDetection; bool shouldAddToSystemPath; + Utils::PathChooser::Kind type; }; //struct PackageDescription struct McuTargetDescription diff --git a/src/plugins/mcusupport/mcutargetfactory.cpp b/src/plugins/mcusupport/mcutargetfactory.cpp index c54a41b4409..f7105c36f5a 100644 --- a/src/plugins/mcusupport/mcutargetfactory.cpp +++ b/src/plugins/mcusupport/mcutargetfactory.cpp @@ -131,7 +131,8 @@ McuPackagePtr McuTargetFactory::createPackage(const PackageDescription &pkgDesc) pkgDesc.versions, {}, createVersionDetection(pkgDesc.versionDetection), - pkgDesc.shouldAddToSystemPath}}; + pkgDesc.shouldAddToSystemPath, + pkgDesc.type}}; } McuToolChainPackage *McuTargetFactory::createToolchain( diff --git a/src/plugins/mcusupport/test/unittest.cpp b/src/plugins/mcusupport/test/unittest.cpp index 86df7b69d27..b6fe45d2aeb 100644 --- a/src/plugins/mcusupport/test/unittest.cpp +++ b/src/plugins/mcusupport/test/unittest.cpp @@ -197,7 +197,8 @@ const PackageDescription Legacy::Constants::QT_FOR_MCUS_SDK_PACKAGE_VALIDATION_PATH, {}, VersionDetection{}, - false}; + false, + Utils::PathChooser::Kind::ExistingDirectory}; const McuTargetDescription::Platform platformDescription{id, "", @@ -393,8 +394,8 @@ bool createFakePath(const FilePath& path, const bool is_file = false) McuSupportTest::McuSupportTest() : targetFactory{settingsMockPtr} - , compilerDescription{armGccLabel, armGccEnvVar, TOOLCHAIN_DIR_CMAKE_VARIABLE, armGccLabel, armGccDirectorySetting, {}, {}, {}, {}, false} - , toochainFileDescription{armGccLabel, armGccEnvVar, TOOLCHAIN_FILE_CMAKE_VARIABLE, armGccLabel, armGccDirectorySetting, {}, {}, {}, {}, false} + , compilerDescription{armGccLabel, armGccEnvVar, TOOLCHAIN_DIR_CMAKE_VARIABLE, armGccLabel, armGccDirectorySetting, {}, {}, {}, {}, false, Utils::PathChooser::Kind::ExistingDirectory } + , toochainFileDescription{armGccLabel, armGccEnvVar, TOOLCHAIN_FILE_CMAKE_VARIABLE, armGccLabel, armGccDirectorySetting, {}, {}, {}, {}, false, Utils::PathChooser::Kind::ExistingDirectory } , targetDescription { "autotest-sourceFile", "2.0.1", @@ -751,7 +752,8 @@ void McuSupportTest::test_createTargets() freeRtosDetectionPath, {}, VersionDetection{}, - true}; + true, + Utils::PathChooser::Kind::ExistingDirectory}; targetDescription.toolchain.id = armGcc; auto [targets, packages]{targetFactory.createTargets(targetDescription, sdkPackagePtr)}; @@ -803,7 +805,8 @@ void McuSupportTest::test_createPackages() freeRtosDetectionPath, {}, VersionDetection{}, - true}; + true, + Utils::PathChooser::Kind::ExistingDirectory}; const auto packages{targetFactory.createPackages(targetDescription)}; QVERIFY(!packages.empty()); @@ -848,7 +851,8 @@ void McuSupportTest::test_useFallbackPathForToolchainWhenPathFromSettingsIsNotAv {}, {}, VersionDetection{}, - false}; + false, + Utils::PathChooser::Kind::ExistingDirectory}; McuTargetDescription::Toolchain toolchainDescription{armGcc, {}, compilerDescription, {}}; EXPECT_CALL(*settingsMockPtr, getPath(QString{armGccDirectorySetting}, _, FilePath{fallbackDir})) @@ -871,7 +875,8 @@ void McuSupportTest::test_usePathFromSettingsForToolchainPath() {}, {}, VersionDetection{}, - false}; + false, + Utils::PathChooser::Kind::ExistingDirectory}; McuTargetDescription::Toolchain toolchainDescription{armGcc, {}, compilerDescription, {}}; EXPECT_CALL(*settingsMockPtr, getPath(QString{armGccDirectorySetting}, _, FilePath{empty})) @@ -1788,5 +1793,16 @@ void McuSupportTest::test_emptyVersionDetectorFromJson() QVERIFY(freeRtos->getVersionDetector() == nullptr); } +void McuSupportTest::test_expectedValueType() +{ + const auto targetDescription = parseDescriptionJson(armgcc_mimxrt1050_evk_freertos_json); + + QCOMPARE(targetDescription.toolchain.file.type, Utils::PathChooser::Kind::File); + QCOMPARE(targetDescription.toolchain.compiler.type, Utils::PathChooser::Kind::ExistingDirectory); + QCOMPARE(targetDescription.boardSdk.type, Utils::PathChooser::Kind::ExistingDirectory); + QCOMPARE(targetDescription.freeRTOS.package.type, Utils::PathChooser::Kind::ExistingDirectory); + QCOMPARE(targetDescription.platform.entries[0].type, + Utils::PathChooser::Kind::ExistingDirectory); +} } // namespace McuSupport::Internal::Test diff --git a/src/plugins/mcusupport/test/unittest.h b/src/plugins/mcusupport/test/unittest.h index 088bad1475a..5d424fd02bc 100644 --- a/src/plugins/mcusupport/test/unittest.h +++ b/src/plugins/mcusupport/test/unittest.h @@ -110,6 +110,8 @@ private slots: void test_emptyVersionDetector(); void test_emptyVersionDetectorFromJson(); + void test_expectedValueType(); + private: QVersionNumber currentQulVersion{2, 0}; PackageMock *freeRtosPackage{new PackageMock};