Kirigami2

platformtheme.h
1 /*
2  * SPDX-FileCopyrightText: 2017 by Marco Martin <[email protected]>
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 
16 #include "kirigami2_export.h"
17 
18 namespace Kirigami
19 {
20 class PlatformThemeData;
21 class PlatformThemePrivate;
22 
23 /**
24  * @class PlatformTheme platformtheme.h <Kirigami/PlatformTheme>
25  *
26  * This class is the base for color management in Kirigami,
27  * different platforms can reimplement this class to integrate with
28  * system platform colors of a given platform
29  *
30  * @see <a href="https://develop.kde.org/docs/getting-started/kirigami/style-colors">Colors and Themes in Kirigami</a>
31  * @see <a href="https://develop.kde.org/hig/style/color">Human Interface Guidelines on Colors</a>
32  */
33 class KIRIGAMI2_EXPORT PlatformTheme : public QObject
34 {
35  Q_OBJECT
36 
37  /**
38  * This enumeration describes the color set for which a color is being selected.
39  *
40  * Color sets define a color "environment", suitable for drawing all parts of a
41  * given region. Colors from different sets should not be combined.
42  */
43  Q_PROPERTY(ColorSet colorSet READ colorSet WRITE setColorSet NOTIFY colorSetChanged)
44 
45  /**
46  * This enumeration describes the color group used to generate the colors.
47  *
48  * The enum value is based upon QPalette::ColorGroup and has the same values.
49  *
50  * It's redefined here in order to make it work with QML
51  * @since KDE Frameworks 4.43
52  */
53  Q_PROPERTY(ColorGroup colorGroup READ colorGroup WRITE setColorGroup NOTIFY colorGroupChanged)
54 
55  /**
56  * If true, the ::colorSet will be inherited from the colorset of a theme of one
57  * of the ancestor items
58  *
59  * default: true
60  */
61  Q_PROPERTY(bool inherit READ inherit WRITE setInherit NOTIFY inheritChanged)
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)
69 
70  /**
71  * Foreground color for disabled areas, usually a mid-gray
72  */
73  Q_PROPERTY(QColor disabledTextColor READ disabledTextColor WRITE setCustomDisabledTextColor RESET setCustomDisabledTextColor NOTIFY colorsChanged)
74 
75  /**
76  * Color for text that has been highlighted, often is a light color while normal text is dark
77  */
78  Q_PROPERTY(
79  QColor highlightedTextColor READ highlightedTextColor WRITE setCustomHighlightedTextColor RESET setCustomHighlightedTextColor NOTIFY colorsChanged)
80 
81  /**
82  * Foreground for areas that are active or requesting attention
83  */
84  Q_PROPERTY(QColor activeTextColor READ activeTextColor WRITE setCustomActiveTextColor RESET setCustomActiveTextColor NOTIFY colorsChanged)
85 
86  /**
87  * Color for links
88  */
89  Q_PROPERTY(QColor linkColor READ linkColor WRITE setCustomLinkColor RESET setCustomLinkColor NOTIFY colorsChanged)
90 
91  /**
92  * Color for visited links, usually a bit darker than linkColor
93  */
94  Q_PROPERTY(QColor visitedLinkColor READ visitedLinkColor WRITE setCustomVisitedLinkColor RESET setCustomVisitedLinkColor NOTIFY colorsChanged)
95 
96  /**
97  * Foreground color for negative areas, such as critical error text
98  */
99  Q_PROPERTY(QColor negativeTextColor READ negativeTextColor WRITE setCustomNegativeTextColor RESET setCustomNegativeTextColor NOTIFY colorsChanged)
100 
101  /**
102  * Foreground color for neutral areas, such as warning texts (but not critical)
103  */
104  Q_PROPERTY(QColor neutralTextColor READ neutralTextColor WRITE setCustomNeutralTextColor RESET setCustomNeutralTextColor NOTIFY colorsChanged)
105 
106  /**
107  * Success messages, trusted content
108  */
109  Q_PROPERTY(QColor positiveTextColor READ positiveTextColor WRITE setCustomPositiveTextColor RESET setCustomPositiveTextColor NOTIFY colorsChanged)
110 
111  // background colors
112  /**
113  * The generic background color
114  */
115  Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setCustomBackgroundColor RESET setCustomBackgroundColor NOTIFY colorsChanged)
116 
117  /**
118  * The generic background color
119  * Alternate background; for example, for use in lists.
120  * This color may be the same as BackgroundNormal,
121  * especially in sets other than View and Window.
122  */
123  Q_PROPERTY(QColor alternateBackgroundColor READ alternateBackgroundColor WRITE setCustomAlternateBackgroundColor RESET setCustomAlternateBackgroundColor
124  NOTIFY colorsChanged)
125 
126  /**
127  * The background color for selected areas
128  */
129  Q_PROPERTY(QColor highlightColor READ highlightColor WRITE setCustomHighlightColor RESET setCustomHighlightColor NOTIFY colorsChanged)
130 
131  /**
132  * Background for areas that are active or requesting attention
133  */
134  Q_PROPERTY(
135  QColor activeBackgroundColor READ activeBackgroundColor WRITE setCustomActiveBackgroundColor RESET setCustomActiveBackgroundColor NOTIFY colorsChanged)
136 
137  /**
138  * Background color for links
139  */
140  Q_PROPERTY(QColor linkBackgroundColor READ linkBackgroundColor WRITE setCustomLinkBackgroundColor RESET setCustomLinkBackgroundColor NOTIFY colorsChanged)
141 
142  /**
143  * Background color for visited links, usually a bit darker than linkBackgroundColor
144  */
145  Q_PROPERTY(QColor visitedLinkBackgroundColor READ visitedLinkBackgroundColor WRITE setCustomVisitedLinkBackgroundColor RESET
146  setCustomVisitedLinkBackgroundColor NOTIFY colorsChanged)
147 
148  /**
149  * Background color for negative areas, such as critical errors and destructive actions
150  */
151  Q_PROPERTY(QColor negativeBackgroundColor READ negativeBackgroundColor WRITE setCustomNegativeBackgroundColor RESET setCustomNegativeBackgroundColor NOTIFY
152  colorsChanged)
153 
154  /**
155  * Background color for neutral areas, such as warnings (but not critical)
156  */
157  Q_PROPERTY(QColor neutralBackgroundColor READ neutralBackgroundColor WRITE setCustomNeutralBackgroundColor RESET setCustomNeutralBackgroundColor NOTIFY
158  colorsChanged)
159 
160  /**
161  * Background color for positive areas, such as success messages and trusted content
162  */
163  Q_PROPERTY(QColor positiveBackgroundColor READ positiveBackgroundColor WRITE setCustomPositiveBackgroundColor RESET setCustomPositiveBackgroundColor NOTIFY
164  colorsChanged)
165 
166  // decoration colors
167  /**
168  * A decoration color that indicates active focus
169  */
170  Q_PROPERTY(QColor focusColor READ focusColor WRITE setCustomFocusColor RESET setCustomFocusColor NOTIFY colorsChanged)
171 
172  /**
173  * A decoration color that indicates mouse hovering
174  */
175  Q_PROPERTY(QColor hoverColor READ hoverColor WRITE setCustomHoverColor RESET setCustomHoverColor NOTIFY colorsChanged)
176 
177  // font and palette
178  Q_PROPERTY(QFont defaultFont READ defaultFont NOTIFY defaultFontChanged)
179 
180  // small font
181  Q_PROPERTY(QFont smallFont READ smallFont NOTIFY defaultFontChanged)
182 
183  // Active palette
184  Q_PROPERTY(QPalette palette READ palette NOTIFY paletteChanged)
185 
186 public:
187  enum ColorSet {
188  View = 0, /** Color set for item views, usually the lightest of all */
189  Window, /** Default Color set for windows and "chrome" areas */
190  Button, /** Color set used by buttons */
191  Selection, /** Color set used by selectged areas */
192  Tooltip, /** Color set used by tooltips */
193  Complementary, /** Color set meant to be complementary to Window: usually is a dark theme for light themes */
194  Header, /** Color set to be used by heading areas of applications, such as toolbars */
195 
196  ColorSetCount, // Number of items in this enum, this should always be the last item.
197  };
198  Q_ENUM(ColorSet)
199 
200  enum ColorGroup {
201  Disabled = QPalette::Disabled,
202  Active = QPalette::Active,
203  Inactive = QPalette::Inactive,
204  Normal = QPalette::Normal,
205 
206  ColorGroupCount, // Number of items in this enum, this should always be the last item.
207  };
208  Q_ENUM(ColorGroup)
209 
210  explicit PlatformTheme(QObject *parent = nullptr);
211  ~PlatformTheme() override;
212 
213  void setColorSet(PlatformTheme::ColorSet);
214  PlatformTheme::ColorSet colorSet() const;
215 
216  void setColorGroup(PlatformTheme::ColorGroup);
217  PlatformTheme::ColorGroup colorGroup() const;
218 
219  bool inherit() const;
220  void setInherit(bool inherit);
221 
222  // foreground colors
223  QColor textColor() const;
224  QColor disabledTextColor() const;
225  QColor highlightedTextColor() const;
226  QColor activeTextColor() const;
227  QColor linkColor() const;
228  QColor visitedLinkColor() const;
229  QColor negativeTextColor() const;
230  QColor neutralTextColor() const;
231  QColor positiveTextColor() const;
232 
233  // background colors
234  QColor backgroundColor() const;
235  QColor alternateBackgroundColor() const;
236  QColor highlightColor() const;
237  QColor activeBackgroundColor() const;
238  QColor linkBackgroundColor() const;
239  QColor visitedLinkBackgroundColor() const;
240  QColor negativeBackgroundColor() const;
241  QColor neutralBackgroundColor() const;
242  QColor positiveBackgroundColor() const;
243 
244  // decoration colors
245  QColor focusColor() const;
246  QColor hoverColor() const;
247 
248  QFont defaultFont() const;
249  QFont smallFont() const;
250 
251  // this may is used by the desktop QQC2 to set the styleoption palettes
252  QPalette palette() const;
253 
254  // this will be used by desktopicon to fetch icons with KIconLoader
255  virtual Q_INVOKABLE QIcon iconFromTheme(const QString &name, const QColor &customColor = Qt::transparent);
256 
257  bool supportsIconColoring() const;
258 
259  // foreground colors
260  void setCustomTextColor(const QColor &color = QColor());
261  void setCustomDisabledTextColor(const QColor &color = QColor());
262  void setCustomHighlightedTextColor(const QColor &color = QColor());
263  void setCustomActiveTextColor(const QColor &color = QColor());
264  void setCustomLinkColor(const QColor &color = QColor());
265  void setCustomVisitedLinkColor(const QColor &color = QColor());
266  void setCustomNegativeTextColor(const QColor &color = QColor());
267  void setCustomNeutralTextColor(const QColor &color = QColor());
268  void setCustomPositiveTextColor(const QColor &color = QColor());
269  // background colors
270  void setCustomBackgroundColor(const QColor &color = QColor());
271  void setCustomAlternateBackgroundColor(const QColor &color = QColor());
272  void setCustomHighlightColor(const QColor &color = QColor());
273  void setCustomActiveBackgroundColor(const QColor &color = QColor());
274  void setCustomLinkBackgroundColor(const QColor &color = QColor());
275  void setCustomVisitedLinkBackgroundColor(const QColor &color = QColor());
276  void setCustomNegativeBackgroundColor(const QColor &color = QColor());
277  void setCustomNeutralBackgroundColor(const QColor &color = QColor());
278  void setCustomPositiveBackgroundColor(const QColor &color = QColor());
279  // decoration colors
280  void setCustomFocusColor(const QColor &color = QColor());
281  void setCustomHoverColor(const QColor &color = QColor());
282 
283  // QML attached property
284  static PlatformTheme *qmlAttachedProperties(QObject *object);
285 
286 Q_SIGNALS:
287  // TODO: parameters to signals as this is also a c++ api
288  void colorsChanged();
289  void defaultFontChanged(const QFont &font);
290  void smallFontChanged(const QFont &font);
291  void colorSetChanged(Kirigami::PlatformTheme::ColorSet colorSet);
292  void colorGroupChanged(Kirigami::PlatformTheme::ColorGroup colorGroup);
293  void paletteChanged(const QPalette &pal);
294  void inheritChanged(bool inherit);
295 
296 protected:
297  // Setters, not accessible from QML but from implementations
298  void setSupportsIconColoring(bool support);
299 
300  // foreground colors
301  void setTextColor(const QColor &color);
302  void setDisabledTextColor(const QColor &color);
303  void setHighlightedTextColor(const QColor &color);
304  void setActiveTextColor(const QColor &color);
305  void setLinkColor(const QColor &color);
306  void setVisitedLinkColor(const QColor &color);
307  void setNegativeTextColor(const QColor &color);
308  void setNeutralTextColor(const QColor &color);
309  void setPositiveTextColor(const QColor &color);
310 
311  // background colors
312  void setBackgroundColor(const QColor &color);
313  void setAlternateBackgroundColor(const QColor &color);
314  void setHighlightColor(const QColor &color);
315  void setActiveBackgroundColor(const QColor &color);
316  void setLinkBackgroundColor(const QColor &color);
317  void setVisitedLinkBackgroundColor(const QColor &color);
318  void setNegativeBackgroundColor(const QColor &color);
319  void setNeutralBackgroundColor(const QColor &color);
320  void setPositiveBackgroundColor(const QColor &color);
321 
322  // decoration colors
323  void setFocusColor(const QColor &color);
324  void setHoverColor(const QColor &color);
325 
326  void setDefaultFont(const QFont &defaultFont);
327  void setSmallFont(const QFont &smallFont);
328 
329 #if KIRIGAMI2_ENABLE_DEPRECATED_SINCE(5, 80)
330  KIRIGAMI2_DEPRECATED_VERSION(5, 80, "setPalette is unused, the palette is autogenerated from PlatformTheme colors now")
331  void setPalette(const QPalette &palette);
332 #endif
333 
334  bool event(QEvent *event) override;
335 
336 private:
337  KIRIGAMI2_NO_EXPORT void update();
338  KIRIGAMI2_NO_EXPORT void updateChildren(QObject *item);
339  KIRIGAMI2_NO_EXPORT void emitSignals();
340  KIRIGAMI2_NO_EXPORT void emitColorChanged();
341  KIRIGAMI2_NO_EXPORT QObject *determineParent(QObject *object);
342 
343  PlatformThemePrivate *d;
344  friend class PlatformThemePrivate;
345  friend class PlatformThemeData;
346 };
347 
348 namespace PlatformThemeEvents
349 {
350 // To avoid the overhead of Qt's signal/slot connections, we use custom events
351 // to communicate with subclasses. This way, we can indicate what actually
352 // changed without needing to add new virtual functions to PlatformTheme which
353 // would break binary compatibility.
354 //
355 // To handle these events in your subclass, override QObject::event() and check
356 // if you receive one of these events, then do what is needed. Finally, make
357 // sure to call PlatformTheme::event() since that will also do some processing
358 // of these events.
359 
360 template<typename T>
361 class KIRIGAMI2_EXPORT PropertyChangedEvent : public QEvent
362 {
363 public:
364  PropertyChangedEvent(PlatformTheme *theme, const T &previous, const T &current)
365  : QEvent(PropertyChangedEvent<T>::type)
366  , sender(theme)
367  , oldValue(previous)
368  , newValue(current)
369  {
370  }
371 
372  PlatformTheme *sender;
373  T oldValue;
374  T newValue;
375 
376  static QEvent::Type type;
377 };
378 
379 using DataChangedEvent = PropertyChangedEvent<std::shared_ptr<PlatformThemeData>>;
380 using ColorSetChangedEvent = PropertyChangedEvent<PlatformTheme::ColorSet>;
381 using ColorGroupChangedEvent = PropertyChangedEvent<PlatformTheme::ColorGroup>;
382 using ColorChangedEvent = PropertyChangedEvent<QColor>;
383 using FontChangedEvent = PropertyChangedEvent<QFont>;
384 
385 }
386 
387 } // namespace Kirigami
388 
389 QML_DECLARE_TYPEINFO(Kirigami::PlatformTheme, QML_HAS_ATTACHED_PROPERTIES)
390 
391 #endif // PLATFORMTHEME_H
@ Complementary
Color set used by tooltips.
@ ColorSetCount
Color set to be used by heading areas of applications, such as toolbars.
Type type(const QSqlDatabase &db)
@ Window
Color set for item views, usually the lightest of all.
@ Button
Default Color set for windows and "chrome" areas.
@ Selection
Color set used by buttons.
@ Header
Color set meant to be complementary to Window: usually is a dark theme for light themes.
transparent
@ Tooltip
Color set used by selectged areas.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Oct 3 2023 03:58:36 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.