10#include <QtCore/qmetaobject.h>
14static_assert(QPalettePrivate::bitPosition(QPalette::ColorGroup(QPalette::NColorGroups - 1),
15 QPalette::ColorRole(QPalette::NColorRoles - 1))
16 <
sizeof(QPalette::ResolveMask) * CHAR_BIT,
17 "The resolve mask type is not wide enough to fit the entire bit mask.");
21 return QColor((a.red() + b.red()) / 2, (a.green() + b.green()) / 2,
22 (a.blue() + b.blue()) / 2, (a.alpha() + b.alpha()) / 2);
26
27
28
29
30
31
32
33
34
37 if (alpha < 0
or alpha > 100)
40 for (
int cg = 0; cg <
int(QPalette::NColorGroups); ++cg) {
41 const QPalette::ColorGroup group = QPalette::ColorGroup(cg);
44 if (!pal.isBrushSet(group, QPalette::PlaceholderText)) {
45 QColor c = pal.color(group, QPalette::Text);
46 const int a = (c.alpha() * alpha) / 100;
48 pal.setColor(group, QPalette::PlaceholderText, c);
56 const int lighter = pal.base().color().lightness() > pal.text().color().lightness() ? 130 : 70;
59 for (
int i = 0; i < QPalette::NColorGroups; ++i) {
60 const QPalette::ColorGroup group =
static_cast<QPalette::ColorGroup>(i);
61 if (!pal.isBrushSet(group, QPalette::Accent)) {
63 const QBrush accentBrush = pal.isBrushSet(group, QPalette::Highlight)
64 ? pal.brush(group, QPalette::Highlight)
65 : pal.brush(group, QPalette::Base).color().lighter(lighter);
66 pal.setBrush(group, QPalette::Accent, accentBrush);
74 button.getHsv(&h, &s, &v);
76 const QBrush whiteBrush = QBrush(Qt::white);
77 const QBrush blackBrush = QBrush(Qt::black);
78 const QBrush baseBrush = v > 128 ? whiteBrush : blackBrush;
79 const QBrush foregroundBrush = v > 128 ? blackBrush : whiteBrush;
80 const QBrush buttonBrush = QBrush(button);
81 const QBrush buttonBrushDark = QBrush(button.darker());
82 const QBrush buttonBrushDark150 = QBrush(button.darker(150));
83 const QBrush buttonBrushLight150 = QBrush(button.lighter(150));
84 pal.setColorGroup(QPalette::Active, foregroundBrush, buttonBrush, buttonBrushLight150,
85 buttonBrushDark, buttonBrushDark150, foregroundBrush, whiteBrush,
86 baseBrush, buttonBrush);
87 pal.setColorGroup(QPalette::Inactive, foregroundBrush, buttonBrush, buttonBrushLight150,
88 buttonBrushDark, buttonBrushDark150, foregroundBrush, whiteBrush,
89 baseBrush, buttonBrush);
90 pal.setColorGroup(QPalette::Disabled, buttonBrushDark, buttonBrush, buttonBrushLight150,
91 buttonBrushDark, buttonBrushDark150, buttonBrushDark,
92 whiteBrush, buttonBrush, buttonBrush);
94 qt_placeholder_from_text(pal);
95 qt_ensure_default_accent_color(pal);
99
100
101
102
103
104
107
108
109
110
111
112
113
114
115
118
119
120
121
122
123
124
125
126
129
130
131
132
133
134
135
136
137
140
141
142
143
144
145
146
149
150
151
152
153
154
155
158
159
160
161
162
163
166
167
168
169
170
171
174
175
176
177
178
179
182
183
184
185
186
187
190
191
192
193
194
195
198
199
200
201
202
203
206
207
208
209
210
211
214
215
216
217
218
219
222
223
224
225
226
227
230
231
232
233
234
235
238
239
240
241
242
243
244
245
246
247
248
251
252
253
254
255
256
257
258
259
260
261
264
265
266
267
268
269
270
273
274
275
276
277
278
281
282
283
284
285
286
289
290
291
292
293
294
297
298
299
300
301
302
303
306
307
308
309
310
311
314
315
316
317
318
319
322
323
324
325
326
327
328
329
330
331
334
335
336
337
340
341
342
343
346
347
348
349
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
413
414
415
416
417
418
419
420
421
422
423
426
427
428
429
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
537
538
539
540
541
542
543
550 if (QGuiApplicationPrivate::app_pal) {
551 d = QGuiApplicationPrivate::app_pal->d;
556 qt_palette_from_color(*
this, Qt::black);
562
563
564
565
566QPalette::QPalette(
const QColor &button)
569 qt_palette_from_color(*
this, button);
573
574
575
576
577QPalette::QPalette(Qt::GlobalColor button)
580 qt_palette_from_color(*
this, button);
584
585
586
587
588
589
590QPalette::QPalette(
const QBrush &windowText,
const QBrush &button,
591 const QBrush &light,
const QBrush &dark,
592 const QBrush &mid,
const QBrush &text,
593 const QBrush &bright_text,
const QBrush &base,
594 const QBrush &window)
597 setColorGroup(All, windowText, button, light, dark, mid, text, bright_text,
600 qt_placeholder_from_text(*
this);
601 qt_ensure_default_accent_color(*
this);
606
607
608
609
610
611
612QPalette::QPalette(
const QColor &windowText,
const QColor &window,
613 const QColor &light,
const QColor &dark,
const QColor &mid,
614 const QColor &text,
const QColor &base)
617 const QBrush windowBrush(window);
618 const QBrush lightBrush(light);
619 setColorGroup(All, QBrush(windowText), windowBrush, lightBrush,
620 QBrush(dark), QBrush(mid), QBrush(text), lightBrush,
621 QBrush(base), windowBrush);
625
626
627
628
629QPalette::QPalette(
const QColor &button,
const QColor &window)
633 window.getHsv(&h, &s, &v);
635 const QBrush windowBrush = QBrush(window);
636 const QBrush whiteBrush = QBrush(Qt::white);
637 const QBrush blackBrush = QBrush(Qt::black);
638 const QBrush baseBrush = v > 128 ? whiteBrush : blackBrush;
639 const QBrush foregroundBrush = v > 128 ? blackBrush : whiteBrush;
640 const QBrush disabledForeground = QBrush(Qt::darkGray);
642 const QBrush buttonBrush = QBrush(button);
643 const QBrush buttonBrushDark = QBrush(button.darker());
644 const QBrush buttonBrushDark150 = QBrush(button.darker(150));
645 const QBrush buttonBrushLight150 = QBrush(button.lighter(150));
648 setColorGroup(Inactive, foregroundBrush, buttonBrush, buttonBrushLight150, buttonBrushDark,
649 buttonBrushDark150, foregroundBrush, whiteBrush, baseBrush,
651 setColorGroup(Active, foregroundBrush, buttonBrush, buttonBrushLight150, buttonBrushDark,
652 buttonBrushDark150, foregroundBrush, whiteBrush, baseBrush,
654 setColorGroup(Disabled, disabledForeground, buttonBrush, buttonBrushLight150,
655 buttonBrushDark, buttonBrushDark150, disabledForeground,
656 whiteBrush, baseBrush, windowBrush);
658 qt_placeholder_from_text(*
this);
659 qt_ensure_default_accent_color(*
this);
663
664
665
666
667QPalette::QPalette(
const QPalette &p)
668 : d(p.d), currentGroup(p.currentGroup)
674
675
676
677
678
679
680
681
682
685
686
689 if (d && !d->ref.deref())
696 d =
new QPalettePrivate;
700
701
702
703
704
705QPalette &QPalette::operator=(
const QPalette &p)
708 currentGroup = p.currentGroup;
709 if (d && !d->ref.deref())
716
717
718
719
722
723
724QPalette::operator QVariant()
const
726 return QVariant::fromValue(*
this);
730
731
732
733
734
735
736
739
740
741
742
743
744
745
746const QBrush &QPalette::brush(ColorGroup gr, ColorRole cr)
const
748 Q_ASSERT(cr < NColorRoles);
749 if (gr >= (
int)NColorGroups) {
753 qWarning(
"QPalette::brush: Unknown ColorGroup: %d", (
int)gr);
757 return d->data->br[gr][cr];
761
762
763
764
765
766
767
770
771
772
773
774
775
776
777
778void QPalette::setBrush(ColorGroup cg, ColorRole cr,
const QBrush &b)
780 Q_ASSERT(cr < NColorRoles);
783 for (uint i = 0; i < NColorGroups; i++)
784 setBrush(ColorGroup(i), cr, b);
790 }
else if (cg >= NColorGroups) {
791 qWarning(
"QPalette::setBrush: Unknown ColorGroup: %d", cg);
795 const auto newResolveMask = d->resolveMask | ResolveMask(1) << QPalettePrivate::bitPosition(cg, cr);
796 const auto valueChanged = d->data->br[cg][cr] != b;
801 d->data->br[cg][cr] = b;
802 }
else if (d->resolveMask != newResolveMask) {
806 d->resolveMask = newResolveMask;
810
811
812
813
814
815
816
817
818
819
820
821
822
823bool QPalette::isBrushSet(ColorGroup cg, ColorRole cr)
const
832 if (cg >= NColorGroups) {
833 qWarning() <<
"Wrong color group:" << cg;
837 if (cr >= NColorRoles) {
838 qWarning() <<
"Wrong color role:" << cr;
842 return d->resolveMask & (ResolveMask(1) << QPalettePrivate::bitPosition(cg, cr));
846
847
848void QPalette::detach()
850 if (d->ref.loadRelaxed() != 1) {
851 QPalettePrivate *x =
new QPalettePrivate(d->data);
852 x->resolveMask = d->resolveMask;
857 d->detach_no = ++QPalettePrivate::qt_palette_private_count;
862
863
864
865
866
867
868
869
870
871
874
875
876
877
878
879
880
881
882
883
884
885bool QPalette::operator==(
const QPalette &p)
const
887 if (isCopyOf(p) || d->data == p.d->data)
889 for(
int grp = 0; grp < (
int)NColorGroups; grp++) {
890 for(
int role = 0; role < (
int)NColorRoles; role++) {
894 if (d->data->br[grp][role] != p.d->data->br[grp][role])
902
903
904
905
906
907bool QPalette::isEqual(QPalette::ColorGroup group1, QPalette::ColorGroup group2)
const
909 if (group1 >= (
int)NColorGroups) {
910 if (group1 == Current) {
911 group1 = currentGroup;
913 qWarning(
"QPalette::brush: Unknown ColorGroup(1): %d", (
int)group1);
917 if (group2 >= (
int)NColorGroups) {
918 if (group2 == Current) {
919 group2 = currentGroup;
921 qWarning(
"QPalette::brush: Unknown ColorGroup(2): %d", (
int)group2);
925 if (group1 == group2)
927 for(
int role = 0; role < (
int)NColorRoles; role++) {
928 if (d->data->br[group1][role] != d->data->br[group2][role])
935
936
937
938
939
940
941qint64 QPalette::cacheKey()
const
943 return (((qint64) d->data->ser_no) << 32) | ((qint64) (d->detach_no));
948 QPalette::ResolveMask mask = {0};
949 for (
int role = 0; role <
int(QPalette::NColorRoles); ++role) {
950 for (
int grp = 0; grp <
int(QPalette::NColorGroups); ++grp) {
951 mask |= (QPalette::ResolveMask(1) << QPalettePrivate::bitPosition(QPalette::ColorGroup(grp), QPalette::ColorRole(role)));
958
959
960
961
962
963
964QPalette QPalette::resolve(
const QPalette &other)
const
966 if ((*
this == other && d->resolveMask == other.d->resolveMask)
967 || d->resolveMask == 0) {
969 o.setResolveMask(d->resolveMask);
973 if (d->resolveMask == allResolveMask())
976 QPalette palette(*
this);
979 for (
int role = 0; role <
int(NColorRoles); ++role) {
984 for (
int grp = 0; grp <
int(NColorGroups); ++grp) {
985 if (!(d->resolveMask & (ResolveMask(1) << QPalettePrivate::bitPosition(ColorGroup(grp), ColorRole(role))))) {
986 palette.d->data.detach();
987 palette.d->data->br[grp][role] = other.d->data->br[grp][role];
992 palette.d->resolveMask |= other.d->resolveMask;
998
999
1000QPalette::ResolveMask QPalette::resolveMask()
const
1002 return d->resolveMask;
1006
1007
1008void QPalette::setResolveMask(QPalette::ResolveMask mask)
1010 if (mask == d->resolveMask)
1014 d->resolveMask = mask;
1018
1019
1020
1021
1022
1023
1026
1027
1029#ifndef QT_NO_DATASTREAM
1032static const int oldRoles[7] = { QPalette::WindowText, QPalette::Window, QPalette::Light,
1033 QPalette::Dark, QPalette::Mid, QPalette::Text, QPalette::Base };
1036
1037
1038
1039
1040
1041
1042
1044QDataStream &operator<<(QDataStream &s,
const QPalette &p)
1046 for (
int grp = 0; grp < (
int)QPalette::NColorGroups; grp++) {
1047 if (s.version() == 1) {
1050 s << p.d->data->br[grp][
oldRoles[i]].color();
1052 int max = (
int)QPalette::NColorRoles;
1053 if (s.version() <= QDataStream::Qt_2_1)
1054 max = QPalette::HighlightedText + 1;
1055 else if (s.version() <= QDataStream::Qt_4_3)
1056 max = QPalette::AlternateBase + 1;
1057 else if (s.version() <= QDataStream::Qt_5_11)
1058 max = QPalette::ToolTipText + 1;
1059 else if (s.version() <= QDataStream::Qt_6_5)
1060 max = QPalette::PlaceholderText + 1;
1062 for (
int r = 0; r < max; r++)
1063 s << p.d->data->br[grp][r];
1074 pal.setColor(grp, (QPalette::ColorRole)oldRoles[i], col);
1079
1080
1081
1082
1083
1084
1085
1089 if (s.version() == 1) {
1091 readV1ColorGroup(s, p, QPalette::Active);
1092 readV1ColorGroup(s, p, QPalette::Disabled);
1093 readV1ColorGroup(s, p, QPalette::Inactive);
1095 int max = QPalette::NColorRoles;
1096 if (s.version() <= QDataStream::Qt_2_1) {
1098 max = QPalette::HighlightedText + 1;
1099 }
else if (s.version() <= QDataStream::Qt_4_3) {
1101 max = QPalette::AlternateBase + 1;
1102 }
else if (s.version() <= QDataStream::Qt_5_11) {
1104 max = QPalette::ToolTipText + 1;
1105 }
else if (s.version() <= QDataStream::Qt_6_5) {
1107 max = QPalette::PlaceholderText + 1;
1112 for(
int grp = 0; grp < (
int)QPalette::NColorGroups; ++grp) {
1113 const QPalette::ColorGroup group =
static_cast<QPalette::ColorGroup>(grp);
1114 for(
int role = 0; role < max; ++role) {
1116 p.setBrush(group, (QPalette::ColorRole)role, tmp);
1120 if (s.version() < QDataStream::Qt_6_6)
1121 p.setBrush(group, QPalette::Accent, p.brush(group, QPalette::Highlight));
1130
1131
1132
1133
1134
1135
1136
1138bool QPalette::isCopyOf(
const QPalette &p)
const
1144
1145
1146
1147
1148
1149
1150
1151void QPalette::setColorGroup(ColorGroup cg,
const QBrush &windowText,
const QBrush &button,
1152 const QBrush &light,
const QBrush &dark,
const QBrush &mid,
1153 const QBrush &text,
const QBrush &bright_text,
const QBrush &base,
1154 const QBrush &window)
1156 QBrush alt_base = QBrush(qt_mix_colors(base.color(), button.color()));
1157 QBrush mid_light = QBrush(qt_mix_colors(button.color(), light.color()));
1158 QColor toolTipBase(255, 255, 220);
1159 QColor toolTipText(0, 0, 0);
1161 setColorGroup(cg, windowText, button, light, dark, mid, text, bright_text, base,
1162 alt_base, window, mid_light, text,
1163 QBrush(Qt::black), QBrush(Qt::darkBlue), QBrush(Qt::white),
1164 QBrush(Qt::blue), QBrush(Qt::magenta), QBrush(toolTipBase),
1165 QBrush(toolTipText));
1167 for (
int cr = Highlight; cr <= LinkVisited; ++cr) {
1169 for (
int group = Active; group < NColorGroups; ++group) {
1170 d->resolveMask &= ~(ResolveMask(1) << QPalettePrivate::bitPosition(ColorGroup(group), ColorRole(cr)));
1173 d->resolveMask &= ~(ResolveMask(1) << QPalettePrivate::bitPosition(ColorGroup(cg), ColorRole(cr)));
1181QPalette::setColorGroup(ColorGroup cg,
const QBrush &foreground,
const QBrush &button,
1182 const QBrush &light,
const QBrush &dark,
const QBrush &mid,
1183 const QBrush &text,
const QBrush &bright_text,
1184 const QBrush &base,
const QBrush &alternate_base,
1185 const QBrush &background,
const QBrush &midlight,
1186 const QBrush &button_text,
const QBrush &shadow,
1187 const QBrush &highlight,
const QBrush &highlighted_text,
1188 const QBrush &link,
const QBrush &link_visited)
1190 setColorGroup(cg, foreground, button, light, dark, mid,
1191 text, bright_text, base, alternate_base, background,
1192 midlight, button_text, shadow, highlight, highlighted_text,
1193 link, link_visited, background, foreground);
1197void QPalette::setColorGroup(ColorGroup cg,
const QBrush &foreground,
const QBrush &button,
1198 const QBrush &light,
const QBrush &dark,
const QBrush &mid,
1199 const QBrush &text,
const QBrush &bright_text,
1200 const QBrush &base,
const QBrush &alternate_base,
1201 const QBrush &background,
const QBrush &midlight,
1202 const QBrush &button_text,
const QBrush &shadow,
1203 const QBrush &highlight,
const QBrush &highlighted_text,
1204 const QBrush &link,
const QBrush &link_visited,
1205 const QBrush &toolTipBase,
const QBrush &toolTipText)
1207 setBrush(cg, WindowText, foreground);
1208 setBrush(cg, Button, button);
1209 setBrush(cg, Light, light);
1210 setBrush(cg, Dark, dark);
1211 setBrush(cg, Mid, mid);
1212 setBrush(cg, Text, text);
1213 setBrush(cg, BrightText, bright_text);
1214 setBrush(cg, Base, base);
1215 setBrush(cg, AlternateBase, alternate_base);
1216 setBrush(cg, Window, background);
1217 setBrush(cg, Midlight, midlight);
1218 setBrush(cg, ButtonText, button_text);
1219 setBrush(cg, Shadow, shadow);
1220 setBrush(cg, Highlight, highlight);
1221 setBrush(cg, HighlightedText, highlighted_text);
1222 setBrush(cg, Link, link);
1223 setBrush(cg, LinkVisited, link_visited);
1224 setBrush(cg, ToolTipBase, toolTipBase);
1225 setBrush(cg, ToolTipText, toolTipText);
1228#ifndef QT_NO_DEBUG_STREAM
1231 const auto groupEnum = QMetaEnum::fromType<QPalette::ColorGroup>();
1233 QString groupString;
1234 for (
int group = 0; group < QPalette::NColorGroups; ++group) {
1235 const auto cg = QPalette::ColorGroup(group);
1237 if (p.isBrushSet(cg, cr)) {
1238 const auto &color = p.color(cg, cr);
1239 groupString += QString::fromUtf8(groupEnum.valueToKey(cg)) + u':' +
1240 color.name(QColor::HexArgb) + u',';
1243 groupString.chop(1);
1250 const auto roleEnum = QMetaEnum::fromType<QPalette::ColorRole>();
1253 for (
int role = 0; role < QPalette::NColorRoles; ++role) {
1254 const auto cr = QPalette::ColorRole(role);
1256 auto groupString = groupsToString(p, cr);
1257 if (!groupString.isEmpty())
1258 roleString += QString::fromUtf8(roleEnum.valueToKey(cr)) + QStringLiteral(
":[") +
1259 groupString + QStringLiteral(
"],");
1268 QDebugStateSaver saver(dbg);
1271 dbg <<
"QPalette(resolve=" << Qt::hex << Qt::showbase << p.resolveMask();
1273 auto roleString = rolesToString(p);
1274 if (!roleString.isEmpty())
1275 dbg <<
',' << roleString;
1285#include "moc_qpalette.cpp"
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)