| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2007 Matthew Woehlke <mw_triad@users.sourceforge.net> |
| 3 | |
| 4 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 | */ |
| 6 | |
| 7 | #include "kstatefulbrush.h" |
| 8 | |
| 9 | #include "kcolorschemehelpers_p.h" |
| 10 | |
| 11 | #include <array> |
| 12 | |
| 13 | class KStatefulBrushPrivate |
| 14 | { |
| 15 | public: |
| 16 | std::array<QBrush, QPalette::NColorGroups> brushes; |
| 17 | }; |
| 18 | |
| 19 | KStatefulBrush::KStatefulBrush() |
| 20 | : d(std::make_unique<KStatefulBrushPrivate>()) |
| 21 | { |
| 22 | } |
| 23 | |
| 24 | KStatefulBrush::~KStatefulBrush() = default; |
| 25 | |
| 26 | KStatefulBrush::KStatefulBrush(KColorScheme::ColorSet set, KColorScheme::ForegroundRole role, KSharedConfigPtr config) |
| 27 | : KStatefulBrush() |
| 28 | { |
| 29 | d->brushes[QPalette::Active] = KColorScheme(QPalette::Active, set, config).foreground(role); |
| 30 | d->brushes[QPalette::Disabled] = KColorScheme(QPalette::Disabled, set, config).foreground(role); |
| 31 | d->brushes[QPalette::Inactive] = KColorScheme(QPalette::Inactive, set, config).foreground(role); |
| 32 | } |
| 33 | |
| 34 | KStatefulBrush::KStatefulBrush(KColorScheme::ColorSet set, KColorScheme::BackgroundRole role, KSharedConfigPtr config) |
| 35 | : KStatefulBrush() |
| 36 | |
| 37 | { |
| 38 | d->brushes[QPalette::Active] = KColorScheme(QPalette::Active, set, config).background(role); |
| 39 | d->brushes[QPalette::Disabled] = KColorScheme(QPalette::Disabled, set, config).background(role); |
| 40 | d->brushes[QPalette::Inactive] = KColorScheme(QPalette::Inactive, set, config).background(role); |
| 41 | } |
| 42 | |
| 43 | KStatefulBrush::KStatefulBrush(KColorScheme::ColorSet set, KColorScheme::DecorationRole role, KSharedConfigPtr config) |
| 44 | : KStatefulBrush() |
| 45 | { |
| 46 | d->brushes[QPalette::Active] = KColorScheme(QPalette::Active, set, config).decoration(role); |
| 47 | d->brushes[QPalette::Disabled] = KColorScheme(QPalette::Disabled, set, config).decoration(role); |
| 48 | d->brushes[QPalette::Inactive] = KColorScheme(QPalette::Inactive, set, config).decoration(role); |
| 49 | } |
| 50 | |
| 51 | KStatefulBrush::KStatefulBrush(const QBrush &brush, KSharedConfigPtr config) |
| 52 | : KStatefulBrush() |
| 53 | { |
| 54 | if (!config) { |
| 55 | config = defaultConfig(); |
| 56 | } |
| 57 | d->brushes[QPalette::Active] = brush; |
| 58 | d->brushes[QPalette::Disabled] = StateEffects(QPalette::Disabled, config).brush(background: brush); |
| 59 | d->brushes[QPalette::Inactive] = StateEffects(QPalette::Inactive, config).brush(background: brush); |
| 60 | } |
| 61 | |
| 62 | KStatefulBrush::KStatefulBrush(const QBrush &brush, const QBrush &background, KSharedConfigPtr config) |
| 63 | : KStatefulBrush() |
| 64 | { |
| 65 | if (!config) { |
| 66 | config = defaultConfig(); |
| 67 | } |
| 68 | d->brushes[QPalette::Active] = brush; |
| 69 | d->brushes[QPalette::Disabled] = StateEffects(QPalette::Disabled, config).brush(foreground: brush, background); |
| 70 | d->brushes[QPalette::Inactive] = StateEffects(QPalette::Inactive, config).brush(foreground: brush, background); |
| 71 | } |
| 72 | |
| 73 | KStatefulBrush::KStatefulBrush(const KStatefulBrush &other) |
| 74 | : KStatefulBrush() |
| 75 | { |
| 76 | d->brushes[QPalette::Active] = other.d->brushes[QPalette::Active]; |
| 77 | d->brushes[QPalette::Disabled] = other.d->brushes[QPalette::Disabled]; |
| 78 | d->brushes[QPalette::Inactive] = other.d->brushes[QPalette::Inactive]; |
| 79 | } |
| 80 | |
| 81 | KStatefulBrush &KStatefulBrush::operator=(const KStatefulBrush &other) |
| 82 | { |
| 83 | d->brushes[QPalette::Active] = other.d->brushes[QPalette::Active]; |
| 84 | d->brushes[QPalette::Disabled] = other.d->brushes[QPalette::Disabled]; |
| 85 | d->brushes[QPalette::Inactive] = other.d->brushes[QPalette::Inactive]; |
| 86 | return *this; |
| 87 | } |
| 88 | |
| 89 | QBrush KStatefulBrush::brush(QPalette::ColorGroup state) const |
| 90 | { |
| 91 | if (state >= QPalette::Active && state < QPalette::NColorGroups) { |
| 92 | return d->brushes[state]; |
| 93 | } else { |
| 94 | return d->brushes[QPalette::Active]; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | QBrush KStatefulBrush::brush(const QPalette &pal) const |
| 99 | { |
| 100 | return brush(state: pal.currentColorGroup()); |
| 101 | } |
| 102 | |