]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/selectionmode/backgroundcolorhelper.cpp
SVN_SILENT made messages (.desktop file) - always resolve ours
[dolphin.git] / src / selectionmode / backgroundcolorhelper.cpp
index 4477d0f2c1dedb4376067a934032f344acd47f0c..42ccb64bd32d496b5dd2f10d406b621bf38d799d 100644 (file)
@@ -1,6 +1,6 @@
 /*
     This file is part of the KDE project
-    SPDX-FileCopyrightText: 2022 Felix Ernst <felixernst@zohomail.eu>
+    SPDX-FileCopyrightText: 2022 Felix Ernst <felixernst@kde.org>
 
     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
 */
@@ -11,7 +11,6 @@
 
 #include <QGuiApplication>
 #include <QPalette>
-#include <QtGlobal>
 #include <QWidget>
 
 using namespace SelectionMode;
@@ -24,11 +23,10 @@ BackgroundColorHelper *BackgroundColorHelper::instance()
     return s_instance;
 }
 
-
 void setBackgroundColorForWidget(QWidget *widget, QColor color)
 {
     QPalette palette;
-    palette.setBrush(QPalette::Active,   QPalette::Window, color);
+    palette.setBrush(QPalette::Active, QPalette::Window, color);
     palette.setBrush(QPalette::Inactive, QPalette::Window, color);
     palette.setBrush(QPalette::Disabled, QPalette::Window, color);
     widget->setAutoFillBackground(true);
@@ -39,24 +37,36 @@ void BackgroundColorHelper::controlBackgroundColor(QWidget *widget)
 {
     setBackgroundColorForWidget(widget, m_backgroundColor);
 
-    Q_ASSERT_X(std::find(m_colorControlledWidgets.begin(), m_colorControlledWidgets.end(), widget) == m_colorControlledWidgets.end(), "controlBackgroundColor",
+    Q_ASSERT_X(std::find(m_colorControlledWidgets.begin(), m_colorControlledWidgets.end(), widget) == m_colorControlledWidgets.end(),
+               "controlBackgroundColor",
                "Duplicate insertion is not necessary because the background color should already automatically update itself on paletteChanged");
     m_colorControlledWidgets.emplace_back(widget);
 }
 
+bool BackgroundColorHelper::eventFilter(QObject *obj, QEvent *event)
+{
+    Q_UNUSED(obj);
+    if (event->type() == QEvent::ApplicationPaletteChange) {
+        slotPaletteChanged();
+    }
+    return false;
+}
+
 BackgroundColorHelper::BackgroundColorHelper()
 {
     updateBackgroundColor();
-    QObject::connect(qApp, &QGuiApplication::paletteChanged, [=](){ slotPaletteChanged(); });
+    qApp->installEventFilter(this);
 }
 
 void BackgroundColorHelper::slotPaletteChanged()
 {
     updateBackgroundColor();
     for (auto i = m_colorControlledWidgets.begin(); i != m_colorControlledWidgets.end(); ++i) {
-        if (!*i) {
+        while (!*i) {
             i = m_colorControlledWidgets.erase(i);
-            continue;
+            if (i == m_colorControlledWidgets.end()) {
+                return;
+            }
         }
         setBackgroundColorForWidget(*i, m_backgroundColor);
     }
@@ -76,17 +86,18 @@ void BackgroundColorHelper::updateBackgroundColor()
     if (std::abs(hueDifference) > 80) {
         newHue = (activeBackgroundColor.hue() + positiveBackgroundColor.hue()) / 2;
     } else {
-        newHue = hueDifference > 0 ?
-            activeBackgroundColor.hue() + 40 :
-            activeBackgroundColor.hue() - 40;
+        newHue = hueDifference > 0 ? activeBackgroundColor.hue() + 40 : activeBackgroundColor.hue() - 40;
         newHue %= 360; // hue needs to be between 0 and 359 per Qt documentation.
     }
 
     m_backgroundColor = QColor::fromHsv(newHue,
-                                        // Saturation should be closer to the active color because otherwise the selection mode color might overpower it.
+                                        // Saturation should be closer to the saturation of the active color
+                                        // because otherwise the selection mode color might overpower it.
                                         .7 * activeBackgroundColor.saturation() + .3 * positiveBackgroundColor.saturation(),
                                         (activeBackgroundColor.value() + positiveBackgroundColor.value()) / 2,
                                         (activeBackgroundColor.alpha() + positiveBackgroundColor.alpha()) / 2);
 }
 
 BackgroundColorHelper *BackgroundColorHelper::s_instance = nullptr;
+
+#include "moc_backgroundcolorhelper.cpp"