11#include <QtCore/qmetaobject.h>
15Q_CONSTINIT QAtomicInt QPalettePrivate::qt_palette_count = Q_BASIC_ATOMIC_INITIALIZER(0);
16Q_CONSTINIT QAtomicInt QPalettePrivate::qt_palette_private_count = Q_BASIC_ATOMIC_INITIALIZER(0);
18static_assert(QPalettePrivate::bitPosition(QPalette::ColorGroup(QPalette::NColorGroups - 1),
19 QPalette::ColorRole(QPalette::NColorRoles - 1))
20 <
sizeof(QPalette::ResolveMask) * CHAR_BIT,
21 "The resolve mask type is not wide enough to fit the entire bit mask.");
25 return QColor((a.red() + b.red()) / 2, (a.green() + b.green()) / 2,
26 (a.blue() + b.blue()) / 2, (a.alpha() + b.alpha()) / 2);
30
31
32
33
34
35
36
37
38
41 if (alpha < 0
or alpha > 100)
44 for (
int cg = 0; cg <
int(QPalette::NColorGroups); ++cg) {
45 const QPalette::ColorGroup group = QPalette::ColorGroup(cg);
48 if (!pal.isBrushSet(group, QPalette::PlaceholderText)) {
49 QColor c = pal.color(group, QPalette::Text);
50 const int a = (c.alpha() * alpha) / 100;
52 pal.setColor(group, QPalette::PlaceholderText, c);
60 const int lighter = pal.base().color().lightness() > pal.text().color().lightness() ? 130 : 70;
63 for (
int i = 0; i < QPalette::NColorGroups; ++i) {
64 const QPalette::ColorGroup group =
static_cast<QPalette::ColorGroup>(i);
65 if (!pal.isBrushSet(group, QPalette::Accent)) {
67 const QBrush accentBrush = pal.isBrushSet(group, QPalette::Highlight)
68 ? pal.brush(group, QPalette::Highlight)
69 : pal.brush(group, QPalette::Base).color().lighter(lighter);
70 pal.setBrush(group, QPalette::Accent, accentBrush);
78 button.getHsv(&h, &s, &v);
80 const QBrush whiteBrush = QBrush(Qt::white);
81 const QBrush blackBrush = QBrush(Qt::black);
82 const QBrush baseBrush = v > 128 ? whiteBrush : blackBrush;
83 const QBrush foregroundBrush = v > 128 ? blackBrush : whiteBrush;
84 const QBrush buttonBrush = QBrush(button);
85 const QBrush buttonBrushDark = QBrush(button.darker());
86 const QBrush buttonBrushDark150 = QBrush(button.darker(150));
87 const QBrush buttonBrushLight150 = QBrush(button.lighter(150));
88 pal.setColorGroup(QPalette::Active, foregroundBrush, buttonBrush, buttonBrushLight150,
89 buttonBrushDark, buttonBrushDark150, foregroundBrush, whiteBrush,
90 baseBrush, buttonBrush);
91 pal.setColorGroup(QPalette::Inactive, foregroundBrush, buttonBrush, buttonBrushLight150,
92 buttonBrushDark, buttonBrushDark150, foregroundBrush, whiteBrush,
93 baseBrush, buttonBrush);
94 pal.setColorGroup(QPalette::Disabled, buttonBrushDark, buttonBrush, buttonBrushLight150,
95 buttonBrushDark, buttonBrushDark150, buttonBrushDark,
96 whiteBrush, buttonBrush, buttonBrush);
98 qt_placeholder_from_text(pal);
99 qt_ensure_default_accent_color(pal);
103
104
105
106
107
108
111
112
113
114
115
116
117
118
119
122
123
124
125
126
127
128
129
130
133
134
135
136
137
138
139
140
141
144
145
146
147
148
149
150
153
154
155
156
157
158
159
162
163
164
165
166
167
170
171
172
173
174
175
178
179
180
181
182
183
186
187
188
189
190
191
194
195
196
197
198
199
202
203
204
205
206
207
210
211
212
213
214
215
218
219
220
221
222
223
226
227
228
229
230
231
234
235
236
237
238
239
242
243
244
245
246
247
248
249
250
251
252
255
256
257
258
259
260
261
262
263
264
265
268
269
270
271
272
273
274
277
278
279
280
281
282
285
286
287
288
289
290
293
294
295
296
297
298
301
302
303
304
305
306
307
310
311
312
313
314
315
318
319
320
321
322
323
326
327
328
329
330
331
332
333
334
335
338
339
340
341
344
345
346
347
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
417
418
419
420
421
422
423
424
425
426
427
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
541
542
543
544
545
546
547
554 if (QGuiApplicationPrivate::app_pal) {
555 d = QGuiApplicationPrivate::app_pal->d;
560 qt_palette_from_color(*
this, Qt::black);
566
567
568
569
570QPalette::QPalette(
const QColor &button)
573 qt_palette_from_color(*
this, button);
577
578
579
580
581QPalette::QPalette(Qt::GlobalColor button)
584 qt_palette_from_color(*
this, button);
588
589
590
591
592
593
594QPalette::QPalette(
const QBrush &windowText,
const QBrush &button,
595 const QBrush &light,
const QBrush &dark,
596 const QBrush &mid,
const QBrush &text,
597 const QBrush &bright_text,
const QBrush &base,
598 const QBrush &window)
601 setColorGroup(All, windowText, button, light, dark, mid, text, bright_text,
604 qt_placeholder_from_text(*
this);
605 qt_ensure_default_accent_color(*
this);
610
611
612
613
614
615
616QPalette::QPalette(
const QColor &windowText,
const QColor &window,
617 const QColor &light,
const QColor &dark,
const QColor &mid,
618 const QColor &text,
const QColor &base)
621 const QBrush windowBrush(window);
622 const QBrush lightBrush(light);
623 setColorGroup(All, QBrush(windowText), windowBrush, lightBrush,
624 QBrush(dark), QBrush(mid), QBrush(text), lightBrush,
625 QBrush(base), windowBrush);
629
630
631
632
633QPalette::QPalette(
const QColor &button,
const QColor &window)
637 window.getHsv(&h, &s, &v);
639 const QBrush windowBrush = QBrush(window);
640 const QBrush whiteBrush = QBrush(Qt::white);
641 const QBrush blackBrush = QBrush(Qt::black);
642 const QBrush baseBrush = v > 128 ? whiteBrush : blackBrush;
643 const QBrush foregroundBrush = v > 128 ? blackBrush : whiteBrush;
644 const QBrush disabledForeground = QBrush(Qt::darkGray);
646 const QBrush buttonBrush = QBrush(button);
647 const QBrush buttonBrushDark = QBrush(button.darker());
648 const QBrush buttonBrushDark150 = QBrush(button.darker(150));
649 const QBrush buttonBrushLight150 = QBrush(button.lighter(150));
652 setColorGroup(Inactive, foregroundBrush, buttonBrush, buttonBrushLight150, buttonBrushDark,
653 buttonBrushDark150, foregroundBrush, whiteBrush, baseBrush,
655 setColorGroup(Active, foregroundBrush, buttonBrush, buttonBrushLight150, buttonBrushDark,
656 buttonBrushDark150, foregroundBrush, whiteBrush, baseBrush,
658 setColorGroup(Disabled, disabledForeground, buttonBrush, buttonBrushLight150,
659 buttonBrushDark, buttonBrushDark150, disabledForeground,
660 whiteBrush, baseBrush, windowBrush);
662 qt_placeholder_from_text(*
this);
663 qt_ensure_default_accent_color(*
this);
667
668
669
670
671QPalette::QPalette(
const QPalette &p)
672 : d(p.d), currentGroup(p.currentGroup)
678
679
680
681
682
683
684
685
686
689
690
693 if (d && !d->ref.deref())
700 d =
new QPalettePrivate;
704
705
706
707
708
709QPalette &QPalette::operator=(
const QPalette &p)
712 currentGroup = p.currentGroup;
713 if (d && !d->ref.deref())
720
721
722
723
726
727
728QPalette::operator QVariant()
const
730 return QVariant::fromValue(*
this);
734
735
736
737
738
739
740
743
744
745
746
747
748
749
750const QBrush &QPalette::brush(ColorGroup gr, ColorRole cr)
const
752 Q_ASSERT(cr < NColorRoles);
753 if (gr >= (
int)NColorGroups) {
757 qWarning(
"QPalette::brush: Unknown ColorGroup: %d", (
int)gr);
761 return d->data->br[gr][cr];
765
766
767
768
769
770
771
774
775
776
777
778
779
780
781
782void QPalette::setBrush(ColorGroup cg, ColorRole cr,
const QBrush &b)
784 Q_ASSERT(cr < NColorRoles);
787 for (uint i = 0; i < NColorGroups; i++)
788 setBrush(ColorGroup(i), cr, b);
794 }
else if (cg >= NColorGroups) {
795 qWarning(
"QPalette::setBrush: Unknown ColorGroup: %d", cg);
799 const auto newResolveMask = d->resolveMask | ResolveMask(1) << QPalettePrivate::bitPosition(cg, cr);
800 const auto valueChanged = d->data->br[cg][cr] != b;
805 d->data->br[cg][cr] = b;
806 }
else if (d->resolveMask != newResolveMask) {
810 d->resolveMask = newResolveMask;
814
815
816
817
818
819
820
821
822
823
824
825
826
827bool QPalette::isBrushSet(ColorGroup cg, ColorRole cr)
const
836 if (cg >= NColorGroups) {
837 qWarning() <<
"Wrong color group:" << cg;
841 if (cr >= NColorRoles) {
842 qWarning() <<
"Wrong color role:" << cr;
846 return d->resolveMask & (ResolveMask(1) << QPalettePrivate::bitPosition(cg, cr));
850
851
852void QPalette::detach()
854 if (d->ref.loadRelaxed() != 1) {
855 QPalettePrivate *x =
new QPalettePrivate(d->data);
856 x->resolveMask = d->resolveMask;
861 d->detach_no = ++QPalettePrivate::qt_palette_private_count;
866
867
868
869
870
871
872
873
874
875
878
879
880
881
882
883
884
885
886
887
888
889bool QPalette::operator==(
const QPalette &p)
const
891 if (isCopyOf(p) || d->data == p.d->data)
893 for(
int grp = 0; grp < (
int)NColorGroups; grp++) {
894 for(
int role = 0; role < (
int)NColorRoles; role++) {
898 if (d->data->br[grp][role] != p.d->data->br[grp][role])
906
907
908
909
910
911bool QPalette::isEqual(QPalette::ColorGroup group1, QPalette::ColorGroup group2)
const
913 if (group1 >= (
int)NColorGroups) {
914 if (group1 == Current) {
915 group1 = currentGroup;
917 qWarning(
"QPalette::brush: Unknown ColorGroup(1): %d", (
int)group1);
921 if (group2 >= (
int)NColorGroups) {
922 if (group2 == Current) {
923 group2 = currentGroup;
925 qWarning(
"QPalette::brush: Unknown ColorGroup(2): %d", (
int)group2);
929 if (group1 == group2)
931 for(
int role = 0; role < (
int)NColorRoles; role++) {
932 if (d->data->br[group1][role] != d->data->br[group2][role])
939
940
941
942
943
944
945qint64 QPalette::cacheKey()
const
947 return (((qint64) d->data->ser_no) << 32) | ((qint64) (d->detach_no));
952 QPalette::ResolveMask mask = {0};
953 for (
int role = 0; role <
int(QPalette::NColorRoles); ++role) {
954 for (
int grp = 0; grp <
int(QPalette::NColorGroups); ++grp) {
955 mask |= (QPalette::ResolveMask(1) << QPalettePrivate::bitPosition(QPalette::ColorGroup(grp), QPalette::ColorRole(role)));
962
963
964
965
966
967
968QPalette QPalette::resolve(
const QPalette &other)
const
970 if ((*
this == other && d->resolveMask == other.d->resolveMask)
971 || d->resolveMask == 0) {
973 o.setResolveMask(d->resolveMask);
977 if (d->resolveMask == allResolveMask())
980 QPalette palette(*
this);
983 for (
int role = 0; role <
int(NColorRoles); ++role) {
988 for (
int grp = 0; grp <
int(NColorGroups); ++grp) {
989 if (!(d->resolveMask & (ResolveMask(1) << QPalettePrivate::bitPosition(ColorGroup(grp), ColorRole(role))))) {
990 palette.d->data.detach();
991 palette.d->data->br[grp][role] = other.d->data->br[grp][role];
996 palette.d->resolveMask |= other.d->resolveMask;
1002
1003
1004QPalette::ResolveMask QPalette::resolveMask()
const
1006 return d->resolveMask;
1010
1011
1012void QPalette::setResolveMask(QPalette::ResolveMask mask)
1014 if (mask == d->resolveMask)
1018 d->resolveMask = mask;
1022
1023
1024
1025
1026
1027
1030
1031
1033#ifndef QT_NO_DATASTREAM
1036static const int oldRoles[7] = { QPalette::WindowText, QPalette::Window, QPalette::Light,
1037 QPalette::Dark, QPalette::Mid, QPalette::Text, QPalette::Base };
1040
1041
1042
1043
1044
1045
1046
1048QDataStream &operator<<(QDataStream &s,
const QPalette &p)
1050 for (
int grp = 0; grp < (
int)QPalette::NColorGroups; grp++) {
1051 if (s.version() == 1) {
1054 s << p.d->data->br[grp][
oldRoles[i]].color();
1056 int max = (
int)QPalette::NColorRoles;
1057 if (s.version() <= QDataStream::Qt_2_1)
1058 max = QPalette::HighlightedText + 1;
1059 else if (s.version() <= QDataStream::Qt_4_3)
1060 max = QPalette::AlternateBase + 1;
1061 else if (s.version() <= QDataStream::Qt_5_11)
1062 max = QPalette::ToolTipText + 1;
1063 else if (s.version() <= QDataStream::Qt_6_5)
1064 max = QPalette::PlaceholderText + 1;
1066 for (
int r = 0; r < max; r++)
1067 s << p.d->data->br[grp][r];
1078 pal.setColor(grp, (QPalette::ColorRole)oldRoles[i], col);
1083
1084
1085
1086
1087
1088
1089
1093 if (s.version() == 1) {
1095 readV1ColorGroup(s, p, QPalette::Active);
1096 readV1ColorGroup(s, p, QPalette::Disabled);
1097 readV1ColorGroup(s, p, QPalette::Inactive);
1099 int max = QPalette::NColorRoles;
1100 if (s.version() <= QDataStream::Qt_2_1) {
1102 max = QPalette::HighlightedText + 1;
1103 }
else if (s.version() <= QDataStream::Qt_4_3) {
1105 max = QPalette::AlternateBase + 1;
1106 }
else if (s.version() <= QDataStream::Qt_5_11) {
1108 max = QPalette::ToolTipText + 1;
1109 }
else if (s.version() <= QDataStream::Qt_6_5) {
1111 max = QPalette::PlaceholderText + 1;
1116 for(
int grp = 0; grp < (
int)QPalette::NColorGroups; ++grp) {
1117 const QPalette::ColorGroup group =
static_cast<QPalette::ColorGroup>(grp);
1118 for(
int role = 0; role < max; ++role) {
1120 p.setBrush(group, (QPalette::ColorRole)role, tmp);
1124 if (s.version() < QDataStream::Qt_6_6)
1125 p.setBrush(group, QPalette::Accent, p.brush(group, QPalette::Highlight));
1134
1135
1136
1137
1138
1139
1140
1142bool QPalette::isCopyOf(
const QPalette &p)
const
1148
1149
1150
1151
1152
1153
1154
1155void QPalette::setColorGroup(ColorGroup cg,
const QBrush &windowText,
const QBrush &button,
1156 const QBrush &light,
const QBrush &dark,
const QBrush &mid,
1157 const QBrush &text,
const QBrush &bright_text,
const QBrush &base,
1158 const QBrush &window)
1160 QBrush alt_base = QBrush(qt_mix_colors(base.color(), button.color()));
1161 QBrush mid_light = QBrush(qt_mix_colors(button.color(), light.color()));
1162 QColor toolTipBase(255, 255, 220);
1163 QColor toolTipText(0, 0, 0);
1165 setColorGroup(cg, windowText, button, light, dark, mid, text, bright_text, base,
1166 alt_base, window, mid_light, text,
1167 QBrush(Qt::black), QBrush(Qt::darkBlue), QBrush(Qt::white),
1168 QBrush(Qt::blue), QBrush(Qt::magenta), QBrush(toolTipBase),
1169 QBrush(toolTipText));
1171 for (
int cr = Highlight; cr <= LinkVisited; ++cr) {
1173 for (
int group = Active; group < NColorGroups; ++group) {
1174 d->resolveMask &= ~(ResolveMask(1) << QPalettePrivate::bitPosition(ColorGroup(group), ColorRole(cr)));
1177 d->resolveMask &= ~(ResolveMask(1) << QPalettePrivate::bitPosition(ColorGroup(cg), ColorRole(cr)));
1185QPalette::setColorGroup(ColorGroup cg,
const QBrush &foreground,
const QBrush &button,
1186 const QBrush &light,
const QBrush &dark,
const QBrush &mid,
1187 const QBrush &text,
const QBrush &bright_text,
1188 const QBrush &base,
const QBrush &alternate_base,
1189 const QBrush &background,
const QBrush &midlight,
1190 const QBrush &button_text,
const QBrush &shadow,
1191 const QBrush &highlight,
const QBrush &highlighted_text,
1192 const QBrush &link,
const QBrush &link_visited)
1194 setColorGroup(cg, foreground, button, light, dark, mid,
1195 text, bright_text, base, alternate_base, background,
1196 midlight, button_text, shadow, highlight, highlighted_text,
1197 link, link_visited, background, foreground);
1201void QPalette::setColorGroup(ColorGroup cg,
const QBrush &foreground,
const QBrush &button,
1202 const QBrush &light,
const QBrush &dark,
const QBrush &mid,
1203 const QBrush &text,
const QBrush &bright_text,
1204 const QBrush &base,
const QBrush &alternate_base,
1205 const QBrush &background,
const QBrush &midlight,
1206 const QBrush &button_text,
const QBrush &shadow,
1207 const QBrush &highlight,
const QBrush &highlighted_text,
1208 const QBrush &link,
const QBrush &link_visited,
1209 const QBrush &toolTipBase,
const QBrush &toolTipText)
1211 setBrush(cg, WindowText, foreground);
1212 setBrush(cg, Button, button);
1213 setBrush(cg, Light, light);
1214 setBrush(cg, Dark, dark);
1215 setBrush(cg, Mid, mid);
1216 setBrush(cg, Text, text);
1217 setBrush(cg, BrightText, bright_text);
1218 setBrush(cg, Base, base);
1219 setBrush(cg, AlternateBase, alternate_base);
1220 setBrush(cg, Window, background);
1221 setBrush(cg, Midlight, midlight);
1222 setBrush(cg, ButtonText, button_text);
1223 setBrush(cg, Shadow, shadow);
1224 setBrush(cg, Highlight, highlight);
1225 setBrush(cg, HighlightedText, highlighted_text);
1226 setBrush(cg, Link, link);
1227 setBrush(cg, LinkVisited, link_visited);
1228 setBrush(cg, ToolTipBase, toolTipBase);
1229 setBrush(cg, ToolTipText, toolTipText);
1232#ifndef QT_NO_DEBUG_STREAM
1235 const auto groupEnum = QMetaEnum::fromType<QPalette::ColorGroup>();
1237 QString groupString;
1238 for (
int group = 0; group < QPalette::NColorGroups; ++group) {
1239 const auto cg = QPalette::ColorGroup(group);
1241 if (p.isBrushSet(cg, cr)) {
1242 const auto &color = p.color(cg, cr);
1243 groupString += QString::fromUtf8(groupEnum.valueToKey(cg)) + u':' +
1244 color.name(QColor::HexArgb) + u',';
1247 groupString.chop(1);
1254 const auto roleEnum = QMetaEnum::fromType<QPalette::ColorRole>();
1257 for (
int role = 0; role < QPalette::NColorRoles; ++role) {
1258 const auto cr = QPalette::ColorRole(role);
1260 auto groupString = groupsToString(p, cr);
1261 if (!groupString.isEmpty())
1262 roleString += QString::fromUtf8(roleEnum.valueToKey(cr)) + QStringLiteral(
":[") +
1263 groupString + QStringLiteral(
"],");
1272 QDebugStateSaver saver(dbg);
1275 dbg <<
"QPalette(resolve=" << Qt::hex << Qt::showbase << p.resolveMask();
1277 auto roleString = rolesToString(p);
1278 if (!roleString.isEmpty())
1279 dbg <<
',' << roleString;
1289#include "moc_qpalette.cpp"
Combined button and popup list for selecting options.
QDataStream & operator>>(QDataStream &s, QKeyCombination &combination)
QDebug operator<<(QDebug dbg, const QFileInfo &fi)
static QString groupsToString(const QPalette &p, QPalette::ColorRole cr)
static void qt_ensure_default_accent_color(QPalette &pal)
static const int NumOldRoles
static void qt_placeholder_from_text(QPalette &pal, int alpha=50)
static constexpr QPalette::ResolveMask allResolveMask()
static QString rolesToString(const QPalette &p)
static void readV1ColorGroup(QDataStream &s, QPalette &pal, QPalette::ColorGroup grp)
static QColor qt_mix_colors(QColor a, QColor b)
static const int oldRoles[7]
static void qt_palette_from_color(QPalette &pal, const QColor &button)