Kirigami2

basictheme.cpp
1/*
2 * SPDX-FileCopyrightText: 2017 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 (auto themeDefinition = qobject_cast<BasicThemeDefinition *>(component.create())) {
54 m_themeDefinition.reset(themeDefinition);
55 } else {
56 const auto errors = component.errors();
57 for (auto error : errors) {
58 qCWarning(KirigamiPlatform) << error.toString();
59 }
60
61 qCWarning(KirigamiPlatform) << "Invalid Theme file, using default Basic theme.";
62 m_themeDefinition = std::make_unique<BasicThemeDefinition>();
63 }
64
65 connect(m_themeDefinition.get(), &BasicThemeDefinition::changed, this, &BasicThemeInstance::onDefinitionChanged);
66
67 return *m_themeDefinition;
68}
69
70void BasicThemeInstance::onDefinitionChanged()
71{
72 for (auto watcher : std::as_const(watchers)) {
73 watcher->sync();
74 }
75}
76
77Q_GLOBAL_STATIC(BasicThemeInstance, basicThemeInstance)
78
79BasicTheme::BasicTheme(QObject *parent)
80 : PlatformTheme(parent)
81{
82 basicThemeInstance()->watchers.append(this);
83
84 sync();
85}
86
87BasicTheme::~BasicTheme()
88{
89 basicThemeInstance()->watchers.removeOne(this);
90}
91
92void BasicTheme::sync()
93{
94 PlatformThemeChangeTracker tracker{this};
95
96 auto &definition = basicThemeInstance()->themeDefinition(qmlEngine(parent()));
97
98 switch (colorSet()) {
99 case BasicTheme::Button:
100 setTextColor(tint(definition.buttonTextColor));
101 setBackgroundColor(tint(definition.buttonBackgroundColor));
102 setAlternateBackgroundColor(tint(definition.buttonAlternateBackgroundColor));
103 setHoverColor(tint(definition.buttonHoverColor));
104 setFocusColor(tint(definition.buttonFocusColor));
105 break;
106 case BasicTheme::View:
107 setTextColor(tint(definition.viewTextColor));
108 setBackgroundColor(tint(definition.viewBackgroundColor));
109 setAlternateBackgroundColor(tint(definition.viewAlternateBackgroundColor));
110 setHoverColor(tint(definition.viewHoverColor));
111 setFocusColor(tint(definition.viewFocusColor));
112 break;
113 case BasicTheme::Selection:
114 setTextColor(tint(definition.selectionTextColor));
115 setBackgroundColor(tint(definition.selectionBackgroundColor));
116 setAlternateBackgroundColor(tint(definition.selectionAlternateBackgroundColor));
117 setHoverColor(tint(definition.selectionHoverColor));
118 setFocusColor(tint(definition.selectionFocusColor));
119 break;
120 case BasicTheme::Tooltip:
121 setTextColor(tint(definition.tooltipTextColor));
122 setBackgroundColor(tint(definition.tooltipBackgroundColor));
123 setAlternateBackgroundColor(tint(definition.tooltipAlternateBackgroundColor));
124 setHoverColor(tint(definition.tooltipHoverColor));
125 setFocusColor(tint(definition.tooltipFocusColor));
126 break;
127 case BasicTheme::Complementary:
128 setTextColor(tint(definition.complementaryTextColor));
129 setBackgroundColor(tint(definition.complementaryBackgroundColor));
130 setAlternateBackgroundColor(tint(definition.complementaryAlternateBackgroundColor));
131 setHoverColor(tint(definition.complementaryHoverColor));
132 setFocusColor(tint(definition.complementaryFocusColor));
133 break;
134 case BasicTheme::Window:
135 default:
136 setTextColor(tint(definition.textColor));
137 setBackgroundColor(tint(definition.backgroundColor));
138 setAlternateBackgroundColor(tint(definition.alternateBackgroundColor));
139 setHoverColor(tint(definition.hoverColor));
140 setFocusColor(tint(definition.focusColor));
141 break;
142 }
143
144 setDisabledTextColor(tint(definition.disabledTextColor));
145 setHighlightColor(tint(definition.highlightColor));
146 setHighlightedTextColor(tint(definition.highlightedTextColor));
147 setActiveTextColor(tint(definition.activeTextColor));
148 setActiveBackgroundColor(tint(definition.activeBackgroundColor));
149 setLinkColor(tint(definition.linkColor));
150 setLinkBackgroundColor(tint(definition.linkBackgroundColor));
151 setVisitedLinkColor(tint(definition.visitedLinkColor));
152 setVisitedLinkBackgroundColor(tint(definition.visitedLinkBackgroundColor));
153 setNegativeTextColor(tint(definition.negativeTextColor));
154 setNegativeBackgroundColor(tint(definition.negativeBackgroundColor));
155 setNeutralTextColor(tint(definition.neutralTextColor));
156 setNeutralBackgroundColor(tint(definition.neutralBackgroundColor));
157 setPositiveTextColor(tint(definition.positiveTextColor));
158 setPositiveBackgroundColor(tint(definition.positiveBackgroundColor));
159
160 setDefaultFont(definition.defaultFont);
161 setSmallFont(definition.smallFont);
162}
163
164bool BasicTheme::event(QEvent *event)
165{
166 if (event->type() == PlatformThemeEvents::DataChangedEvent::type) {
167 sync();
168 }
169
170 if (event->type() == PlatformThemeEvents::ColorSetChangedEvent::type) {
171 sync();
172 }
173
174 if (event->type() == PlatformThemeEvents::ColorGroupChangedEvent::type) {
175 sync();
176 }
177
178 if (event->type() == PlatformThemeEvents::ColorChangedEvent::type) {
179 basicThemeInstance()->themeDefinition(qmlEngine(parent())).syncToQml(this);
180 }
181
182 if (event->type() == PlatformThemeEvents::FontChangedEvent::type) {
183 basicThemeInstance()->themeDefinition(qmlEngine(parent())).syncToQml(this);
184 }
185
186 return PlatformTheme::event(event);
187}
188
189QColor BasicTheme::tint(const QColor &color)
190{
191 if (QQuickItem *item = qobject_cast<QQuickItem *>(parent()); item && !item->isEnabled()) {
192 return QColor::fromHsvF(color.hueF(), color.saturationF() * 0.5, color.valueF() * 0.8);
193 }
194 switch (colorGroup()) {
195 case PlatformTheme::Inactive:
196 return QColor::fromHsvF(color.hueF(), color.saturationF() * 0.5, color.valueF());
197 case PlatformTheme::Disabled:
198 return QColor::fromHsvF(color.hueF(), color.saturationF() * 0.5, color.valueF() * 0.8);
199 default:
200 return color;
201 }
202}
203
204}
205}
206
207#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
bool isEnabled() 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 Sat Dec 21 2024 16:56:52 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.