File indexing completed on 2024-11-24 04:59:27

0001 /*
0002     SPDX-FileCopyrightText: 2023 Marco Martin <mart@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "plasmatheme.h"
0008 #include <KIconLoader>
0009 #include <QDebug>
0010 #include <QGuiApplication>
0011 #include <QPalette>
0012 #include <QQmlContext>
0013 #include <QQmlEngine>
0014 #include <QScopeGuard>
0015 
0016 #include <KConfigGroup>
0017 #include <KIconColors>
0018 
0019 PlasmaTheme::PlasmaTheme(QObject *parent)
0020     : PlatformTheme(parent)
0021 {
0022     setSupportsIconColoring(true);
0023 
0024     auto parentItem = qobject_cast<QQuickItem *>(parent);
0025     if (parentItem) {
0026         connect(parentItem, &QQuickItem::enabledChanged, this, &PlasmaTheme::syncColors);
0027         connect(parentItem, &QQuickItem::visibleChanged, this, &PlasmaTheme::syncColors);
0028     }
0029 
0030     setDefaultFont(qGuiApp->font());
0031 
0032     KSharedConfigPtr ptr = KSharedConfig::openConfig();
0033     KConfigGroup general(ptr->group(QStringLiteral("general")));
0034 
0035     setSmallFont(general.readEntry("smallestReadableFont", []() {
0036         auto smallFont = qApp->font();
0037 #ifndef Q_OS_WIN
0038         if (smallFont.pixelSize() != -1) {
0039             smallFont.setPixelSize(smallFont.pixelSize() - 2);
0040         } else {
0041             smallFont.setPointSize(smallFont.pointSize() - 2);
0042         }
0043 #endif
0044         return smallFont;
0045     }()));
0046 
0047     syncColors();
0048     connect(&m_theme, &Plasma::Theme::themeChanged, this, &PlasmaTheme::syncColors);
0049 }
0050 
0051 PlasmaTheme::~PlasmaTheme()
0052 {
0053 }
0054 
0055 QIcon PlasmaTheme::iconFromTheme(const QString &name, const QColor &customColor)
0056 {
0057     if (customColor != Qt::transparent) {
0058         KIconColors colors;
0059         colors.setText(customColor);
0060         return KDE::icon(name, colors);
0061     } else {
0062         return KDE::icon(name);
0063     }
0064 }
0065 
0066 void PlasmaTheme::syncColors()
0067 {
0068     if (QCoreApplication::closingDown()) {
0069         return;
0070     }
0071 
0072     Plasma::Theme::ColorGroup group;
0073     switch (colorSet()) {
0074     case View:
0075         group = Plasma::Theme::ViewColorGroup;
0076         break;
0077     case Button:
0078         group = Plasma::Theme::ButtonColorGroup;
0079         break;
0080     case Tooltip:
0081         group = Plasma::Theme::ToolTipColorGroup;
0082         break;
0083     case Complementary:
0084         group = Plasma::Theme::ComplementaryColorGroup;
0085         break;
0086     case Header:
0087         group = Plasma::Theme::HeaderColorGroup;
0088         break;
0089     case Selection: // Plasma::Theme doesn't have selection group
0090     case Window:
0091     default:
0092         group = Plasma::Theme::NormalColorGroup;
0093     }
0094 
0095     // foreground
0096     setTextColor(m_theme.color(Plasma::Theme::TextColor, group));
0097     setDisabledTextColor(m_theme.color(Plasma::Theme::DisabledTextColor, group));
0098     setHighlightedTextColor(m_theme.color(Plasma::Theme::HighlightedTextColor, group));
0099     // Plasma::Theme doesn't have ActiveText, use PositiveTextColor
0100     setActiveTextColor(m_theme.color(Plasma::Theme::PositiveTextColor, group));
0101     setLinkColor(m_theme.color(Plasma::Theme::LinkColor, group));
0102     setVisitedLinkColor(m_theme.color(Plasma::Theme::VisitedLinkColor, group));
0103     setNegativeTextColor(m_theme.color(Plasma::Theme::NegativeTextColor, group));
0104     setNeutralTextColor(m_theme.color(Plasma::Theme::NeutralTextColor, group));
0105     setPositiveTextColor(m_theme.color(Plasma::Theme::PositiveTextColor, group));
0106 
0107     // background
0108     setBackgroundColor(m_theme.color(Plasma::Theme::BackgroundColor, group));
0109     setHighlightColor(m_theme.color(Plasma::Theme::HighlightColor, group));
0110     // Plasma::Theme doesn't have AlternateBackground
0111     setAlternateBackgroundColor(m_theme.color(Plasma::Theme::BackgroundColor, group));
0112 
0113     // Plasma::Theme doesn't have any different background color type
0114     setActiveBackgroundColor(m_theme.color(Plasma::Theme::BackgroundColor, group));
0115     setLinkBackgroundColor(m_theme.color(Plasma::Theme::BackgroundColor, group));
0116     setVisitedLinkBackgroundColor(m_theme.color(Plasma::Theme::BackgroundColor, group));
0117     setNegativeBackgroundColor(m_theme.color(Plasma::Theme::BackgroundColor, group));
0118     setNeutralBackgroundColor(m_theme.color(Plasma::Theme::BackgroundColor, group));
0119     setPositiveBackgroundColor(m_theme.color(Plasma::Theme::BackgroundColor, group));
0120 
0121     // decoration
0122     setHoverColor(m_theme.color(Plasma::Theme::HoverColor, group));
0123     setFocusColor(m_theme.color(Plasma::Theme::FocusColor, group));
0124 }
0125 
0126 bool PlasmaTheme::event(QEvent *event)
0127 {
0128     if (event->type() == Kirigami::Platform::PlatformThemeEvents::ColorSetChangedEvent::type) {
0129         syncColors();
0130     }
0131 
0132     if (event->type() == Kirigami::Platform::PlatformThemeEvents::ColorGroupChangedEvent::type) {
0133         syncColors();
0134     }
0135 
0136     return PlatformTheme::event(event);
0137 }
0138 
0139 #include "moc_plasmatheme.cpp"