From 08b780205a91aef12141cc0650a6197794f09e00 Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Tue, 8 Nov 2016 14:39:17 +0100 Subject: [PATCH] Theme: Use QVariantMap for theme properties QML can directly mirror this into a JavaScript object, so there is no need to manually copy the values into a different container. Also, QQmlPropertyMap is very expensive as it will listen to changes to any value in the map. We never use this. It would be entirely enough to update the whole map at once if we ever want to update the theme at runtime. Change-Id: Ie2b549e9af51d620801808c87d6f659cc7a06c04 Reviewed-by: Thomas Hartmann --- src/libs/timeline/timelinetheme.cpp | 8 ++--- src/libs/utils/theme/theme.cpp | 35 ++++++++++--------- src/libs/utils/theme/theme.h | 2 +- src/libs/utils/theme/theme_p.h | 2 ++ .../components/componentcore/theming.cpp | 26 +++++++------- .../components/componentcore/theming.h | 2 +- .../itemlibrary/itemlibrarywidget.cpp | 3 +- .../itemlibrary/itemlibrarywidget.h | 1 - .../propertyeditorqmlbackend.cpp | 3 +- .../propertyeditor/propertyeditorqmlbackend.h | 1 - .../stateseditor/stateseditorwidget.cpp | 3 +- .../stateseditor/stateseditorwidget.h | 1 - src/plugins/welcome/welcomeplugin.cpp | 12 +------ 13 files changed, 42 insertions(+), 57 deletions(-) diff --git a/src/libs/timeline/timelinetheme.cpp b/src/libs/timeline/timelinetheme.cpp index 853c42c972a..0ca005b2c86 100644 --- a/src/libs/timeline/timelinetheme.cpp +++ b/src/libs/timeline/timelinetheme.cpp @@ -104,12 +104,8 @@ public: void TimelineTheme::setupTheme(QQmlEngine *engine) { - QQmlPropertyMap *themePropertyMap = new QQmlPropertyMap(engine); - const QVariantHash creatorTheme = Utils::creatorTheme()->values(); - for (auto it = creatorTheme.constBegin(); it != creatorTheme.constEnd(); ++it) - themePropertyMap->insert(it.key(), it.value()); - - engine->rootContext()->setContextProperty(QLatin1String("creatorTheme"), themePropertyMap); + engine->rootContext()->setContextProperty(QLatin1String("creatorTheme"), + Utils::creatorTheme()->values()); engine->addImageProvider(QLatin1String("icons"), new TimelineImageIconProvider); } diff --git a/src/libs/utils/theme/theme.cpp b/src/libs/utils/theme/theme.cpp index 10ee20217d4..2c860fd7305 100644 --- a/src/libs/utils/theme/theme.cpp +++ b/src/libs/utils/theme/theme.cpp @@ -141,26 +141,27 @@ void Theme::setDisplayName(const QString &name) d->displayName = name; } -QVariantHash Theme::values() const +const QVariantMap &Theme::values() const { - QVariantHash result; - const QMetaObject &m = *metaObject(); - { - const QMetaEnum e = m.enumerator(m.indexOfEnumerator("Color")); - for (int i = 0, total = e.keyCount(); i < total; ++i) { - const QString key = QLatin1String(e.key(i)); - const QPair &var = d->colors.at(i); - result.insert(key, var.first); + if (d->values.isEmpty()) { + const QMetaObject &m = *metaObject(); + { + const QMetaEnum e = m.enumerator(m.indexOfEnumerator("Color")); + for (int i = 0, total = e.keyCount(); i < total; ++i) { + const QString key = QLatin1String(e.key(i)); + const QPair &var = d->colors.at(i); + d->values.insert(key, var.first); + } + } + { + const QMetaEnum e = m.enumerator(m.indexOfEnumerator("Flag")); + for (int i = 0, total = e.keyCount(); i < total; ++i) { + const QString key = QLatin1String(e.key(i)); + d->values.insert(key, flag(static_cast(i))); + } } } - { - const QMetaEnum e = m.enumerator(m.indexOfEnumerator("Flag")); - for (int i = 0, total = e.keyCount(); i < total; ++i) { - const QString key = QLatin1String(e.key(i)); - result.insert(key, flag(static_cast(i))); - } - } - return result; + return d->values; } static QColor readColor(const QString &color) diff --git a/src/libs/utils/theme/theme.h b/src/libs/utils/theme/theme.h index a859871d542..0c94b4731d1 100644 --- a/src/libs/utils/theme/theme.h +++ b/src/libs/utils/theme/theme.h @@ -340,7 +340,7 @@ public: QString displayName() const; void setDisplayName(const QString &displayName); - QVariantHash values() const; + const QVariantMap &values() const; void readSettings(QSettings &settings); diff --git a/src/libs/utils/theme/theme_p.h b/src/libs/utils/theme/theme_p.h index 4170ec7cd9f..ad1761d0b8a 100644 --- a/src/libs/utils/theme/theme_p.h +++ b/src/libs/utils/theme/theme_p.h @@ -48,6 +48,8 @@ public: QVector gradients; QVector flags; QMap palette; + + QVariantMap values; }; QTCREATOR_UTILS_EXPORT void setCreatorTheme(Theme *theme); diff --git a/src/plugins/qmldesigner/components/componentcore/theming.cpp b/src/plugins/qmldesigner/components/componentcore/theming.cpp index d1429ff45bc..e74a0561e86 100644 --- a/src/plugins/qmldesigner/components/componentcore/theming.cpp +++ b/src/plugins/qmldesigner/components/componentcore/theming.cpp @@ -33,11 +33,13 @@ namespace QmlDesigner { -void Theming::insertTheme(QQmlPropertyMap *map) +const QVariantMap &Theming::theme() { - const QVariantHash creatorTheme = Utils::creatorTheme()->values(); - for (auto it = creatorTheme.constBegin(); it != creatorTheme.constEnd(); ++it) - map->insert(it.key(), it.value()); + static QVariantMap map; + if (!map.isEmpty()) + return map; + + map = Utils::creatorTheme()->values(); /* Define QmlDesigner colors and remove alpha channels */ const QColor backgroundColor = Utils::creatorTheme()->color(Utils::Theme::QmlDesigner_BackgroundColor); @@ -58,18 +60,18 @@ void Theming::insertTheme(QQmlPropertyMap *map) tabDark = tabDark.darker(260); } - map->insert("QmlDesignerBackgroundColorDarker", darkerBackground); - map->insert("QmlDesignerBackgroundColorDarkAlternate", backgroundColor); - map->insert("QmlDesignerTabLight", tabLight); - map->insert("QmlDesignerTabDark", tabDark); - map->insert("QmlDesignerButtonColor", buttonColor); - map->insert("QmlDesignerBorderColor", Utils::creatorTheme()->color(Utils::Theme::SplitterColor)); + map.insert("QmlDesignerBackgroundColorDarker", darkerBackground); + map.insert("QmlDesignerBackgroundColorDarkAlternate", backgroundColor); + map.insert("QmlDesignerTabLight", tabLight); + map.insert("QmlDesignerTabDark", tabDark); + map.insert("QmlDesignerButtonColor", buttonColor); + map.insert("QmlDesignerBorderColor", Utils::creatorTheme()->color(Utils::Theme::SplitterColor)); + return map; } QString Theming::replaceCssColors(const QString &input) { - QQmlPropertyMap map; - insertTheme(&map); + const QVariantMap &map = theme(); QRegExp rx("creatorTheme\\.(\\w+);"); int pos = 0; diff --git a/src/plugins/qmldesigner/components/componentcore/theming.h b/src/plugins/qmldesigner/components/componentcore/theming.h index cb582994bfa..d9a11ce7b53 100644 --- a/src/plugins/qmldesigner/components/componentcore/theming.h +++ b/src/plugins/qmldesigner/components/componentcore/theming.h @@ -33,7 +33,7 @@ namespace QmlDesigner { class Theming { public: - static void insertTheme(QQmlPropertyMap *map); + static const QVariantMap &theme(); static QString replaceCssColors(const QString &input); static void registerIconProvider(QQmlEngine *engine); }; diff --git a/src/plugins/qmldesigner/components/itemlibrary/itemlibrarywidget.cpp b/src/plugins/qmldesigner/components/itemlibrary/itemlibrarywidget.cpp index 6bd6a75be05..ad59291fe6b 100644 --- a/src/plugins/qmldesigner/components/itemlibrary/itemlibrarywidget.cpp +++ b/src/plugins/qmldesigner/components/itemlibrary/itemlibrarywidget.cpp @@ -83,8 +83,7 @@ ItemLibraryWidget::ItemLibraryWidget(QWidget *parent) : rootContext->setContextProperty(QStringLiteral("itemLibraryIconWidth"), m_itemIconSize.width()); rootContext->setContextProperty(QStringLiteral("itemLibraryIconHeight"), m_itemIconSize.height()); rootContext->setContextProperty(QStringLiteral("rootView"), this); - Theming::insertTheme(&m_themeProperties); - rootContext->setContextProperty(QLatin1String("creatorTheme"), &m_themeProperties); + rootContext->setContextProperty(QLatin1String("creatorTheme"), Theming::theme()); m_itemViewQuickWidget->rootContext()->setContextProperty(QStringLiteral("highlightColor"), Utils::StyleHelper::notTooBrightHighlightColor()); diff --git a/src/plugins/qmldesigner/components/itemlibrary/itemlibrarywidget.h b/src/plugins/qmldesigner/components/itemlibrary/itemlibrarywidget.h index ca8cd82c319..148b0b90dc8 100644 --- a/src/plugins/qmldesigner/components/itemlibrary/itemlibrarywidget.h +++ b/src/plugins/qmldesigner/components/itemlibrary/itemlibrarywidget.h @@ -132,7 +132,6 @@ private: QPointer m_model; FilterChangeFlag m_filterFlag; ItemLibraryEntry m_currentitemLibraryEntry; - QQmlPropertyMap m_themeProperties; }; } diff --git a/src/plugins/qmldesigner/components/propertyeditor/propertyeditorqmlbackend.cpp b/src/plugins/qmldesigner/components/propertyeditor/propertyeditorqmlbackend.cpp index 3d4884d9bd5..ff299af8707 100644 --- a/src/plugins/qmldesigner/components/propertyeditor/propertyeditorqmlbackend.cpp +++ b/src/plugins/qmldesigner/components/propertyeditor/propertyeditorqmlbackend.cpp @@ -103,8 +103,7 @@ PropertyEditorQmlBackend::PropertyEditorQmlBackend(PropertyEditorView *propertyE m_contextObject->setModel(propertyEditor->model()); m_contextObject->insertInQmlContext(context()); - Theming::insertTheme(&m_themeProperties); - context()->setContextProperty(QLatin1String("creatorTheme"), &m_themeProperties); + context()->setContextProperty(QLatin1String("creatorTheme"), Theming::theme()); QObject::connect(&m_backendValuesPropertyMap, &DesignerPropertyMap::valueChanged, propertyEditor, &PropertyEditorView::changeValue); } diff --git a/src/plugins/qmldesigner/components/propertyeditor/propertyeditorqmlbackend.h b/src/plugins/qmldesigner/components/propertyeditor/propertyeditorqmlbackend.h index 0e073b20beb..a272b35aef0 100644 --- a/src/plugins/qmldesigner/components/propertyeditor/propertyeditorqmlbackend.h +++ b/src/plugins/qmldesigner/components/propertyeditor/propertyeditorqmlbackend.h @@ -103,7 +103,6 @@ private: QScopedPointer m_propertyEditorTransaction; QScopedPointer m_dummyPropertyEditorValue; QScopedPointer m_contextObject; - QQmlPropertyMap m_themeProperties; }; } //QmlDesigner diff --git a/src/plugins/qmldesigner/components/stateseditor/stateseditorwidget.cpp b/src/plugins/qmldesigner/components/stateseditor/stateseditorwidget.cpp index ae869763cd0..a9a96cf05d2 100644 --- a/src/plugins/qmldesigner/components/stateseditor/stateseditorwidget.cpp +++ b/src/plugins/qmldesigner/components/stateseditor/stateseditorwidget.cpp @@ -99,8 +99,7 @@ StatesEditorWidget::StatesEditorWidget(StatesEditorView *statesEditorView, State rootContext()->setContextProperty(QLatin1String("canAddNewStates"), true); - Theming::insertTheme(&m_themeProperties); - rootContext()->setContextProperty(QLatin1String("creatorTheme"), &m_themeProperties); + rootContext()->setContextProperty(QLatin1String("creatorTheme"), Theming::theme()); Theming::registerIconProvider(engine()); diff --git a/src/plugins/qmldesigner/components/stateseditor/stateseditorwidget.h b/src/plugins/qmldesigner/components/stateseditor/stateseditorwidget.h index eb0c26ce041..fc78ecb4501 100644 --- a/src/plugins/qmldesigner/components/stateseditor/stateseditorwidget.h +++ b/src/plugins/qmldesigner/components/stateseditor/stateseditorwidget.h @@ -67,7 +67,6 @@ private: QPointer m_statesEditorView; Internal::StatesEditorImageProvider *m_imageProvider; QShortcut *m_qmlSourceUpdateShortcut; - QQmlPropertyMap m_themeProperties; }; } diff --git a/src/plugins/welcome/welcomeplugin.cpp b/src/plugins/welcome/welcomeplugin.cpp index ac3d3a01fa5..884eeb30db5 100644 --- a/src/plugins/welcome/welcomeplugin.cpp +++ b/src/plugins/welcome/welcomeplugin.cpp @@ -158,7 +158,6 @@ private: void sceneGraphError(QQuickWindow::SceneGraphError, const QString &message); void facilitateQml(QQmlEngine *engine); void addPages(const QList &pages); - void applyTheme(); void addKeyboardShortcuts(); QWidget *m_modeWidget; @@ -166,7 +165,6 @@ private: QMap m_idPageMap; QList m_pluginList; int m_activePlugin; - QQmlPropertyMap m_themeProperties; QStringList m_recentProjectsShortcuts; QStringList m_sessionsShortcuts; }; @@ -197,7 +195,6 @@ WelcomeMode::WelcomeMode() layout->setSpacing(0); m_welcomePage = new QQuickWidget; - applyTheme(); // initialize background color and theme properties m_welcomePage->setResizeMode(QQuickWidget::SizeRootObjectToView); m_welcomePage->setObjectName(QLatin1String("WelcomePage")); @@ -217,13 +214,6 @@ WelcomeMode::WelcomeMode() setWidget(m_modeWidget); } -void WelcomeMode::applyTheme() -{ - const QVariantHash creatorTheme = Utils::creatorTheme()->values(); - for (auto it = creatorTheme.constBegin(); it != creatorTheme.constEnd(); ++it) - m_themeProperties.insert(it.key(), it.value()); -} - void WelcomeMode::addKeyboardShortcuts() { const int actionsCount = 9; @@ -294,7 +284,7 @@ void WelcomeMode::facilitateQml(QQmlEngine *engine) QQmlContext *ctx = engine->rootContext(); ctx->setContextProperty(QLatin1String("welcomeMode"), this); - ctx->setContextProperty(QLatin1String("creatorTheme"), &m_themeProperties); + ctx->setContextProperty(QLatin1String("creatorTheme"), Utils::creatorTheme()->values()); ctx->setContextProperty(QLatin1String("useNativeText"), true); }