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
539
540
543
544
545
546
547
548
549
556 if (QGuiApplicationPrivate::app_pal) {
557 d = QGuiApplicationPrivate::app_pal->d;
562 qt_palette_from_color(*
this, Qt::black);
568
569
570
571
572QPalette::QPalette(
const QColor &button)
575 qt_palette_from_color(*
this, button);
579
580
581
582
583QPalette::QPalette(Qt::GlobalColor button)
586 qt_palette_from_color(*
this, button);
590
591
592
593
594
595
596QPalette::QPalette(
const QBrush &windowText,
const QBrush &button,
597 const QBrush &light,
const QBrush &dark,
598 const QBrush &mid,
const QBrush &text,
599 const QBrush &bright_text,
const QBrush &base,
600 const QBrush &window)
603 setColorGroup(All, windowText, button, light, dark, mid, text, bright_text,
606 qt_placeholder_from_text(*
this);
607 qt_ensure_default_accent_color(*
this);
612
613
614
615
616
617
618QPalette::QPalette(
const QColor &windowText,
const QColor &window,
619 const QColor &light,
const QColor &dark,
const QColor &mid,
620 const QColor &text,
const QColor &base)
623 const QBrush windowBrush(window);
624 const QBrush lightBrush(light);
625 setColorGroup(All, QBrush(windowText), windowBrush, lightBrush,
626 QBrush(dark), QBrush(mid), QBrush(text), lightBrush,
627 QBrush(base), windowBrush);
631
632
633
634
635QPalette::QPalette(
const QColor &button,
const QColor &window)
639 window.getHsv(&h, &s, &v);
641 const QBrush windowBrush = QBrush(window);
642 const QBrush whiteBrush = QBrush(Qt::white);
643 const QBrush blackBrush = QBrush(Qt::black);
644 const QBrush baseBrush = v > 128 ? whiteBrush : blackBrush;
645 const QBrush foregroundBrush = v > 128 ? blackBrush : whiteBrush;
646 const QBrush disabledForeground = QBrush(Qt::darkGray);
648 const QBrush buttonBrush = QBrush(button);
649 const QBrush buttonBrushDark = QBrush(button.darker());
650 const QBrush buttonBrushDark150 = QBrush(button.darker(150));
651 const QBrush buttonBrushLight150 = QBrush(button.lighter(150));
654 setColorGroup(Inactive, foregroundBrush, buttonBrush, buttonBrushLight150, buttonBrushDark,
655 buttonBrushDark150, foregroundBrush, whiteBrush, baseBrush,
657 setColorGroup(Active, foregroundBrush, buttonBrush, buttonBrushLight150, buttonBrushDark,
658 buttonBrushDark150, foregroundBrush, whiteBrush, baseBrush,
660 setColorGroup(Disabled, disabledForeground, buttonBrush, buttonBrushLight150,
661 buttonBrushDark, buttonBrushDark150, disabledForeground,
662 whiteBrush, baseBrush, windowBrush);
664 qt_placeholder_from_text(*
this);
665 qt_ensure_default_accent_color(*
this);
669
670
671
672
673QPalette::QPalette(
const QPalette &p)
674 : d(p.d), currentGroup(p.currentGroup)
680
681
682
683
684
685
686
687
688
691
692
695 if (d && !d->ref.deref())
702 d =
new QPalettePrivate;
706
707
708
709
710
711QPalette &QPalette::operator=(
const QPalette &p)
714 currentGroup = p.currentGroup;
715 if (d && !d->ref.deref())
722
723
724
725
728
729
730QPalette::operator QVariant()
const
732 return QVariant::fromValue(*
this);
736
737
738
739
740
741
742
745
746
747
748
749
750
751
752const QBrush &QPalette::brush(ColorGroup gr, ColorRole cr)
const
754 Q_ASSERT(cr < NColorRoles);
755 if (gr >= (
int)NColorGroups) {
759 qWarning(
"QPalette::brush: Unknown ColorGroup: %d", (
int)gr);
763 return d->data->br[gr][cr];
767
768
769
770
771
772
773
776
777
778
779
780
781
782
783
784void QPalette::setBrush(ColorGroup cg, ColorRole cr,
const QBrush &b)
786 Q_ASSERT(cr < NColorRoles);
789 for (uint i = 0; i < NColorGroups; i++)
790 setBrush(ColorGroup(i), cr, b);
796 }
else if (cg >= NColorGroups) {
797 qWarning(
"QPalette::setBrush: Unknown ColorGroup: %d", cg);
801 const auto newResolveMask = d->resolveMask | ResolveMask(1) << QPalettePrivate::bitPosition(cg, cr);
802 const auto valueChanged = d->data->br[cg][cr] != b;
807 d->data->br[cg][cr] = b;
808 }
else if (d->resolveMask != newResolveMask) {
812 d->resolveMask = newResolveMask;
816
817
818
819
820
821
822
823
824
825
826
827
828
829bool QPalette::isBrushSet(ColorGroup cg, ColorRole cr)
const
838 if (cg >= NColorGroups) {
839 qWarning() <<
"Wrong color group:" << cg;
843 if (cr >= NColorRoles) {
844 qWarning() <<
"Wrong color role:" << cr;
848 return d->resolveMask & (ResolveMask(1) << QPalettePrivate::bitPosition(cg, cr));
852
853
854void QPalette::detach()
856 if (d->ref.loadRelaxed() != 1) {
857 QPalettePrivate *x =
new QPalettePrivate(d->data);
858 x->resolveMask = d->resolveMask;
863 d->detach_no = ++QPalettePrivate::qt_palette_private_count;
868
869
870
871
872
873
874
875
876
877
880
881
882
883
884
885
886
887
888
889
890
891bool QPalette::operator==(
const QPalette &p)
const
893 if (isCopyOf(p) || d->data == p.d->data)
895 for(
int grp = 0; grp < (
int)NColorGroups; grp++) {
896 for(
int role = 0; role < (
int)NColorRoles; role++) {
900 if (d->data->br[grp][role] != p.d->data->br[grp][role])
908
909
910
911
912
913bool QPalette::isEqual(QPalette::ColorGroup group1, QPalette::ColorGroup group2)
const
915 if (group1 >= (
int)NColorGroups) {
916 if (group1 == Current) {
917 group1 = currentGroup;
919 qWarning(
"QPalette::brush: Unknown ColorGroup(1): %d", (
int)group1);
923 if (group2 >= (
int)NColorGroups) {
924 if (group2 == Current) {
925 group2 = currentGroup;
927 qWarning(
"QPalette::brush: Unknown ColorGroup(2): %d", (
int)group2);
931 if (group1 == group2)
933 for(
int role = 0; role < (
int)NColorRoles; role++) {
934 if (d->data->br[group1][role] != d->data->br[group2][role])
941
942
943
944
945
946
947qint64 QPalette::cacheKey()
const
949 return (((qint64) d->data->ser_no) << 32) | ((qint64) (d->detach_no));
954 QPalette::ResolveMask mask = {0};
955 for (
int role = 0; role <
int(QPalette::NColorRoles); ++role) {
956 for (
int grp = 0; grp <
int(QPalette::NColorGroups); ++grp) {
957 mask |= (QPalette::ResolveMask(1) << QPalettePrivate::bitPosition(QPalette::ColorGroup(grp), QPalette::ColorRole(role)));
964
965
966
967
968
969
970QPalette QPalette::resolve(
const QPalette &other)
const
972 if ((*
this == other && d->resolveMask == other.d->resolveMask)
973 || d->resolveMask == 0) {
975 o.setResolveMask(d->resolveMask);
979 if (d->resolveMask == allResolveMask())
982 QPalette palette(*
this);
985 for (
int role = 0; role <
int(NColorRoles); ++role) {
990 for (
int grp = 0; grp <
int(NColorGroups); ++grp) {
991 if (!(d->resolveMask & (ResolveMask(1) << QPalettePrivate::bitPosition(ColorGroup(grp), ColorRole(role))))) {
992 palette.d->data.detach();
993 palette.d->data->br[grp][role] = other.d->data->br[grp][role];
998 palette.d->resolveMask |= other.d->resolveMask;
1004
1005
1006QPalette::ResolveMask QPalette::resolveMask()
const
1008 return d->resolveMask;
1012
1013
1014void QPalette::setResolveMask(QPalette::ResolveMask mask)
1016 if (mask == d->resolveMask)
1020 d->resolveMask = mask;
1024
1025
1026
1027
1028
1029
1032
1033
1035#ifndef QT_NO_DATASTREAM
1038static const int oldRoles[7] = { QPalette::WindowText, QPalette::Window, QPalette::Light,
1039 QPalette::Dark, QPalette::Mid, QPalette::Text, QPalette::Base };
1042
1043
1044
1045
1046
1047
1048
1050QDataStream &operator<<(QDataStream &s,
const QPalette &p)
1052 for (
int grp = 0; grp < (
int)QPalette::NColorGroups; grp++) {
1053 if (s.version() == 1) {
1056 s << p.d->data->br[grp][
oldRoles[i]].color();
1058 int max = (
int)QPalette::NColorRoles;
1059 if (s.version() <= QDataStream::Qt_2_1)
1060 max = QPalette::HighlightedText + 1;
1061 else if (s.version() <= QDataStream::Qt_4_3)
1062 max = QPalette::AlternateBase + 1;
1063 else if (s.version() <= QDataStream::Qt_5_11)
1064 max = QPalette::ToolTipText + 1;
1065 else if (s.version() <= QDataStream::Qt_6_5)
1066 max = QPalette::PlaceholderText + 1;
1068 for (
int r = 0; r < max; r++)
1069 s << p.d->data->br[grp][r];
1080 pal.setColor(grp, (QPalette::ColorRole)oldRoles[i], col);
1085
1086
1087
1088
1089
1090
1091
1095 if (s.version() == 1) {
1097 readV1ColorGroup(s, p, QPalette::Active);
1098 readV1ColorGroup(s, p, QPalette::Disabled);
1099 readV1ColorGroup(s, p, QPalette::Inactive);
1101 int max = QPalette::NColorRoles;
1102 if (s.version() <= QDataStream::Qt_2_1) {
1104 max = QPalette::HighlightedText + 1;
1105 }
else if (s.version() <= QDataStream::Qt_4_3) {
1107 max = QPalette::AlternateBase + 1;
1108 }
else if (s.version() <= QDataStream::Qt_5_11) {
1110 max = QPalette::ToolTipText + 1;
1111 }
else if (s.version() <= QDataStream::Qt_6_5) {
1113 max = QPalette::PlaceholderText + 1;
1118 for(
int grp = 0; grp < (
int)QPalette::NColorGroups; ++grp) {
1119 const QPalette::ColorGroup group =
static_cast<QPalette::ColorGroup>(grp);
1120 for(
int role = 0; role < max; ++role) {
1122 p.setBrush(group, (QPalette::ColorRole)role, tmp);
1126 if (s.version() < QDataStream::Qt_6_6)
1127 p.setBrush(group, QPalette::Accent, p.brush(group, QPalette::Highlight));
1136
1137
1138
1139
1140
1141
1142
1144bool QPalette::isCopyOf(
const QPalette &p)
const
1150
1151
1152
1153
1154
1155
1156
1157void QPalette::setColorGroup(ColorGroup cg,
const QBrush &windowText,
const QBrush &button,
1158 const QBrush &light,
const QBrush &dark,
const QBrush &mid,
1159 const QBrush &text,
const QBrush &bright_text,
const QBrush &base,
1160 const QBrush &window)
1162 QBrush alt_base = QBrush(qt_mix_colors(base.color(), button.color()));
1163 QBrush mid_light = QBrush(qt_mix_colors(button.color(), light.color()));
1164 QColor toolTipBase(255, 255, 220);
1165 QColor toolTipText(0, 0, 0);
1167 setColorGroup(cg, windowText, button, light, dark, mid, text, bright_text, base,
1168 alt_base, window, mid_light, text,
1169 QBrush(Qt::black), QBrush(Qt::darkBlue), QBrush(Qt::white),
1170 QBrush(Qt::blue), QBrush(Qt::magenta), QBrush(toolTipBase),
1171 QBrush(toolTipText));
1173 for (
int cr = Highlight; cr <= LinkVisited; ++cr) {
1175 for (
int group = Active; group < NColorGroups; ++group) {
1176 d->resolveMask &= ~(ResolveMask(1) << QPalettePrivate::bitPosition(ColorGroup(group), ColorRole(cr)));
1179 d->resolveMask &= ~(ResolveMask(1) << QPalettePrivate::bitPosition(ColorGroup(cg), ColorRole(cr)));
1187QPalette::setColorGroup(ColorGroup cg,
const QBrush &foreground,
const QBrush &button,
1188 const QBrush &light,
const QBrush &dark,
const QBrush &mid,
1189 const QBrush &text,
const QBrush &bright_text,
1190 const QBrush &base,
const QBrush &alternate_base,
1191 const QBrush &background,
const QBrush &midlight,
1192 const QBrush &button_text,
const QBrush &shadow,
1193 const QBrush &highlight,
const QBrush &highlighted_text,
1194 const QBrush &link,
const QBrush &link_visited)
1196 setColorGroup(cg, foreground, button, light, dark, mid,
1197 text, bright_text, base, alternate_base, background,
1198 midlight, button_text, shadow, highlight, highlighted_text,
1199 link, link_visited, background, foreground);
1203void QPalette::setColorGroup(ColorGroup cg,
const QBrush &foreground,
const QBrush &button,
1204 const QBrush &light,
const QBrush &dark,
const QBrush &mid,
1205 const QBrush &text,
const QBrush &bright_text,
1206 const QBrush &base,
const QBrush &alternate_base,
1207 const QBrush &background,
const QBrush &midlight,
1208 const QBrush &button_text,
const QBrush &shadow,
1209 const QBrush &highlight,
const QBrush &highlighted_text,
1210 const QBrush &link,
const QBrush &link_visited,
1211 const QBrush &toolTipBase,
const QBrush &toolTipText)
1213 setBrush(cg, WindowText, foreground);
1214 setBrush(cg, Button, button);
1215 setBrush(cg, Light, light);
1216 setBrush(cg, Dark, dark);
1217 setBrush(cg, Mid, mid);
1218 setBrush(cg, Text, text);
1219 setBrush(cg, BrightText, bright_text);
1220 setBrush(cg, Base, base);
1221 setBrush(cg, AlternateBase, alternate_base);
1222 setBrush(cg, Window, background);
1223 setBrush(cg, Midlight, midlight);
1224 setBrush(cg, ButtonText, button_text);
1225 setBrush(cg, Shadow, shadow);
1226 setBrush(cg, Highlight, highlight);
1227 setBrush(cg, HighlightedText, highlighted_text);
1228 setBrush(cg, Link, link);
1229 setBrush(cg, LinkVisited, link_visited);
1230 setBrush(cg, ToolTipBase, toolTipBase);
1231 setBrush(cg, ToolTipText, toolTipText);
1234#ifndef QT_NO_DEBUG_STREAM
1237 const auto groupEnum = QMetaEnum::fromType<QPalette::ColorGroup>();
1239 QString groupString;
1240 for (
int group = 0; group < QPalette::NColorGroups; ++group) {
1241 const auto cg = QPalette::ColorGroup(group);
1243 if (p.isBrushSet(cg, cr)) {
1244 const auto &color = p.color(cg, cr);
1245 groupString += QString::fromUtf8(groupEnum.valueToKey(cg)) + u':' +
1246 color.name(QColor::HexArgb) + u',';
1249 groupString.chop(1);
1256 const auto roleEnum = QMetaEnum::fromType<QPalette::ColorRole>();
1259 for (
int role = 0; role < QPalette::NColorRoles; ++role) {
1260 const auto cr = QPalette::ColorRole(role);
1262 auto groupString = groupsToString(p, cr);
1263 if (!groupString.isEmpty())
1264 roleString += QString::fromUtf8(roleEnum.valueToKey(cr)) + QStringLiteral(
":[") +
1265 groupString + QStringLiteral(
"],");
1274 QDebugStateSaver saver(dbg);
1277 dbg <<
"QPalette(resolve=" << Qt::hex << Qt::showbase << p.resolveMask();
1279 auto roleString = rolesToString(p);
1280 if (!roleString.isEmpty())
1281 dbg <<
',' << roleString;
1291#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)