From a6e20e4874d3f46f311625e471f1b7fbc64964b2 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Tue, 21 Feb 2012 14:45:07 +0100 Subject: Handle theme change in QApplication/QGuiApplication. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Re-initialize palette and fonts. Change-Id: Ib312747886a31e8370f8d99536d52cbcf8afe8a4 Reviewed-by: Morten Johan Sørvig --- src/gui/kernel/qguiapplication.cpp | 70 ++++++++++++++++++++----- src/gui/kernel/qguiapplication_p.h | 3 ++ src/plugins/platforms/windows/qwindowstheme.cpp | 5 +- src/widgets/kernel/qapplication.cpp | 33 +++++++++--- src/widgets/kernel/qapplication_p.h | 4 +- 5 files changed, 93 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/gui/kernel/qguiapplication.cpp b/src/gui/kernel/qguiapplication.cpp index 4b309096c9..2e7441d62c 100644 --- a/src/gui/kernel/qguiapplication.cpp +++ b/src/gui/kernel/qguiapplication.cpp @@ -99,6 +99,14 @@ QPlatformTheme *QGuiApplicationPrivate::platform_theme = 0; QList QGuiApplicationPrivate::generic_plugin_list; +enum ApplicationResourceFlags +{ + ApplicationPaletteExplicitlySet = 0x1, + ApplicationFontExplicitlySet = 0x2 +}; + +static unsigned applicationResourceFlags = 0; + bool QGuiApplicationPrivate::app_do_modal = false; QPalette *QGuiApplicationPrivate::app_pal = 0; // default application palette @@ -144,6 +152,33 @@ static bool qt_detectRTLLanguage() " and Arabic) to get proper widget layout.") == QLatin1String("RTL")); } +static void initPalette() +{ + if (!QGuiApplicationPrivate::app_pal) + if (const QPalette *themePalette = QGuiApplicationPrivate::platformTheme()->palette()) + QGuiApplicationPrivate::app_pal = new QPalette(*themePalette); + if (!QGuiApplicationPrivate::app_pal) + QGuiApplicationPrivate::app_pal = new QPalette(Qt::black); +} + +static inline void clearPalette() +{ + delete QGuiApplicationPrivate::app_pal; + QGuiApplicationPrivate::app_pal = 0; +} + +static void initFontUnlocked() +{ + if (!QGuiApplicationPrivate::app_font) + QGuiApplicationPrivate::app_font = + new QFont(QGuiApplicationPrivate::platformIntegration()->fontDatabase()->defaultFont()); +} + +static inline void clearFontUnlocked() +{ + delete QGuiApplicationPrivate::app_font; + QGuiApplicationPrivate::app_font = 0; +} /*! \class QGuiApplication @@ -312,8 +347,7 @@ QGuiApplication::~QGuiApplication() delete QGuiApplicationPrivate::qt_clipboard; QGuiApplicationPrivate::qt_clipboard = 0; - delete QGuiApplicationPrivate::app_pal; - QGuiApplicationPrivate::app_pal = 0; + clearPalette(); qUnregisterGuiVariant(); @@ -671,8 +705,8 @@ QGuiApplicationPrivate::~QGuiApplicationPrivate() delete generic_plugin_list.at(i); generic_plugin_list.clear(); - delete app_font; - app_font = 0; + clearFontUnlocked(); + QFont::cleanup(); #ifndef QT_NO_CURSOR @@ -1090,6 +1124,8 @@ void QGuiApplicationPrivate::processWindowStateChangedEvent(QWindowSystemInterfa void QGuiApplicationPrivate::processThemeChanged(QWindowSystemInterfacePrivate::ThemeChangeEvent *tce) { + if (self) + self->notifyThemeChanged(); if (QWindow *window = tce->window.data()) { QEvent e(QEvent::ThemeChange); QGuiApplication::sendSpontaneousEvent(window, &e); @@ -1579,13 +1615,10 @@ QClipboard * QGuiApplication::clipboard() \sa setPalette() */ + QPalette QGuiApplication::palette() { - if (!QGuiApplicationPrivate::app_pal) - if (const QPalette *themePalette = QGuiApplicationPrivate::platformTheme()->palette()) - QGuiApplicationPrivate::app_pal = new QPalette(*themePalette); - if (!QGuiApplicationPrivate::app_pal) - QGuiApplicationPrivate::app_pal = new QPalette(Qt::black); + initPalette(); return *QGuiApplicationPrivate::app_pal; } @@ -1602,6 +1635,7 @@ void QGuiApplication::setPalette(const QPalette &pal) QGuiApplicationPrivate::app_pal = new QPalette(pal); else *QGuiApplicationPrivate::app_pal = pal; + applicationResourceFlags |= ApplicationPaletteExplicitlySet; } /*! @@ -1612,9 +1646,7 @@ void QGuiApplication::setPalette(const QPalette &pal) QFont QGuiApplication::font() { QMutexLocker locker(&applicationFontMutex); - if (!QGuiApplicationPrivate::app_font) - QGuiApplicationPrivate::app_font = - new QFont(QGuiApplicationPrivate::platformIntegration()->fontDatabase()->defaultFont()); + initFontUnlocked(); return *QGuiApplicationPrivate::app_font; } @@ -1630,6 +1662,7 @@ void QGuiApplication::setFont(const QFont &font) QGuiApplicationPrivate::app_font = new QFont(font); else *QGuiApplicationPrivate::app_font = font; + applicationResourceFlags |= ApplicationFontExplicitlySet; } /*! @@ -2034,4 +2067,17 @@ QPixmap QGuiApplicationPrivate::getPixmapCursor(Qt::CursorShape cshape) return QPixmap(); } +void QGuiApplicationPrivate::notifyThemeChanged() +{ + if (!(applicationResourceFlags & ApplicationPaletteExplicitlySet)) { + clearPalette(); + initPalette(); + } + if (!(applicationResourceFlags & ApplicationFontExplicitlySet)) { + QMutexLocker locker(&applicationFontMutex); + clearFontUnlocked(); + initFontUnlocked(); + } +} + QT_END_NAMESPACE diff --git a/src/gui/kernel/qguiapplication_p.h b/src/gui/kernel/qguiapplication_p.h index 66670e8e25..68546ce0cf 100644 --- a/src/gui/kernel/qguiapplication_p.h +++ b/src/gui/kernel/qguiapplication_p.h @@ -200,6 +200,9 @@ public: }; QHash synthesizedMousePoints; +protected: + virtual void notifyThemeChanged(); + private: void init(); diff --git a/src/plugins/platforms/windows/qwindowstheme.cpp b/src/plugins/platforms/windows/qwindowstheme.cpp index 7276db4bb6..5350b3ca3f 100644 --- a/src/plugins/platforms/windows/qwindowstheme.cpp +++ b/src/plugins/platforms/windows/qwindowstheme.cpp @@ -52,6 +52,7 @@ #include #include #include +#include QT_BEGIN_NAMESPACE @@ -265,10 +266,10 @@ QPlatformDialogHelper *QWindowsTheme::createPlatformDialogHelper(DialogType type return QWindowsDialogs::createHelper(type); } -void QWindowsTheme::windowsThemeChanged(QWindow * /* window */) +void QWindowsTheme::windowsThemeChanged(QWindow * window) { refresh(); - // QWindowSystemInterface::handleThemeChange(window); + QWindowSystemInterface::handleThemeChange(window); } QT_END_NAMESPACE diff --git a/src/widgets/kernel/qapplication.cpp b/src/widgets/kernel/qapplication.cpp index 134b1809bb..650d4af38b 100644 --- a/src/widgets/kernel/qapplication.cpp +++ b/src/widgets/kernel/qapplication.cpp @@ -128,6 +128,21 @@ Q_CORE_EXPORT void qt_call_post_routines(); QApplicationPrivate *QApplicationPrivate::self = 0; +static void initSystemPalette() +{ + if (!QApplicationPrivate::sys_pal) + if (const QPalette *themePalette = QGuiApplicationPrivate::platformTheme()->palette()) + QApplicationPrivate::setSystemPalette(*themePalette); + if (!QApplicationPrivate::sys_pal && QApplicationPrivate::app_style) + QApplicationPrivate::setSystemPalette(QApplicationPrivate::app_style->standardPalette()); +} + +static void clearSystemPalette() +{ + delete QApplicationPrivate::sys_pal; + QApplicationPrivate::sys_pal = 0; +} + #ifdef Q_OS_WINCE int QApplicationPrivate::autoMaximizeThreshold = -1; bool QApplicationPrivate::autoSipEnabled = false; @@ -365,6 +380,7 @@ QString QApplicationPrivate::styleSheet; // default application styles QPointer QApplicationPrivate::leaveAfterRelease = 0; int QApplicationPrivate::app_cspec = QApplication::NormalColor; + QPalette *QApplicationPrivate::sys_pal = 0; // default system palette QPalette *QApplicationPrivate::set_pal = 0; // default palette set by programmer @@ -783,8 +799,7 @@ QApplication::~QApplication() delete QApplicationPrivate::app_pal; QApplicationPrivate::app_pal = 0; - delete QApplicationPrivate::sys_pal; - QApplicationPrivate::sys_pal = 0; + clearSystemPalette(); delete QApplicationPrivate::set_pal; QApplicationPrivate::set_pal = 0; app_palettes()->clear(); @@ -1084,11 +1099,8 @@ QStyle *QApplication::style() // take ownership of the style QApplicationPrivate::app_style->setParent(qApp); - if (!QApplicationPrivate::sys_pal) - if (const QPalette *themePalette = QGuiApplicationPrivate::platformTheme()->palette()) - QApplicationPrivate::setSystemPalette(*themePalette); - if (!QApplicationPrivate::sys_pal) - QApplicationPrivate::setSystemPalette(QApplicationPrivate::app_style->standardPalette()); + initSystemPalette(); + if (QApplicationPrivate::set_pal) // repolish set palette with the new style QApplication::setPalette(*QApplicationPrivate::set_pal); @@ -4631,6 +4643,13 @@ void QApplicationPrivate::translateTouchCancel(QTouchDevice *device, ulong times } } +void QApplicationPrivate::notifyThemeChanged() +{ + QGuiApplicationPrivate::notifyThemeChanged(); + clearSystemPalette(); + initSystemPalette(); +} + #ifndef QT_NO_GESTURES QGestureManager* QGestureManager::instance() { diff --git a/src/widgets/kernel/qapplication_p.h b/src/widgets/kernel/qapplication_p.h index e728868182..7a880986bf 100644 --- a/src/widgets/kernel/qapplication_p.h +++ b/src/widgets/kernel/qapplication_p.h @@ -260,7 +260,9 @@ public: static QPalette *sys_pal; static QPalette *set_pal; -private: +protected: + void notifyThemeChanged(); + public: static QFont *sys_font; static QFont *set_font; -- cgit v1.2.3