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 enum ColorGroup { Active, Disabled, Inactive, NColorGroups, Current, All, Normal = Active };
50 Q_ENUM(ColorGroup)
51 enum ColorRole { WindowText, Button, Light, Midlight, Dark, Mid,
52 Text, BrightText, ButtonText, Base, Window, Shadow,
53 Highlight, HighlightedText,
54 Link, LinkVisited,
55 AlternateBase,
56 NoRole,
57 ToolTipBase, ToolTipText,
58 PlaceholderText,
59 Accent,
60 NColorRoles = Accent + 1,
61 };
62 Q_ENUM(ColorRole)
63
64 inline ColorGroup currentColorGroup() const { return currentGroup; }
65 inline void setCurrentColorGroup(ColorGroup cg) { currentGroup = cg; }
66
67 inline const QColor &color(ColorGroup cg, ColorRole cr) const
68 { return brush(cg, cr).color(); }
69 const QBrush &brush(ColorGroup cg, ColorRole cr) const;
70 inline void setColor(ColorGroup cg, ColorRole cr, const QColor &color);
71 inline void setColor(ColorRole cr, const QColor &color);
72 inline void setBrush(ColorRole cr, const QBrush &brush);
73 bool isBrushSet(ColorGroup cg, ColorRole cr) const;
74 void setBrush(ColorGroup cg, ColorRole cr, const QBrush &brush);
75 void setColorGroup(ColorGroup cr, const QBrush &windowText, const QBrush &button,
76 const QBrush &light, const QBrush &dark, const QBrush &mid,
77 const QBrush &text, const QBrush &bright_text, const QBrush &base,
78 const QBrush &window);
79 bool isEqual(ColorGroup cr1, ColorGroup cr2) const;
80
81 inline const QColor &color(ColorRole cr) const { return color(cg: Current, cr); }
82 inline const QBrush &brush(ColorRole cr) const { return brush(cg: Current, cr); }
83 inline const QBrush &windowText() const { return brush(cr: WindowText); }
84 inline const QBrush &button() const { return brush(cr: Button); }
85 inline const QBrush &light() const { return brush(cr: Light); }
86 inline const QBrush &dark() const { return brush(cr: Dark); }
87 inline const QBrush &mid() const { return brush(cr: Mid); }
88 inline const QBrush &text() const { return brush(cr: Text); }
89 inline const QBrush &base() const { return brush(cr: Base); }
90 inline const QBrush &alternateBase() const { return brush(cr: AlternateBase); }
91 inline const QBrush &toolTipBase() const { return brush(cr: ToolTipBase); }
92 inline const QBrush &toolTipText() const { return brush(cr: ToolTipText); }
93 inline const QBrush &window() const { return brush(cr: Window); }
94 inline const QBrush &midlight() const { return brush(cr: Midlight); }
95 inline const QBrush &brightText() const { return brush(cr: BrightText); }
96 inline const QBrush &buttonText() const { return brush(cr: ButtonText); }
97 inline const QBrush &shadow() const { return brush(cr: Shadow); }
98 inline const QBrush &highlight() const { return brush(cr: Highlight); }
99 inline const QBrush &highlightedText() const { return brush(cr: HighlightedText); }
100 inline const QBrush &link() const { return brush(cr: Link); }
101 inline const QBrush &linkVisited() const { return brush(cr: LinkVisited); }
102 inline const QBrush &placeholderText() const { return brush(cr: PlaceholderText); }
103 inline const QBrush &accent() const { return brush(cr: Accent); }
104
105 bool operator==(const QPalette &p) const;
106 inline bool operator!=(const QPalette &p) const { return !(operator==(p)); }
107 bool isCopyOf(const QPalette &p) const;
108
109 qint64 cacheKey() const;
110
111 QPalette resolve(const QPalette &other) const;
112
113 using ResolveMask = quint64;
114 ResolveMask resolveMask() const;
115 void setResolveMask(ResolveMask mask);
116
117private:
118 void setColorGroup(ColorGroup cr, const QBrush &windowText, const QBrush &button,
119 const QBrush &light, const QBrush &dark, const QBrush &mid,
120 const QBrush &text, const QBrush &bright_text,
121 const QBrush &base, const QBrush &alternate_base,
122 const QBrush &window, const QBrush &midlight,
123 const QBrush &button_text, const QBrush &shadow,
124 const QBrush &highlight, const QBrush &highlighted_text,
125 const QBrush &link, const QBrush &link_visited);
126 void setColorGroup(ColorGroup cr, const QBrush &windowText, const QBrush &button,
127 const QBrush &light, const QBrush &dark, const QBrush &mid,
128 const QBrush &text, const QBrush &bright_text,
129 const QBrush &base, const QBrush &alternate_base,
130 const QBrush &window, const QBrush &midlight,
131 const QBrush &button_text, const QBrush &shadow,
132 const QBrush &highlight, const QBrush &highlighted_text,
133 const QBrush &link, const QBrush &link_visited,
134 const QBrush &toolTipBase, const QBrush &toolTipText);
135 void init();
136 void detach();
137
138 QPalettePrivate *d;
139 ColorGroup currentGroup{Active};
140
141 friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &s, const QPalette &p);
142};
143
144Q_DECLARE_SHARED(QPalette)
145
146inline void QPalette::setColor(ColorGroup acg, ColorRole acr,
147 const QColor &acolor)
148{ setBrush(cg: acg, cr: acr, brush: QBrush(acolor)); }
149inline void QPalette::setColor(ColorRole acr, const QColor &acolor)
150{ setColor(acg: All, acr, acolor); }
151inline void QPalette::setBrush(ColorRole acr, const QBrush &abrush)
152{ setBrush(cg: All, cr: acr, brush: abrush); }
153
154/*****************************************************************************
155 QPalette stream functions
156 *****************************************************************************/
157#ifndef QT_NO_DATASTREAM
158Q_GUI_EXPORT QDataStream &operator<<(QDataStream &ds, const QPalette &p);
159Q_GUI_EXPORT QDataStream &operator>>(QDataStream &ds, QPalette &p);
160#endif // QT_NO_DATASTREAM
161
162#ifndef QT_NO_DEBUG_STREAM
163Q_GUI_EXPORT QDebug operator<<(QDebug, const QPalette &);
164#endif
165
166QT_END_NAMESPACE
167
168#endif // QPALETTE_H
169

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