Kirigami2

platformtheme.h
1/*
2 * SPDX-FileCopyrightText: 2017 Marco Martin <mart@kde.org>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7#ifndef KIRIGAMI_PLATFORMTHEME_H
8#define KIRIGAMI_PLATFORMTHEME_H
9
10#include <QColor>
11#include <QIcon>
12#include <QObject>
13#include <QPalette>
14#include <QQuickItem>
15#include <qqmlregistration.h>
16
17#include "kirigamiplatform_export.h"
18
19namespace Kirigami
20{
21namespace Platform
22{
23class PlatformThemeData;
24class PlatformThemePrivate;
25
26/**
27 * @class PlatformTheme platformtheme.h <Kirigami/PlatformTheme>
28 *
29 * This class is the base for color management in Kirigami,
30 * different platforms can reimplement this class to integrate with
31 * system platform colors of a given platform
32 */
33class KIRIGAMIPLATFORM_EXPORT PlatformTheme : public QObject
34{
36 QML_NAMED_ELEMENT(Theme)
38 QML_UNCREATABLE("Attached Property")
39
40 /**
41 * This enumeration describes the color set for which a color is being selected.
42 *
43 * Color sets define a color "environment", suitable for drawing all parts of a
44 * given region. Colors from different sets should not be combined.
45 */
46 Q_PROPERTY(ColorSet colorSet READ colorSet WRITE setColorSet NOTIFY colorSetChanged FINAL)
47
48 /**
49 * This enumeration describes the color group used to generate the colors.
50 * The enum value is based upon QPalette::ColorGroup and has the same values.
51 * It's redefined here in order to make it work with QML.
52 * @since 4.43
53 */
54 Q_PROPERTY(ColorGroup colorGroup READ colorGroup WRITE setColorGroup NOTIFY colorGroupChanged FINAL)
55
56 /**
57 * If true, the colorSet will be inherited from the colorset of a theme of one
58 * of the ancestor items
59 * default: true
60 */
61 Q_PROPERTY(bool inherit READ inherit WRITE setInherit NOTIFY inheritChanged FINAL)
62
63 // foreground colors
64 /**
65 * Color for normal foregrounds, usually text, but not limited to it,
66 * anything that should be painted with a clear contrast should use this color
67 */
68 Q_PROPERTY(QColor textColor READ textColor WRITE setCustomTextColor RESET setCustomTextColor NOTIFY colorsChanged FINAL)
69
70 /**
71 * Foreground color for disabled areas, usually a mid-gray
72 * @note Depending on the implementation, the color used for this property may not be
73 * based on the disabled palette. For example, for the Plasma implementation,
74 * "Inactive Text Color" of the active palette is used.
75 */
76 Q_PROPERTY(QColor disabledTextColor READ disabledTextColor WRITE setCustomDisabledTextColor RESET setCustomDisabledTextColor NOTIFY colorsChanged FINAL)
77
78 /**
79 * Color for text that has been highlighted, often is a light color while normal text is dark
80 */
82 QColor highlightedTextColor READ highlightedTextColor WRITE setCustomHighlightedTextColor RESET setCustomHighlightedTextColor NOTIFY colorsChanged)
83
84 /**
85 * Foreground for areas that are active or requesting attention
86 */
87 Q_PROPERTY(QColor activeTextColor READ activeTextColor WRITE setCustomActiveTextColor RESET setCustomActiveTextColor NOTIFY colorsChanged FINAL)
88
89 /**
90 * Color for links
91 */
92 Q_PROPERTY(QColor linkColor READ linkColor WRITE setCustomLinkColor RESET setCustomLinkColor NOTIFY colorsChanged FINAL)
93
94 /**
95 * Color for visited links, usually a bit darker than linkColor
96 */
97 Q_PROPERTY(QColor visitedLinkColor READ visitedLinkColor WRITE setCustomVisitedLinkColor RESET setCustomVisitedLinkColor NOTIFY colorsChanged FINAL)
98
99 /**
100 * Foreground color for negative areas, such as critical error text
101 */
102 Q_PROPERTY(QColor negativeTextColor READ negativeTextColor WRITE setCustomNegativeTextColor RESET setCustomNegativeTextColor NOTIFY colorsChanged FINAL)
103
104 /**
105 * Foreground color for neutral areas, such as warning texts (but not critical)
106 */
107 Q_PROPERTY(QColor neutralTextColor READ neutralTextColor WRITE setCustomNeutralTextColor RESET setCustomNeutralTextColor NOTIFY colorsChanged FINAL)
108
109 /**
110 * Success messages, trusted content
111 */
112 Q_PROPERTY(QColor positiveTextColor READ positiveTextColor WRITE setCustomPositiveTextColor RESET setCustomPositiveTextColor NOTIFY colorsChanged FINAL)
113
114 // background colors
115 /**
116 * The generic background color
117 */
118 Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setCustomBackgroundColor RESET setCustomBackgroundColor NOTIFY colorsChanged FINAL)
119
120 /**
121 * The generic background color
122 * Alternate background; for example, for use in lists.
123 * This color may be the same as BackgroundNormal,
124 * especially in sets other than View and Window.
125 */
126 Q_PROPERTY(QColor alternateBackgroundColor READ alternateBackgroundColor WRITE setCustomAlternateBackgroundColor RESET setCustomAlternateBackgroundColor
127 NOTIFY colorsChanged)
128
129 /**
130 * The background color for selected areas
131 */
132 Q_PROPERTY(QColor highlightColor READ highlightColor WRITE setCustomHighlightColor RESET setCustomHighlightColor NOTIFY colorsChanged FINAL)
133
134 /**
135 * Background for areas that are active or requesting attention
136 */
138 QColor activeBackgroundColor READ activeBackgroundColor WRITE setCustomActiveBackgroundColor RESET setCustomActiveBackgroundColor NOTIFY colorsChanged)
139
140 /**
141 * Background color for links
142 */
144 QColor linkBackgroundColor READ linkBackgroundColor WRITE setCustomLinkBackgroundColor RESET setCustomLinkBackgroundColor NOTIFY colorsChanged FINAL)
145
146 /**
147 * Background color for visited links, usually a bit darker than linkBackgroundColor
148 */
149 Q_PROPERTY(QColor visitedLinkBackgroundColor READ visitedLinkBackgroundColor WRITE setCustomVisitedLinkBackgroundColor RESET
150 setCustomVisitedLinkBackgroundColor NOTIFY colorsChanged)
151
152 /**
153 * Background color for negative areas, such as critical errors and destructive actions
154 */
155 Q_PROPERTY(QColor negativeBackgroundColor READ negativeBackgroundColor WRITE setCustomNegativeBackgroundColor RESET setCustomNegativeBackgroundColor NOTIFY
156 colorsChanged)
157
158 /**
159 * Background color for neutral areas, such as warnings (but not critical)
160 */
161 Q_PROPERTY(QColor neutralBackgroundColor READ neutralBackgroundColor WRITE setCustomNeutralBackgroundColor RESET setCustomNeutralBackgroundColor NOTIFY
162 colorsChanged)
163
164 /**
165 * Background color for positive areas, such as success messages and trusted content
166 */
167 Q_PROPERTY(QColor positiveBackgroundColor READ positiveBackgroundColor WRITE setCustomPositiveBackgroundColor RESET setCustomPositiveBackgroundColor NOTIFY
168 colorsChanged)
169
170 // decoration colors
171 /**
172 * A decoration color that indicates active focus
173 */
174 Q_PROPERTY(QColor focusColor READ focusColor WRITE setCustomFocusColor RESET setCustomFocusColor NOTIFY colorsChanged FINAL)
175
176 /**
177 * A decoration color that indicates mouse hovering
178 */
179 Q_PROPERTY(QColor hoverColor READ hoverColor WRITE setCustomHoverColor RESET setCustomHoverColor NOTIFY colorsChanged FINAL)
180
181 /**
182 * Hint for item views to actually make use of the alternate background color feature
183 */
185 bool useAlternateBackgroundColor READ useAlternateBackgroundColor WRITE setUseAlternateBackgroundColor NOTIFY useAlternateBackgroundColorChanged FINAL)
186
187 // font and palette
188 Q_PROPERTY(QFont defaultFont READ defaultFont NOTIFY defaultFontChanged FINAL)
189
190 // small font
191 Q_PROPERTY(QFont smallFont READ smallFont NOTIFY smallFontChanged FINAL)
192
193 // fixed width font
194 Q_PROPERTY(QFont fixedWidthFont READ fixedWidthFont NOTIFY fixedWidthFontChanged FINAL)
195
196 // Active palette
197 Q_PROPERTY(QPalette palette READ palette NOTIFY paletteChanged FINAL)
198
199 // Frame contrast value, usually used for separators and outlines
200 // Value is between 0.0 and 1.0
201 Q_PROPERTY(qreal frameContrast READ frameContrast CONSTANT FINAL)
202
203 // Returns half of the frameContrast value; used by Separator.Weight.Light
204 // Value is between 0.0 and 1.0
205 Q_PROPERTY(qreal lightFrameContrast READ lightFrameContrast CONSTANT FINAL)
206
207public:
208 enum ColorSet {
209 /** Color set for item views, usually the lightest of all */
210 View = 0,
211 /** Default Color set for windows and "chrome" areas */
213 /** Color set used by buttons */
215 /** Color set used by selected areas */
217 /** Color set used by tooltips */
219 /** Color set meant to be complementary to Window: usually is a dark theme for light themes */
221 /** Color set to be used by heading areas of applications, such as toolbars */
223 // Number of items in this enum, this should always be the last item.
224 ColorSetCount,
225 };
226 Q_ENUM(ColorSet)
227
228 enum ColorGroup {
229 Disabled = QPalette::Disabled,
230 Active = QPalette::Active,
231 Inactive = QPalette::Inactive,
232 Normal = QPalette::Normal,
233
234 ColorGroupCount, // Number of items in this enum, this should always be the last item.
235 };
236 Q_ENUM(ColorGroup)
237
238 explicit PlatformTheme(QObject *parent = nullptr);
239 ~PlatformTheme() override;
240
241 void setColorSet(PlatformTheme::ColorSet);
242 PlatformTheme::ColorSet colorSet() const;
243
244 void setColorGroup(PlatformTheme::ColorGroup);
245 PlatformTheme::ColorGroup colorGroup() const;
246
247 bool inherit() const;
248 void setInherit(bool inherit);
249
250 // foreground colors
251 QColor textColor() const;
252 QColor disabledTextColor() const;
253 QColor highlightedTextColor() const;
254 QColor activeTextColor() const;
255 QColor linkColor() const;
256 QColor visitedLinkColor() const;
257 QColor negativeTextColor() const;
258 QColor neutralTextColor() const;
259 QColor positiveTextColor() const;
260
261 // background colors
262 QColor backgroundColor() const;
263 QColor alternateBackgroundColor() const;
264 QColor highlightColor() const;
265 QColor activeBackgroundColor() const;
266 QColor linkBackgroundColor() const;
267 QColor visitedLinkBackgroundColor() const;
268 QColor negativeBackgroundColor() const;
269 QColor neutralBackgroundColor() const;
270 QColor positiveBackgroundColor() const;
271
272 // decoration colors
273 QColor focusColor() const;
274 QColor hoverColor() const;
275
276 QFont defaultFont() const;
277 QFont smallFont() const;
278 QFont fixedWidthFont() const;
279
280 // this may is used by the desktop QQC2 to set the styleoption palettes
281 QPalette palette() const;
282
283 qreal frameContrast() const;
284 qreal lightFrameContrast() const;
285
286 // this will be used by desktopicon to fetch icons with KIconLoader
287 virtual Q_INVOKABLE QIcon iconFromTheme(const QString &name, const QColor &customColor = Qt::transparent);
288
289 bool supportsIconColoring() const;
290
291 // foreground colors
292 void setCustomTextColor(const QColor &color = QColor());
293 void setCustomDisabledTextColor(const QColor &color = QColor());
294 void setCustomHighlightedTextColor(const QColor &color = QColor());
295 void setCustomActiveTextColor(const QColor &color = QColor());
296 void setCustomLinkColor(const QColor &color = QColor());
297 void setCustomVisitedLinkColor(const QColor &color = QColor());
298 void setCustomNegativeTextColor(const QColor &color = QColor());
299 void setCustomNeutralTextColor(const QColor &color = QColor());
300 void setCustomPositiveTextColor(const QColor &color = QColor());
301 // background colors
302 void setCustomBackgroundColor(const QColor &color = QColor());
303 void setCustomAlternateBackgroundColor(const QColor &color = QColor());
304 void setCustomHighlightColor(const QColor &color = QColor());
305 void setCustomActiveBackgroundColor(const QColor &color = QColor());
306 void setCustomLinkBackgroundColor(const QColor &color = QColor());
307 void setCustomVisitedLinkBackgroundColor(const QColor &color = QColor());
308 void setCustomNegativeBackgroundColor(const QColor &color = QColor());
309 void setCustomNeutralBackgroundColor(const QColor &color = QColor());
310 void setCustomPositiveBackgroundColor(const QColor &color = QColor());
311 // decoration colors
312 void setCustomFocusColor(const QColor &color = QColor());
313 void setCustomHoverColor(const QColor &color = QColor());
314
315 bool useAlternateBackgroundColor() const;
316 void setUseAlternateBackgroundColor(bool alternate);
317
318 // QML attached property
319 static PlatformTheme *qmlAttachedProperties(QObject *object);
320
321Q_SIGNALS:
322 void colorsChanged();
323 void defaultFontChanged(const QFont &font);
324 void smallFontChanged(const QFont &font);
325 void fixedWidthFontChanged(const QFont &font);
326 void colorSetChanged(Kirigami::Platform::PlatformTheme::ColorSet colorSet);
327 void colorGroupChanged(Kirigami::Platform::PlatformTheme::ColorGroup colorGroup);
328 void paletteChanged(const QPalette &pal);
329 void inheritChanged(bool inherit);
330 void useAlternateBackgroundColorChanged(bool alternate);
331
332protected:
333 // Setters, not accessible from QML but from implementations
334 void setSupportsIconColoring(bool support);
335
336 // foreground colors
337 void setTextColor(const QColor &color);
338 void setDisabledTextColor(const QColor &color);
339 void setHighlightedTextColor(const QColor &color);
340 void setActiveTextColor(const QColor &color);
341 void setLinkColor(const QColor &color);
342 void setVisitedLinkColor(const QColor &color);
343 void setNegativeTextColor(const QColor &color);
344 void setNeutralTextColor(const QColor &color);
345 void setPositiveTextColor(const QColor &color);
346
347 // background colors
348 void setBackgroundColor(const QColor &color);
349 void setAlternateBackgroundColor(const QColor &color);
350 void setHighlightColor(const QColor &color);
351 void setActiveBackgroundColor(const QColor &color);
352 void setLinkBackgroundColor(const QColor &color);
353 void setVisitedLinkBackgroundColor(const QColor &color);
354 void setNegativeBackgroundColor(const QColor &color);
355 void setNeutralBackgroundColor(const QColor &color);
356 void setPositiveBackgroundColor(const QColor &color);
357
358 // decoration colors
359 void setFocusColor(const QColor &color);
360 void setHoverColor(const QColor &color);
361
362 void setDefaultFont(const QFont &defaultFont);
363 void setSmallFont(const QFont &smallFont);
364 void setFixedWidthFont(const QFont &fixedWidthFont);
365
366 bool event(QEvent *event) override;
367
368private:
369 KIRIGAMIPLATFORM_NO_EXPORT void update();
370 KIRIGAMIPLATFORM_NO_EXPORT void updateChildren(QObject *item);
371 KIRIGAMIPLATFORM_NO_EXPORT QObject *determineParent(QObject *object);
372 KIRIGAMIPLATFORM_NO_EXPORT void emitSignalsForChanges(int changes);
373
374 PlatformThemePrivate *d;
375 friend class PlatformThemePrivate;
376 friend class PlatformThemeData;
377 friend class PlatformThemeChangeTracker;
378};
379
380/**
381 * A class that tracks changes to PlatformTheme properties and emits signals at the right moment.
382 *
383 * This should be used by PlatformTheme implementations to ensure that multiple
384 * changes to a PlatformTheme's properties do not emit multiple change signals,
385 * instead batching all of them into a single signal emission. This then ensures
386 * things making use of PlatformTheme aren't needlessly redrawn or redrawn in a
387 * partially changed state.
388 *
389 * @since 6.7
390 *
391 */
392class KIRIGAMIPLATFORM_EXPORT PlatformThemeChangeTracker
393{
394public:
395 /**
396 * Flags used to indicate changes made to certain properties.
397 */
398 enum class PropertyChange : uint8_t {
399 None = 0,
400 ColorSet = 1 << 0,
401 ColorGroup = 1 << 1,
402 Color = 1 << 2,
403 Palette = 1 << 3,
404 Font = 1 << 4,
405 Data = 1 << 5,
406 All = ColorSet | ColorGroup | Color | Palette | Font | Data,
407 };
408 Q_DECLARE_FLAGS(PropertyChanges, PropertyChange)
409
410 PlatformThemeChangeTracker(PlatformTheme *theme, PropertyChanges changes = PropertyChange::None);
412
413 void markDirty(PropertyChanges changes);
414
415private:
416 PlatformTheme *m_theme;
417
418 // Per-PlatformTheme data that we need for PlatformThemeChangeBlocker.
419 // We don't want to store this in PlatformTheme since that would increase the
420 // size of every instance of PlatformTheme while it's only used when we want to
421 // block property change signal emissions. So instead we store it in a separate
422 // hash using the PlatformTheme as key.
423 struct Data {
424 PropertyChanges changes;
425 };
426
427 std::shared_ptr<Data> m_data;
428
429 inline static QHash<PlatformTheme *, std::weak_ptr<Data>> s_blockedChanges;
430};
431
432namespace PlatformThemeEvents
433{
434// To avoid the overhead of Qt's signal/slot connections, we use custom events
435// to communicate with subclasses. This way, we can indicate what actually
436// changed without needing to add new virtual functions to PlatformTheme which
437// would break binary compatibility.
438//
439// To handle these events in your subclass, override QObject::event() and check
440// if you receive one of these events, then do what is needed. Finally, make
441// sure to call PlatformTheme::event() since that will also do some processing
442// of these events.
443
444template<typename T>
445class KIRIGAMIPLATFORM_EXPORT PropertyChangedEvent : public QEvent
446{
447public:
448 PropertyChangedEvent(PlatformTheme *theme, const T &previous, const T &current)
449 : QEvent(PropertyChangedEvent<T>::type)
450 , sender(theme)
451 , oldValue(previous)
452 , newValue(current)
453 {
454 }
455
456 PlatformTheme *sender;
457 T oldValue;
458 T newValue;
459
460 static QEvent::Type type;
461};
462
463using DataChangedEvent = PropertyChangedEvent<std::shared_ptr<PlatformThemeData>>;
464using ColorSetChangedEvent = PropertyChangedEvent<PlatformTheme::ColorSet>;
465using ColorGroupChangedEvent = PropertyChangedEvent<PlatformTheme::ColorGroup>;
466using ColorChangedEvent = PropertyChangedEvent<QColor>;
467using FontChangedEvent = PropertyChangedEvent<QFont>;
468
469}
470
471}
472} // namespace Kirigami
473
474Q_DECLARE_OPERATORS_FOR_FLAGS(Kirigami::Platform::PlatformThemeChangeTracker::PropertyChanges)
475
476#endif // PLATFORMTHEME_H
A class that tracks changes to PlatformTheme properties and emits signals at the right moment.
PropertyChange
Flags used to indicate changes made to certain properties.
This class is the base for color management in Kirigami, different platforms can reimplement this cla...
QColor neutralTextColor
Foreground color for neutral areas, such as warning texts (but not critical)
QColor textColor
Color for normal foregrounds, usually text, but not limited to it, anything that should be painted wi...
QColor activeTextColor
Foreground for areas that are active or requesting attention.
bool inherit
If true, the colorSet will be inherited from the colorset of a theme of one of the ancestor items def...
QColor activeBackgroundColor
Background for areas that are active or requesting attention.
QColor highlightColor
The background color for selected areas.
QColor linkBackgroundColor
Background color for links.
QColor visitedLinkBackgroundColor
Background color for visited links, usually a bit darker than linkBackgroundColor.
ColorGroup colorGroup
This enumeration describes the color group used to generate the colors.
QColor negativeBackgroundColor
Background color for negative areas, such as critical errors and destructive actions.
QColor positiveBackgroundColor
Background color for positive areas, such as success messages and trusted content.
QColor negativeTextColor
Foreground color for negative areas, such as critical error text.
QColor linkColor
Color for links.
@ Tooltip
Color set used by tooltips.
@ Button
Color set used by buttons.
@ Header
Color set to be used by heading areas of applications, such as toolbars.
@ View
Color set for item views, usually the lightest of all.
@ Window
Default Color set for windows and "chrome" areas.
@ Selection
Color set used by selected areas.
@ Complementary
Color set meant to be complementary to Window: usually is a dark theme for light themes.
QColor neutralBackgroundColor
Background color for neutral areas, such as warnings (but not critical)
ColorSet colorSet
This enumeration describes the color set for which a color is being selected.
QColor visitedLinkColor
Color for visited links, usually a bit darker than linkColor.
QColor alternateBackgroundColor
The generic background color Alternate background; for example, for use in lists.
QColor disabledTextColor
Foreground color for disabled areas, usually a mid-gray.
QColor focusColor
A decoration color that indicates active focus.
QColor backgroundColor
The generic background color.
QColor highlightedTextColor
Color for text that has been highlighted, often is a light color while normal text is dark.
QColor positiveTextColor
Success messages, trusted content.
QColor hoverColor
A decoration color that indicates mouse hovering.
bool useAlternateBackgroundColor
Hint for item views to actually make use of the alternate background color feature.
Type type(const QSqlDatabase &db)
QObject(QObject *parent)
Q_OBJECTQ_OBJECT
Q_PROPERTY(...)
transparent
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Apr 18 2025 12:03:26 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.