File indexing completed on 2024-04-21 05:45:26

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 #ifndef BACKGROUNDCOLORHELPER_H
0009 #define BACKGROUNDCOLORHELPER_H
0010 
0011 #include <QColor>
0012 #include <QPointer>
0013 
0014 #include <memory>
0015 
0016 class QWidget;
0017 
0018 namespace SelectionMode
0019 {
0020 
0021 /**
0022  * @brief A Singleton class for managing the colors of selection mode widgets.
0023  */
0024 class BackgroundColorHelper
0025 {
0026 public:
0027     static BackgroundColorHelper *instance();
0028 
0029     /**
0030      * Changes the background color of @p widget to a distinct color scheme matching color which makes it clear that the widget belongs to the selection mode.
0031      * The background color of @p widget will from now on be updated automatically when the palette of the application changes.
0032      */
0033     void controlBackgroundColor(QWidget *widget);
0034 
0035 private:
0036     BackgroundColorHelper();
0037 
0038     /**
0039      * Called when the palette of the application changes.
0040      * Triggers updateBackgroundColor() and the updates the background color of m_colorControlledWidgets.
0041      * @see updateBackgroundColor
0042      */
0043     void slotPaletteChanged();
0044 
0045     /** Calculates a new m_colorControlledWidgets based on the current colour scheme of the application. */
0046     void updateBackgroundColor();
0047 
0048 private:
0049     /// The widgets who have given up control over the background color to BackgroundColorHelper.
0050     std::vector<QPointer<QWidget>> m_colorControlledWidgets;
0051     /// The color to be used for the widgets' backgrounds.
0052     QColor m_backgroundColor;
0053 
0054     /// Singleton object
0055     static BackgroundColorHelper *s_instance;
0056 };
0057 
0058 }
0059 
0060 #endif // BACKGROUNDCOLORHELPER_H