Kirigami2

basictheme.cpp
1/*
2 * SPDX-FileCopyrightText: 2017 by Marco Martin <mart@kde.org>
3 * SPDX-FileCopyrightText: 2021 Arjen Hiemstra <ahiemstra@heimr.nl>
4 *
5 * SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7
8#include "basictheme_p.h"
9#include "styleselector.h"
10
11#include <QFile>
12#include <QGuiApplication>
13
14#include "kirigamiplatform_logging.h"
15
16namespace Kirigami
17{
18namespace Platform
19{
20
21BasicThemeDefinition::BasicThemeDefinition(QObject *parent)
22 : QObject(parent)
23{
24 defaultFont = qGuiApp->font();
25
26 smallFont = qGuiApp->font();
27 smallFont.setPointSize(smallFont.pointSize() - 2);
28}
29
30void BasicThemeDefinition::syncToQml(PlatformTheme *object)
31{
32 auto item = qobject_cast<QQuickItem *>(object->parent());
33 if (item && qmlAttachedPropertiesObject<PlatformTheme>(item, false) == object) {
34 Q_EMIT sync(item);
35 }
36}
37
38BasicThemeInstance::BasicThemeInstance(QObject *parent)
39 : QObject(parent)
40{
41}
42
43BasicThemeDefinition &BasicThemeInstance::themeDefinition(QQmlEngine *engine)
44{
45 if (m_themeDefinition) {
46 return *m_themeDefinition;
47 }
48
49 auto themeUrl = StyleSelector::componentUrl(QStringLiteral("Theme.qml"));
50 QQmlComponent component(engine);
51 component.loadUrl(themeUrl);
52
53 if (!component.isError()) {
54 auto result = component.create();
55 if (auto themeDefinition = qobject_cast<BasicThemeDefinition *>(result)) {
56 m_themeDefinition.reset(themeDefinition);
57 } else {
58 const auto errors = component.errors();
59 for (auto error : errors) {
60 qCWarning(KirigamiPlatform) << error.toString();
61 }
62
63 qCWarning(KirigamiPlatform) << "Invalid Theme file, using default Basic theme.";
64 m_themeDefinition = std::make_unique<BasicThemeDefinition>();
65 }
66 } else {
67 qCDebug(KirigamiPlatform) << "No Theme file found, using default Basic theme";
68 m_themeDefinition = std::make_unique<BasicThemeDefinition>();
69 }
70
71 connect(m_themeDefinition.get(), &BasicThemeDefinition::changed, this, &BasicThemeInstance::onDefinitionChanged);
72
73 return *m_themeDefinition;
74}
75
76void BasicThemeInstance::onDefinitionChanged()
77{
78 for (auto watcher : std::as_const(watchers)) {
79 watcher->sync();
80 }
81}
82
83Q_GLOBAL_STATIC(BasicThemeInstance, basicThemeInstance)
84
85BasicTheme::BasicTheme(QObject *parent)
86 : PlatformTheme(parent)
87{
88 basicThemeInstance()->watchers.append(this);
89
90 sync();
91}
92
93BasicTheme::~BasicTheme()
94{
95 basicThemeInstance()->watchers.removeOne(this);
96}
97
98void BasicTheme::sync()
99{
100 auto &definition = basicThemeInstance()->themeDefinition(qmlEngine(parent()));
101
102 switch (colorSet()) {
103 case BasicTheme::Button:
104 setTextColor(tint(definition.buttonTextColor));
105 setBackgroundColor(tint(definition.buttonBackgroundColor));
106 setAlternateBackgroundColor(tint(definition.buttonAlternateBackgroundColor));
107 setHoverColor(tint(definition.buttonHoverColor));
108 setFocusColor(tint(definition.buttonFocusColor));
109 break;
110 case BasicTheme::View:
111 setTextColor(tint(definition.viewTextColor));
112 setBackgroundColor(tint(definition.viewBackgroundColor));
113 setAlternateBackgroundColor(tint(definition.viewAlternateBackgroundColor));
114 setHoverColor(tint(definition.viewHoverColor));
115 setFocusColor(tint(definition.viewFocusColor));
116 break;
117 case BasicTheme::Selection:
118 setTextColor(tint(definition.selectionTextColor));
119 setBackgroundColor(tint(definition.selectionBackgroundColor));
120 setAlternateBackgroundColor(tint(definition.selectionAlternateBackgroundColor));
121 setHoverColor(tint(definition.selectionHoverColor));
122 setFocusColor(tint(definition.selectionFocusColor));
123 break;
124 case BasicTheme::Tooltip:
125 setTextColor(tint(definition.tooltipTextColor));
126 setBackgroundColor(tint(definition.tooltipBackgroundColor));
127 setAlternateBackgroundColor(tint(definition.tooltipAlternateBackgroundColor));
128 setHoverColor(tint(definition.tooltipHoverColor));
129 setFocusColor(tint(definition.tooltipFocusColor));
130 break;
131 case BasicTheme::Complementary:
132 setTextColor(tint(definition.complementaryTextColor));
133 setBackgroundColor(tint(definition.complementaryBackgroundColor));
134 setAlternateBackgroundColor(tint(definition.complementaryAlternateBackgroundColor));
135 setHoverColor(tint(definition.complementaryHoverColor));
136 setFocusColor(tint(definition.complementaryFocusColor));
137 break;
138 case BasicTheme::Window:
139 default:
140 setTextColor(tint(definition.textColor));
141 setBackgroundColor(tint(definition.backgroundColor));
142 setAlternateBackgroundColor(tint(definition.alternateBackgroundColor));
143 setHoverColor(tint(definition.hoverColor));
144 setFocusColor(tint(definition.focusColor));
145 break;
146 }
147
148 setDisabledTextColor(tint(definition.disabledTextColor));
149 setHighlightColor(tint(definition.highlightColor));
150 setHighlightedTextColor(tint(definition.highlightedTextColor));
151 setActiveTextColor(tint(definition.activeTextColor));
152 setActiveBackgroundColor(tint(definition.activeBackgroundColor));
153 setLinkColor(tint(definition.linkColor));
154 setLinkBackgroundColor(tint(definition.linkBackgroundColor));
155 setVisitedLinkColor(tint(definition.visitedLinkColor));
156 setVisitedLinkBackgroundColor(tint(definition.visitedLinkBackgroundColor));
157 setNegativeTextColor(tint(definition.negativeTextColor));
158 setNegativeBackgroundColor(tint(definition.negativeBackgroundColor));
159 setNeutralTextColor(tint(definition.neutralTextColor));
160 setNeutralBackgroundColor(tint(definition.neutralBackgroundColor));
161 setPositiveTextColor(tint(definition.positiveTextColor));
162 setPositiveBackgroundColor(tint(definition.positiveBackgroundColor));
163
164 setDefaultFont(definition.defaultFont);
165 setSmallFont(definition.smallFont);
166}
167
168bool BasicTheme::event(QEvent *event)
169{
170 if (event->type() == PlatformThemeEvents::DataChangedEvent::type) {
171 sync();
172 }
173
174 if (event->type() == PlatformThemeEvents::ColorSetChangedEvent::type) {
175 sync();
176 }
177
178 if (event->type() == PlatformThemeEvents::ColorGroupChangedEvent::type) {
179 sync();
180 }
181
182 if (event->type() == PlatformThemeEvents::ColorChangedEvent::type) {
183 basicThemeInstance()->themeDefinition(qmlEngine(parent())).syncToQml(this);
184 }
185
186 if (event->type() == PlatformThemeEvents::FontChangedEvent::type) {
187 basicThemeInstance()->themeDefinition(qmlEngine(parent())).syncToQml(this);
188 }
189
190 return PlatformTheme::event(event);
191}
192
193QColor BasicTheme::tint(const QColor &color)
194{
195 switch (colorGroup()) {
196 case PlatformTheme::Inactive:
197 return QColor::fromHsvF(color.hueF(), color.saturationF() * 0.5, color.valueF());
198 case PlatformTheme::Disabled:
199 return QColor::fromHsvF(color.hueF(), color.saturationF() * 0.5, color.valueF() * 0.8);
200 default:
201 return color;
202 }
203}
204
205}
206}
207
208#include "moc_basictheme_p.cpp"
AKONADI_CALENDAR_EXPORT KCalendarCore::Event::Ptr event(const Akonadi::Item &item)
KGUIADDONS_EXPORT QColor tint(const QColor &base, const QColor &color, qreal amount=0.3)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
QColor fromHsvF(float h, float s, float v, float a)
float hueF() const const
float saturationF() const const
float valueF() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 24 2024 11:50:33 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.