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
This commit is contained in:
Aaron Seigo 2011-12-04 13:55:05 +01:00
parent 4aa690ccc8
commit 27531a3173
3 changed files with 127 additions and 31 deletions

View File

@ -140,6 +140,7 @@ set(plasma_LIB_SRCS
private/storage.cpp
private/storagethread.cpp
private/style.cpp
private/themedwidgetinterface.cpp
private/trustedonlyauthorization.cpp
private/tooltip.cpp
private/wallpaperrenderthread.cpp

View File

@ -0,0 +1,85 @@
/******************************************************************************
* Copyright 2011 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. *
*******************************************************************************/
#include "themedwidgetinterface_p.h"
#include "theme.h"
namespace Plasma
{
PaletteHelper *PaletteHelper::s_paletteHelper = 0;
PaletteHelper::PaletteHelper()
: QObject()
{
generatePalettes();
connect(Theme::defaultTheme(), SIGNAL(themeChanged()), this, SLOT(generatePalettes()));
connect(KGlobalSettings::self(), SIGNAL(kdisplayPaletteChanged()), this, SLOT(generatePalettes()));
}
PaletteHelper *PaletteHelper::self()
{
if (!s_paletteHelper) {
s_paletteHelper = new PaletteHelper;
}
return s_paletteHelper;
}
void PaletteHelper::generatePalettes()
{
Theme *theme = Theme::defaultTheme();
QColor color = theme->color(Theme::TextColor);
palette = qApp->palette();
palette.setColor(QPalette::Normal, QPalette::WindowText, color);
palette.setColor(QPalette::Inactive, QPalette::WindowText, color);
palette.setColor(QPalette::Normal, QPalette::Link, theme->color(Theme::LinkColor));
palette.setColor(QPalette::Normal, QPalette::LinkVisited, theme->color(Theme::VisitedLinkColor));
qreal alpha = color.alphaF();
color.setAlphaF(0.6);
palette.setColor(QPalette::Disabled, QPalette::WindowText, color);
color.setAlphaF(alpha);
palette.setColor(QPalette::Normal, QPalette::Text, color);
palette.setColor(QPalette::Inactive, QPalette::Text, color);
const QColor buttonColor = Theme::defaultTheme()->color(Theme::ButtonTextColor);
palette.setColor(QPalette::Normal, QPalette::ButtonText, buttonColor);
palette.setColor(QPalette::Inactive, QPalette::ButtonText, buttonColor);
//FIXME: hardcoded colors .. looks incorrect
palette.setColor(QPalette::Normal, QPalette::Base, QColor(0,0,0,0));
palette.setColor(QPalette::Inactive, QPalette::Base, QColor(0,0,0,0));
buttonPalette = palette;
buttonPalette.setColor(QPalette::Normal, QPalette::Text, buttonColor);
buttonPalette.setColor(QPalette::Inactive, QPalette::Text, buttonColor);
emit palettesUpdated();
}
} // namespace Plasma
#include "themedwidgetinterface_p.moc"

View File

@ -28,6 +28,28 @@
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
{
@ -36,10 +58,10 @@ public:
: q(publicClass),
customPalette(false),
customFont(false),
buttonColorForText(false)
buttonColorForText(false),
internalPaletteChange(false)
{
QObject::connect(Theme::defaultTheme(), SIGNAL(themeChanged()), q, SLOT(setPalette()));
QObject::connect(KGlobalSettings::self(), SIGNAL(kdisplayPaletteChanged()), q, SLOT(setPalette()));
QObject::connect(PaletteHelper::self(), SIGNAL(palettesUpdated()), q, SLOT(setPalette()));
}
void initTheming()
@ -52,33 +74,10 @@ public:
void setPalette()
{
if (!customPalette) {
QColor color = Theme::defaultTheme()->color(Theme::TextColor);
QPalette p = q->palette();
p.setColor(QPalette::Normal, QPalette::WindowText, color);
p.setColor(QPalette::Inactive, QPalette::WindowText, color);
p.setColor(QPalette::Normal, QPalette::Link, Theme::defaultTheme()->color(Theme::LinkColor));
p.setColor(QPalette::Normal, QPalette::LinkVisited, Theme::defaultTheme()->color(Theme::VisitedLinkColor));
qreal alpha = color.alphaF();
color.setAlphaF(0.6);
p.setColor(QPalette::Disabled, QPalette::WindowText, color);
color.setAlphaF(alpha);
const QColor buttonColor = Theme::defaultTheme()->color(Theme::ButtonTextColor);
p.setColor(QPalette::Normal, QPalette::Text, buttonColorForText ? buttonColor : color);
p.setColor(QPalette::Inactive, QPalette::Text, buttonColorForText ? buttonColor : color);
p.setColor(QPalette::Normal, QPalette::ButtonText, buttonColor);
p.setColor(QPalette::Inactive, QPalette::ButtonText, buttonColor);
//FIXME: hardcoded colors .. looks incorrect
p.setColor(QPalette::Normal, QPalette::Base, QColor(0,0,0,0));
p.setColor(QPalette::Inactive, QPalette::Base, QColor(0,0,0,0));
q->setPalette(p);
customPalette = false;
internalPaletteChange = true;
q->setPalette((buttonColorForText ? PaletteHelper::self()->buttonPalette
: PaletteHelper::self()->palette));
internalPaletteChange = false;
}
if (!customFont) {
@ -95,7 +94,10 @@ public:
break;
case QEvent::PaletteChange:
customPalette = true;
if (!internalPaletteChange &&
q->palette() != (buttonColorForText ? PaletteHelper::self()->buttonPalette : PaletteHelper::self()->palette)) {
customPalette = true;
}
break;
default:
@ -103,6 +105,13 @@ public:
}
}
void setWidget(QWidget *widget)
{
internalPaletteChange = true;
q->setWidget(widget);
internalPaletteChange = false;
}
void event(QEvent *event)
{
if (event->type() == QEvent::Show) {
@ -114,6 +123,7 @@ public:
bool customPalette : 1;
bool customFont : 1;
bool buttonColorForText : 1;
bool internalPaletteChange : 1;
};
} // namespace Plasma