File indexing completed on 2024-04-14 14:18:51

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 #if KCONFIGWIDGETS_BUILD_DEPRECATED_SINCE(5, 84)
0012 #include <QWidget>
0013 #endif
0014 
0015 #include <array>
0016 
0017 class KStatefulBrushPrivate
0018 {
0019 public:
0020     std::array<QBrush, QPalette::NColorGroups> brushes;
0021 };
0022 
0023 KStatefulBrush::KStatefulBrush()
0024     : d(std::make_unique<KStatefulBrushPrivate>())
0025 {
0026 }
0027 
0028 KStatefulBrush::~KStatefulBrush() = default;
0029 
0030 KStatefulBrush::KStatefulBrush(KColorScheme::ColorSet set, KColorScheme::ForegroundRole role, KSharedConfigPtr config)
0031     : KStatefulBrush()
0032 {
0033     d->brushes[QPalette::Active] = KColorScheme(QPalette::Active, set, config).foreground(role);
0034     d->brushes[QPalette::Disabled] = KColorScheme(QPalette::Disabled, set, config).foreground(role);
0035     d->brushes[QPalette::Inactive] = KColorScheme(QPalette::Inactive, set, config).foreground(role);
0036 }
0037 
0038 KStatefulBrush::KStatefulBrush(KColorScheme::ColorSet set, KColorScheme::BackgroundRole role, KSharedConfigPtr config)
0039     : KStatefulBrush()
0040 
0041 {
0042     d->brushes[QPalette::Active] = KColorScheme(QPalette::Active, set, config).background(role);
0043     d->brushes[QPalette::Disabled] = KColorScheme(QPalette::Disabled, set, config).background(role);
0044     d->brushes[QPalette::Inactive] = KColorScheme(QPalette::Inactive, set, config).background(role);
0045 }
0046 
0047 KStatefulBrush::KStatefulBrush(KColorScheme::ColorSet set, KColorScheme::DecorationRole role, KSharedConfigPtr config)
0048     : KStatefulBrush()
0049 {
0050     d->brushes[QPalette::Active] = KColorScheme(QPalette::Active, set, config).decoration(role);
0051     d->brushes[QPalette::Disabled] = KColorScheme(QPalette::Disabled, set, config).decoration(role);
0052     d->brushes[QPalette::Inactive] = KColorScheme(QPalette::Inactive, set, config).decoration(role);
0053 }
0054 
0055 KStatefulBrush::KStatefulBrush(const QBrush &brush, KSharedConfigPtr config)
0056     : KStatefulBrush()
0057 {
0058     if (!config) {
0059         config = defaultConfig();
0060     }
0061     d->brushes[QPalette::Active] = brush;
0062     d->brushes[QPalette::Disabled] = StateEffects(QPalette::Disabled, config).brush(brush);
0063     d->brushes[QPalette::Inactive] = StateEffects(QPalette::Inactive, config).brush(brush);
0064 }
0065 
0066 KStatefulBrush::KStatefulBrush(const QBrush &brush, const QBrush &background, KSharedConfigPtr config)
0067     : KStatefulBrush()
0068 {
0069     if (!config) {
0070         config = defaultConfig();
0071     }
0072     d->brushes[QPalette::Active] = brush;
0073     d->brushes[QPalette::Disabled] = StateEffects(QPalette::Disabled, config).brush(brush, background);
0074     d->brushes[QPalette::Inactive] = StateEffects(QPalette::Inactive, config).brush(brush, background);
0075 }
0076 
0077 KStatefulBrush::KStatefulBrush(const KStatefulBrush &other)
0078     : KStatefulBrush()
0079 {
0080     d->brushes[QPalette::Active] = other.d->brushes[QPalette::Active];
0081     d->brushes[QPalette::Disabled] = other.d->brushes[QPalette::Disabled];
0082     d->brushes[QPalette::Inactive] = other.d->brushes[QPalette::Inactive];
0083 }
0084 
0085 KStatefulBrush &KStatefulBrush::operator=(const KStatefulBrush &other)
0086 {
0087     d->brushes[QPalette::Active] = other.d->brushes[QPalette::Active];
0088     d->brushes[QPalette::Disabled] = other.d->brushes[QPalette::Disabled];
0089     d->brushes[QPalette::Inactive] = other.d->brushes[QPalette::Inactive];
0090     return *this;
0091 }
0092 
0093 QBrush KStatefulBrush::brush(QPalette::ColorGroup state) const
0094 {
0095     if (state >= QPalette::Active && state < QPalette::NColorGroups) {
0096         return d->brushes[state];
0097     } else {
0098         return d->brushes[QPalette::Active];
0099     }
0100 }
0101 
0102 QBrush KStatefulBrush::brush(const QPalette &pal) const
0103 {
0104     return brush(pal.currentColorGroup());
0105 }
0106 
0107 #if KCONFIGWIDGETS_BUILD_DEPRECATED_SINCE(5, 84)
0108 QBrush KStatefulBrush::brush(const QWidget *widget) const
0109 {
0110     if (widget) {
0111         return brush(widget->palette());
0112     } else {
0113         return QBrush();
0114     }
0115 }
0116 #endif