File indexing completed on 2024-04-28 17:06:23

0001 /*
0002     SPDX-FileCopyrightText: 2010 Jan Lepper <dehtris@yahoo.de>
0003     SPDX-FileCopyrightText: 2010-2022 Krusader Krew <https://krusader.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "listpanelframe.h"
0009 
0010 #include "../krglobal.h"
0011 #include "krcolorcache.h"
0012 
0013 // QtCore
0014 #include <QUrl>
0015 // QtGui
0016 #include <QDragEnterEvent>
0017 #include <QGuiApplication>
0018 
0019 #include <KConfigCore/KSharedConfig>
0020 #include <KCoreAddons/KUrlMimeData>
0021 
0022 ListPanelFrame::ListPanelFrame(QWidget *parent, const QString &color)
0023     : QFrame(parent)
0024     , color(color)
0025 {
0026     if (!color.isEmpty()) {
0027         colorsChanged();
0028         refreshColors(false);
0029         setAutoFillBackground(true);
0030         connect(&KrColorCache::getColorCache(), &KrColorCache::colorsRefreshed, this, &ListPanelFrame::colorsChanged);
0031     }
0032 }
0033 
0034 void ListPanelFrame::dragEnterEvent(QDragEnterEvent *e)
0035 {
0036     if (acceptDrops()) {
0037         QList<QUrl> URLs = KUrlMimeData::urlsFromMimeData(e->mimeData());
0038         e->setAccepted(!URLs.isEmpty());
0039     } else
0040         QFrame::dragEnterEvent(e);
0041 }
0042 
0043 void ListPanelFrame::colorsChanged()
0044 {
0045     QPalette p = QGuiApplication::palette();
0046     const QColor &windowForeground = p.color(QPalette::Active, QPalette::WindowText);
0047     const QColor &windowBackground = p.color(QPalette::Active, QPalette::Window);
0048 
0049     KConfigGroup gc(krConfig, "Colors");
0050     QColor fgAct = getColor(gc, color + " Foreground Active", p.color(QPalette::Active, QPalette::HighlightedText), windowForeground);
0051     QColor bgAct = getColor(gc, color + " Background Active", p.color(QPalette::Active, QPalette::Highlight), windowBackground);
0052     QColor fgInact = getColor(gc, color + " Foreground Inactive", p.color(QPalette::Active, QPalette::Text), windowForeground);
0053     QColor bgInact = getColor(gc, color + " Background Inactive", p.color(QPalette::Active, QPalette::Base), windowBackground);
0054 
0055     palActive = palInactive = palette();
0056 
0057     // --- active ---
0058     // foreground
0059     palActive.setColor(QPalette::WindowText, fgAct);
0060     palActive.setColor(QPalette::ButtonText, fgAct);
0061     // background
0062     palActive.setColor(QPalette::Window, bgAct);
0063     palActive.setColor(QPalette::Button, bgAct);
0064 
0065     // --- inactive ---
0066     // foreground
0067     palInactive.setColor(QPalette::WindowText, fgInact);
0068     palInactive.setColor(QPalette::ButtonText, fgInact);
0069     // background
0070     palInactive.setColor(QPalette::Window, bgInact);
0071     palInactive.setColor(QPalette::Button, bgInact);
0072 }
0073 
0074 void ListPanelFrame::refreshColors(bool active)
0075 {
0076     if (color.isEmpty())
0077         return;
0078     setPalette(active ? palActive : palInactive);
0079 }
0080 
0081 QColor ListPanelFrame::getColor(KConfigGroup &cg, const QString &name, const QColor &def, const QColor &kdedef)
0082 {
0083     if (cg.readEntry(name, QString()) == "KDE default")
0084         return kdedef;
0085     else if (!cg.readEntry(name, QString()).isEmpty())
0086         return cg.readEntry(name, def);
0087     return def;
0088 }