diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2018-02-22 23:29:44 +0100 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2018-02-27 11:19:37 +0000 |
commit | cf0905a25cc02a1ad2a242319e7ad9748c0a64f3 (patch) | |
tree | ca40c76cc7974de2d5cbf00bd9c7c7cfe2268899 | |
parent | 0836a69bdf84c7f0d63ef081c838b98bb38de41e (diff) |
QQuickTheme: don't inherit QPlatformTheme
Use QPlatformTheme as a fallback instead of inheriting from it. This
way, Qt Quick Controls 2 themes don't mess up the fonts and palettes
of Qt Quick Controls 1 and Qt Widgets applications.
Note: QQuickTheme::Font and QQuickTheme::Palette enums are copies of
the respective enums in QPlatformTheme, for now. This is the simplest
first step, but later on, we can have our own set of enums that cover
controls, such as Switch, that were previously entirely missing from
QPlatformTheme.
Task-number: QTBUG-51921
Change-Id: I8efe0ba2d03d65bc12b55b533ba9f2fab5320348
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
-rw-r--r-- | src/quickcontrols2/qquickstyleplugin.cpp | 10 | ||||
-rw-r--r-- | src/quickcontrols2/qquickstyleplugin_p.h | 3 | ||||
-rw-r--r-- | src/quicktemplates2/qquicktheme.cpp | 25 | ||||
-rw-r--r-- | src/quicktemplates2/qquicktheme_p.h | 66 | ||||
-rw-r--r-- | src/quicktemplates2/qquicktheme_p_p.h | 1 | ||||
-rw-r--r-- | tests/auto/font/tst_font.cpp | 21 | ||||
-rw-r--r-- | tests/auto/palette/tst_palette.cpp | 14 | ||||
-rw-r--r-- | tests/auto/qquickapplicationwindow/tst_qquickapplicationwindow.cpp | 11 |
8 files changed, 106 insertions, 45 deletions
diff --git a/src/quickcontrols2/qquickstyleplugin.cpp b/src/quickcontrols2/qquickstyleplugin.cpp index f2deb7ba..61f845cb 100644 --- a/src/quickcontrols2/qquickstyleplugin.cpp +++ b/src/quickcontrols2/qquickstyleplugin.cpp @@ -129,6 +129,8 @@ QQuickStylePlugin::QQuickStylePlugin(QObject *parent) : QQmlExtensionPlugin(pare QQuickStylePlugin::~QQuickStylePlugin() { + if (QQuickTheme::current() == m_theme) + QQuickTheme::setCurrent(nullptr); } void QQuickStylePlugin::registerTypes(const char *uri) @@ -141,13 +143,13 @@ void QQuickStylePlugin::initializeEngine(QQmlEngine *engine, const char *uri) Q_UNUSED(engine); Q_UNUSED(uri); - // make sure not to re-create the proxy theme if initializeEngine() + // make sure not to re-create the theme if initializeEngine() // is called multiple times, like in case of qml2puppet (QTBUG-54995) - if (!m_theme.isNull()) + if (m_theme) return; if (isCurrent()) { - m_theme.reset(createTheme()); + m_theme = createTheme(); if (m_theme) { const QFont *font = nullptr; const QPalette *palette = nullptr; @@ -160,7 +162,7 @@ void QQuickStylePlugin::initializeEngine(QQmlEngine *engine, const char *uri) #endif m_theme->setDefaultFont(font); m_theme->setDefaultPalette(palette); - QGuiApplicationPrivate::platform_theme = m_theme.data(); + QQuickTheme::setCurrent(m_theme); } } } diff --git a/src/quickcontrols2/qquickstyleplugin_p.h b/src/quickcontrols2/qquickstyleplugin_p.h index 24f8e564..771a07e2 100644 --- a/src/quickcontrols2/qquickstyleplugin_p.h +++ b/src/quickcontrols2/qquickstyleplugin_p.h @@ -48,7 +48,6 @@ // We mean it. // -#include <QtCore/qscopedpointer.h> #include <QtQml/qqmlextensionplugin.h> #include <QtQuickControls2/private/qtquickcontrols2global_p.h> @@ -74,7 +73,7 @@ public: QUrl typeUrl(const QString &name = QString()) const; private: - QScopedPointer<QQuickTheme> m_theme; + QQuickTheme *m_theme = nullptr; }; QT_END_NAMESPACE diff --git a/src/quicktemplates2/qquicktheme.cpp b/src/quicktemplates2/qquicktheme.cpp index 0f9bc5de..02389ed0 100644 --- a/src/quicktemplates2/qquicktheme.cpp +++ b/src/quicktemplates2/qquicktheme.cpp @@ -37,10 +37,13 @@ #include "qquicktheme_p.h" #include "qquicktheme_p_p.h" +#include <QtGui/qpa/qplatformtheme.h> #include <QtGui/private/qguiapplication_p.h> QT_BEGIN_NAMESPACE +QScopedPointer<QQuickTheme> QQuickThemePrivate::current; + QQuickTheme::QQuickTheme() : d_ptr(new QQuickThemePrivate) { @@ -50,15 +53,27 @@ QQuickTheme::~QQuickTheme() { } +QQuickTheme *QQuickTheme::current() +{ + return QQuickThemePrivate::current.data(); +} + +void QQuickTheme::setCurrent(QQuickTheme *theme) +{ + QQuickThemePrivate::current.reset(theme); +} + QFont QQuickTheme::themeFont(Font type) { const QFont *font = nullptr; - if (QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme()) + if (QQuickTheme *theme = current()) font = theme->font(type); + else if (QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme()) + font = theme->font(static_cast<QPlatformTheme::Font>(type)); if (font) { QFont f = *font; - if (type == QPlatformTheme::SystemFont) + if (type == SystemFont) f.resolve(0); return f; } @@ -69,12 +84,14 @@ QFont QQuickTheme::themeFont(Font type) QPalette QQuickTheme::themePalette(Palette type) { const QPalette *palette = nullptr; - if (QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme()) + if (QQuickTheme *theme = current()) palette = theme->palette(type); + else if (QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme()) + palette = theme->palette(static_cast<QPlatformTheme::Palette>(type)); if (palette) { QPalette f = *palette; - if (type == QPlatformTheme::SystemPalette) + if (type == SystemPalette) f.resolve(0); return f; } diff --git a/src/quicktemplates2/qquicktheme_p.h b/src/quicktemplates2/qquicktheme_p.h index bfd777ab..f4efd359 100644 --- a/src/quicktemplates2/qquicktheme_p.h +++ b/src/quicktemplates2/qquicktheme_p.h @@ -48,7 +48,7 @@ // We mean it. // -#include <QtQuickTemplates2/private/qquickproxytheme_p.h> +#include <QtQuickTemplates2/private/qtquicktemplates2global_p.h> #include <QtCore/qscopedpointer.h> #include <QtGui/qfont.h> #include <QtGui/qpalette.h> @@ -57,17 +57,73 @@ QT_BEGIN_NAMESPACE class QQuickThemePrivate; -class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickTheme : public QQuickProxyTheme +class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickTheme { public: QQuickTheme(); - ~QQuickTheme(); + virtual ~QQuickTheme(); + + static QQuickTheme *current(); + static void setCurrent(QQuickTheme *theme); + + enum Font { + SystemFont, + MenuFont, + MenuBarFont, + MenuItemFont, + MessageBoxFont, + LabelFont, + TipLabelFont, + StatusBarFont, + TitleBarFont, + MdiSubWindowTitleFont, + DockWidgetTitleFont, + PushButtonFont, + CheckBoxFont, + RadioButtonFont, + ToolButtonFont, + ItemViewFont, + ListViewFont, + HeaderViewFont, + ListBoxFont, + ComboMenuItemFont, + ComboLineEditFont, + SmallFont, + MiniFont, + FixedFont, + GroupBoxTitleFont, + TabButtonFont, + EditorFont, + NFonts + }; + + enum Palette { + SystemPalette, + ToolTipPalette, + ToolButtonPalette, + ButtonPalette, + CheckBoxPalette, + RadioButtonPalette, + HeaderPalette, + ComboBoxPalette, + ItemViewPalette, + MessageBoxLabelPelette, + MessageBoxLabelPalette = MessageBoxLabelPelette, + TabBarPalette, + LabelPalette, + GroupBoxPalette, + MenuPalette, + MenuBarPalette, + TextEditPalette, + TextLineEditPalette, + NPalettes + }; static QFont themeFont(Font type); static QPalette themePalette(Palette type); - const QFont *font(Font type = SystemFont) const override; - const QPalette *palette(Palette type = SystemPalette) const override; + virtual const QFont *font(Font type = SystemFont) const; + virtual const QPalette *palette(Palette type = SystemPalette) const; void setDefaultFont(const QFont *defaultFont); void setDefaultPalette(const QPalette *defaultPalette); diff --git a/src/quicktemplates2/qquicktheme_p_p.h b/src/quicktemplates2/qquicktheme_p_p.h index 5c20610d..b8cdc4aa 100644 --- a/src/quicktemplates2/qquicktheme_p_p.h +++ b/src/quicktemplates2/qquicktheme_p_p.h @@ -60,6 +60,7 @@ public: return theme->d_func(); } + static QScopedPointer<QQuickTheme> current; QScopedPointer<const QFont> defaultFont; QScopedPointer<const QPalette> defaultPalette; }; diff --git a/tests/auto/font/tst_font.cpp b/tests/auto/font/tst_font.cpp index a585e970..f3f9dbf5 100644 --- a/tests/auto/font/tst_font.cpp +++ b/tests/auto/font/tst_font.cpp @@ -180,10 +180,10 @@ void tst_font::inheritance() QCOMPARE(grandChild->property("font").value<QFont>(), windowFont); } -class TestFontTheme : public QQuickProxyTheme +class TestFontTheme : public QQuickTheme { public: - TestFontTheme(QPlatformTheme *theme) : QQuickProxyTheme(theme) + TestFontTheme() { std::fill(fonts, fonts + QQuickTheme::NFonts, static_cast<QFont *>(0)); @@ -192,8 +192,6 @@ public: font.setPixelSize(i + 10); fonts[i] = new QFont(font); } - - QGuiApplicationPrivate::platform_theme = this; } const QFont *font(Font type = SystemFont) const override @@ -266,9 +264,9 @@ void tst_font::defaultFont() QQmlComponent component(&engine); component.setData(QString("import QtQuick.Controls 2.2; %1 { }").arg(control).toUtf8(), QUrl()); - // The call to setData() above causes QQuickDefaultTheme to be set as the platform theme, + // The call to setData() above causes QQuickDefaultTheme to be set as the current theme, // so we must make sure we only set our theme afterwards. - TestFontTheme theme(QGuiApplicationPrivate::platform_theme); + QQuickTheme::setCurrent(new TestFontTheme); QScopedPointer<QObject> object(component.create()); QVERIFY2(!object.isNull(), qPrintable(component.errorString())); @@ -276,16 +274,9 @@ void tst_font::defaultFont() QVariant var = object->property("font"); QVERIFY(var.isValid()); - const QFont *expectedFont = theme.font(fontType); - QVERIFY(expectedFont); - + QFont expectedFont = QQuickTheme::themeFont(fontType); QFont actualFont = var.value<QFont>(); - - if (actualFont != *expectedFont) { - qDebug() << QTest::currentDataTag() << actualFont << *expectedFont; - } - - QCOMPARE(actualFont, *expectedFont); + QCOMPARE(actualFont, expectedFont); } void tst_font::listView_data() diff --git a/tests/auto/palette/tst_palette.cpp b/tests/auto/palette/tst_palette.cpp index 356162a0..f0ce77f0 100644 --- a/tests/auto/palette/tst_palette.cpp +++ b/tests/auto/palette/tst_palette.cpp @@ -216,10 +216,10 @@ void tst_palette::inheritance() QCOMPARE(grandChild->property("palette").value<QPalette>(), windowPalette); } -class TestTheme : public QQuickProxyTheme +class TestTheme : public QQuickTheme { public: - TestTheme(QPlatformTheme *theme) : QQuickProxyTheme(theme) + TestTheme() { std::fill(palettes, palettes + QQuickTheme::NPalettes, static_cast<QPalette *>(0)); @@ -271,8 +271,6 @@ public: palette.setColor(QPalette::Base, Qt::magenta); palettes[QQuickTheme::TextLineEditPalette] = new QPalette(palette); - - QGuiApplicationPrivate::platform_theme = this; } const QPalette *palette(Palette type = SystemPalette) const override @@ -341,7 +339,7 @@ void tst_palette::defaultPalette() QFETCH(QString, control); QFETCH(QQuickTheme::Palette, paletteType); - TestTheme theme(QGuiApplicationPrivate::platform_theme); + QQuickTheme::setCurrent(new TestTheme); QQmlEngine engine; QQmlComponent component(&engine); @@ -353,11 +351,9 @@ void tst_palette::defaultPalette() QVariant var = object->property("palette"); QVERIFY(var.isValid()); - const QPalette *expectedPalette = theme.palette(paletteType); - QVERIFY(expectedPalette); - + QPalette expectedPalette = QQuickTheme::themePalette(paletteType); QPalette actualPalette = var.value<QPalette>(); - QCOMPARE(actualPalette, *expectedPalette); + QCOMPARE(actualPalette, expectedPalette); } void tst_palette::listView_data() diff --git a/tests/auto/qquickapplicationwindow/tst_qquickapplicationwindow.cpp b/tests/auto/qquickapplicationwindow/tst_qquickapplicationwindow.cpp index 8e934e33..6228a15d 100644 --- a/tests/auto/qquickapplicationwindow/tst_qquickapplicationwindow.cpp +++ b/tests/auto/qquickapplicationwindow/tst_qquickapplicationwindow.cpp @@ -50,7 +50,7 @@ #include <QtQuickTemplates2/private/qquickpopup_p.h> #include <QtQuickTemplates2/private/qquicktextarea_p.h> #include <QtQuickTemplates2/private/qquicktextfield_p.h> -#include <QtQuickTemplates2/private/qquickproxytheme_p.h> +#include <QtQuickTemplates2/private/qquicktheme_p.h> #include "../shared/util.h" #include "../shared/visualtestutil.h" @@ -555,11 +555,10 @@ void tst_QQuickApplicationWindow::font() QCOMPARE(item6->font(), font); } -class TestTheme : public QQuickProxyTheme +class TestTheme : public QQuickTheme { public: - TestTheme(QPlatformTheme *theme) : QQuickProxyTheme(theme), m_font("Courier") - { QGuiApplicationPrivate::platform_theme = this; } + TestTheme() : m_font("Courier") { } const QFont *font(Font type = SystemFont) const override { @@ -572,7 +571,7 @@ public: void tst_QQuickApplicationWindow::defaultFont() { - TestTheme theme(QGuiApplicationPrivate::platform_theme); + QQuickTheme::setCurrent(new TestTheme); QQmlEngine engine; QQmlComponent component(&engine); @@ -581,7 +580,7 @@ void tst_QQuickApplicationWindow::defaultFont() QScopedPointer<QQuickApplicationWindow> window; window.reset(static_cast<QQuickApplicationWindow *>(component.create())); QVERIFY(!window.isNull()); - QCOMPARE(window->font(), *theme.font()); + QCOMPARE(window->font(), QQuickTheme::themeFont(QQuickTheme::SystemFont)); } void tst_QQuickApplicationWindow::locale() |