plasma-framework/private/themedwidgetinterface_p.h
Aaron Seigo 27531a3173 the palette gets updated by all sorts of events on the scene now (incl. reparenting!), so make sure the palette is _really_ changing before marking it as custom
this fixes widget colours not updating with theme changes -> if, for instance, an offscreen widget is created, this results in a reparenting (to null)
and that in turn creates a palette changed event ... which was being intepretted as a custom palette being set and that would disable future updates
due to theme changes.

this also introduces a small helper which computes the palettes once for use by all widgets as a small performance improvement.

BUG:261967
2011-12-04 14:00:32 +01:00

132 lines
3.9 KiB
C++

/******************************************************************************
* Copyright 2009 by Aaron Seigo <aseigo@kde.org> *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public License *
* along with this library; see the file COPYING.LIB. If not, write to *
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
* Boston, MA 02110-1301, USA. *
*******************************************************************************/
#ifndef THEMEDWIDGETINTERFACE_P_H
#define THEMEDWIDGETINTERFACE_P_H
#include "kglobalsettings.h"
#include <QApplication>
#include "theme.h"
namespace Plasma
{
class PaletteHelper : public QObject
{
Q_OBJECT
public:
static PaletteHelper *self();
public Q_SLOTS:
void generatePalettes();
Q_SIGNALS:
void palettesUpdated();
public:
QPalette palette;
QPalette buttonPalette;
private:
PaletteHelper();
static PaletteHelper *s_paletteHelper;
};
template <class T>
class ThemedWidgetInterface
{
public:
ThemedWidgetInterface(T *publicClass)
: q(publicClass),
customPalette(false),
customFont(false),
buttonColorForText(false),
internalPaletteChange(false)
{
QObject::connect(PaletteHelper::self(), SIGNAL(palettesUpdated()), q, SLOT(setPalette()));
}
void initTheming()
{
customPalette = false;
customFont = false;
setPalette();
}
void setPalette()
{
if (!customPalette) {
internalPaletteChange = true;
q->setPalette((buttonColorForText ? PaletteHelper::self()->buttonPalette
: PaletteHelper::self()->palette));
internalPaletteChange = false;
}
if (!customFont) {
q->setFont(Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont));
customFont = false;
}
}
void changeEvent(QEvent *event)
{
switch (event->type()) {
case QEvent::FontChange:
customFont = q->font() != QApplication::font();
break;
case QEvent::PaletteChange:
if (!internalPaletteChange &&
q->palette() != (buttonColorForText ? PaletteHelper::self()->buttonPalette : PaletteHelper::self()->palette)) {
customPalette = true;
}
break;
default:
break;
}
}
void setWidget(QWidget *widget)
{
internalPaletteChange = true;
q->setWidget(widget);
internalPaletteChange = false;
}
void event(QEvent *event)
{
if (event->type() == QEvent::Show) {
customFont = q->font() != QApplication::font();
}
}
T *q;
bool customPalette : 1;
bool customFont : 1;
bool buttonColorForText : 1;
bool internalPaletteChange : 1;
};
} // namespace Plasma
#endif