1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QPALETTE_H
5#define QPALETTE_H
6
7#include <QtGui/qtguiglobal.h>
8#include <QtGui/qwindowdefs.h>
9#include <QtGui/qcolor.h>
10#include <QtGui/qbrush.h>
11
12QT_BEGIN_NAMESPACE
13
14
15class QPalettePrivate;
16class QVariant;
17
18class Q_GUI_EXPORT QPalette
19{
20 Q_GADGET
21public:
22 QPalette();
23 QPalette(const QColor &button);
24 QPalette(Qt::GlobalColor button);
25 QPalette(const QColor &button, const QColor &window);
26 QPalette(const QBrush &windowText, const QBrush &button, const QBrush &light,
27 const QBrush &dark, const QBrush &mid, const QBrush &text,
28 const QBrush &bright_text, const QBrush &base, const QBrush &window);
29 QPalette(const QColor &windowText, const QColor &window, const QColor &light,
30 const QColor &dark, const QColor &mid, const QColor &text, const QColor &base);
31 QPalette(const QPalette &palette);
32 ~QPalette();
33 QPalette &operator=(const QPalette &palette);
34 QPalette(QPalette &&other) noexcept
35 : d(std::exchange(obj&: other.d, new_val: nullptr)), currentGroup(other.currentGroup)
36 {}
37 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QPalette)
38
39 void swap(QPalette &other) noexcept
40 {
41 std::swap(a&: currentGroup, b&: other.currentGroup);
42 qt_ptr_swap(lhs&: d, rhs&: other.d);
43 }
44
45 operator QVariant() const;
46
47 // Do not change the order, the serialization format depends on it
48 // Ensure these values are kept in sync with QQuickColorGroup's properties
49 // and QCss::KnownValue!
50 enum ColorGroup { Active, Disabled, Inactive, NColorGroups, Current, All, Normal = Active };
51 Q_ENUM(ColorGroup)
52 enum ColorRole { WindowText, Button, Light, Midlight, Dark, Mid,
53 Text, BrightText, ButtonText, Base, Window, Shadow,
54 Highlight, HighlightedText,
55 Link, LinkVisited,
56 AlternateBase,
57 NoRole,
58 ToolTipBase, ToolTipText,
59 PlaceholderText,
60 Accent,
61 NColorRoles = Accent + 1,
62 };
63 Q_ENUM(ColorRole)
64
65 inline ColorGroup currentColorGroup() const { return currentGroup; }
66 inline void setCurrentColorGroup(ColorGroup cg) { currentGroup = cg; }
67
68 inline const QColor &color(ColorGroup cg, ColorRole cr) const
69 { return brush(cg, cr).color(); }
70 const QBrush &brush(ColorGroup cg, ColorRole cr) const;
71 inline void setColor(ColorGroup cg, ColorRole cr, const QColor &color);
72 inline void setColor(ColorRole cr, const QColor &color);
73 inline void setBrush(ColorRole cr, const QBrush &brush);
74 bool isBrushSet(ColorGroup cg, ColorRole cr) const;
75 void setBrush(ColorGroup cg, ColorRole cr, const QBrush &brush);
76 void setColorGroup(ColorGroup cr, const QBrush &windowText, const QBrush &button,
77 const QBrush &light, const QBrush &dark, const QBrush &mid,
78 const QBrush &text, const QBrush &bright_text, const QBrush &base,
79 const QBrush &window);
80 bool isEqual(ColorGroup cr1, ColorGroup cr2) const;
81
82 inline const QColor &color(ColorRole cr) const { return color(cg: Current, cr); }
83 inline const QBrush &brush(ColorRole cr) const { return brush(cg: Current, cr); }
84 inline const QBrush &windowText() const { return brush(cr: WindowText); }
85 inline const QBrush &button() const { return brush(cr: Button); }
86 inline const QBrush &light() const { return brush(cr: Light); }
87 inline const QBrush &dark() const { return brush(cr: Dark); }
88 inline const QBrush &mid() const { return brush(cr: Mid); }
89 inline const QBrush &text() const { return brush(cr: Text); }
90 inline const QBrush &base() const { return brush(cr: Base); }
91 inline const QBrush &alternateBase() const { return brush(cr: AlternateBase); }
92 inline const QBrush &toolTipBase() const { return brush(cr: ToolTipBase); }
93 inline const QBrush &toolTipText() const { return brush(cr: ToolTipText); }
94 inline const QBrush &window() const { return brush(cr: Window); }
95 inline const QBrush &midlight() const { return brush(cr: Midlight); }
96 inline const QBrush &brightText() const { return brush(cr: BrightText); }
97 inline const QBrush &buttonText() const { return brush(cr: ButtonText); }
98 inline const QBrush &shadow() const { return brush(cr: Shadow); }
99 inline const QBrush &highlight() const { return brush(cr: Highlight); }
100 inline const QBrush &highlightedText() const { return brush(cr: HighlightedText); }
101 inline const QBrush &link() const { return brush(cr: Link); }
102 inline const QBrush &linkVisited() const { return brush(cr: LinkVisited); }
103 inline const QBrush &placeholderText() const { return brush(cr: PlaceholderText); }
104 inline const QBrush &accent() const { return brush(cr: Accent); }
105
106 bool operator==(const QPalette &p) const;
107 inline bool operator!=(const QPalette &p) const { return !(operator==(p)); }
108 bool isCopyOf(const QPalette &p) const;
109
110 qint64 cacheKey() const;
111
112 QPalette resolve(const QPalette &other) const;
113
114 using ResolveMask = quint64;
115 ResolveMask resolveMask() const;
116 void setResolveMask(ResolveMask mask);
117
118private:
119 void setColorGroup(ColorGroup cr, const QBrush &windowText, const QBrush &button,
120 const QBrush &light, const QBrush &dark, const QBrush &mid,
121 const QBrush &text, const QBrush &bright_text,
122 const QBrush &base, const QBrush &alternate_base,
123 const QBrush &window, const QBrush &midlight,
124 const QBrush &button_text, const QBrush &shadow,
125 const QBrush &highlight, const QBrush &highlighted_text,
126 const QBrush &link, const QBrush &link_visited);
127 void setColorGroup(ColorGroup cr, const QBrush &windowText, const QBrush &button,
128 const QBrush &light, const QBrush &dark, const QBrush &mid,
129 const QBrush &text, const QBrush &bright_text,
130 const QBrush &base, const QBrush &alternate_base,
131 const QBrush &window, const QBrush &midlight,
132 const QBrush &button_text, const QBrush &shadow,
133 const QBrush &highlight, const QBrush &highlighted_text,
134 const QBrush &link, const QBrush &link_visited,
135 const QBrush &toolTipBase, const QBrush &toolTipText);
136 void init();
137 void detach();
138
139 QPalettePrivate *d;
140 ColorGroup currentGroup{Active};
141
142 friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &s, const QPalette &p);
143};
144
145Q_DECLARE_SHARED(QPalette)
146
147inline void QPalette::setColor(ColorGroup acg, ColorRole acr,
148 const QColor &acolor)
149{ setBrush(cg: acg, cr: acr, brush: QBrush(acolor)); }
150inline void QPalette::setColor(ColorRole acr, const QColor &acolor)
151{ setColor(acg: All, acr, acolor); }
152inline void QPalette::setBrush(ColorRole acr, const QBrush &abrush)
153{ setBrush(cg: All, cr: acr, brush: abrush); }
154
155/*****************************************************************************
156 QPalette stream functions
157 *****************************************************************************/
158#ifndef QT_NO_DATASTREAM
159Q_GUI_EXPORT QDataStream &operator<<(QDataStream &ds, const QPalette &p);
160Q_GUI_EXPORT QDataStream &operator>>(QDataStream &ds, QPalette &p);
161#endif // QT_NO_DATASTREAM
162
163#ifndef QT_NO_DEBUG_STREAM
164Q_GUI_EXPORT QDebug operator<<(QDebug, const QPalette &);
165#endif
166
167QT_END_NAMESPACE
168
169#endif // QPALETTE_H
170

source code of qtbase/src/gui/kernel/qpalette.h