1// Copyright (C) 2020 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#include "qquickcolorgroup_p.h"
4
5#include <QScopeGuard>
6
7#include <QtQuick/private/qquickabstractpaletteprovider_p.h>
8#include <QtQuick/private/qquickpalette_p.h>
9#include <QtQuick/private/qquickpalettecolorprovider_p.h>
10
11QT_BEGIN_NAMESPACE
12
13/*!
14 \internal
15
16 \class QQuickColorGroup
17 \brief The QQuickColorGroup class represents a set of colors.
18 \inmodule QtQuick
19 \since 6.0
20
21 Used by QQuickPalette to provide different groups of colors by roles.
22
23 \sa QPalette::ColorRole
24 //! internal \sa QQuickPalette, QQuickAbstractPaletteProvider
25 */
26
27/*!
28 \qmltype ColorGroup
29 \instantiates QQuickColorGroup
30 \inherits QtObject
31 \inqmlmodule QtQuick
32 \ingroup qtquick-visual
33 \brief The set of colors by roles.
34
35 The ColorGroup type is used to define a set of colors with certain roles. Although a ColorGroup
36 has no visual appearance, it defines colors used to customize rendered items.
37
38 Default values of colors are equal to active group colors of default-constructed \c QPalette.
39
40 The following code can be used to create a color group with some colors specified:
41
42 \code
43 ColorGroup {
44 alternateBase: "red"
45 base: "green"
46 }
47 \endcode
48
49 The \l Palette type exposes color groups for each QML item state.
50 */
51
52/*!
53 \qmlproperty color QtQuick::ColorGroup::accent
54 \since 6.6
55
56 A color that typically contrasts or compliments \l base, \l window, and \l
57 button colors. It usually represents the users' choice of desktop
58 personalisation. Styling of interactive components is a typical use case.
59 Unless explicitly set, it defaults to \l highlight.
60*/
61
62/*!
63 \qmlproperty color QtQuick::ColorGroup::alternateBase
64
65 Used as the alternate background color in item views with alternating row colors.
66*/
67
68/*!
69 \qmlproperty color QtQuick::ColorGroup::base
70
71 Used mostly as the background color for text editor controls and item views.
72 It is usually white or another light color.
73*/
74
75/*!
76 \qmlproperty color QtQuick::ColorGroup::brightText
77
78 A text color that is very different from \c windowText, and contrasts
79 well with e.g. \c dark. Typically used for text that needs to be drawn
80 where \c text, \c windowText or \c buttonText would
81 give poor contrast, such as on highlighted buttons.
82*/
83
84/*!
85 \qmlproperty color QtQuick::ColorGroup::button
86
87 The general button background color. This background can be different from
88 \c window as some styles require a different background color for buttons.
89*/
90
91/*!
92 \qmlproperty color QtQuick::ColorGroup::buttonText
93
94 A foreground color used with the \c palette color.
95*/
96
97/*!
98 \qmlproperty color QtQuick::ColorGroup::dark
99
100 A foreground color used with the \c palette color.
101*/
102
103/*!
104 \qmlproperty color QtQuick::ColorGroup::highlight
105
106 A color to indicate a selected item or the current item.
107*/
108
109/*!
110 \qmlproperty color QtQuick::ColorGroup::highlightedText
111
112 A text color that contrasts with \c highlight.
113*/
114
115/*!
116 \qmlproperty color QtQuick::ColorGroup::light
117
118 Lighter than \c button.
119*/
120
121/*!
122 \qmlproperty color QtQuick::ColorGroup::link
123
124 A text color used for hyperlinks.
125*/
126
127/*!
128 \qmlproperty color QtQuick::ColorGroup::linkVisited
129
130 A text color used for already visited hyperlinks.
131*/
132
133/*!
134 \qmlproperty color QtQuick::ColorGroup::mid
135
136 Between \c palette.button and \c dark.
137*/
138
139/*!
140 \qmlproperty color QtQuick::ColorGroup::midlight
141
142 Between \c button and \c light.
143*/
144
145/*!
146 \qmlproperty color QtQuick::ColorGroup::shadow
147
148 A very dark color.
149*/
150
151/*!
152 \qmlproperty color QtQuick::ColorGroup::text
153
154 The foreground color used with \c base. This is usually the same as
155 the \c windowText, in which case it must provide good contrast with
156 \c window and \c base.
157*/
158
159/*!
160 \qmlproperty color QtQuick::ColorGroup::toolTipBase
161
162 Used as the background color for tooltips.
163*/
164
165/*!
166 \qmlproperty color QtQuick::ColorGroup::toolTipText
167
168 Used as the foreground color for tooltips.
169*/
170
171/*!
172 \qmlproperty color QtQuick::ColorGroup::window
173
174 A general background color.
175*/
176
177/*!
178 \qmlproperty color QtQuick::ColorGroup::windowText
179
180 A general foreground color.
181*/
182
183/*!
184 \qmlproperty color QtQuick::ColorGroup::placeholderText
185 \since 6.2
186
187 Used as the place holder color for editable single line fields.
188*/
189
190/*!
191 \qmlsignal QtQuick::ColorGroup::changed
192
193 Additional signal indicates that the current state of this color group
194 has been changed. Usually it means that one of the colors is changed.
195*/
196
197/*!
198 Construct object in default state.
199 */
200QQuickColorGroup::QQuickColorGroup(QQuickPalette &parent)
201 : QObject(&parent)
202 , m_groupTag(defaultGroupTag())
203 , m_colorProvider(parent.colorProvider().shared_from_this())
204{
205}
206
207QPalette::ColorGroup QQuickColorGroup::currentColorGroup() const
208{
209 return groupTag();
210}
211
212QColor QQuickColorGroup::alternateBase() const
213{
214 return color(role: QPalette::AlternateBase);
215}
216
217void QQuickColorGroup::setAlternateBase(const QColor &color)
218{
219 setColor(role: QPalette::AlternateBase, color, notifier: &QQuickColorGroup::alternateBaseChanged);
220}
221
222void QQuickColorGroup::resetAlternateBase()
223{
224 resetColor(role: QPalette::AlternateBase, notifier: &QQuickColorGroup::alternateBaseChanged);
225}
226
227QColor QQuickColorGroup::base() const
228{
229 return color(role: QPalette::Base);
230}
231
232void QQuickColorGroup::setBase(const QColor &color)
233{
234 setColor(role: QPalette::Base, color, notifier: &QQuickColorGroup::baseChanged);
235}
236
237void QQuickColorGroup::resetBase()
238{
239 resetColor(role: QPalette::Base, notifier: &QQuickColorGroup::baseChanged);
240}
241
242QColor QQuickColorGroup::brightText() const
243{
244 return color(role: QPalette::BrightText);
245}
246
247void QQuickColorGroup::setBrightText(const QColor &color)
248{
249 setColor(role: QPalette::BrightText, color, notifier: &QQuickColorGroup::brightTextChanged);
250}
251
252void QQuickColorGroup::resetBrightText()
253{
254 resetColor(role: QPalette::BrightText, notifier: &QQuickColorGroup::brightTextChanged);
255}
256
257QColor QQuickColorGroup::button() const
258{
259 return color(role: QPalette::Button);
260}
261
262void QQuickColorGroup::setButton(const QColor &color)
263{
264 setColor(role: QPalette::Button, color, notifier: &QQuickColorGroup::buttonChanged);
265}
266
267void QQuickColorGroup::resetButton()
268{
269 resetColor(role: QPalette::Button, notifier: &QQuickColorGroup::buttonChanged);
270}
271
272QColor QQuickColorGroup::buttonText() const
273{
274 return color(role: QPalette::ButtonText);
275}
276
277void QQuickColorGroup::setButtonText(const QColor &color)
278{
279 setColor(role: QPalette::ButtonText, color, notifier: &QQuickColorGroup::buttonTextChanged);
280}
281
282void QQuickColorGroup::resetButtonText()
283{
284 resetColor(role: QPalette::ButtonText, notifier: &QQuickColorGroup::buttonTextChanged);
285}
286
287QColor QQuickColorGroup::dark() const
288{
289 return color(role: QPalette::Dark);
290}
291
292void QQuickColorGroup::setDark(const QColor &color)
293{
294 setColor(role: QPalette::Dark, color, notifier: &QQuickColorGroup::darkChanged);
295}
296
297void QQuickColorGroup::resetDark()
298{
299 resetColor(role: QPalette::Dark, notifier: &QQuickColorGroup::darkChanged);
300}
301
302QColor QQuickColorGroup::highlight() const
303{
304 return color(role: QPalette::Highlight);
305}
306
307void QQuickColorGroup::setHighlight(const QColor &color)
308{
309 setColor(role: QPalette::Highlight, color, notifier: &QQuickColorGroup::highlightChanged);
310}
311
312void QQuickColorGroup::resetHighlight()
313{
314 resetColor(role: QPalette::Highlight, notifier: &QQuickColorGroup::highlightChanged);
315}
316
317QColor QQuickColorGroup::highlightedText() const
318{
319 return color(role: QPalette::HighlightedText);
320}
321
322void QQuickColorGroup::setHighlightedText(const QColor &color)
323{
324 setColor(role: QPalette::HighlightedText, color, notifier: &QQuickColorGroup::highlightedTextChanged);
325}
326
327void QQuickColorGroup::resetHighlightedText()
328{
329 resetColor(role: QPalette::HighlightedText, notifier: &QQuickColorGroup::highlightedTextChanged);
330}
331
332QColor QQuickColorGroup::light() const
333{
334 return color(role: QPalette::Light);
335}
336
337void QQuickColorGroup::setLight(const QColor &color)
338{
339 setColor(role: QPalette::Light, color, notifier: &QQuickColorGroup::lightChanged);
340}
341
342void QQuickColorGroup::resetLight()
343{
344 resetColor(role: QPalette::Light, notifier: &QQuickColorGroup::lightChanged);
345}
346
347QColor QQuickColorGroup::link() const
348{
349 return color(role: QPalette::Link);
350}
351
352void QQuickColorGroup::setLink(const QColor &color)
353{
354 setColor(role: QPalette::Link, color, notifier: &QQuickColorGroup::linkChanged);
355}
356
357void QQuickColorGroup::resetLink()
358{
359 resetColor(role: QPalette::Link, notifier: &QQuickColorGroup::linkChanged);
360}
361
362QColor QQuickColorGroup::linkVisited() const
363{
364 return color(role: QPalette::LinkVisited);
365}
366
367void QQuickColorGroup::setLinkVisited(const QColor &color)
368{
369 setColor(role: QPalette::LinkVisited, color, notifier: &QQuickColorGroup::linkVisitedChanged);
370}
371
372void QQuickColorGroup::resetLinkVisited()
373{
374 resetColor(role: QPalette::LinkVisited, notifier: &QQuickColorGroup::linkVisitedChanged);
375}
376
377QColor QQuickColorGroup::mid() const
378{
379 return color(role: QPalette::Mid);
380}
381
382void QQuickColorGroup::setMid(const QColor &color)
383{
384 setColor(role: QPalette::Mid, color, notifier: &QQuickColorGroup::midChanged);
385}
386
387void QQuickColorGroup::resetMid()
388{
389 resetColor(role: QPalette::Mid, notifier: &QQuickColorGroup::midChanged);
390}
391
392QColor QQuickColorGroup::midlight() const
393{
394 return color(role: QPalette::Midlight);
395}
396
397void QQuickColorGroup::setMidlight(const QColor &color)
398{
399 setColor(role: QPalette::Midlight, color, notifier: &QQuickColorGroup::midlightChanged);
400}
401
402void QQuickColorGroup::resetMidlight()
403{
404 resetColor(role: QPalette::Midlight, notifier: &QQuickColorGroup::midlightChanged);
405}
406
407QColor QQuickColorGroup::shadow() const
408{
409 return color(role: QPalette::Shadow);
410}
411
412void QQuickColorGroup::setShadow(const QColor &color)
413{
414 setColor(role: QPalette::Shadow, color, notifier: &QQuickColorGroup::shadowChanged);
415}
416
417void QQuickColorGroup::resetShadow()
418{
419 resetColor(role: QPalette::Shadow, notifier: &QQuickColorGroup::shadowChanged);
420}
421
422QColor QQuickColorGroup::text() const
423{
424 return color(role: QPalette::Text);
425}
426
427void QQuickColorGroup::setText(const QColor &color)
428{
429 setColor(role: QPalette::Text, color, notifier: &QQuickColorGroup::textChanged);
430}
431
432void QQuickColorGroup::resetText()
433{
434 resetColor(role: QPalette::Text, notifier: &QQuickColorGroup::textChanged);
435}
436
437QColor QQuickColorGroup::toolTipBase() const
438{
439 return color(role: QPalette::ToolTipBase);
440}
441
442void QQuickColorGroup::setToolTipBase(const QColor &color)
443{
444 setColor(role: QPalette::ToolTipBase, color, notifier: &QQuickColorGroup::toolTipBaseChanged);
445}
446
447void QQuickColorGroup::resetToolTipBase()
448{
449 resetColor(role: QPalette::ToolTipBase, notifier: &QQuickColorGroup::toolTipBaseChanged);
450}
451
452QColor QQuickColorGroup::toolTipText() const
453{
454 return color(role: QPalette::ToolTipText);
455}
456
457void QQuickColorGroup::setToolTipText(const QColor &color)
458{
459 setColor(role: QPalette::ToolTipText, color, notifier: &QQuickColorGroup::toolTipTextChanged);
460}
461
462void QQuickColorGroup::resetToolTipText()
463{
464 resetColor(role: QPalette::ToolTipText, notifier: &QQuickColorGroup::toolTipTextChanged);
465}
466
467QColor QQuickColorGroup::window() const
468{
469 return color(role: QPalette::Window);
470}
471
472void QQuickColorGroup::setWindow(const QColor &color)
473{
474 setColor(role: QPalette::Window, color, notifier: &QQuickColorGroup::windowChanged);
475}
476
477void QQuickColorGroup::resetWindow()
478{
479 resetColor(role: QPalette::Window, notifier: &QQuickColorGroup::windowChanged);
480}
481
482QColor QQuickColorGroup::windowText() const
483{
484 return color(role: QPalette::WindowText);
485}
486
487void QQuickColorGroup::setWindowText(const QColor &color)
488{
489 setColor(role: QPalette::WindowText, color, notifier: &QQuickColorGroup::windowTextChanged);
490}
491
492void QQuickColorGroup::resetWindowText()
493{
494 resetColor(role: QPalette::WindowText, notifier: &QQuickColorGroup::windowTextChanged);
495}
496
497QColor QQuickColorGroup::placeholderText() const
498{
499 return color(role: QPalette::PlaceholderText);
500}
501
502void QQuickColorGroup::setPlaceholderText(const QColor &color)
503{
504 setColor(role: QPalette::PlaceholderText, color, notifier: &QQuickColorGroup::placeholderTextChanged);
505}
506
507void QQuickColorGroup::resetPlaceholderText()
508{
509 resetColor(role: QPalette::PlaceholderText, notifier: &QQuickColorGroup::placeholderTextChanged);
510}
511
512QColor QQuickColorGroup::accent() const
513{
514 return color(role: QPalette::Accent);
515}
516
517void QQuickColorGroup::setAccent(const QColor &color)
518{
519 setColor(role: QPalette::Accent, color, notifier: &QQuickColorGroup::accentChanged);
520}
521
522void QQuickColorGroup::resetAccent()
523{
524 resetColor(role: QPalette::Accent, notifier: &QQuickColorGroup::accentChanged);
525}
526
527QPalette::ColorGroup QQuickColorGroup::groupTag() const
528{
529 return m_groupTag;
530}
531
532QQuickColorGroup::QQuickColorGroup(QObject *parent)
533 : QObject(parent)
534 , m_groupTag(defaultGroupTag())
535 , m_colorProvider(std::make_shared<QQuickPaletteColorProvider>())
536{
537}
538
539void QQuickColorGroup::setGroupTag(QPalette::ColorGroup tag)
540{
541 if (m_groupTag != tag) {
542 m_groupTag = tag;
543 Q_EMIT changed();
544 }
545}
546
547const QQuickPaletteColorProvider &QQuickColorGroup::colorProvider() const
548{
549 Q_ASSERT(m_colorProvider);
550 return *m_colorProvider;
551}
552
553QQuickPaletteColorProvider &QQuickColorGroup::colorProvider()
554{
555 return const_cast<QQuickPaletteColorProvider &>(
556 const_cast<const QQuickColorGroup*>(this)->colorProvider());
557}
558
559QQuickColorGroup *QQuickColorGroup::createWithParent(QQuickPalette &parent)
560{
561 return new QQuickColorGroup(parent);
562}
563
564QColor QQuickColorGroup::color(QPalette::ColorRole role) const
565{
566 return colorProvider().color(group: currentColorGroup(), role);
567}
568
569void QQuickColorGroup::setColor(QPalette::ColorRole role, QColor color, Notifier notifier)
570{
571 if (colorProvider().setColor(group: groupTag(), role, color)) {
572 Q_EMIT (this->*notifier)();
573 Q_EMIT changed();
574 }
575}
576
577void QQuickColorGroup::resetColor(QPalette::ColorRole role, Notifier notifier)
578{
579 if (colorProvider().resetColor(group: groupTag(), role)) {
580 Q_EMIT (this->*notifier)();
581 Q_EMIT changed();
582 }
583}
584
585QT_END_NAMESPACE
586
587#include "moc_qquickcolorgroup_p.cpp"
588

source code of qtdeclarative/src/quick/items/qquickcolorgroup.cpp