File indexing completed on 2024-11-24 03:36:14

0001 /*
0002     SPDX-FileCopyrightText: 2007 Matthew Woehlke <mw_triad@users.sourceforge.net>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "kstatefulbrush.h"
0008 
0009 #include "kcolorschemehelpers_p.h"
0010 
0011 #include <array>
0012 
0013 class KStatefulBrushPrivate
0014 {
0015 public:
0016     std::array<QBrush, QPalette::NColorGroups> brushes;
0017 };
0018 
0019 KStatefulBrush::KStatefulBrush()
0020     : d(std::make_unique<KStatefulBrushPrivate>())
0021 {
0022 }
0023 
0024 KStatefulBrush::~KStatefulBrush() = default;
0025 
0026 KStatefulBrush::KStatefulBrush(KColorScheme::ColorSet set, KColorScheme::ForegroundRole role, KSharedConfigPtr config)
0027     : KStatefulBrush()
0028 {
0029     d->brushes[QPalette::Active] = KColorScheme(QPalette::Active, set, config).foreground(role);
0030     d->brushes[QPalette::Disabled] = KColorScheme(QPalette::Disabled, set, config).foreground(role);
0031     d->brushes[QPalette::Inactive] = KColorScheme(QPalette::Inactive, set, config).foreground(role);
0032 }
0033 
0034 KStatefulBrush::KStatefulBrush(KColorScheme::ColorSet set, KColorScheme::BackgroundRole role, KSharedConfigPtr config)
0035     : KStatefulBrush()
0036 
0037 {
0038     d->brushes[QPalette::Active] = KColorScheme(QPalette::Active, set, config).background(role);
0039     d->brushes[QPalette::Disabled] = KColorScheme(QPalette::Disabled, set, config).background(role);
0040     d->brushes[QPalette::Inactive] = KColorScheme(QPalette::Inactive, set, config).background(role);
0041 }
0042 
0043 KStatefulBrush::KStatefulBrush(KColorScheme::ColorSet set, KColorScheme::DecorationRole role, KSharedConfigPtr config)
0044     : KStatefulBrush()
0045 {
0046     d->brushes[QPalette::Active] = KColorScheme(QPalette::Active, set, config).decoration(role);
0047     d->brushes[QPalette::Disabled] = KColorScheme(QPalette::Disabled, set, config).decoration(role);
0048     d->brushes[QPalette::Inactive] = KColorScheme(QPalette::Inactive, set, config).decoration(role);
0049 }
0050 
0051 KStatefulBrush::KStatefulBrush(const QBrush &brush, KSharedConfigPtr config)
0052     : KStatefulBrush()
0053 {
0054     if (!config) {
0055         config = defaultConfig();
0056     }
0057     d->brushes[QPalette::Active] = brush;
0058     d->brushes[QPalette::Disabled] = StateEffects(QPalette::Disabled, config).brush(brush);
0059     d->brushes[QPalette::Inactive] = StateEffects(QPalette::Inactive, config).brush(brush);
0060 }
0061 
0062 KStatefulBrush::KStatefulBrush(const QBrush &brush, const QBrush &background, KSharedConfigPtr config)
0063     : KStatefulBrush()
0064 {
0065     if (!config) {
0066         config = defaultConfig();
0067     }
0068     d->brushes[QPalette::Active] = brush;
0069     d->brushes[QPalette::Disabled] = StateEffects(QPalette::Disabled, config).brush(brush, background);
0070     d->brushes[QPalette::Inactive] = StateEffects(QPalette::Inactive, config).brush(brush, background);
0071 }
0072 
0073 KStatefulBrush::KStatefulBrush(const KStatefulBrush &other)
0074     : KStatefulBrush()
0075 {
0076     d->brushes[QPalette::Active] = other.d->brushes[QPalette::Active];
0077     d->brushes[QPalette::Disabled] = other.d->brushes[QPalette::Disabled];
0078     d->brushes[QPalette::Inactive] = other.d->brushes[QPalette::Inactive];
0079 }
0080 
0081 KStatefulBrush &KStatefulBrush::operator=(const KStatefulBrush &other)
0082 {
0083     d->brushes[QPalette::Active] = other.d->brushes[QPalette::Active];
0084     d->brushes[QPalette::Disabled] = other.d->brushes[QPalette::Disabled];
0085     d->brushes[QPalette::Inactive] = other.d->brushes[QPalette::Inactive];
0086     return *this;
0087 }
0088 
0089 QBrush KStatefulBrush::brush(QPalette::ColorGroup state) const
0090 {
0091     if (state >= QPalette::Active && state < QPalette::NColorGroups) {
0092         return d->brushes[state];
0093     } else {
0094         return d->brushes[QPalette::Active];
0095     }
0096 }
0097 
0098 QBrush KStatefulBrush::brush(const QPalette &pal) const
0099 {
0100     return brush(pal.currentColorGroup());
0101 }