Introduce the ColorScope class

it's an import in core, and advertises itself as an "attached property"
with this we can:
say that all its chidren are of a certain context, like "button" or
"complementary"

then anywhere there will be available an attached property, as ColorScope,
so like:

PlasmaCore.ColorScope {
 group: PlasmaCore.Theme.Complementary

 PlasmaComponents.Label {
   text: "foo"
   color: ColorScope.textColor
 }
}
This commit is contained in:
Marco Martin 2014-06-19 17:38:31 +02:00
parent f22dd8e571
commit 8eb1bc10f5
11 changed files with 217 additions and 16 deletions

View File

@ -17,6 +17,7 @@ endif()
set(corebindings_SRCS set(corebindings_SRCS
corebindingsplugin.cpp corebindingsplugin.cpp
colorscope.cpp
datamodel.cpp datamodel.cpp
datasource.cpp datasource.cpp
# runnermodel.cpp # runnermodel.cpp

View File

@ -0,0 +1,100 @@
/***************************************************************************
* Copyright 2011 Marco Martin <mart@kde.org> *
* Copyright 2011 Artur Duque de Souza <asouza@kde.org> *
* Copyright 2013 Sebastian Kügler <sebas@kde.org> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program 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 General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
***************************************************************************/
#include "colorscope.h"
#include <QQmlContext>
#include <QQmlEngine>
ColorScope *ColorScope::s_colorScope = 0;
ColorScope::ColorScope(QQuickItem *parent)
: QQuickItem(parent),
m_group(Plasma::Theme::NormalColorGroup)
{
}
ColorScope::~ColorScope()
{
}
ColorScope *ColorScope::qmlAttachedProperties(QObject *object)
{
QObject *p = object;
while (p) {
ColorScope *c = qobject_cast<ColorScope *>(p);
if (c) {
return c;
}
//this will be parent() for qobjects, parentItem for QQuickItems
QQuickItem *item = qobject_cast<QQuickItem *>(p);
if (item) {
p = item->parentItem();
} else {
p = p->parent();
}
}
if (!s_colorScope) {
s_colorScope = new ColorScope;
}
return s_colorScope;
}
void ColorScope::setColorGroup(Plasma::Theme::ColorGroup group)
{
if (m_group == group) {
return;
}
m_group = group;
emit colorGroupChanged();
emit colorsChanged();
}
Plasma::Theme::ColorGroup ColorScope::colorGroup() const
{
return m_group;
}
QColor ColorScope::textColor() const
{
return m_theme.color(Plasma::Theme::TextColor, m_group);
}
QColor ColorScope::highlightColor() const
{
return m_theme.color(Plasma::Theme::BackgroundColor, m_group);
}
QColor ColorScope::backgroundColor() const
{
return m_theme.color(Plasma::Theme::BackgroundColor, m_group);
}
#include "moc_colorscope.cpp"

View File

@ -0,0 +1,66 @@
/***************************************************************************
* Copyright 2014 Marco Martin <mart@kde.org> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program 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 General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
***************************************************************************/
#ifndef COLORSCOPE_H
#define COLORSCOPE_H
#include <QQuickItem>
#include <QWeakPointer>
#include <QtCore/QVariant>
#include <Plasma/Plasma>
#include <Plasma/Theme>
class QQuickItem;
class ColorScope : public QQuickItem
{
Q_OBJECT
Q_PROPERTY(Plasma::Theme::ColorGroup colorGroup READ colorGroup WRITE setColorGroup)
Q_PROPERTY(QColor textColor READ textColor NOTIFY colorsChanged)
Q_PROPERTY(QColor highlightColor READ highlightColor NOTIFY colorsChanged)
Q_PROPERTY(QColor backgroundColor READ backgroundColor NOTIFY colorsChanged)
public:
ColorScope(QQuickItem *parent = 0);
~ColorScope();
void setColorGroup(Plasma::Theme::ColorGroup group);
Plasma::Theme::ColorGroup colorGroup() const;
QColor textColor() const;
QColor highlightColor() const;
QColor backgroundColor() const;
////NEEDED BY QML TO CREATE ATTACHED PROPERTIES
static ColorScope *qmlAttachedProperties(QObject *object);
Q_SIGNALS:
void colorGroupChanged();
void colorsChanged();
private:
Plasma::Theme m_theme;
Plasma::Theme::ColorGroup m_group;
static ColorScope *s_colorScope;
};
QML_DECLARE_TYPEINFO(ColorScope, QML_HAS_ATTACHED_PROPERTIES)
#endif

View File

@ -38,6 +38,7 @@
#include "dialog.h" #include "dialog.h"
#include "iconitem.h" #include "iconitem.h"
#include "serviceoperationstatus.h" #include "serviceoperationstatus.h"
#include "colorscope.h"
#include "tooltip.h" #include "tooltip.h"
#include "units.h" #include "units.h"
@ -81,6 +82,7 @@ void CoreBindingsPlugin::registerTypes(const char *uri)
//qmlRegisterType<ThemeProxy>(uri, 2, 0, "Theme"); //qmlRegisterType<ThemeProxy>(uri, 2, 0, "Theme");
qmlRegisterUncreatableType<Plasma::Theme>(uri, 2, 0, "Theme", "It is not possible to instantiate Theme directly."); qmlRegisterUncreatableType<Plasma::Theme>(uri, 2, 0, "Theme", "It is not possible to instantiate Theme directly.");
qmlRegisterType<ColorScope>(uri, 2, 0, "ColorScope");
qmlRegisterType<Plasma::DataSource>(uri, 2, 0, "DataSource"); qmlRegisterType<Plasma::DataSource>(uri, 2, 0, "DataSource");
qmlRegisterType<Plasma::DataModel>(uri, 2, 0, "DataModel"); qmlRegisterType<Plasma::DataModel>(uri, 2, 0, "DataModel");

View File

@ -526,17 +526,47 @@ void ThemePrivate::saveSvgElementsCache()
} }
} }
QColor ThemePrivate::color(Theme::ColorRole role) const QColor ThemePrivate::color(Theme::ColorRole role, Theme::ColorGroup group) const
{ {
switch (role) { switch (role) {
case Theme::TextColor: case Theme::TextColor: {
return colorScheme.foreground(KColorScheme::NormalText).color(); switch (group) {
case Theme::ButtonColorGroup:
return buttonColorScheme.foreground(KColorScheme::NormalText).color();
case Theme::ViewColorGroup:
return viewColorScheme.foreground(KColorScheme::NormalText).color();
case Theme::ComplementaryColorGroup:
return color(Theme::ComplementaryTextColor, group);
default:
return colorScheme.foreground(KColorScheme::NormalText).color();
}
}
case Theme::HighlightColor: case Theme::HighlightColor: {
return colorScheme.decoration(KColorScheme::HoverColor).color(); switch (group) {
case Theme::ButtonColorGroup:
return buttonColorScheme.decoration(KColorScheme::HoverColor).color();
case Theme::ViewColorGroup:
return viewColorScheme.decoration(KColorScheme::HoverColor).color();
case Theme::ComplementaryColorGroup:
return color(Theme::ComplementaryHoverColor, group);
default:
return colorScheme.decoration(KColorScheme::HoverColor).color();
}
}
case Theme::BackgroundColor: case Theme::BackgroundColor: {
return colorScheme.background(KColorScheme::NormalBackground).color(); switch (group) {
case Theme::ButtonColorGroup:
return buttonColorScheme.background(KColorScheme::NormalBackground).color();
case Theme::ViewColorGroup:
return viewColorScheme.background(KColorScheme::NormalBackground).color();
case Theme::ComplementaryColorGroup:
return color(Theme::ComplementaryBackgroundColor, group);
default:
return colorScheme.background(KColorScheme::NormalBackground).color();
}
}
case Theme::ButtonTextColor: case Theme::ButtonTextColor:
return buttonColorScheme.foreground(KColorScheme::NormalText).color(); return buttonColorScheme.foreground(KColorScheme::NormalText).color();

View File

@ -79,7 +79,7 @@ public:
const QString processStyleSheet(const QString &css); const QString processStyleSheet(const QString &css);
const QString svgStyleSheet(Plasma::Theme::ColorGroup group); const QString svgStyleSheet(Plasma::Theme::ColorGroup group);
QColor color(Theme::ColorRole role) const; QColor color(Theme::ColorRole role, Theme::ColorGroup group = Theme::NormalColorGroup) const;
public Q_SLOTS: public Q_SLOTS:
void compositingChanged(bool active); void compositingChanged(bool active);

View File

@ -686,7 +686,7 @@ qreal Svg::devicePixelRatio()
return d->devicePixelRatio; return d->devicePixelRatio;
} }
void Svg::setColorGroup(Theme::ColorGroup group) void Svg::setColorGroup(Plasma::Theme::ColorGroup group)
{ {
if (d->colorGroup == group) { if (d->colorGroup == group) {
return; return;
@ -695,9 +695,10 @@ void Svg::setColorGroup(Theme::ColorGroup group)
d->colorGroup = group; d->colorGroup = group;
d->renderer = 0; d->renderer = 0;
emit colorGroupChanged(); emit colorGroupChanged();
emit repaintNeeded();
} }
Theme::ColorGroup Svg::colorGroup() const Plasma::Theme::ColorGroup Svg::colorGroup() const
{ {
return d->colorGroup; return d->colorGroup;
} }

View File

@ -61,7 +61,7 @@ class PLASMA_EXPORT Svg : public QObject
Q_PROPERTY(bool multipleImages READ containsMultipleImages WRITE setContainsMultipleImages) Q_PROPERTY(bool multipleImages READ containsMultipleImages WRITE setContainsMultipleImages)
Q_PROPERTY(QString imagePath READ imagePath WRITE setImagePath NOTIFY imagePathChanged) Q_PROPERTY(QString imagePath READ imagePath WRITE setImagePath NOTIFY imagePathChanged)
Q_PROPERTY(bool usingRenderingCache READ isUsingRenderingCache WRITE setUsingRenderingCache) Q_PROPERTY(bool usingRenderingCache READ isUsingRenderingCache WRITE setUsingRenderingCache)
Q_PROPERTY(Theme::ColorGroup colorGroup READ colorGroup WRITE setColorGroup NOTIFY colorGroupChanged); Q_PROPERTY(Plasma::Theme::ColorGroup colorGroup READ colorGroup WRITE setColorGroup NOTIFY colorGroupChanged);
public: public:
/** /**
@ -101,12 +101,12 @@ public:
* make them use ButtonTextColor/ButtonBackgroundColor * make them use ButtonTextColor/ButtonBackgroundColor
* or ViewTextColor/ViewBackgroundColor * or ViewTextColor/ViewBackgroundColor
*/ */
void setColorGroup(Theme::ColorGroup group); void setColorGroup(Plasma::Theme::ColorGroup group);
/** /**
* @return the color group for this Svg * @return the color group for this Svg
*/ */
Theme::ColorGroup colorGroup() const; Plasma::Theme::ColorGroup colorGroup() const;
/** /**
* Returns a pixmap of the SVG represented by this object. * Returns a pixmap of the SVG represented by this object.

View File

@ -272,9 +272,9 @@ KSharedConfigPtr Theme::colorScheme() const
return d->colors; return d->colors;
} }
QColor Theme::color(ColorRole role) const QColor Theme::color(ColorRole role, ColorGroup group) const
{ {
return d->color(role); return d->color(role, group);
} }
void Theme::setUseGlobalSettings(bool useGlobal) void Theme::setUseGlobalSettings(bool useGlobal)

View File

@ -185,8 +185,9 @@ public:
* Returns the text color to be used by items resting on the background * Returns the text color to be used by items resting on the background
* *
* @param role which role (usage pattern) to get the color for * @param role which role (usage pattern) to get the color for
* @param group which group we want a color of
*/ */
QColor color(ColorRole role) const; QColor color(ColorRole role, ColorGroup group = NormalColorGroup) const;
/** /**
* Tells the theme whether to follow the global settings or use application * Tells the theme whether to follow the global settings or use application