]>
cloud.milkyroute.net Git - dolphin.git/blob - src/selectionmode/backgroundcolorhelper.cpp
799db43d596dfe20a72f6365f55730e78bcb7d6e
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2022 Felix Ernst <felixernst@zohomail.eu>
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
8 #include "backgroundcolorhelper.h"
10 #include <KColorScheme>
12 #include <QGuiApplication>
16 using namespace SelectionMode
;
18 BackgroundColorHelper
*BackgroundColorHelper::instance()
21 s_instance
= new BackgroundColorHelper
;
27 void setBackgroundColorForWidget(QWidget
*widget
, QColor color
)
30 palette
.setBrush(QPalette::Active
, QPalette::Window
, color
);
31 palette
.setBrush(QPalette::Inactive
, QPalette::Window
, color
);
32 palette
.setBrush(QPalette::Disabled
, QPalette::Window
, color
);
33 widget
->setAutoFillBackground(true);
34 widget
->setPalette(palette
);
37 void BackgroundColorHelper::controlBackgroundColor(QWidget
*widget
)
39 setBackgroundColorForWidget(widget
, m_backgroundColor
);
41 Q_ASSERT_X(std::find(m_colorControlledWidgets
.begin(), m_colorControlledWidgets
.end(), widget
) == m_colorControlledWidgets
.end(), "controlBackgroundColor",
42 "Duplicate insertion is not necessary because the background color should already automatically update itself on paletteChanged");
43 m_colorControlledWidgets
.emplace_back(widget
);
46 BackgroundColorHelper::BackgroundColorHelper()
48 updateBackgroundColor();
49 QObject::connect(qApp
, &QGuiApplication::paletteChanged
, [=](){ slotPaletteChanged(); });
52 void BackgroundColorHelper::slotPaletteChanged()
54 updateBackgroundColor();
55 for (auto i
= m_colorControlledWidgets
.begin(); i
!= m_colorControlledWidgets
.end(); ++i
) {
57 i
= m_colorControlledWidgets
.erase(i
);
58 if (i
== m_colorControlledWidgets
.end()) {
62 setBackgroundColorForWidget(*i
, m_backgroundColor
);
66 void BackgroundColorHelper::updateBackgroundColor()
68 // We use colors from the color scheme for mixing so it fits the theme.
69 const auto colorScheme
= KColorScheme(QPalette::Normal
, KColorScheme::Window
);
70 const auto activeBackgroundColor
= colorScheme
.background(KColorScheme::BackgroundRole::ActiveBackground
).color();
71 // We use the positive color for mixing so the end product doesn't look like a warning or error.
72 const auto positiveBackgroundColor
= colorScheme
.background(KColorScheme::BackgroundRole::PositiveBackground
).color();
74 // Make sure the new background color has a meaningfully different hue than the activeBackgroundColor.
75 const int hueDifference
= positiveBackgroundColor
.hue() - activeBackgroundColor
.hue();
77 if (std::abs(hueDifference
) > 80) {
78 newHue
= (activeBackgroundColor
.hue() + positiveBackgroundColor
.hue()) / 2;
80 newHue
= hueDifference
> 0 ?
81 activeBackgroundColor
.hue() + 40 :
82 activeBackgroundColor
.hue() - 40;
83 newHue
%= 360; // hue needs to be between 0 and 359 per Qt documentation.
86 m_backgroundColor
= QColor::fromHsv(newHue
,
87 // Saturation should be closer to the saturation of the active color
88 // because otherwise the selection mode color might overpower it.
89 .7 * activeBackgroundColor
.saturation() + .3 * positiveBackgroundColor
.saturation(),
90 (activeBackgroundColor
.value() + positiveBackgroundColor
.value()) / 2,
91 (activeBackgroundColor
.alpha() + positiveBackgroundColor
.alpha()) / 2);
94 BackgroundColorHelper
*BackgroundColorHelper::s_instance
= nullptr;