Warning, file /system/dolphin/src/selectionmode/backgroundcolorhelper.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 2022 Felix Ernst <felixernst@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #include "backgroundcolorhelper.h"
0009 
0010 #include <KColorScheme>
0011 
0012 #include <QGuiApplication>
0013 #include <QPalette>
0014 #include <QWidget>
0015 
0016 using namespace SelectionMode;
0017 
0018 BackgroundColorHelper *BackgroundColorHelper::instance()
0019 {
0020     if (!s_instance) {
0021         s_instance = new BackgroundColorHelper;
0022     }
0023     return s_instance;
0024 }
0025 
0026 void setBackgroundColorForWidget(QWidget *widget, QColor color)
0027 {
0028     QPalette palette;
0029     palette.setBrush(QPalette::Active, QPalette::Window, color);
0030     palette.setBrush(QPalette::Inactive, QPalette::Window, color);
0031     palette.setBrush(QPalette::Disabled, QPalette::Window, color);
0032     widget->setAutoFillBackground(true);
0033     widget->setPalette(palette);
0034 }
0035 
0036 void BackgroundColorHelper::controlBackgroundColor(QWidget *widget)
0037 {
0038     setBackgroundColorForWidget(widget, m_backgroundColor);
0039 
0040     Q_ASSERT_X(std::find(m_colorControlledWidgets.begin(), m_colorControlledWidgets.end(), widget) == m_colorControlledWidgets.end(),
0041                "controlBackgroundColor",
0042                "Duplicate insertion is not necessary because the background color should already automatically update itself on paletteChanged");
0043     m_colorControlledWidgets.emplace_back(widget);
0044 }
0045 
0046 BackgroundColorHelper::BackgroundColorHelper()
0047 {
0048     updateBackgroundColor();
0049     QObject::connect(qApp, &QGuiApplication::paletteChanged, qApp, [=]() {
0050         slotPaletteChanged();
0051     });
0052 }
0053 
0054 void BackgroundColorHelper::slotPaletteChanged()
0055 {
0056     updateBackgroundColor();
0057     for (auto i = m_colorControlledWidgets.begin(); i != m_colorControlledWidgets.end(); ++i) {
0058         while (!*i) {
0059             i = m_colorControlledWidgets.erase(i);
0060             if (i == m_colorControlledWidgets.end()) {
0061                 return;
0062             }
0063         }
0064         setBackgroundColorForWidget(*i, m_backgroundColor);
0065     }
0066 }
0067 
0068 void BackgroundColorHelper::updateBackgroundColor()
0069 {
0070     // We use colors from the color scheme for mixing so it fits the theme.
0071     const auto colorScheme = KColorScheme(QPalette::Normal, KColorScheme::Window);
0072     const auto activeBackgroundColor = colorScheme.background(KColorScheme::BackgroundRole::ActiveBackground).color();
0073     // We use the positive color for mixing so the end product doesn't look like a warning or error.
0074     const auto positiveBackgroundColor = colorScheme.background(KColorScheme::BackgroundRole::PositiveBackground).color();
0075 
0076     // Make sure the new background color has a meaningfully different hue than the activeBackgroundColor.
0077     const int hueDifference = positiveBackgroundColor.hue() - activeBackgroundColor.hue();
0078     int newHue;
0079     if (std::abs(hueDifference) > 80) {
0080         newHue = (activeBackgroundColor.hue() + positiveBackgroundColor.hue()) / 2;
0081     } else {
0082         newHue = hueDifference > 0 ? activeBackgroundColor.hue() + 40 : activeBackgroundColor.hue() - 40;
0083         newHue %= 360; // hue needs to be between 0 and 359 per Qt documentation.
0084     }
0085 
0086     m_backgroundColor = QColor::fromHsv(newHue,
0087                                         // Saturation should be closer to the saturation of the active color
0088                                         // because otherwise the selection mode color might overpower it.
0089                                         .7 * activeBackgroundColor.saturation() + .3 * positiveBackgroundColor.saturation(),
0090                                         (activeBackgroundColor.value() + positiveBackgroundColor.value()) / 2,
0091                                         (activeBackgroundColor.alpha() + positiveBackgroundColor.alpha()) / 2);
0092 }
0093 
0094 BackgroundColorHelper *BackgroundColorHelper::s_instance = nullptr;