diff --git a/src/plugins/qmldesigner/components/componentcore/changestyleaction.cpp b/src/plugins/qmldesigner/components/componentcore/changestyleaction.cpp index f98a32b9698..5a405098039 100644 --- a/src/plugins/qmldesigner/components/componentcore/changestyleaction.cpp +++ b/src/plugins/qmldesigner/components/componentcore/changestyleaction.cpp @@ -48,6 +48,24 @@ static QString styleConfigFileName(const QString &qmlFileName) ChangeStyleWidgetAction::ChangeStyleWidgetAction(QObject *parent) : QWidgetAction(parent) { + // The Default style was renamed to Basic in Qt 6. In Qt 6, "Default" + // will result in a platform-specific style being chosen. + items = { + {"Basic", "Basic", {}}, + {"Default", "Default", {}}, + {"Fusion", "Fusion", {}}, + {"Imagine", "Imagine", {}}, + {"Material Light", "Material", "Light"}, + {"Material Dark", "Material", "Dark"}, + {"Universal Light", "Universal", "Light"}, + {"Universal Dark", "Universal", "Dark"}, + {"Universal System", "Universal", "System"} + }; + + if (Utils::HostOsInfo::isMacHost()) + items.append({"macOS", "macOS", {}}); + if (Utils::HostOsInfo::isWindowsHost()) + items.append({"Windows", "Windows", {}}); } void ChangeStyleWidgetAction::handleModelUpdate(const QString &style) @@ -55,6 +73,48 @@ void ChangeStyleWidgetAction::handleModelUpdate(const QString &style) emit modelUpdated(style); } +const QList ChangeStyleWidgetAction::styleItems() const +{ + return items; +} + +void ChangeStyleWidgetAction::changeStyle(const QString &style) +{ + if (style.isEmpty()) + return; + + const Utils::FilePath configFileName = Utils::FilePath::fromString(styleConfigFileName(qmlFileName)); + + if (configFileName.exists()) { + QSettings infiFile(configFileName.toString(), QSettings::IniFormat); + + int contains = -1; + + for (const auto &item : qAsConst(items)) { + if (item.displayName == style) { + contains = items.indexOf(item); + break; + } + } + + if (contains >= 0) { + const QString styleName = items.at(contains).styleName; + const QString styleTheme = items.at(contains).styleTheme; + + infiFile.setValue("Controls/Style", styleName); + + if (!styleTheme.isEmpty()) + infiFile.setValue((styleName + "/Theme"), styleTheme); + } + else { + infiFile.setValue("Controls/Style", style); + } + + if (view) + view->resetPuppet(); + } +} + const char enabledTooltip[] = QT_TRANSLATE_NOOP("ChangeStyleWidgetAction", "Change style for Qt Quick Controls 2."); const char disbledTooltip[] = QT_TRANSLATE_NOOP("ChangeStyleWidgetAction", @@ -64,18 +124,10 @@ QWidget *ChangeStyleWidgetAction::createWidget(QWidget *parent) { auto comboBox = new QComboBox(parent); comboBox->setToolTip(tr(enabledTooltip)); - // The Default style was renamed to Basic in Qt 6. In Qt 6, "Default" - // will result in a platform-specific style being chosen. - comboBox->addItem("Basic"); - comboBox->addItem("Default"); - comboBox->addItem("Fusion"); - comboBox->addItem("Imagine"); - if (Utils::HostOsInfo::isMacHost()) - comboBox->addItem("macOS"); - comboBox->addItem("Material"); - comboBox->addItem("Universal"); - if (Utils::HostOsInfo::isWindowsHost()) - comboBox->addItem("Windows"); + + for (const auto &item : qAsConst(items)) + comboBox->addItem(item.displayName); + comboBox->setEditable(true); comboBox->setCurrentIndex(0); @@ -97,23 +149,8 @@ QWidget *ChangeStyleWidgetAction::createWidget(QWidget *parent) } }); - connect(comboBox, - &QComboBox::textActivated, - this, - [this](const QString &style) { - - if (style.isEmpty()) - return; - - const Utils::FilePath configFileName = Utils::FilePath::fromString(styleConfigFileName(qmlFileName)); - - if (configFileName.exists()) { - QSettings infiFile(configFileName.toString(), QSettings::IniFormat); - infiFile.setValue("Controls/Style", style); - if (view) - view->resetPuppet(); - } - }); + connect(comboBox, &QComboBox::textActivated, + this, &ChangeStyleWidgetAction::changeStyle); return comboBox; } @@ -135,11 +172,27 @@ void ChangeStyleAction::currentContextChanged(const SelectionContext &selectionC if (Utils::FilePath::fromString(confFileName).exists()) { QSettings infiFile(confFileName, QSettings::IniFormat); - m_action->handleModelUpdate(infiFile.value("Controls/Style", "Basic").toString()); + const QString styleName = infiFile.value("Controls/Style", "Basic").toString(); + const QString styleTheme = infiFile.value(styleName + "/Theme", "").toString(); + const auto items = m_action->styleItems(); + + QString comboBoxEntry = styleName; + + for (const auto &item : items) { + if (item.styleName == styleName) { + if (!styleTheme.isEmpty() && (item.styleTheme == styleTheme)) { + comboBoxEntry.append(" "); + comboBoxEntry.append(styleTheme); + + break; + } + } + } + + m_action->handleModelUpdate(comboBoxEntry); } else { m_action->handleModelUpdate(""); } - } } diff --git a/src/plugins/qmldesigner/components/componentcore/changestyleaction.h b/src/plugins/qmldesigner/components/componentcore/changestyleaction.h index fe6d275e12c..fab1b9de50c 100644 --- a/src/plugins/qmldesigner/components/componentcore/changestyleaction.h +++ b/src/plugins/qmldesigner/components/componentcore/changestyleaction.h @@ -38,6 +38,24 @@ namespace QmlDesigner { class AbstractView; +struct StyleWidgetEntry { + QString displayName; + + QString styleName; + QString styleTheme; + + bool operator==(const StyleWidgetEntry &entry) const { + if (displayName != entry.displayName) + return false; + if (styleName != entry.styleName) + return false; + if (styleTheme != entry.styleTheme) + return false; + + return true; + }; +}; + class ChangeStyleWidgetAction : public QWidgetAction { Q_OBJECT @@ -46,6 +64,11 @@ public: explicit ChangeStyleWidgetAction(QObject *parent = nullptr); void handleModelUpdate(const QString &style); + const QList styleItems() const; + +public slots: + void changeStyle(const QString &style); + protected: QWidget *createWidget(QWidget *parent) override; @@ -55,6 +78,8 @@ signals: public: QString qmlFileName; QPointer view; + + QList items; }; class ChangeStyleAction : public ActionInterface