From 17deadf5c378bd1c78fb5876fb4d5aa148f3f751 Mon Sep 17 00:00:00 2001 From: Thomas Hartmann Date: Thu, 13 Jan 2022 19:46:28 +0100 Subject: StudioWelcome: Show combobox for crash reporter This highlights to the user if the crash reporter is enabled. The patch also includes some adjustments for the design. Change-Id: I1a0be0d2b98df937dbeeb6bf8063f1aaa78793f5 Reviewed-by: Mahmoud Badri --- .../qml/splashscreen/Welcome_splash.qml | 132 +++++++++++++-------- src/plugins/studiowelcome/studiowelcomeplugin.cpp | 27 ++++- 2 files changed, 106 insertions(+), 53 deletions(-) diff --git a/src/plugins/studiowelcome/qml/splashscreen/Welcome_splash.qml b/src/plugins/studiowelcome/qml/splashscreen/Welcome_splash.qml index e7ef726611..412ceb7733 100644 --- a/src/plugins/studiowelcome/qml/splashscreen/Welcome_splash.qml +++ b/src/plugins/studiowelcome/qml/splashscreen/Welcome_splash.qml @@ -38,8 +38,8 @@ Rectangle { gradient: Gradient { orientation: Gradient.Horizontal - GradientStop { position: 0.0; color: "#333d56" } - GradientStop { position: 1.0; color: "#000728" } + GradientStop { position: 0.0; color: "#1d212a" } + GradientStop { position: 1.0; color: "#232c56" } } signal goNext @@ -56,33 +56,36 @@ Rectangle { if (crashReportingEnabled) { var configureButton = "" - + qsTr("[Configure]") + ""; + + qsTr("[Configure]") + ""; var settingPath = Qt.platform.os === "osx" - ? qsTr("Qt Creator > Preferences > Environment > System") - : qsTr("Tools > Options > Environment > System") + ? qsTr("Qt Creator > Preferences > Environment > System") + : qsTr("Tools > Options > Environment > System") var strOn = qsTr("Qt Design Studio collects crash reports for the sole purpose of fixing bugs. " - + "You can disable this feature under %1. %2").arg(settingPath).arg(configureButton) - var strOff = qsTr("Qt Design Studio can collect crash reports for the sole purpose of fixing bugs. " - + "You can enable this feature under %1. %2").arg(settingPath).arg(configureButton) + + "You can disable this feature under %1. %2").arg(settingPath).arg(configureButton) + var strOff = qsTr("Qt Design Studio can collect crash reports for the sole purpose of fixing bugs. " + + "You can enable this feature under %1. %2").arg(settingPath).arg(configureButton) crash_reporting_text.text = crashReportingOn ? strOn : strOff; + crashReportCheckBox.visible = true } } Image { id: logo - x: 16 - y: 16 + x: 15 + y: 11 + width: 66 + height: 50 source: "welcome_windows/logo.png" } Text { id: qt_design_studio - x: 16 - y: 93 - width: 250 - height: 55 - color: "#4cd265" + x: 13 + y: 81 + width: 336 + height: 46 + color: "#25709a" text: qsTr("Qt Design Studio") font.pixelSize: 36 font.family: StudioFonts.titilliumWeb_light @@ -90,46 +93,46 @@ Rectangle { Text { id: software_for_ui - x: 16 - y: 141 - width: 250 + x: 15 + y: 124 + width: 300 height: 30 color: "#ffffff" text: qsTr("Software for UI and UX Designers") renderType: Text.QtRendering - font.pixelSize: 18 + font.pixelSize: 15 font.family: StudioFonts.titilliumWeb_light } Text { id: copyright - x: 16 - y: 183 + x: 15 + y: 155 width: 270 height: 24 color: "#ffffff" - text: qsTr("Copyright 2008 - 2021 The Qt Company") - font.pixelSize: 16 + text: qsTr("Copyright 2008 - 2022 The Qt Company") + font.pixelSize: 14 font.family: StudioFonts.titilliumWeb_light } Text { id: all_rights_reserved - x: 16 - y: 207 + x: 15 + y: 174 width: 250 height: 24 color: "#ffffff" text: qsTr("All Rights Reserved") - font.pixelSize: 16 + font.pixelSize: 14 font.family: StudioFonts.titilliumWeb_light } Text { id: marketing_1 - x: 16 - y: 252 - width: 355 + x: 15 + y: 206 + width: 406 height: 31 color: "#ffffff" text: qsTr("Multi-paradigm language for creating highly dynamic applications.") @@ -141,9 +144,9 @@ Rectangle { Text { id: marketing_2 - x: 16 - y: 273 - width: 311 + x: 15 + y: 229 + width: 341 height: 31 color: "#ffffff" text: qsTr("Run your concepts and prototypes on your final hardware.") @@ -155,9 +158,9 @@ Rectangle { Text { id: marketing_3 - x: 16 - y: 294 - width: 311 + x: 15 + y: 252 + width: 336 height: 31 color: "#ffffff" text: qsTr("Seamless integration between designer and developer.") @@ -171,8 +174,8 @@ Rectangle { id: crash_reporting_text color: "#ffffff" textFormat: Text.RichText - x: 16 - y: 330 + x: 15 + y: 280 width: 311 wrapMode: Text.WordWrap font.family: StudioFonts.titilliumWeb_light @@ -229,41 +232,66 @@ Rectangle { ColumnLayout { anchors.left: parent.left anchors.bottom: parent.bottom - anchors.margins: 16 + anchors.leftMargin: 16 + anchors.bottomMargin: 10 + spacing: 3 CheckBox { - id: doNotShowCheckBox - text: qsTr("Do not show this again") + id: usageStatisticCheckBox + text: qsTr("Enable Usage Statistics") + checked: usageStatisticModel.usageStatisticEnabled padding: 0 spacing: 12 + onCheckedChanged: usageStatisticModel.setTelemetryEnabled(usageStatisticCheckBox.checked) + contentItem: Text { - text: doNotShowCheckBox.text + text: usageStatisticCheckBox.text color: "#ffffff" - leftPadding: doNotShowCheckBox.indicator.width + doNotShowCheckBox.spacing + leftPadding: usageStatisticCheckBox.indicator.width + usageStatisticCheckBox.spacing + font.pixelSize: 12 } } CheckBox { - id: usageStatisticCheckBox - text: qsTr("Enable Usage Statistics") - checked: usageStatisticModel.usageStatisticEnabled - padding: 0 + id: crashReportCheckBox + text: qsTr("Enable Crash Reports") spacing: 12 + checked: usageStatisticModel.crashReporterEnabled + visible: false - onCheckedChanged: usageStatisticModel.setTelemetryEnabled(usageStatisticCheckBox.checked) + onCheckedChanged: { + usageStatisticModel.setCrashReporterEnabled(crashReportCheckBox.checked) + welcome_splash.onPluginInitialized(true, crashReportCheckBox.checked) + } contentItem: Text { - text: usageStatisticCheckBox.text color: "#ffffff" - leftPadding: usageStatisticCheckBox.indicator.width + usageStatisticCheckBox.spacing + text: crashReportCheckBox.text + leftPadding: crashReportCheckBox.indicator.width + crashReportCheckBox.spacing + font.pixelSize: 12 + } + padding: 0 + } + + CheckBox { + id: doNotShowCheckBox + text: qsTr("Do not show this again") + padding: 0 + spacing: 12 + + contentItem: Text { + text: doNotShowCheckBox.text + color: "#ffffff" + leftPadding: doNotShowCheckBox.indicator.width + doNotShowCheckBox.spacing + font.pixelSize: 12 } } } RowLayout { x: 16 - y: 330 + y: 277 visible: welcome_splash.loadingPlugins Text { @@ -309,8 +337,8 @@ Rectangle { Text { id: all_rights_reserved1 - x: 16 - y: 75 + x: 15 + y: 65 color: "#ffffff" text: qsTr("Community Edition") font.pixelSize: 13 diff --git a/src/plugins/studiowelcome/studiowelcomeplugin.cpp b/src/plugins/studiowelcome/studiowelcomeplugin.cpp index bead95e667..d88dbf5072 100644 --- a/src/plugins/studiowelcome/studiowelcomeplugin.cpp +++ b/src/plugins/studiowelcome/studiowelcomeplugin.cpp @@ -88,6 +88,7 @@ const char DO_NOT_SHOW_SPLASHSCREEN_AGAIN_KEY[] = "StudioSplashScreen"; const char DETAILED_USAGE_STATISTICS[] = "DetailedUsageStatistics"; const char STATISTICS_COLLECTION_MODE[] = "StatisticsCollectionMode"; const char NO_TELEMETRY[] = "NoTelemetry"; +const char CRASH_REPORTER_SETTING[] = "CrashReportingEnabled"; QPointer s_view = nullptr; static StudioWelcomePlugin *s_pluginInstance = nullptr; @@ -122,6 +123,8 @@ class UsageStatisticPluginModel : public QObject Q_OBJECT Q_PROPERTY(bool usageStatisticEnabled MEMBER m_usageStatisticEnabled NOTIFY usageStatisticChanged) + Q_PROPERTY(bool crashReporterEnabled MEMBER m_crashReporterEnabled NOTIFY crashReporterEnabledChanged) + public: explicit UsageStatisticPluginModel(QObject *parent = nullptr) : QObject(parent) @@ -135,7 +138,27 @@ public: QVariant value = settings->value(STATISTICS_COLLECTION_MODE); m_usageStatisticEnabled = value.isValid() && value.toString() == DETAILED_USAGE_STATISTICS; + m_crashReporterEnabled = Core::ICore::settings()->value(CRASH_REPORTER_SETTING, false).toBool(); + emit usageStatisticChanged(); + emit crashReporterEnabledChanged(); + } + + Q_INVOKABLE void setCrashReporterEnabled(bool b) + { + if (m_crashReporterEnabled == b) + return; + + Core::ICore::settings()->setValue(CRASH_REPORTER_SETTING, b); + + s_pluginInstance->pauseRemoveSplashTimer(); + + const QString restartText = tr("The change will take effect after restart."); + Core::RestartDialog restartDialog(Core::ICore::dialogParent(), restartText); + restartDialog.exec(); + + s_pluginInstance->resumeRemoveSplashTimer(); + setupModel(); } Q_INVOKABLE void setTelemetryEnabled(bool b) @@ -160,9 +183,11 @@ public: signals: void usageStatisticChanged(); + void crashReporterEnabledChanged(); private: bool m_usageStatisticEnabled = false; + bool m_crashReporterEnabled = false; }; class ProjectModel : public QAbstractListModel @@ -564,7 +589,7 @@ bool StudioWelcomePlugin::delayedInitialize() #ifdef ENABLE_CRASHPAD const bool crashReportingEnabled = true; - const bool crashReportingOn = Core::ICore::settings()->value("CrashReportingEnabled", false).toBool(); + const bool crashReportingOn = Core::ICore::settings()->value(CRASH_REPORTER_SETTING, false).toBool(); #else const bool crashReportingEnabled = false; const bool crashReportingOn = false; -- cgit v1.2.1 From b01be6496375496a4e48807e9b7036ac9ae7a534 Mon Sep 17 00:00:00 2001 From: David Schulz Date: Fri, 14 Jan 2022 14:35:07 +0100 Subject: Editor: fix crash in function hint widget Fixes: QTCREATORBUG-26872 Change-Id: I0c79af2a74af96bfba350b62b5072b874e6efcd2 Reviewed-by: Eike Ziller --- src/plugins/texteditor/codeassist/functionhintproposalwidget.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/texteditor/codeassist/functionhintproposalwidget.cpp b/src/plugins/texteditor/codeassist/functionhintproposalwidget.cpp index e96071cec5..91a00386e1 100644 --- a/src/plugins/texteditor/codeassist/functionhintproposalwidget.cpp +++ b/src/plugins/texteditor/codeassist/functionhintproposalwidget.cpp @@ -221,7 +221,7 @@ void FunctionHintProposalWidget::closeProposal() bool FunctionHintProposalWidget::proposalIsVisible() const { - return d->m_popupFrame->isVisible(); + return d->m_popupFrame && d->m_popupFrame->isVisible(); } void FunctionHintProposalWidget::abort() -- cgit v1.2.1 From fc605c8c6f9009f0ff9326562b86bf0469e8e32e Mon Sep 17 00:00:00 2001 From: Thomas Hartmann Date: Fri, 14 Jan 2022 13:48:30 +0100 Subject: StudioWelcome: Slight adjustements to Splash Screen MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Anchoring the text to the checkbox and use configure instead of enable/disable, because there already is the checkbox. Change-Id: Ied6f231cec54d1fbf924b34bf0a8850cefc8bffb Reviewed-by: Mahmoud Badri Reviewed-by: Jarko Vihriala Reviewed-by: Kimmo Leppälä --- src/plugins/studiowelcome/qml/splashscreen/Welcome_splash.qml | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/plugins/studiowelcome/qml/splashscreen/Welcome_splash.qml b/src/plugins/studiowelcome/qml/splashscreen/Welcome_splash.qml index 412ceb7733..86a1dae392 100644 --- a/src/plugins/studiowelcome/qml/splashscreen/Welcome_splash.qml +++ b/src/plugins/studiowelcome/qml/splashscreen/Welcome_splash.qml @@ -60,12 +60,10 @@ Rectangle { var settingPath = Qt.platform.os === "osx" ? qsTr("Qt Creator > Preferences > Environment > System") : qsTr("Tools > Options > Environment > System") - var strOn = qsTr("Qt Design Studio collects crash reports for the sole purpose of fixing bugs. " - + "You can disable this feature under %1. %2").arg(settingPath).arg(configureButton) - var strOff = qsTr("Qt Design Studio can collect crash reports for the sole purpose of fixing bugs. " - + "You can enable this feature under %1. %2").arg(settingPath).arg(configureButton) + var strConfigure = qsTr("Qt Design Studio collects usage statistics and crash reports for the sole purpose of fixing bugs and improving the tool. " + + "You can configure the crash reporter under %1. %2").arg(settingPath).arg(configureButton) - crash_reporting_text.text = crashReportingOn ? strOn : strOff; + crash_reporting_text.text = strConfigure crashReportCheckBox.visible = true } } @@ -173,11 +171,13 @@ Rectangle { Text { id: crash_reporting_text color: "#ffffff" + anchors.bottom: columnLayout.top textFormat: Text.RichText x: 15 y: 280 width: 311 wrapMode: Text.WordWrap + anchors.bottomMargin: 8 font.family: StudioFonts.titilliumWeb_light font.pixelSize: 12 font.wordSpacing: 0 @@ -230,6 +230,7 @@ Rectangle { } ColumnLayout { + id: columnLayout anchors.left: parent.left anchors.bottom: parent.bottom anchors.leftMargin: 16 -- cgit v1.2.1 From c1c147a9dc99cba08582e83ef00883ece87d9e4f Mon Sep 17 00:00:00 2001 From: Samuel Ghinet Date: Mon, 10 Jan 2022 15:48:29 +0200 Subject: QDS-5691 Create a tab for Recent choices The Recents should store presets, rather than normal project items, while the rest of tabs are to store normal project (i.e. wizard) items but with the default screen size written under the wizard name. In this patch I also did a few renames: e.g. the Presets view now uses a PresetModel rather than ProjectModel, because we now store presets. A Preset is a higher level concept than Project / Wizard item: it can be a project/wizard item with pre-defined configurations; and now we can have multiple presets using the same Wizard factory. Renamed struct ProjectCategory to WizardCategory, because the items are grouped by the category of the wizard (i.e. the "category" property of IWizardFactory) I extracted a class, PresetData, to hold the data that is being shared by the PresetModel (items in the view) and the PresetCategoryModel (header/tab items). It stored both information on normal presets and on recent presets. Made changes to JsonWizardFactory so that I could extract the list of screen sizes without requiring to build a wizard object first. This is important, because multiple JsonWizard objects cannot be created at the same time and I need to show the screen sizes of multiple presets / wizards as the Presets view is opened. This also required class WizardFactories to use JsonWizardFactory instead of Core::IWizardFactory -- since "screen sizes" are a particularity of the json wizards, not of all kinds of wizards. Also, fixed a TODO in WizardHandler::reset() method. Also, added a few utilities I had need of, in algorithm.h. Change-Id: Ifd986e2def19b2e112f0aa1ab3db63d522736321 Reviewed-by: Qt CI Bot Reviewed-by: Mahmoud Badri Reviewed-by: Thomas Hartmann --- .../newprojectdialog/NewProjectDialog.qml | 2 +- .../imports/NewProjectDialog/NewProjectView.qml | 10 +- .../jsonwizard/jsonwizardfactory.cpp | 53 +++++ .../projectexplorer/jsonwizard/jsonwizardfactory.h | 2 + src/plugins/studiowelcome/CMakeLists.txt | 4 +- src/plugins/studiowelcome/algorithm.h | 108 ++++++++++ src/plugins/studiowelcome/newprojectmodel.cpp | 108 ---------- src/plugins/studiowelcome/newprojectmodel.h | 146 ------------- src/plugins/studiowelcome/presetmodel.cpp | 156 ++++++++++++++ src/plugins/studiowelcome/presetmodel.h | 180 ++++++++++++++++ src/plugins/studiowelcome/qdsnewdialog.cpp | 62 ++++-- src/plugins/studiowelcome/qdsnewdialog.h | 23 +- src/plugins/studiowelcome/recentpresets.cpp | 110 ++++++++++ src/plugins/studiowelcome/recentpresets.h | 59 +++++ src/plugins/studiowelcome/screensizemodel.h | 2 +- src/plugins/studiowelcome/studiowelcome.pro | 6 +- src/plugins/studiowelcome/studiowelcome.qbs | 6 +- src/plugins/studiowelcome/stylemodel.h | 2 +- src/plugins/studiowelcome/wizardfactories.cpp | 55 +++-- src/plugins/studiowelcome/wizardfactories.h | 24 ++- src/plugins/studiowelcome/wizardhandler.cpp | 51 ++++- src/plugins/studiowelcome/wizardhandler.h | 13 +- tests/auto/qml/qmldesigner/wizard/CMakeLists.txt | 7 +- .../qml/qmldesigner/wizard/presetmodel-test.cpp | 234 ++++++++++++++++++++ .../qml/qmldesigner/wizard/recentpresets-test.cpp | 239 +++++++++++++++++++++ tests/auto/qml/qmldesigner/wizard/test-main.cpp | 52 +++++ tests/auto/qml/qmldesigner/wizard/test-utilities.h | 3 + .../qmldesigner/wizard/wizardfactories-test.cpp | 133 ++++++++---- 28 files changed, 1468 insertions(+), 382 deletions(-) create mode 100644 src/plugins/studiowelcome/algorithm.h delete mode 100644 src/plugins/studiowelcome/newprojectmodel.cpp delete mode 100644 src/plugins/studiowelcome/newprojectmodel.h create mode 100644 src/plugins/studiowelcome/presetmodel.cpp create mode 100644 src/plugins/studiowelcome/presetmodel.h create mode 100644 src/plugins/studiowelcome/recentpresets.cpp create mode 100644 src/plugins/studiowelcome/recentpresets.h create mode 100644 tests/auto/qml/qmldesigner/wizard/presetmodel-test.cpp create mode 100644 tests/auto/qml/qmldesigner/wizard/recentpresets-test.cpp create mode 100644 tests/auto/qml/qmldesigner/wizard/test-main.cpp diff --git a/share/qtcreator/qmldesigner/newprojectdialog/NewProjectDialog.qml b/share/qtcreator/qmldesigner/newprojectdialog/NewProjectDialog.qml index aa5b346b4e..9176af9e34 100644 --- a/share/qtcreator/qmldesigner/newprojectdialog/NewProjectDialog.qml +++ b/share/qtcreator/qmldesigner/newprojectdialog/NewProjectDialog.qml @@ -185,7 +185,7 @@ Item { anchors.fill: parent onClicked: { tabBarRow.currIndex = index - projectModel.setPage(index) + presetModel.setPage(index) projectView.currentIndex = 0 projectView.currentIndexChanged() diff --git a/share/qtcreator/qmldesigner/newprojectdialog/imports/NewProjectDialog/NewProjectView.qml b/share/qtcreator/qmldesigner/newprojectdialog/imports/NewProjectDialog/NewProjectView.qml index 4270c0946a..e011057892 100644 --- a/share/qtcreator/qmldesigner/newprojectdialog/imports/NewProjectDialog/NewProjectView.qml +++ b/share/qtcreator/qmldesigner/newprojectdialog/imports/NewProjectDialog/NewProjectView.qml @@ -49,17 +49,17 @@ GridView { } ] - model: projectModel + model: presetModel // called by onModelReset and when user clicks on an item, or when the header item is changed. onCurrentIndexChanged: { - dialogBox.selectedProject = projectView.currentIndex - var source = dialogBox.currentProjectQmlPath() + dialogBox.selectedPreset = projectView.currentIndex + var source = dialogBox.currentPresetQmlPath() loader.source = source } Connections { - target: projectModel + target: presetModel // called when data is set (setWizardFactories) function onModelReset() { @@ -76,7 +76,7 @@ GridView { background: null function fontIconCode(index) { - var code = projectModel.fontIconCode(index) + var code = presetModel.fontIconCode(index) return code ? code : StudioTheme.Constants.wizardsUnknown } diff --git a/src/plugins/projectexplorer/jsonwizard/jsonwizardfactory.cpp b/src/plugins/projectexplorer/jsonwizard/jsonwizardfactory.cpp index 5439d98d2b..946d2d92ab 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonwizardfactory.cpp +++ b/src/plugins/projectexplorer/jsonwizard/jsonwizardfactory.cpp @@ -278,6 +278,59 @@ QVariant JsonWizardFactory::getDataValue(const QLatin1String &key, const QVarian return retVal; } +std::pair JsonWizardFactory::screenSizeInfoFromPage(const QString &pageType) const +{ + /* Retrieving the ScreenFactor "trKey" values from pages[i]/data[j]/data["items"], where + * pages[i] is the page of type `pageType` and data[j] is the data item with name ScreenFactor + */ + + const Utils::Id id = Utils::Id::fromString(Constants::PAGE_ID_PREFIX + pageType); + + const auto it = std::find_if(std::cbegin(m_pages), std::cend(m_pages), [&id](const Page &page) { + return page.typeId == id; + }); + + if (it == std::cend(m_pages)) + return {}; + + const QVariant data = it->data; + if (data.type() != QVariant::List) + return {}; + + const QVariant screenFactorField = Utils::findOrDefault(data.toList(), + [](const QVariant &field) { + const QVariantMap m = field.toMap(); + return "ScreenFactor" == m["name"]; + }); + + if (screenFactorField.type() != QVariant::Map) + return {}; + + const QVariant screenFactorData = screenFactorField.toMap()["data"]; + if (screenFactorData.type() != QVariant::Map) + return {}; + + const QVariantMap screenFactorDataMap = screenFactorData.toMap(); + if (not screenFactorDataMap.contains("items")) + return {}; + + bool ok = false; + const int index = screenFactorDataMap["index"].toInt(&ok); + const QVariantList items = screenFactorDataMap["items"].toList(); + if (items.isEmpty()) + return {}; + + QStringList values = Utils::transform(items, [](const QVariant &item) { + const QVariantMap m = item.toMap(); + return m["trKey"].toString(); + }); + + if (values.isEmpty()) + return {}; + + return std::make_pair(index, values); +} + JsonWizardFactory::Page JsonWizardFactory::parsePage(const QVariant &value, QString *errorMessage) { JsonWizardFactory::Page p; diff --git a/src/plugins/projectexplorer/jsonwizard/jsonwizardfactory.h b/src/plugins/projectexplorer/jsonwizard/jsonwizardfactory.h index 2ecabe6bc3..347692bf65 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonwizardfactory.h +++ b/src/plugins/projectexplorer/jsonwizard/jsonwizardfactory.h @@ -83,6 +83,8 @@ public: bool isAvailable(Utils::Id platformId) const override; + std::pair screenSizeInfoFromPage(const QString &pageType) const; + private: Utils::Wizard *runWizardImpl(const Utils::FilePath &path, QWidget *parent, Utils::Id platform, const QVariantMap &variables, bool showWizard = true) override; diff --git a/src/plugins/studiowelcome/CMakeLists.txt b/src/plugins/studiowelcome/CMakeLists.txt index e1f899d2fd..48c07d15b9 100644 --- a/src/plugins/studiowelcome/CMakeLists.txt +++ b/src/plugins/studiowelcome/CMakeLists.txt @@ -6,14 +6,16 @@ add_qtc_plugin(StudioWelcome SOURCES studiowelcomeplugin.cpp studiowelcomeplugin.h newprojectdialogimageprovider.cpp newprojectdialogimageprovider.h - newprojectmodel.cpp newprojectmodel.h + presetmodel.cpp presetmodel.h examplecheckout.cpp examplecheckout.h studiowelcome_global.h qdsnewdialog.cpp qdsnewdialog.h wizardfactories.cpp wizardfactories.h createproject.cpp createproject.h wizardhandler.cpp wizardhandler.h + recentpresets.cpp recentpresets.h screensizemodel.h + algorithm.h stylemodel.h stylemodel.cpp studiowelcome.qrc "${PROJECT_SOURCE_DIR}/src/share/3rdparty/studiofonts/studiofonts.qrc" diff --git a/src/plugins/studiowelcome/algorithm.h b/src/plugins/studiowelcome/algorithm.h new file mode 100644 index 0000000000..b7e53da5f1 --- /dev/null +++ b/src/plugins/studiowelcome/algorithm.h @@ -0,0 +1,108 @@ + +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** 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. +** +****************************************************************************/ + +#pragma once + +#include + +namespace Utils { + +//////// FIND +template +[[nodiscard]] typename Utils::optional findOptional(const C &container, + F function) +{ + auto begin = std::cbegin(container); + auto end = std::cend(container); + + auto it = std::find_if(begin, end, function); + return it == end ? nullopt : make_optional(*it); +} + +///////// FILTER +template +[[nodiscard]] C filterOut(const C &container, const T &value = T()) +{ + C out; + std::copy_if(std::begin(container), std::end(container), inserter(out), [&](const auto &item) { + return item != value; + }); + return out; +} + +template +[[nodiscard]] C filtered(const C &container) +{ + return filterOut(container, typename C::value_type{}); +} + +/////// MODIFY +template +void concat(C &out, const SC &container) +{ + std::copy(std::begin(container), std::end(container), inserter(out)); +} + +template +void erase_one(C &container, const T &value) +{ + typename C::const_iterator i = std::find(std::cbegin(container), std::cend(container), value); + if (i == std::cend(container)) + return; + + container.erase(i); +} + +template +void prepend(C &container, const T &value) +{ + container.insert(std::cbegin(container), value); +} + +/////// OTHER +template +[[nodiscard]] RC flatten(const SC &container) +{ + RC result; + + for (const auto &innerContainer : container) + concat(result, innerContainer); + + return result; +} + +template class C, + typename T, + typename... TArgs, + typename RT = typename std::decay_t::value_type, + typename RC = C> +[[nodiscard]] auto flatten(const C &container) +{ + using SC = C; + return flatten(container); +} + +} // namespace Utils diff --git a/src/plugins/studiowelcome/newprojectmodel.cpp b/src/plugins/studiowelcome/newprojectmodel.cpp deleted file mode 100644 index 3d5d9246d2..0000000000 --- a/src/plugins/studiowelcome/newprojectmodel.cpp +++ /dev/null @@ -1,108 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of Qt Creator. -** -** 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. -** -****************************************************************************/ - -#include "newprojectmodel.h" - -using namespace StudioWelcome; - -/****************** BaseNewProjectModel ******************/ - -BaseNewProjectModel::BaseNewProjectModel(QObject *parent) - : QAbstractListModel(parent) -{} - -QHash BaseNewProjectModel::roleNames() const -{ - QHash roleNames; - roleNames[Qt::UserRole] = "name"; - return roleNames; -} - -void BaseNewProjectModel::setProjects(const ProjectsByCategory &projectsByCategory) -{ - beginResetModel(); - - for (auto &[id, category] : projectsByCategory) { - m_categories.push_back(category.name); - m_projects.push_back(category.items); - } - - endResetModel(); -} - -/****************** NewProjectCategoryModel ******************/ - -NewProjectCategoryModel::NewProjectCategoryModel(QObject *parent) - : BaseNewProjectModel(parent) -{} - -int NewProjectCategoryModel::rowCount(const QModelIndex &) const -{ - return static_cast(categories().size()); -} - -QVariant NewProjectCategoryModel::data(const QModelIndex &index, int role) const -{ - Q_UNUSED(role) - return categories().at(index.row()); -} - -/****************** NewProjectModel ******************/ - -NewProjectModel::NewProjectModel(QObject *parent) - : BaseNewProjectModel(parent) -{} - -int NewProjectModel::rowCount(const QModelIndex &) const -{ - if (projects().empty()) - return 0; - - return static_cast(projectsOfCurrentCategory().size()); -} - -QVariant NewProjectModel::data(const QModelIndex &index, int role) const -{ - Q_UNUSED(role) - return projectsOfCurrentCategory().at(index.row()).name; -} - -void NewProjectModel::setPage(int index) -{ - beginResetModel(); - - m_page = static_cast(index); - - endResetModel(); -} - -QString NewProjectModel::fontIconCode(int index) const -{ - Utils::optional projectItem = project(index); - if (!projectItem) - return ""; - - return projectItem->fontIconCode; -} diff --git a/src/plugins/studiowelcome/newprojectmodel.h b/src/plugins/studiowelcome/newprojectmodel.h deleted file mode 100644 index ec7cc005ad..0000000000 --- a/src/plugins/studiowelcome/newprojectmodel.h +++ /dev/null @@ -1,146 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of Qt Creator. -** -** 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. -** -****************************************************************************/ - -#pragma once - -#include -#include -#include - -#include -#include - -namespace Utils { -class Wizard; -} - -namespace StudioWelcome { - -struct ProjectItem -{ - QString name; - QString categoryId; - QString description; - QUrl qmlPath; - QString fontIconCode; - std::function create; -}; - -inline QDebug &operator<<(QDebug &d, const ProjectItem &item) -{ - d << "name=" << item.name; - d << "; category = " << item.categoryId; - - return d; -} - -struct ProjectCategory -{ - QString id; - QString name; - std::vector items; -}; - -inline QDebug &operator<<(QDebug &d, const ProjectCategory &cat) -{ - d << "id=" << cat.id; - d << "; name=" << cat.name; - d << "; items=" << cat.items; - - return d; -} - -using ProjectsByCategory = std::map; - -/****************** BaseNewProjectModel ******************/ - -class BaseNewProjectModel : public QAbstractListModel -{ - using ProjectItems = std::vector>; - using Categories = std::vector; - -public: - explicit BaseNewProjectModel(QObject *parent = nullptr); - QHash roleNames() const override; - void setProjects(const ProjectsByCategory &projects); - -protected: - const ProjectItems &projects() const { return m_projects; } - const Categories &categories() const { return m_categories; } - -private: - ProjectItems m_projects; - Categories m_categories; -}; - -/****************** NewProjectCategoryModel ******************/ - -class NewProjectCategoryModel : public BaseNewProjectModel -{ -public: - explicit NewProjectCategoryModel(QObject *parent = nullptr); - int rowCount(const QModelIndex &parent) const override; - QVariant data(const QModelIndex &index, int role) const override; -}; - -/****************** NewProjectModel ******************/ - -class NewProjectModel : public BaseNewProjectModel -{ - Q_OBJECT -public: - explicit NewProjectModel(QObject *parent = nullptr); - int rowCount(const QModelIndex &parent) const override; - QVariant data(const QModelIndex &index, int role) const override; - - Q_INVOKABLE void setPage(int index); // called from QML when view's header item is clicked - Q_INVOKABLE QString fontIconCode(int index) const; - - int page() const { return static_cast(m_page); } - - Utils::optional project(size_t selection) const - { - if (projects().empty()) - return {}; - - if (m_page < projects().size()) { - const std::vector projectsOfCategory = projects().at(m_page); - if (selection < projectsOfCategory.size()) - return projects().at(m_page).at(selection); - } - return {}; - } - - bool empty() const { return projects().empty(); } - -private: - const std::vector projectsOfCurrentCategory() const - { return projects().at(m_page); } - -private: - size_t m_page = 0; -}; - -} // namespace StudioWelcome diff --git a/src/plugins/studiowelcome/presetmodel.cpp b/src/plugins/studiowelcome/presetmodel.cpp new file mode 100644 index 0000000000..b8db0503f2 --- /dev/null +++ b/src/plugins/studiowelcome/presetmodel.cpp @@ -0,0 +1,156 @@ +/**************************************************************************** +** +** Copyright (C) 2022 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** 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. +** +****************************************************************************/ + +#include "presetmodel.h" +#include +#include + +#include "algorithm.h" + +using namespace StudioWelcome; + +/****************** PresetData ******************/ + +void PresetData::setData(const PresetsByCategory &presetsByCategory, + const std::vector &loadedRecents) +{ + QTC_ASSERT(!presetsByCategory.empty(), return); + m_recents = loadedRecents; + + if (!m_recents.empty()) { + m_categories.push_back("Recents"); + m_presets.push_back({}); + } + + for (auto &[id, category] : presetsByCategory) { + m_categories.push_back(category.name); + m_presets.push_back(category.items); + } + + PresetItems presets = Utils::flatten(m_presets); + + std::vector recentPresets = makeRecentPresets(presets); + + if (!m_recents.empty()) + m_presets[0] = recentPresets; +} + +std::vector PresetData::makeRecentPresets(const PresetItems &wizardPresets) +{ + static const PresetItem empty; + + PresetItems result; + + for (const RecentPreset &recent : m_recents) { + auto item = Utils::findOptional(wizardPresets, [&recent](const PresetItem &item) { + return item.categoryId == std::get<0>(recent) && item.name == std::get<1>(recent); + }); + + if (item) { + item->screenSizeName = std::get<2>(recent); + result.push_back(item.value()); + } + } + + return result; +} + +/****************** BasePresetModel ******************/ + +BasePresetModel::BasePresetModel(const PresetData *data, QObject *parent) + : QAbstractListModel(parent) + , m_data{data} +{} + +QHash BasePresetModel::roleNames() const +{ + QHash roleNames; + roleNames[Qt::UserRole] = "name"; + return roleNames; +} + +/****************** PresetCategoryModel ******************/ + +PresetCategoryModel::PresetCategoryModel(const PresetData *data, QObject *parent) + : BasePresetModel(data, parent) +{} + +int PresetCategoryModel::rowCount(const QModelIndex &) const +{ + return static_cast(m_data->categories().size()); +} + +QVariant PresetCategoryModel::data(const QModelIndex &index, int role) const +{ + Q_UNUSED(role) + return m_data->categories().at(index.row()); +} + +/****************** PresetModel ******************/ + +PresetModel::PresetModel(const PresetData *data, QObject *parent) + : BasePresetModel(data, parent) +{} + +QHash PresetModel::roleNames() const +{ + QHash roleNames; + roleNames[Qt::UserRole] = "name"; + roleNames[Qt::UserRole + 1] = "size"; + return roleNames; +} + +int PresetModel::rowCount(const QModelIndex &) const +{ + if (m_data->presets().empty()) + return 0; + + return static_cast(presetsOfCurrentCategory().size()); +} + +QVariant PresetModel::data(const QModelIndex &index, int role) const +{ + Q_UNUSED(role) + PresetItem preset = presetsOfCurrentCategory().at(index.row()); + return QVariant::fromValue(preset.name + "\n" + preset.screenSizeName); +} + +void PresetModel::setPage(int index) +{ + beginResetModel(); + + m_page = static_cast(index); + + endResetModel(); +} + +QString PresetModel::fontIconCode(int index) const +{ + Utils::optional presetItem = preset(index); + if (!presetItem) + return {}; + + return presetItem->fontIconCode; +} diff --git a/src/plugins/studiowelcome/presetmodel.h b/src/plugins/studiowelcome/presetmodel.h new file mode 100644 index 0000000000..a1c9b0e7d2 --- /dev/null +++ b/src/plugins/studiowelcome/presetmodel.h @@ -0,0 +1,180 @@ +/**************************************************************************** +** +** Copyright (C) 2022 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** 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. +** +****************************************************************************/ + +#pragma once + +#include +#include +#include + +#include +#include + +#include "recentpresets.h" + +namespace Utils { +class Wizard; +} + +namespace StudioWelcome { + +struct PresetItem +{ + QString name; + QString categoryId; + QString screenSizeName; + QString description; + QUrl qmlPath; + QString fontIconCode; + std::function create; +}; + +inline QDebug &operator<<(QDebug &d, const PresetItem &item) +{ + d << "name=" << item.name; + d << "; category = " << item.categoryId; + d << "; size = " << item.screenSizeName; + + return d; +} + +inline bool operator==(const PresetItem &lhs, const PresetItem &rhs) +{ + return lhs.categoryId == rhs.categoryId && lhs.name == rhs.name; +} + +struct WizardCategory +{ + QString id; + QString name; + std::vector items; +}; + +inline QDebug &operator<<(QDebug &d, const WizardCategory &cat) +{ + d << "id=" << cat.id; + d << "; name=" << cat.name; + d << "; items=" << cat.items; + + return d; +} + +using PresetsByCategory = std::map; +using PresetItems = std::vector; +using Categories = std::vector; + +/****************** PresetData ******************/ + +class PresetData +{ +public: + void setData(const PresetsByCategory &presets, const std::vector &recents); + + const std::vector &presets() const { return m_presets; } + const Categories &categories() const { return m_categories; } + +private: + std::vector makeRecentPresets(const PresetItems &wizardPresets); + +private: + std::vector m_presets; + Categories m_categories; + std::vector m_recents; +}; + +/****************** PresetCategoryModel ******************/ + +class BasePresetModel : public QAbstractListModel +{ +public: + BasePresetModel(const PresetData *data, QObject *parent = nullptr); + QHash roleNames() const override; + + void reset() + { + beginResetModel(); + endResetModel(); + } + +protected: + const PresetData *m_data = nullptr; +}; + +/****************** PresetCategoryModel ******************/ + +class PresetCategoryModel : public BasePresetModel +{ +public: + PresetCategoryModel(const PresetData *data, QObject *parent = nullptr); + int rowCount(const QModelIndex &parent) const override; + QVariant data(const QModelIndex &index, int role) const override; +}; + +/****************** PresetModel ******************/ + +class PresetModel : public BasePresetModel +{ + Q_OBJECT + +public: + PresetModel(const PresetData *data, QObject *parent = nullptr); + QHash roleNames() const override; + int rowCount(const QModelIndex &parent) const override; + QVariant data(const QModelIndex &index, int role) const override; + + Q_INVOKABLE void setPage(int index); // called from QML when view's header item is clicked + Q_INVOKABLE QString fontIconCode(int index) const; + + int page() const { return static_cast(m_page); } + + Utils::optional preset(size_t selection) const + { + auto presets = m_data->presets(); + if (presets.empty()) + return {}; + + if (m_page < presets.size()) { + const std::vector presetsOfCategory = presets.at(m_page); + if (selection < presetsOfCategory.size()) + return presets.at(m_page).at(selection); + } + return {}; + } + + bool empty() const { return m_data->presets().empty(); } + +private: + const std::vector presetsOfCurrentCategory() const + { + return m_data->presets().at(m_page); + } + + std::vector presets() const { return m_data->presets(); } + +private: + size_t m_page = 0; +}; + +} // namespace StudioWelcome diff --git a/src/plugins/studiowelcome/qdsnewdialog.cpp b/src/plugins/studiowelcome/qdsnewdialog.cpp index 50e3a085ca..83a966caeb 100644 --- a/src/plugins/studiowelcome/qdsnewdialog.cpp +++ b/src/plugins/studiowelcome/qdsnewdialog.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include "createproject.h" @@ -67,16 +68,17 @@ QString uniqueProjectName(const QString &path) QdsNewDialog::QdsNewDialog(QWidget *parent) : m_dialog{new QQuickWidget(parent)} - , m_categoryModel{new NewProjectCategoryModel(this)} - , m_projectModel{new NewProjectModel(this)} + , m_categoryModel{new PresetCategoryModel(&m_presetData, this)} + , m_presetModel{new PresetModel(&m_presetData, this)} , m_screenSizeModel{new ScreenSizeModel(this)} , m_styleModel{new StyleModel(this)} + , m_recentsStore{Core::ICore::settings()} { setParent(m_dialog); m_dialog->rootContext()->setContextProperties(QVector{ {{"categoryModel"}, QVariant::fromValue(m_categoryModel.data())}, - {{"projectModel"}, QVariant::fromValue(m_projectModel.data())}, + {{"presetModel"}, QVariant::fromValue(m_presetModel.data())}, {{"screenSizeModel"}, QVariant::fromValue(m_screenSizeModel.data())}, {{"styleModel"}, QVariant::fromValue(m_styleModel.data())}, {{"dialogBox"}, QVariant::fromValue(this)}, @@ -94,7 +96,7 @@ QdsNewDialog::QdsNewDialog(QWidget *parent) m_dialog->setWindowModality(Qt::ApplicationModal); m_dialog->setWindowFlags(Qt::Dialog); m_dialog->setAttribute(Qt::WA_DeleteOnClose); - m_dialog->setMinimumSize(1066, 554); + m_dialog->setMinimumSize(1149, 554); QSize screenSize = m_dialog->screen()->geometry().size(); if (screenSize.height() < 1080) @@ -174,7 +176,12 @@ void QdsNewDialog::onWizardCreated(QStandardItemModel *screenSizeModel, QStandar m_styleModel->setBackendModel(styleModel); if (m_qmlDetailsLoaded) { + int index = m_wizard.screenSizeIndex(m_currentPreset->screenSizeName); + if (index > -1) + setScreenSizeIndex(index); + m_screenSizeModel->reset(); + emit haveVirtualKeyboardChanged(); emit haveTargetQtVersionChanged(); @@ -186,12 +193,12 @@ void QdsNewDialog::onWizardCreated(QStandardItemModel *screenSizeModel, QStandar m_styleModel->reset(); } -QString QdsNewDialog::currentProjectQmlPath() const +QString QdsNewDialog::currentPresetQmlPath() const { - if (!m_currentProject || m_currentProject->qmlPath.isEmpty()) - return ""; + if (!m_currentPreset || m_currentPreset->qmlPath.isEmpty()) + return {}; - return m_currentProject->qmlPath.toString(); + return m_currentPreset->qmlPath.toString(); } void QdsNewDialog::setScreenSizeIndex(int index) @@ -259,11 +266,14 @@ void QdsNewDialog::setWizardFactories(QList factories_, WizardFactories factories{factories_, m_dialog, platform}; - m_categoryModel->setProjects(factories.projectsGroupedByCategory()); // calls model reset - m_projectModel->setProjects(factories.projectsGroupedByCategory()); // calls model reset + std::vector recents = m_recentsStore.fetchAll(); + m_presetData.setData(factories.presetsGroupedByCategory(), recents); - if (m_qmlSelectedProject > -1) - setSelectedProject(m_qmlSelectedProject); + m_categoryModel->reset(); + m_presetModel->reset(); + + if (m_qmlSelectedPreset > -1) + setSelectedPreset(m_qmlSelectedPreset); if (factories.empty()) return; // TODO: some message box? @@ -277,8 +287,13 @@ void QdsNewDialog::setWizardFactories(QList factories_, m_qmlProjectLocation = Utils::FilePath::fromString(QDir::toNativeSeparators(projectLocation.toString())); emit projectLocationChanged(); // So that QML knows to update the field - if (m_qmlDetailsLoaded) + if (m_qmlDetailsLoaded) { + int index = m_wizard.screenSizeIndex(m_currentPreset->screenSizeName); + if (index > -1) + setScreenSizeIndex(index); + m_screenSizeModel->reset(); + } if (m_qmlStylesLoaded) m_styleModel->reset(); @@ -318,6 +333,11 @@ void QdsNewDialog::accept() .withTargetQtVersion(m_qmlTargetQtVersionIndex) .execute(); + PresetItem item = m_wizard.preset(); + QString screenSize = m_wizard.screenSizeName(m_qmlScreenSizeIndex); + + m_recentsStore.add(item.categoryId, item.name, screenSize); + m_dialog->close(); m_dialog->deleteLater(); m_dialog = nullptr; @@ -340,17 +360,17 @@ QString QdsNewDialog::chooseProjectLocation() return QDir::toNativeSeparators(newPath.toString()); } -void QdsNewDialog::setSelectedProject(int selection) +void QdsNewDialog::setSelectedPreset(int selection) { - if (m_qmlSelectedProject != selection || m_projectPage != m_projectModel->page()) { - m_qmlSelectedProject = selection; + if (m_qmlSelectedPreset != selection || m_presetPage != m_presetModel->page()) { + m_qmlSelectedPreset = selection; - m_currentProject = m_projectModel->project(m_qmlSelectedProject); - if (m_currentProject) { - setProjectDescription(m_currentProject->description); + m_currentPreset = m_presetModel->preset(m_qmlSelectedPreset); + if (m_currentPreset) { + setProjectDescription(m_currentPreset->description); - m_projectPage = m_projectModel->page(); - m_wizard.reset(m_currentProject.value(), m_qmlSelectedProject, m_qmlProjectLocation); + m_presetPage = m_presetModel->page(); + m_wizard.reset(m_currentPreset.value(), m_qmlSelectedPreset); } } } diff --git a/src/plugins/studiowelcome/qdsnewdialog.h b/src/plugins/studiowelcome/qdsnewdialog.h index 8e331bd125..476750e540 100644 --- a/src/plugins/studiowelcome/qdsnewdialog.h +++ b/src/plugins/studiowelcome/qdsnewdialog.h @@ -32,9 +32,10 @@ #include #include "wizardhandler.h" -#include "newprojectmodel.h" +#include "presetmodel.h" #include "screensizemodel.h" #include "stylemodel.h" +#include "recentpresets.h" QT_BEGIN_NAMESPACE class QStandardItemModel; @@ -46,7 +47,7 @@ class QdsNewDialog : public QObject, public Core::NewDialog Q_OBJECT public: - Q_PROPERTY(int selectedProject MEMBER m_qmlSelectedProject WRITE setSelectedProject) + Q_PROPERTY(int selectedPreset MEMBER m_qmlSelectedPreset WRITE setSelectedPreset) Q_PROPERTY(QString projectName MEMBER m_qmlProjectName WRITE setProjectName NOTIFY projectNameChanged) Q_PROPERTY(QString projectLocation MEMBER m_qmlProjectLocation READ projectLocation WRITE setProjectLocation NOTIFY projectLocationChanged) Q_PROPERTY(QString projectDescription MEMBER m_qmlProjectDescription READ projectDescription WRITE setProjectDescription NOTIFY projectDescriptionChanged) @@ -64,7 +65,8 @@ public: Q_PROPERTY(bool detailsLoaded MEMBER m_qmlDetailsLoaded) Q_PROPERTY(bool stylesLoaded MEMBER m_qmlStylesLoaded) - Q_INVOKABLE QString currentProjectQmlPath() const; + Q_INVOKABLE QString currentPresetQmlPath() const; + // TODO: screen size index should better be a property Q_INVOKABLE void setScreenSizeIndex(int index); // called when ComboBox item is "activated" Q_INVOKABLE int screenSizeIndex() const; Q_INVOKABLE void setTargetQtVersion(int index); @@ -79,7 +81,7 @@ public: const QVariantMap &extraVariables) override; void setWindowTitle(const QString &title) override { m_dialog->setWindowTitle(title); } void showDialog() override; - void setSelectedProject(int selection); + void setSelectedPreset(int selection); void setStyleIndex(int index); int getStyleIndex() const; @@ -135,14 +137,16 @@ private slots: private: QQuickWidget *m_dialog = nullptr; - QPointer m_categoryModel; - QPointer m_projectModel; + + PresetData m_presetData; + QPointer m_categoryModel; + QPointer m_presetModel; QPointer m_screenSizeModel; QPointer m_styleModel; QString m_qmlProjectName; Utils::FilePath m_qmlProjectLocation; QString m_qmlProjectDescription; - int m_qmlSelectedProject = -1; + int m_qmlSelectedPreset = -1; int m_qmlScreenSizeIndex = -1; int m_qmlTargetQtVersionIndex = -1; // m_qmlStyleIndex is like a cache, so it needs to be updated on get() @@ -155,7 +159,7 @@ private: QString m_qmlStatusMessage; QString m_qmlStatusType; - int m_projectPage = -1; // i.e. the page in the Presets View + int m_presetPage = -1; // i.e. the page in the Presets View QString m_qmlCustomWidth; QString m_qmlCustomHeight; @@ -163,9 +167,10 @@ private: bool m_qmlDetailsLoaded = false; bool m_qmlStylesLoaded = false; - Utils::optional m_currentProject; + Utils::optional m_currentPreset; WizardHandler m_wizard; + RecentPresetsStore m_recentsStore; }; } //namespace StudioWelcome diff --git a/src/plugins/studiowelcome/recentpresets.cpp b/src/plugins/studiowelcome/recentpresets.cpp new file mode 100644 index 0000000000..f7ea5b9d61 --- /dev/null +++ b/src/plugins/studiowelcome/recentpresets.cpp @@ -0,0 +1,110 @@ +/**************************************************************************** +** +** Copyright (C) 2022 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** 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. +** +****************************************************************************/ + +#include "recentpresets.h" +#include "algorithm.h" + +#include + +#include +#include +#include + +using Core::ICore; +using Utils::QtcSettings; + +using namespace StudioWelcome; + +constexpr char GROUP_NAME[] = "RecentPresets"; +constexpr char WIZARDS[] = "Wizards"; + +void RecentPresetsStore::add(const QString &categoryId, const QString &name, const QString &sizeName) +{ + std::vector existing = fetchAll(); + QStringList encodedRecents = addRecentToExisting(RecentPreset{categoryId, name, sizeName}, + existing); + + m_settings->beginGroup(GROUP_NAME); + m_settings->setValue(WIZARDS, encodedRecents); + m_settings->endGroup(); + m_settings->sync(); +} + +QStringList RecentPresetsStore::addRecentToExisting(const RecentPreset &preset, + std::vector &recents) +{ + Utils::erase_one(recents, preset); + Utils::prepend(recents, preset); + + if (recents.size() > m_max) + recents.pop_back(); + + return encodeRecentPresets(recents); +} + +std::vector RecentPresetsStore::fetchAll() const +{ + m_settings->beginGroup(GROUP_NAME); + QVariant value = m_settings->value(WIZARDS); + m_settings->endGroup(); + + std::vector result; + + if (value.type() == QVariant::String) + result.push_back(decodeOneRecentPreset(value.toString())); + else if (value.type() == QVariant::StringList) + Utils::concat(result, decodeRecentPresets(value.toList())); + + const RecentPreset empty; + return Utils::filtered(result, [&empty](const RecentPreset &recent) { return recent != empty; }); +} + +QStringList RecentPresetsStore::encodeRecentPresets(const std::vector &recents) +{ + return Utils::transform(recents, [](const RecentPreset &p) -> QString { + return std::get<0>(p) + "/" + std::get<1>(p) + ":" + std::get<2>(p); + }); +} + +RecentPreset RecentPresetsStore::decodeOneRecentPreset(const QString &encoded) +{ + QRegularExpression pattern{R"(^(\S+)/(.+):(\d+ x \d+))"}; + auto m = pattern.match(encoded); + if (!m.hasMatch()) + return RecentPreset{}; + + QString category = m.captured(1); + QString name = m.captured(2); + QString size = m.captured(3); + + return std::make_tuple(category, name, size); +} + +std::vector RecentPresetsStore::decodeRecentPresets(const QVariantList &values) +{ + return Utils::transform(values, [](const QVariant &value) { + return decodeOneRecentPreset(value.toString()); + }); +} diff --git a/src/plugins/studiowelcome/recentpresets.h b/src/plugins/studiowelcome/recentpresets.h new file mode 100644 index 0000000000..3c223d8df5 --- /dev/null +++ b/src/plugins/studiowelcome/recentpresets.h @@ -0,0 +1,59 @@ +/**************************************************************************** +** +** Copyright (C) 2022 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** 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. +** +****************************************************************************/ + +#pragma once + +#include +#include +#include + +namespace StudioWelcome { + +// preset category, preset name, size name +using RecentPreset = std::tuple; + +class RecentPresetsStore +{ +public: + explicit RecentPresetsStore(QSettings *settings) + : m_settings{settings} + {} + + void setMaximum(int n) { m_max = n; } + void add(const QString &categoryId, const QString &name, const QString &sizeName); + std::vector fetchAll() const; + +private: + QStringList addRecentToExisting(const RecentPreset &preset, std::vector &recents); + static QStringList encodeRecentPresets(const std::vector &recents); + static std::vector decodeRecentPresets(const QVariantList &values); + static RecentPreset decodeOneRecentPreset(const QString &encoded); + +private: + QSettings *m_settings = nullptr; + int m_max = 10; +}; + +} // namespace StudioWelcome diff --git a/src/plugins/studiowelcome/screensizemodel.h b/src/plugins/studiowelcome/screensizemodel.h index 7ea68966b8..597811146e 100644 --- a/src/plugins/studiowelcome/screensizemodel.h +++ b/src/plugins/studiowelcome/screensizemodel.h @@ -84,7 +84,7 @@ public: return item->text(); } - return ""; + return {}; } QHash roleNames() const override diff --git a/src/plugins/studiowelcome/studiowelcome.pro b/src/plugins/studiowelcome/studiowelcome.pro index 9b7e756a56..3fb9fbb1cc 100644 --- a/src/plugins/studiowelcome/studiowelcome.pro +++ b/src/plugins/studiowelcome/studiowelcome.pro @@ -17,9 +17,10 @@ HEADERS += \ wizardfactories.h \ wizardhandler.h \ createproject.h \ - newprojectmodel.h \ + presetmodel.h \ examplecheckout.h \ screensizemodel.h \ + recentpresets.h \ stylemodel.h SOURCES += \ @@ -29,8 +30,9 @@ SOURCES += \ wizardhandler.cpp \ createproject.cpp \ newprojectdialogimageprovider.cpp \ - newprojectmodel.cpp \ + presetmodel.cpp \ examplecheckout.cpp \ + recentpresets.cpp \ stylemodel.cpp OTHER_FILES += \ diff --git a/src/plugins/studiowelcome/studiowelcome.qbs b/src/plugins/studiowelcome/studiowelcome.qbs index a0b81b872d..efb5c64e4f 100644 --- a/src/plugins/studiowelcome/studiowelcome.qbs +++ b/src/plugins/studiowelcome/studiowelcome.qbs @@ -21,8 +21,8 @@ QtcPlugin { "examplecheckout.cpp", "newprojectdialogimageprovider.h", "newprojectdialogimageprovider.cpp", - "newprojectmodel.cpp", - "newprojectmodel.h", + "presetmodel.cpp", + "presetmodel.h", "qdsnewdialog.cpp", "qdsnewdialog.h", "screensizemodel.h", @@ -36,6 +36,8 @@ QtcPlugin { "wizardfactories.h", "wizardhandler.cpp", "wizardhandler.h", + "recentpresets.cpp", + "recentpresets.h" ] Group { diff --git a/src/plugins/studiowelcome/stylemodel.h b/src/plugins/studiowelcome/stylemodel.h index 132b7251f8..554a71c02b 100644 --- a/src/plugins/studiowelcome/stylemodel.h +++ b/src/plugins/studiowelcome/stylemodel.h @@ -58,7 +58,7 @@ public: return item->text(); } - return ""; + return {}; } QHash roleNames() const override diff --git a/src/plugins/studiowelcome/wizardfactories.cpp b/src/plugins/studiowelcome/wizardfactories.cpp index 08479ac65b..dfd21918e8 100644 --- a/src/plugins/studiowelcome/wizardfactories.cpp +++ b/src/plugins/studiowelcome/wizardfactories.cpp @@ -23,30 +23,42 @@ ** ****************************************************************************/ +#include "wizardfactories.h" +#include "algorithm.h" + #include #include -#include -#include "wizardfactories.h" +#include #include using namespace StudioWelcome; WizardFactories::GetIconUnicodeFunc WizardFactories::m_getIconUnicode = &QmlDesigner::Theme::getIconUnicode; -WizardFactories::WizardFactories(QList &factories, QWidget *wizardParent, const Utils::Id &platform) +WizardFactories::WizardFactories(const QList &factories, + QWidget *wizardParent, + const Utils::Id &platform) : m_wizardParent{wizardParent} , m_platform{platform} - , m_factories{factories} { + m_factories = Utils::filtered(Utils::transform(factories, [](Core::IWizardFactory *f) { + return qobject_cast(f); + })); + sortByCategoryAndId(); filter(); - m_projectItems = makeProjectItemsGroupedByCategory(); + m_presetItems = makePresetItemsGroupedByCategory(); +} + +const Core::IWizardFactory *WizardFactories::front() const +{ + return m_factories.front(); } void WizardFactories::sortByCategoryAndId() { - Utils::sort(m_factories, [](Core::IWizardFactory *lhs, Core::IWizardFactory *rhs) { + Utils::sort(m_factories, [](JsonWizardFactory *lhs, JsonWizardFactory *rhs) { if (lhs->category() == rhs->category()) return lhs->id().toString() < rhs->id().toString(); else @@ -56,34 +68,43 @@ void WizardFactories::sortByCategoryAndId() void WizardFactories::filter() { - QList acceptedFactories = Utils::filtered(m_factories, [&](auto *wizard) { + QList acceptedFactories = Utils::filtered(m_factories, [&](auto *wizard) { return wizard->isAvailable(m_platform) - && wizard->kind() == Core::IWizardFactory::ProjectWizard - && wizard->requiredFeatures().contains("QtStudio"); + && wizard->kind() == JsonWizardFactory::ProjectWizard + && wizard->requiredFeatures().contains("QtStudio"); }); m_factories = acceptedFactories; } -ProjectItem WizardFactories::makeProjectItem(Core::IWizardFactory *f, QWidget *parent, +PresetItem WizardFactories::makePresetItem(JsonWizardFactory *f, QWidget *parent, const Utils::Id &platform) { using namespace std::placeholders; + QString sizeName; + auto [index, screenSizes] = f->screenSizeInfoFromPage("Fields"); + + if (index < 0 || index >= screenSizes.size()) + sizeName.clear(); + else + sizeName = screenSizes[index]; + return { /*.name =*/f->displayName(), /*.categoryId =*/f->category(), - /*. description =*/f->description(), + /*.screenSizeName=*/sizeName, + /*.description =*/f->description(), /*.qmlPath =*/f->detailsPageQmlPath(), /*.fontIconCode =*/m_getIconUnicode(f->fontIconName()), - /*.create =*/ std::bind(&Core::IWizardFactory::runWizard, f, _1, parent, platform, + /*.create =*/ std::bind(&JsonWizardFactory::runWizard, f, _1, parent, platform, QVariantMap(), false), }; } -std::map WizardFactories::makeProjectItemsGroupedByCategory() +std::map WizardFactories::makePresetItemsGroupedByCategory() { - QMap categories; + QMap categories; for (auto *f : std::as_const(m_factories)) { if (!categories.contains(f->category())) { @@ -92,12 +113,12 @@ std::map WizardFactories::makeProjectItemsGroupedByCat /*.name =*/ f->displayCategory(), /*.items = */ { - makeProjectItem(f, m_wizardParent, m_platform), + makePresetItem(f, m_wizardParent, m_platform), }, }; } else { - auto projectItem = makeProjectItem(f, m_wizardParent, m_platform); - categories[f->category()].items.push_back(projectItem); + auto presetItem = makePresetItem(f, m_wizardParent, m_platform); + categories[f->category()].items.push_back(presetItem); } } diff --git a/src/plugins/studiowelcome/wizardfactories.h b/src/plugins/studiowelcome/wizardfactories.h index ce3179e82c..aa209362de 100644 --- a/src/plugins/studiowelcome/wizardfactories.h +++ b/src/plugins/studiowelcome/wizardfactories.h @@ -25,7 +25,7 @@ #pragma once -#include "newprojectmodel.h" +#include "presetmodel.h" #include @@ -33,6 +33,12 @@ namespace Core { class IWizardFactory; } +namespace ProjectExplorer { +class JsonWizardFactory; +} + +using ProjectExplorer::JsonWizardFactory; + namespace StudioWelcome { class WizardFactories @@ -41,12 +47,12 @@ public: using GetIconUnicodeFunc = QString (*)(const QString &); public: - WizardFactories(QList &factories, QWidget *wizardParent, + WizardFactories(const QList &factories, QWidget *wizardParent, const Utils::Id &platform); - const Core::IWizardFactory *front() const { return m_factories.front(); } - const std::map &projectsGroupedByCategory() const - { return m_projectItems; } + const Core::IWizardFactory *front() const; + const std::map &presetsGroupedByCategory() const + { return m_presetItems; } bool empty() const { return m_factories.empty(); } static GetIconUnicodeFunc setIconUnicodeCallback(GetIconUnicodeFunc cb) @@ -58,15 +64,15 @@ private: void sortByCategoryAndId(); void filter(); - ProjectItem makeProjectItem(Core::IWizardFactory *f, QWidget *parent, const Utils::Id &platform); - std::map makeProjectItemsGroupedByCategory(); + PresetItem makePresetItem(JsonWizardFactory *f, QWidget *parent, const Utils::Id &platform); + std::map makePresetItemsGroupedByCategory(); private: QWidget *m_wizardParent; Utils::Id m_platform; // filter wizards to only those supported by this platform. - QList m_factories; - std::map m_projectItems; + QList m_factories; + std::map m_presetItems; static GetIconUnicodeFunc m_getIconUnicode; }; diff --git a/src/plugins/studiowelcome/wizardhandler.cpp b/src/plugins/studiowelcome/wizardhandler.cpp index bf9f0f31f7..cd176707ac 100644 --- a/src/plugins/studiowelcome/wizardhandler.cpp +++ b/src/plugins/studiowelcome/wizardhandler.cpp @@ -38,18 +38,17 @@ using namespace StudioWelcome; -void WizardHandler::reset(const ProjectItem &projectInfo, int projectSelection, const Utils::FilePath &location) +void WizardHandler::reset(const PresetItem &presetInfo, int presetSelection) { - m_projectItem = projectInfo; - m_projectLocation = location; - m_selectedProject = projectSelection; + m_preset = presetInfo; + m_selectedPreset = presetSelection; if (!m_wizard) { setupWizard(); } else { QObject::connect(m_wizard, &QObject::destroyed, this, &WizardHandler::onWizardResetting); - // DON'T SET `m_selectedProject = -1` --- we are switching now to a separate project. + // DON'T SET `m_selectedPreset = -1` --- we are switching now to a separate preset. emit deletingWizard(); m_wizard->deleteLater(); @@ -60,7 +59,7 @@ void WizardHandler::destroyWizard() { emit deletingWizard(); - m_selectedProject = -1; + m_selectedPreset = -1; m_wizard->deleteLater(); m_wizard = nullptr; m_detailsPage = nullptr; @@ -68,7 +67,7 @@ void WizardHandler::destroyWizard() void WizardHandler::setupWizard() { - m_wizard = m_projectItem.create(m_projectLocation); + m_wizard = m_preset.create(m_projectLocation); if (!m_wizard) { emit wizardCreationFailed(); return; @@ -161,8 +160,8 @@ void WizardHandler::onWizardResetting() // if have a wizard request pending => create new wizard // note: we always have a wizard request pending here, unless the dialogbox was requested to be destroyed. - // if m_selectedProject != -1 => the wizard was destroyed as a result of reset to a different project type - if (m_selectedProject > -1) + // if m_selectedPreset != -1 => the wizard was destroyed as a result of reset to a different preset type + if (m_selectedPreset > -1) setupWizard(); } @@ -175,6 +174,20 @@ void WizardHandler::setScreenSizeIndex(int index) cbfield->selectRow(index); } +QString WizardHandler::screenSizeName(int index) const +{ + auto *field = m_detailsPage->jsonField("ScreenFactor"); + auto *cbfield = dynamic_cast(field); + QTC_ASSERT(cbfield, return ""); + + QStandardItemModel *model = cbfield->model(); + if (index < 0 || index >= model->rowCount()) + return {}; + + QString text = model->item(index)->text(); + return text; +} + int WizardHandler::screenSizeIndex() const { auto *field = m_detailsPage->jsonField("ScreenFactor"); @@ -184,6 +197,24 @@ int WizardHandler::screenSizeIndex() const return cbfield->selectedRow(); } +int WizardHandler::screenSizeIndex(const QString &sizeName) const +{ + auto *field = m_detailsPage->jsonField("ScreenFactor"); + auto *cbfield = dynamic_cast(field); + QTC_ASSERT(cbfield, return false); + + const QStandardItemModel *model = cbfield->model(); + for (int i = 0; i < model->rowCount(); ++i) { + const QStandardItem *item = model->item(i, 0); + const QString text = item->text(); + + if (text == sizeName) + return i; + } + + return -1; +} + void WizardHandler::setTargetQtVersionIndex(int index) { auto *field = m_detailsPage->jsonField("TargetQtVersion"); @@ -250,7 +281,7 @@ void WizardHandler::run(const std::function &processPage) m_wizard->next(); } while (-1 != nextId); - m_selectedProject = -1; + m_selectedPreset = -1; // Note: don't call `emit deletingWizard()` here. diff --git a/src/plugins/studiowelcome/wizardhandler.h b/src/plugins/studiowelcome/wizardhandler.h index ad9424bc44..112accfb15 100644 --- a/src/plugins/studiowelcome/wizardhandler.h +++ b/src/plugins/studiowelcome/wizardhandler.h @@ -25,7 +25,7 @@ #pragma once -#include "newprojectmodel.h" +#include "presetmodel.h" #include #include @@ -47,9 +47,10 @@ class WizardHandler: public QObject Q_OBJECT public: - //TODO: location should not be needed in reset() -- only when creating the project - void reset(const ProjectItem &projectInfo, int projectSelection, const Utils::FilePath &location); + void reset(const PresetItem &presetInfo, int presetSelection); void setScreenSizeIndex(int index); + int screenSizeIndex(const QString &sizeName) const; + QString screenSizeName(int index) const; int screenSizeIndex() const; void setTargetQtVersionIndex(int index); bool haveTargetQtVersion() const; @@ -65,6 +66,8 @@ public: void run(const std::function &processPage); + PresetItem preset() const { return m_preset; } + signals: void deletingWizard(); void wizardCreated(QStandardItemModel *screenSizeModel, QStandardItemModel *styleModel); @@ -88,9 +91,9 @@ private: Utils::Wizard *m_wizard = nullptr; ProjectExplorer::JsonFieldPage *m_detailsPage = nullptr; - int m_selectedProject = -1; + int m_selectedPreset = -1; - ProjectItem m_projectItem; + PresetItem m_preset; Utils::FilePath m_projectLocation; }; diff --git a/tests/auto/qml/qmldesigner/wizard/CMakeLists.txt b/tests/auto/qml/qmldesigner/wizard/CMakeLists.txt index ca3a0ac7ec..de84a6e931 100644 --- a/tests/auto/qml/qmldesigner/wizard/CMakeLists.txt +++ b/tests/auto/qml/qmldesigner/wizard/CMakeLists.txt @@ -5,7 +5,7 @@ set(WITH_TESTS ON) find_package(Googletest MODULE) add_qtc_test(tst_qml_wizard - DEPENDS Core Utils StudioWelcome QmlDesigner Googletest + DEPENDS Core Utils StudioWelcome ProjectExplorer QmlDesigner Googletest DEFINES QT_CREATOR QMLDESIGNER_TEST @@ -17,8 +17,13 @@ add_qtc_test(tst_qml_wizard SOURCES wizardfactories-test.cpp stylemodel-test.cpp + recentpresets-test.cpp + presetmodel-test.cpp test-utilities.h + test-main.cpp "${StudioWelcomeDir}/wizardfactories.cpp" "${StudioWelcomeDir}/stylemodel.cpp" + "${StudioWelcomeDir}/recentpresets.cpp" + "${StudioWelcomeDir}/presetmodel.cpp" ) diff --git a/tests/auto/qml/qmldesigner/wizard/presetmodel-test.cpp b/tests/auto/qml/qmldesigner/wizard/presetmodel-test.cpp new file mode 100644 index 0000000000..dc855b67a3 --- /dev/null +++ b/tests/auto/qml/qmldesigner/wizard/presetmodel-test.cpp @@ -0,0 +1,234 @@ +/**************************************************************************** +** +** Copyright (C) 2022 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** 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. +** +****************************************************************************/ + +#include "test-utilities.h" + +#include "presetmodel.h" + +using namespace StudioWelcome; +using ::testing::ElementsAre; +using ::testing::ElementsAreArray; +using ::testing::PrintToString; + +namespace StudioWelcome { +void PrintTo(const PresetItem &item, std::ostream *os) +{ + *os << "{categId: " << item.categoryId << ", " + << "name: " << item.name; + + if (!item.screenSizeName.isEmpty()) + *os << ", size: " << item.screenSizeName; + + *os << "}"; +} + +} // namespace StudioWelcome + +namespace { +std::pair aCategory(const QString &categId, + const QString &categName, + const std::vector &names) +{ + std::vector items = Utils::transform(names, [&categId](const QString &name) { + return PresetItem{name, categId}; + }); + return std::make_pair(categId, WizardCategory{categId, categName, items}); +} + +MATCHER_P2(PresetIs, category, name, PrintToString(PresetItem{name, category})) +{ + return arg.categoryId == category && arg.name == name; +} + +MATCHER_P3(PresetIs, category, name, size, PrintToString(PresetItem{name, category, size})) +{ + return arg.categoryId == category && arg.name == name && size == arg.screenSizeName; +} + +} // namespace + +/******************* TESTS *******************/ + +TEST(QdsPresetModel, whenHaveNoPresetsNoRecentsReturnEmpty) +{ + PresetData data; + + ASSERT_THAT(data.presets(), SizeIs(0)); + ASSERT_THAT(data.categories(), SizeIs(0)); +} + +TEST(QdsPresetModel, haveSameArraySizeForPresetsAndCategories) +{ + PresetData data; + + data.setData( + { + aCategory("A.categ", "A", {"item a", "item b"}), + aCategory("B.categ", "B", {"item c", "item d"}), + }, + {/*recents*/}); + + ASSERT_THAT(data.presets(), SizeIs(2)); + ASSERT_THAT(data.categories(), SizeIs(2)); +} + +TEST(QdsPresetModel, haveWizardPresetsNoRecents) +{ + // Given + PresetData data; + + // When + data.setData( + { + aCategory("A.categ", "A", {"item a", "item b"}), + aCategory("B.categ", "B", {"item c", "item d"}), + }, + {/*recents*/}); + + // Then + ASSERT_THAT(data.categories(), ElementsAre("A", "B")); + ASSERT_THAT(data.presets()[0], + ElementsAre(PresetIs("A.categ", "item a"), PresetIs("A.categ", "item b"))); + ASSERT_THAT(data.presets()[1], + ElementsAre(PresetIs("B.categ", "item c"), PresetIs("B.categ", "item d"))); +} + +TEST(QdsPresetModel, haveRecentsNoWizardPresets) +{ + PresetData data; + + data.setData({/*wizardPresets*/}, + { + {"A.categ", "Desktop", "640 x 480"}, + {"B.categ", "Mobile", "800 x 600"}, + }); + + ASSERT_THAT(data.categories(), IsEmpty()); + ASSERT_THAT(data.presets(), IsEmpty()); +} + +TEST(QdsPresetModel, recentsAddedBeforeWizardPresets) +{ + // Given + PresetData data; + + // When + data.setData( + /*wizard presets*/ + { + aCategory("A.categ", "A", {"Desktop", "item b"}), + aCategory("B.categ", "B", {"item c", "Mobile"}), + }, + /*recents*/ + { + {"A.categ", "Desktop", "800 x 600"}, + {"B.categ", "Mobile", "640 x 480"}, + }); + + // Then + ASSERT_THAT(data.categories(), ElementsAre("Recents", "A", "B")); + + ASSERT_THAT(data.presets(), + ElementsAreArray( + {ElementsAre(PresetIs("A.categ", "Desktop"), PresetIs("B.categ", "Mobile")), + + ElementsAre(PresetIs("A.categ", "Desktop"), PresetIs("A.categ", "item b")), + ElementsAre(PresetIs("B.categ", "item c"), PresetIs("B.categ", "Mobile"))})); +} + +TEST(QdsPresetModel, recentsShouldNotSorted) +{ + // Given + PresetData data; + + // When + data.setData( + /*wizard presets*/ + { + aCategory("A.categ", "A", {"Desktop", "item b"}), + aCategory("B.categ", "B", {"item c", "Mobile"}), + aCategory("Z.categ", "Z", {"Z.desktop"}), + }, + /*recents*/ + { + {"Z.categ", "Z.desktop", "200 x 300"}, + {"B.categ", "Mobile", "200 x 300"}, + {"A.categ", "Desktop", "200 x 300"}, + }); + + // Then + ASSERT_THAT(data.presets()[0], + ElementsAre(PresetIs("Z.categ", "Z.desktop"), + PresetIs("B.categ", "Mobile"), + PresetIs("A.categ", "Desktop"))); +} + +TEST(QdsPresetModel, recentsOfSameWizardProjectButDifferentSizesAreRecognizedAsDifferentPresets) +{ + // Given + PresetData data; + + // When + data.setData( + /*wizard presets*/ + { + aCategory("A.categ", "A", {"Desktop"}), + aCategory("B.categ", "B", {"Mobile"}), + }, + /*recents*/ + { + {"B.categ", "Mobile", "400 x 400"}, + {"B.categ", "Mobile", "200 x 300"}, + {"A.categ", "Desktop", "640 x 480"}, + }); + + // Then + ASSERT_THAT(data.presets()[0], + ElementsAre(PresetIs("B.categ", "Mobile", "400 x 400"), + PresetIs("B.categ", "Mobile", "200 x 300"), + PresetIs("A.categ", "Desktop", "640 x 480"))); +} + +TEST(QdsPresetModel, outdatedRecentsAreNotShown) +{ + // Given + PresetData data; + + // When + data.setData( + /*wizard presets*/ + { + aCategory("A.categ", "A", {"Desktop"}), + aCategory("B.categ", "B", {"Mobile"}), + }, + /*recents*/ + { + {"B.categ", "NoLongerExists", "400 x 400"}, + {"A.categ", "Desktop", "640 x 480"}, + }); + + // Then + ASSERT_THAT(data.presets()[0], ElementsAre(PresetIs("A.categ", "Desktop", "640 x 480"))); +} diff --git a/tests/auto/qml/qmldesigner/wizard/recentpresets-test.cpp b/tests/auto/qml/qmldesigner/wizard/recentpresets-test.cpp new file mode 100644 index 0000000000..b1bb0e0626 --- /dev/null +++ b/tests/auto/qml/qmldesigner/wizard/recentpresets-test.cpp @@ -0,0 +1,239 @@ +/**************************************************************************** +** +** Copyright (C) 2022 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** 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. +** +****************************************************************************/ + +#include "test-utilities.h" + +#include +#include +#include + +#include "recentpresets.h" +#include "utils/filepath.h" +#include "utils/temporarydirectory.h" + +using namespace StudioWelcome; + +constexpr char GROUP_NAME[] = "RecentPresets"; +constexpr char ITEMS[] = "Wizards"; + +class QdsRecentPresets : public ::testing::Test +{ +protected: + RecentPresetsStore aStoreWithRecents(const QStringList &items) + { + settings.beginGroup(GROUP_NAME); + settings.setValue(ITEMS, items); + settings.endGroup(); + + return RecentPresetsStore{&settings}; + } + + RecentPresetsStore aStoreWithOne(const QVariant &item) + { + settings.beginGroup(GROUP_NAME); + settings.setValue(ITEMS, item); + settings.endGroup(); + + return RecentPresetsStore{&settings}; + } + +protected: + Utils::TemporaryDirectory tempDir{"recentpresets-XXXXXX"}; + QSettings settings{tempDir.filePath("test").toString(), QSettings::IniFormat}; + +private: + QString settingsPath; +}; + +/******************* TESTS *******************/ + +TEST_F(QdsRecentPresets, readFromEmptyStore) +{ + RecentPresetsStore store{&settings}; + + std::vector recents = store.fetchAll(); + + ASSERT_THAT(recents, IsEmpty()); +} + +TEST_F(QdsRecentPresets, readEmptyRecentPresets) +{ + RecentPresetsStore store = aStoreWithOne(""); + + std::vector recents = store.fetchAll(); + + ASSERT_THAT(recents, IsEmpty()); +} + +TEST_F(QdsRecentPresets, readOneRecentPresetAsList) +{ + RecentPresetsStore store = aStoreWithRecents({"category/preset:640 x 480"}); + + std::vector recents = store.fetchAll(); + + ASSERT_THAT(recents, ElementsAre(RecentPreset("category", "preset", "640 x 480"))); +} + +TEST_F(QdsRecentPresets, readOneRecentPresetAsString) +{ + RecentPresetsStore store = aStoreWithOne("category/preset:200 x 300"); + + std::vector recents = store.fetchAll(); + + ASSERT_THAT(recents, ElementsAre(RecentPreset("category", "preset", "200 x 300"))); +} + +TEST_F(QdsRecentPresets, readBadRecentPresetAsString) +{ + RecentPresetsStore store = aStoreWithOne("no_category_only_preset"); + + std::vector recents = store.fetchAll(); + + ASSERT_THAT(recents, IsEmpty()); +} + +TEST_F(QdsRecentPresets, readBadRecentPresetAsInt) +{ + RecentPresetsStore store = aStoreWithOne(32); + + std::vector recents = store.fetchAll(); + + ASSERT_THAT(recents, IsEmpty()); +} + +TEST_F(QdsRecentPresets, readBadRecentPresetsInList) +{ + RecentPresetsStore store = aStoreWithRecents({"bad1", // no category, no size + "categ/name:800 x 600", // good + "categ/bad2", //no size + "categ/bad3:", //no size + "categ 1/bad4:200 x 300", // category has space + "categ/bad5: 400 x 300", // size starts with space + "categ/bad6:400"}); // bad size + + std::vector recents = store.fetchAll(); + + ASSERT_THAT(recents, ElementsAre(RecentPreset("categ", "name", "800 x 600"))); +} + +TEST_F(QdsRecentPresets, readTwoRecentPresets) +{ + RecentPresetsStore store = aStoreWithRecents( + {"category_1/preset 1:640 x 480", "category_2/preset 2:320 x 200"}); + + std::vector recents = store.fetchAll(); + + ASSERT_THAT(recents, + ElementsAre(RecentPreset("category_1", "preset 1", "640 x 480"), + RecentPreset("category_2", "preset 2", "320 x 200"))); +} + +TEST_F(QdsRecentPresets, addFirstRecentPreset) +{ + RecentPresetsStore store{&settings}; + + store.add("A.Category", "Normal Application", "400 x 600"); + std::vector recents = store.fetchAll(); + + ASSERT_THAT(recents, ElementsAre(RecentPreset("A.Category", "Normal Application", "400 x 600"))); +} + +TEST_F(QdsRecentPresets, addExistingFirstRecentPreset) +{ + RecentPresetsStore store = aStoreWithRecents({"category/preset"}); + + store.add("category", "preset", "200 x 300"); + std::vector recents = store.fetchAll(); + + ASSERT_THAT(recents, ElementsAre(RecentPreset("category", "preset", "200 x 300"))); +} + +TEST_F(QdsRecentPresets, addSecondRecentPreset) +{ + RecentPresetsStore store = aStoreWithRecents({"A.Category/Preset 1:800 x 600"}); + + store.add("A.Category", "Preset 2", "640 x 480"); + std::vector recents = store.fetchAll(); + + ASSERT_THAT(recents, + ElementsAre(RecentPreset("A.Category", "Preset 2", "640 x 480"), + RecentPreset("A.Category", "Preset 1", "800 x 600"))); +} + +TEST_F(QdsRecentPresets, addSecondRecentPresetSameKindButDifferentSize) +{ + RecentPresetsStore store = aStoreWithRecents({"A.Category/Preset:800 x 600"}); + + store.add("A.Category", "Preset", "640 x 480"); + std::vector recents = store.fetchAll(); + + ASSERT_THAT(recents, + ElementsAre(RecentPreset("A.Category", "Preset", "640 x 480"), + RecentPreset("A.Category", "Preset", "800 x 600"))); +} + +TEST_F(QdsRecentPresets, fetchesRecentPresetsInTheReverseOrderTheyWereAdded) +{ + RecentPresetsStore store{&settings}; + + store.add("A.Category", "Preset 1", "640 x 480"); + store.add("A.Category", "Preset 2", "640 x 480"); + store.add("A.Category", "Preset 3", "800 x 600"); + std::vector recents = store.fetchAll(); + + ASSERT_THAT(recents, + ElementsAre(RecentPreset("A.Category", "Preset 3", "800 x 600"), + RecentPreset("A.Category", "Preset 2", "640 x 480"), + RecentPreset("A.Category", "Preset 1", "640 x 480"))); +} + +TEST_F(QdsRecentPresets, addingAnExistingRecentPresetMakesItTheFirst) +{ + RecentPresetsStore store = aStoreWithRecents({"A.Category/Preset 1:200 x 300", + "A.Category/Preset 2:200 x 300", + "A.Category/Preset 3:640 x 480"}); + + store.add("A.Category", "Preset 3", "640 x 480"); + std::vector recents = store.fetchAll(); + + ASSERT_THAT(recents, + ElementsAre(RecentPreset("A.Category", "Preset 3", "640 x 480"), + RecentPreset("A.Category", "Preset 1", "200 x 300"), + RecentPreset("A.Category", "Preset 2", "200 x 300"))); +} + +TEST_F(QdsRecentPresets, addingTooManyRecentPresetsRemovesTheOldestOne) +{ + RecentPresetsStore store = aStoreWithRecents( + {"A.Category/Preset 2:200 x 300", "A.Category/Preset 1:200 x 300"}); + store.setMaximum(2); + + store.add("A.Category", "Preset 3", "200 x 300"); + std::vector recents = store.fetchAll(); + + ASSERT_THAT(recents, + ElementsAre(RecentPreset("A.Category", "Preset 3", "200 x 300"), + RecentPreset("A.Category", "Preset 2", "200 x 300"))); +} diff --git a/tests/auto/qml/qmldesigner/wizard/test-main.cpp b/tests/auto/qml/qmldesigner/wizard/test-main.cpp new file mode 100644 index 0000000000..d419674ea1 --- /dev/null +++ b/tests/auto/qml/qmldesigner/wizard/test-main.cpp @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +* Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** 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. +** +****************************************************************************/ + +#include "test-utilities.h" + +#include + +class Environment : public testing::Environment +{ +public: + void SetUp() override + { + const QString temporayDirectoryPath = QDir::tempPath() + "/QtCreator-UnitTests-XXXXXX"; + Utils::TemporaryDirectory::setMasterTemporaryDirectory(temporayDirectoryPath); + qputenv("TMPDIR", Utils::TemporaryDirectory::masterDirectoryPath().toUtf8()); + qputenv("TEMP", Utils::TemporaryDirectory::masterDirectoryPath().toUtf8()); + } + + void TearDown() override {} +}; + +int main(int argc, char **argv) { + ::testing::InitGoogleTest(&argc, argv); + + auto environment = std::make_unique(); + testing::AddGlobalTestEnvironment(environment.release()); + + return RUN_ALL_TESTS(); +} + diff --git a/tests/auto/qml/qmldesigner/wizard/test-utilities.h b/tests/auto/qml/qmldesigner/wizard/test-utilities.h index 3f708e121d..b368e532f4 100644 --- a/tests/auto/qml/qmldesigner/wizard/test-utilities.h +++ b/tests/auto/qml/qmldesigner/wizard/test-utilities.h @@ -26,6 +26,7 @@ ** ****************************************************************************/ +#include "gmock/gmock-matchers.h" #include #include @@ -38,8 +39,10 @@ using ::testing::Return; using ::testing::AtLeast; using ::testing::ElementsAreArray; +using ::testing::ElementsAre; using ::testing::IsEmpty; using ::testing::Not; +using ::testing::SizeIs; QT_BEGIN_NAMESPACE diff --git a/tests/auto/qml/qmldesigner/wizard/wizardfactories-test.cpp b/tests/auto/qml/qmldesigner/wizard/wizardfactories-test.cpp index 97ae04cccc..2e6a87617d 100644 --- a/tests/auto/qml/qmldesigner/wizard/wizardfactories-test.cpp +++ b/tests/auto/qml/qmldesigner/wizard/wizardfactories-test.cpp @@ -26,15 +26,17 @@ #include "test-utilities.h" #include +#include #include "wizardfactories.h" using namespace StudioWelcome; using Core::IWizardFactory; +using ProjectExplorer::JsonWizardFactory; namespace { -class MockWizardFactory : public IWizardFactory +class MockWizardFactory : public JsonWizardFactory { public: MOCK_METHOD(Utils::Wizard *, runWizardImpl, @@ -47,6 +49,8 @@ public: ), (override)); + MOCK_METHOD((std::pair), screenSizeInfoFromPage, (const QString &), (const)); + MOCK_METHOD(bool, isAvailable, (Utils::Id), (const, override)); }; @@ -78,12 +82,14 @@ protected: // a good wizard factory is a wizard factory that is not filtered out, and which is available on // platform `this->platform` - IWizardFactory *aGoodWizardFactory(const QString &name = "", const QString &id = "", const QString &categoryId = "") + IWizardFactory *aGoodWizardFactory(const QString &name = "", const QString &id = "", + const QString &categoryId = "", const std::pair &sizes = {}) { MockWizardFactory *factory = new MockWizardFactory; m_factories.push_back(std::unique_ptr(factory)); - configureFactory(*factory, IWizardFactory::ProjectWizard, /*req QtStudio*/true, {platform, true}); + configureFactory(*factory, IWizardFactory::ProjectWizard, /*req QtStudio*/true, + {platform, true}, sizes); if (!name.isEmpty()) factory->setDisplayName(name); @@ -97,7 +103,8 @@ protected: void configureFactory(MockWizardFactory &factory, IWizardFactory::WizardKind kind, bool requiresQtStudio = true, - const QPair &availableOnPlatform = {}) + const QPair &availableOnPlatform = {}, + const QPair &sizes = {}) { if (kind == IWizardFactory::ProjectWizard) { QSet supported{Utils::Id{"QmlProjectManager.QmlProject"}}; @@ -127,6 +134,14 @@ protected: .Times(AtLeast(1)) .WillRepeatedly(Return(value)); } + + auto screenSizes = (sizes == std::pair{} + ? std::make_pair(0, QStringList({"640 x 480"})) + : sizes); + + EXPECT_CALL(factory, screenSizeInfoFromPage(QString("Fields"))) + .Times(AtLeast(0)) + .WillRepeatedly(Return(screenSizes)); } WizardFactories makeWizardFactoriesHandler(QList source, @@ -143,15 +158,21 @@ private: WizardFactories::GetIconUnicodeFunc oldIconUnicodeFunc; }; -inline QStringList projectNames(const ProjectCategory &cat) +QStringList presetNames(const WizardCategory &cat) +{ + QStringList result = Utils::transform(cat.items, &PresetItem::name); + return result; +} + +QStringList screenSizes(const WizardCategory &cat) { - QStringList result = Utils::transform(cat.items, &ProjectItem::name); + QStringList result = Utils::transform(cat.items, &PresetItem::screenSizeName); return result; } -inline QStringList categoryNames(const std::map &projects) +QStringList categoryNames(const std::map &presets) { - QMap qmap{projects}; + const QMap qmap{presets}; return qmap.keys(); } @@ -166,9 +187,9 @@ TEST_F(QdsWizardFactories, haveEmptyListOfWizardFactories) /*get wizards supporting platform*/ "platform" ); - std::map projects = wf.projectsGroupedByCategory(); + std::map presets = wf.presetsGroupedByCategory(); - ASSERT_THAT(projects, IsEmpty()); + ASSERT_THAT(presets, IsEmpty()); } TEST_F(QdsWizardFactories, filtersOutNonProjectWizardFactories) @@ -178,9 +199,9 @@ TEST_F(QdsWizardFactories, filtersOutNonProjectWizardFactories) /*get wizards supporting platform*/ platform ); - std::map projects = wf.projectsGroupedByCategory(); + std::map presets = wf.presetsGroupedByCategory(); - ASSERT_THAT(projects, IsEmpty()); + ASSERT_THAT(presets, IsEmpty()); } TEST_F(QdsWizardFactories, filtersOutWizardFactoriesUnavailableForPlatform) @@ -190,9 +211,9 @@ TEST_F(QdsWizardFactories, filtersOutWizardFactoriesUnavailableForPlatform) /*get wizards supporting platform*/ "Non-Desktop" ); - std::map projects = wf.projectsGroupedByCategory(); + std::map presets = wf.presetsGroupedByCategory(); - ASSERT_THAT(projects, IsEmpty()); + ASSERT_THAT(presets, IsEmpty()); } TEST_F(QdsWizardFactories, filtersOutWizardFactoriesThatDontRequireQtStudio) @@ -203,18 +224,48 @@ TEST_F(QdsWizardFactories, filtersOutWizardFactoriesThatDontRequireQtStudio) }, /*get wizards supporting platform*/ platform); - std::map projects = wf.projectsGroupedByCategory(); + std::map presets = wf.presetsGroupedByCategory(); - ASSERT_THAT(projects, IsEmpty()); + ASSERT_THAT(presets, IsEmpty()); } TEST_F(QdsWizardFactories, doesNotFilterOutAGoodWizardFactory) { WizardFactories wf = makeWizardFactoriesHandler({aGoodWizardFactory()}, platform); - std::map projects = wf.projectsGroupedByCategory(); + std::map presets = wf.presetsGroupedByCategory(); + + ASSERT_THAT(presets, Not(IsEmpty())); +} + +TEST_F(QdsWizardFactories, DISABLED_buildsPresetItemWithCorrectSizeName) +{ + WizardFactories wf = makeWizardFactoriesHandler( + { + aGoodWizardFactory("A", "A_id", "A.category", {1, {"size 0", "size 1"}}), + }, + platform); + + std::map presets = wf.presetsGroupedByCategory(); - ASSERT_THAT(projects, Not(IsEmpty())); + ASSERT_THAT(categoryNames(presets), ElementsAreArray({"A.category"})); + ASSERT_THAT(presetNames(presets["A.category"]), ElementsAreArray({"A"})); + ASSERT_THAT(screenSizes(presets["A.category"]), ElementsAreArray({"size 1"})); +} + +TEST_F(QdsWizardFactories, whenSizeInfoIsBadBuildsPresetItemWithEmptySizeName) +{ + WizardFactories wf = makeWizardFactoriesHandler( + { + aGoodWizardFactory("A", "A_id", "A.category", {1, {/*empty*/}}), + }, + platform); + + std::map presets = wf.presetsGroupedByCategory(); + + ASSERT_THAT(categoryNames(presets), ElementsAreArray({"A.category"})); + ASSERT_THAT(presetNames(presets["A.category"]), ElementsAreArray({"A"})); + ASSERT_THAT(screenSizes(presets["A.category"]), ElementsAreArray({""})); } TEST_F(QdsWizardFactories, sortsWizardFactoriesByCategory) @@ -226,11 +277,11 @@ TEST_F(QdsWizardFactories, sortsWizardFactoriesByCategory) }, platform); - std::map projects = wf.projectsGroupedByCategory(); + std::map presets = wf.presetsGroupedByCategory(); - ASSERT_THAT(categoryNames(projects), ElementsAreArray({"A.category", "Z.category"})); - ASSERT_THAT(projectNames(projects["A.category"]), ElementsAreArray({"X"})); - ASSERT_THAT(projectNames(projects["Z.category"]), ElementsAreArray({"B"})); + ASSERT_THAT(categoryNames(presets), ElementsAreArray({"A.category", "Z.category"})); + ASSERT_THAT(presetNames(presets["A.category"]), ElementsAreArray({"X"})); + ASSERT_THAT(presetNames(presets["Z.category"]), ElementsAreArray({"B"})); } TEST_F(QdsWizardFactories, sortsWizardFactoriesById) @@ -242,10 +293,10 @@ TEST_F(QdsWizardFactories, sortsWizardFactoriesById) }, platform); - std::map projects = wf.projectsGroupedByCategory(); + std::map presets = wf.presetsGroupedByCategory(); - ASSERT_THAT(categoryNames(projects), ElementsAreArray({"category"})); - ASSERT_THAT(projectNames(projects["category"]), ElementsAreArray({"X", "B"})); + ASSERT_THAT(categoryNames(presets), ElementsAreArray({"category"})); + ASSERT_THAT(presetNames(presets["category"]), ElementsAreArray({"X", "B"})); } TEST_F(QdsWizardFactories, groupsWizardFactoriesByCategory) @@ -258,14 +309,14 @@ TEST_F(QdsWizardFactories, groupsWizardFactoriesByCategory) }, platform); - std::map projects = wf.projectsGroupedByCategory(); + std::map presets = wf.presetsGroupedByCategory(); - ASSERT_THAT(categoryNames(projects), ElementsAreArray({"A.category", "Z.category"})); - ASSERT_THAT(projectNames(projects["A.category"]), ElementsAreArray({"A", "B"})); - ASSERT_THAT(projectNames(projects["Z.category"]), ElementsAreArray({"C"})); + ASSERT_THAT(categoryNames(presets), ElementsAreArray({"A.category", "Z.category"})); + ASSERT_THAT(presetNames(presets["A.category"]), ElementsAreArray({"A", "B"})); + ASSERT_THAT(presetNames(presets["Z.category"]), ElementsAreArray({"C"})); } -TEST_F(QdsWizardFactories, createsProjectItemAndCategoryCorrectlyFromWizardFactory) +TEST_F(QdsWizardFactories, createsPresetItemAndCategoryCorrectlyFromWizardFactory) { IWizardFactory *source = aGoodWizardFactory("myName", "myId", "myCategoryId"); @@ -280,23 +331,19 @@ TEST_F(QdsWizardFactories, createsProjectItemAndCategoryCorrectlyFromWizardFacto WizardFactories wf = makeWizardFactoriesHandler({source}, platform); - std::map projects = wf.projectsGroupedByCategory(); + std::map presets = wf.presetsGroupedByCategory(); - ASSERT_THAT(categoryNames(projects), ElementsAreArray({"myCategoryId"})); - ASSERT_THAT(projectNames(projects["myCategoryId"]), ElementsAreArray({"myName"})); + ASSERT_THAT(categoryNames(presets), ElementsAreArray({"myCategoryId"})); + ASSERT_THAT(presetNames(presets["myCategoryId"]), ElementsAreArray({"myName"})); - auto category = projects["myCategoryId"]; + auto category = presets["myCategoryId"]; ASSERT_EQ("myCategoryId", category.id); ASSERT_EQ("myDisplayCategory", category.name); - auto projectItem = projects["myCategoryId"].items[0]; - ASSERT_EQ("myName", projectItem.name); - ASSERT_EQ("myDescription", projectItem.description); - ASSERT_EQ("qrc:/my/qml/path", projectItem.qmlPath.toString()); - ASSERT_EQ("\uABCD", projectItem.fontIconCode); + auto presetItem = presets["myCategoryId"].items[0]; + ASSERT_EQ("myName", presetItem.name); + ASSERT_EQ("myDescription", presetItem.description); + ASSERT_EQ("qrc:/my/qml/path", presetItem.qmlPath.toString()); + ASSERT_EQ("\uABCD", presetItem.fontIconCode); } -int main(int argc, char **argv) { - ::testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} -- cgit v1.2.1