]> cloud.milkyroute.net Git - dolphin.git/blob - src/selectionmode/backgroundcolorhelper.cpp
DolphinView: Remove -1 interval, add setAutoActivationEnabled
[dolphin.git] / src / selectionmode / backgroundcolorhelper.cpp
1 /*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2022 Felix Ernst <felixernst@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 */
7
8 #include "backgroundcolorhelper.h"
9
10 #include <KColorScheme>
11
12 #include <QGuiApplication>
13 #include <QPalette>
14 #include <QWidget>
15
16 using namespace SelectionMode;
17
18 BackgroundColorHelper *BackgroundColorHelper::instance()
19 {
20 if (!s_instance) {
21 s_instance = new BackgroundColorHelper;
22 }
23 return s_instance;
24 }
25
26 void setBackgroundColorForWidget(QWidget *widget, QColor color)
27 {
28 QPalette palette;
29 palette.setBrush(QPalette::Active, QPalette::Window, color);
30 palette.setBrush(QPalette::Inactive, QPalette::Window, color);
31 palette.setBrush(QPalette::Disabled, QPalette::Window, color);
32 widget->setAutoFillBackground(true);
33 widget->setPalette(palette);
34 }
35
36 void BackgroundColorHelper::controlBackgroundColor(QWidget *widget)
37 {
38 setBackgroundColorForWidget(widget, m_backgroundColor);
39
40 Q_ASSERT_X(std::find(m_colorControlledWidgets.begin(), m_colorControlledWidgets.end(), widget) == m_colorControlledWidgets.end(),
41 "controlBackgroundColor",
42 "Duplicate insertion is not necessary because the background color should already automatically update itself on paletteChanged");
43 m_colorControlledWidgets.emplace_back(widget);
44 }
45
46 bool BackgroundColorHelper::eventFilter(QObject *obj, QEvent *event)
47 {
48 Q_UNUSED(obj);
49 if (event->type() == QEvent::ApplicationPaletteChange) {
50 slotPaletteChanged();
51 }
52 return false;
53 }
54
55 BackgroundColorHelper::BackgroundColorHelper()
56 {
57 updateBackgroundColor();
58 qApp->installEventFilter(this);
59 }
60
61 void BackgroundColorHelper::slotPaletteChanged()
62 {
63 updateBackgroundColor();
64 for (auto i = m_colorControlledWidgets.begin(); i != m_colorControlledWidgets.end(); ++i) {
65 while (!*i) {
66 i = m_colorControlledWidgets.erase(i);
67 if (i == m_colorControlledWidgets.end()) {
68 return;
69 }
70 }
71 setBackgroundColorForWidget(*i, m_backgroundColor);
72 }
73 }
74
75 void BackgroundColorHelper::updateBackgroundColor()
76 {
77 // We use colors from the color scheme for mixing so it fits the theme.
78 const auto colorScheme = KColorScheme(QPalette::Normal, KColorScheme::Window);
79 const auto activeBackgroundColor = colorScheme.background(KColorScheme::BackgroundRole::ActiveBackground).color();
80 // We use the positive color for mixing so the end product doesn't look like a warning or error.
81 const auto positiveBackgroundColor = colorScheme.background(KColorScheme::BackgroundRole::PositiveBackground).color();
82
83 // Make sure the new background color has a meaningfully different hue than the activeBackgroundColor.
84 const int hueDifference = positiveBackgroundColor.hue() - activeBackgroundColor.hue();
85 int newHue;
86 if (std::abs(hueDifference) > 80) {
87 newHue = (activeBackgroundColor.hue() + positiveBackgroundColor.hue()) / 2;
88 } else {
89 newHue = hueDifference > 0 ? activeBackgroundColor.hue() + 40 : activeBackgroundColor.hue() - 40;
90 newHue %= 360; // hue needs to be between 0 and 359 per Qt documentation.
91 }
92
93 m_backgroundColor = QColor::fromHsv(newHue,
94 // Saturation should be closer to the saturation of the active color
95 // because otherwise the selection mode color might overpower it.
96 .7 * activeBackgroundColor.saturation() + .3 * positiveBackgroundColor.saturation(),
97 (activeBackgroundColor.value() + positiveBackgroundColor.value()) / 2,
98 (activeBackgroundColor.alpha() + positiveBackgroundColor.alpha()) / 2);
99 }
100
101 BackgroundColorHelper *BackgroundColorHelper::s_instance = nullptr;
102
103 #include "moc_backgroundcolorhelper.cpp"