Move tracking of widget specific app palettes to QApplicationPrivate

Change-Id: I43cc25207026f174e46534baedf08e0c300728d1
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
This commit is contained in:
Tor Arne Vestbø 2019-12-20 17:51:01 +01:00
parent 0c37bc11b5
commit 2a02487ff0
2 changed files with 23 additions and 25 deletions

View File

@ -414,11 +414,9 @@ bool Q_WIDGETS_EXPORT qt_tab_all_widgets()
} }
// ######## move to QApplicationPrivate // ######## move to QApplicationPrivate
// Default application palettes and fonts (per widget type) // Default fonts (per widget type)
Q_GLOBAL_STATIC(PaletteHash, app_palettes)
Q_GLOBAL_STATIC(FontHash, app_fonts) Q_GLOBAL_STATIC(FontHash, app_fonts)
// Exported accessors for use outside of this file // Exported accessor for use outside of this file
PaletteHash *qt_app_palettes_hash() { return app_palettes(); }
FontHash *qt_app_fonts_hash() { return app_fonts(); } FontHash *qt_app_fonts_hash() { return app_fonts(); }
QWidgetList *QApplicationPrivate::popupWidgets = 0; // has keyboard input focus QWidgetList *QApplicationPrivate::popupWidgets = 0; // has keyboard input focus
@ -643,7 +641,8 @@ void QApplicationPrivate::initializeWidgetPaletteHash()
QPlatformTheme *platformTheme = QGuiApplicationPrivate::platformTheme(); QPlatformTheme *platformTheme = QGuiApplicationPrivate::platformTheme();
if (!platformTheme) if (!platformTheme)
return; return;
app_palettes()->clear();
widgetPalettes.clear();
setPossiblePalette(platformTheme->palette(QPlatformTheme::ToolButtonPalette), "QToolButton"); setPossiblePalette(platformTheme->palette(QPlatformTheme::ToolButtonPalette), "QToolButton");
setPossiblePalette(platformTheme->palette(QPlatformTheme::ButtonPalette), "QAbstractButton"); setPossiblePalette(platformTheme->palette(QPlatformTheme::ButtonPalette), "QAbstractButton");
@ -802,7 +801,7 @@ QApplication::~QApplication()
delete QApplicationPrivate::app_pal; delete QApplicationPrivate::app_pal;
QApplicationPrivate::app_pal = 0; QApplicationPrivate::app_pal = 0;
clearSystemPalette(); clearSystemPalette();
app_palettes()->clear(); QApplicationPrivate::widgetPalettes.clear();
delete QApplicationPrivate::sys_font; delete QApplicationPrivate::sys_font;
QApplicationPrivate::sys_font = 0; QApplicationPrivate::sys_font = 0;
@ -1315,6 +1314,8 @@ void QApplication::setGlobalStrut(const QSize& strut)
QApplicationPrivate::app_strut = strut; QApplicationPrivate::app_strut = strut;
} }
// Widget specific palettes
QApplicationPrivate::PaletteHash QApplicationPrivate::widgetPalettes;
/*! /*!
\fn QPalette QApplication::palette(const QWidget* widget) \fn QPalette QApplication::palette(const QWidget* widget)
@ -1329,15 +1330,13 @@ void QApplication::setGlobalStrut(const QSize& strut)
*/ */
QPalette QApplication::palette(const QWidget* w) QPalette QApplication::palette(const QWidget* w)
{ {
typedef PaletteHash::const_iterator PaletteHashConstIt; auto &widgetPalettes = QApplicationPrivate::widgetPalettes;
if (w && !widgetPalettes.isEmpty()) {
PaletteHash *hash = app_palettes(); auto it = widgetPalettes.constFind(w->metaObject()->className());
if (w && hash && hash->size()) { const auto cend = widgetPalettes.constEnd();
PaletteHashConstIt it = hash->constFind(w->metaObject()->className());
const PaletteHashConstIt cend = hash->constEnd();
if (it != cend) if (it != cend)
return *it; return *it;
for (it = hash->constBegin(); it != cend; ++it) { for (it = widgetPalettes.constBegin(); it != cend; ++it) {
if (w->inherits(it.key())) if (w->inherits(it.key()))
return it.value(); return it.value();
} }
@ -1354,10 +1353,10 @@ QPalette QApplication::palette(const QWidget* w)
*/ */
QPalette QApplication::palette(const char *className) QPalette QApplication::palette(const char *className)
{ {
PaletteHash *hash = app_palettes(); auto &widgetPalettes = QApplicationPrivate::widgetPalettes;
if (className && hash && hash->size()) { if (className && !widgetPalettes.isEmpty()) {
QHash<QByteArray, QPalette>::ConstIterator it = hash->constFind(className); auto it = widgetPalettes.constFind(className);
if (it != hash->constEnd()) if (it != widgetPalettes.constEnd())
return *it; return *it;
} }
@ -1372,7 +1371,6 @@ void QApplicationPrivate::setPalette_helper(const QPalette &palette, const char*
QApplicationPrivate::app_style->polish(pal); // NB: non-const reference QApplicationPrivate::app_style->polish(pal); // NB: non-const reference
bool all = false; bool all = false;
PaletteHash *hash = app_palettes();
if (!className) { if (!className) {
if (!QGuiApplicationPrivate::setPalette(pal)) if (!QGuiApplicationPrivate::setPalette(pal))
return; return;
@ -1380,13 +1378,13 @@ void QApplicationPrivate::setPalette_helper(const QPalette &palette, const char*
if (!QApplicationPrivate::sys_pal || !palette.isCopyOf(*QApplicationPrivate::sys_pal)) if (!QApplicationPrivate::sys_pal || !palette.isCopyOf(*QApplicationPrivate::sys_pal))
QCoreApplication::setAttribute(Qt::AA_SetPalette); QCoreApplication::setAttribute(Qt::AA_SetPalette);
if (hash && hash->size()) { if (!widgetPalettes.isEmpty()) {
all = true; all = true;
if (clearWidgetPaletteHash) if (clearWidgetPaletteHash)
hash->clear(); widgetPalettes.clear();
} }
} else if (hash) { } else {
hash->insert(className, pal); widgetPalettes.insert(className, pal);
} }
if (qApp) if (qApp)

View File

@ -94,9 +94,6 @@ extern QClipboard *qt_clipboard;
typedef QHash<QByteArray, QFont> FontHash; typedef QHash<QByteArray, QFont> FontHash;
Q_WIDGETS_EXPORT FontHash *qt_app_fonts_hash(); Q_WIDGETS_EXPORT FontHash *qt_app_fonts_hash();
typedef QHash<QByteArray, QPalette> PaletteHash;
PaletteHash *qt_app_palettes_hash();
#define QApplicationPrivateBase QGuiApplicationPrivate #define QApplicationPrivateBase QGuiApplicationPrivate
class Q_WIDGETS_EXPORT QApplicationPrivate : public QApplicationPrivateBase class Q_WIDGETS_EXPORT QApplicationPrivate : public QApplicationPrivateBase
@ -193,6 +190,9 @@ public:
static void initializeWidgetFontHash(); static void initializeWidgetFontHash();
static void setSystemFont(const QFont &font); static void setSystemFont(const QFont &font);
using PaletteHash = QHash<QByteArray, QPalette>;
static PaletteHash widgetPalettes;
static QApplicationPrivate *instance() { return self; } static QApplicationPrivate *instance() { return self; }
#ifdef QT_KEYPAD_NAVIGATION #ifdef QT_KEYPAD_NAVIGATION