From 8eb1bc10f5d744cbe529535e42fc721082b471a1 Mon Sep 17 00:00:00 2001 From: Marco Martin Date: Thu, 19 Jun 2014 17:38:31 +0200 Subject: [PATCH] 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 } } --- src/declarativeimports/core/CMakeLists.txt | 1 + src/declarativeimports/core/colorscope.cpp | 100 ++++++++++++++++++ src/declarativeimports/core/colorscope.h | 66 ++++++++++++ .../core/corebindingsplugin.cpp | 2 + src/desktoptheme/breeze/icons/document.svgz | Bin 2082 -> 2082 bytes src/plasma/private/theme_p.cpp | 44 ++++++-- src/plasma/private/theme_p.h | 2 +- src/plasma/svg.cpp | 5 +- src/plasma/svg.h | 6 +- src/plasma/theme.cpp | 4 +- src/plasma/theme.h | 3 +- 11 files changed, 217 insertions(+), 16 deletions(-) create mode 100644 src/declarativeimports/core/colorscope.cpp create mode 100644 src/declarativeimports/core/colorscope.h diff --git a/src/declarativeimports/core/CMakeLists.txt b/src/declarativeimports/core/CMakeLists.txt index dd3fdb4de..e3d2f4e7c 100644 --- a/src/declarativeimports/core/CMakeLists.txt +++ b/src/declarativeimports/core/CMakeLists.txt @@ -17,6 +17,7 @@ endif() set(corebindings_SRCS corebindingsplugin.cpp + colorscope.cpp datamodel.cpp datasource.cpp # runnermodel.cpp diff --git a/src/declarativeimports/core/colorscope.cpp b/src/declarativeimports/core/colorscope.cpp new file mode 100644 index 000000000..4115052ac --- /dev/null +++ b/src/declarativeimports/core/colorscope.cpp @@ -0,0 +1,100 @@ +/*************************************************************************** + * Copyright 2011 Marco Martin * + * Copyright 2011 Artur Duque de Souza * + * Copyright 2013 Sebastian Kügler * + * * + * 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 +#include + + +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(p); + if (c) { + return c; + } + //this will be parent() for qobjects, parentItem for QQuickItems + QQuickItem *item = qobject_cast(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" diff --git a/src/declarativeimports/core/colorscope.h b/src/declarativeimports/core/colorscope.h new file mode 100644 index 000000000..b90bb6143 --- /dev/null +++ b/src/declarativeimports/core/colorscope.h @@ -0,0 +1,66 @@ +/*************************************************************************** + * Copyright 2014 Marco Martin * + * * + * 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 +#include +#include +#include +#include + +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 diff --git a/src/declarativeimports/core/corebindingsplugin.cpp b/src/declarativeimports/core/corebindingsplugin.cpp index 865be4f57..ebd9fd73a 100644 --- a/src/declarativeimports/core/corebindingsplugin.cpp +++ b/src/declarativeimports/core/corebindingsplugin.cpp @@ -38,6 +38,7 @@ #include "dialog.h" #include "iconitem.h" #include "serviceoperationstatus.h" +#include "colorscope.h" #include "tooltip.h" #include "units.h" @@ -81,6 +82,7 @@ void CoreBindingsPlugin::registerTypes(const char *uri) //qmlRegisterType(uri, 2, 0, "Theme"); qmlRegisterUncreatableType(uri, 2, 0, "Theme", "It is not possible to instantiate Theme directly."); + qmlRegisterType(uri, 2, 0, "ColorScope"); qmlRegisterType(uri, 2, 0, "DataSource"); qmlRegisterType(uri, 2, 0, "DataModel"); diff --git a/src/desktoptheme/breeze/icons/document.svgz b/src/desktoptheme/breeze/icons/document.svgz index d9c424a35d7c5f4d9ecf7fa99ec49b78c9d3a043..401185fdfcc8eddf2f4fdbbcbf7519afd68ad91d 100644 GIT binary patch delta 16 XcmZ1^utdevicePixelRatio; } -void Svg::setColorGroup(Theme::ColorGroup group) +void Svg::setColorGroup(Plasma::Theme::ColorGroup group) { if (d->colorGroup == group) { return; @@ -695,9 +695,10 @@ void Svg::setColorGroup(Theme::ColorGroup group) d->colorGroup = group; d->renderer = 0; emit colorGroupChanged(); + emit repaintNeeded(); } -Theme::ColorGroup Svg::colorGroup() const +Plasma::Theme::ColorGroup Svg::colorGroup() const { return d->colorGroup; } diff --git a/src/plasma/svg.h b/src/plasma/svg.h index 04489b189..647dad640 100644 --- a/src/plasma/svg.h +++ b/src/plasma/svg.h @@ -61,7 +61,7 @@ class PLASMA_EXPORT Svg : public QObject Q_PROPERTY(bool multipleImages READ containsMultipleImages WRITE setContainsMultipleImages) Q_PROPERTY(QString imagePath READ imagePath WRITE setImagePath NOTIFY imagePathChanged) 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: /** @@ -101,12 +101,12 @@ public: * make them use ButtonTextColor/ButtonBackgroundColor * or ViewTextColor/ViewBackgroundColor */ - void setColorGroup(Theme::ColorGroup group); + void setColorGroup(Plasma::Theme::ColorGroup group); /** * @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. diff --git a/src/plasma/theme.cpp b/src/plasma/theme.cpp index dd3788943..f08cebdd9 100644 --- a/src/plasma/theme.cpp +++ b/src/plasma/theme.cpp @@ -272,9 +272,9 @@ KSharedConfigPtr Theme::colorScheme() const 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) diff --git a/src/plasma/theme.h b/src/plasma/theme.h index 4f7c1fe6b..6f39dc438 100644 --- a/src/plasma/theme.h +++ b/src/plasma/theme.h @@ -185,8 +185,9 @@ public: * 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 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