Libplasma

plasmatheme.cpp
1/*
2 SPDX-FileCopyrightText: 2023 Marco Martin <mart@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "plasmatheme.h"
8#include <KIconLoader>
9#include <QDebug>
10#include <QGuiApplication>
11#include <QPalette>
12#include <QQmlContext>
13#include <QQmlEngine>
14#include <QScopeGuard>
15
16#include <KColorScheme>
17#include <KConfigGroup>
18#include <KIconColors>
19
20PlasmaTheme::PlasmaTheme(QObject *parent)
21 : PlatformTheme(parent)
22{
23 setSupportsIconColoring(true);
24
25 auto parentItem = qobject_cast<QQuickItem *>(parent);
26 if (parentItem) {
27 connect(parentItem, &QQuickItem::enabledChanged, this, &PlasmaTheme::syncColors);
28 connect(parentItem, &QQuickItem::visibleChanged, this, [this, parentItem] {
29 if (!parentItem->isVisible()) {
30 return;
31 }
32 syncColors();
33 });
34 }
35
36 setDefaultFont(qGuiApp->font());
37
38 KSharedConfigPtr ptr = KSharedConfig::openConfig();
39 KConfigGroup general(ptr->group(QStringLiteral("general")));
40
41 setSmallFont(general.readEntry("smallestReadableFont", []() {
42 auto smallFont = qApp->font();
43#ifndef Q_OS_WIN
44 if (smallFont.pixelSize() != -1) {
45 smallFont.setPixelSize(smallFont.pixelSize() - 2);
46 } else {
47 smallFont.setPointSize(smallFont.pointSize() - 2);
48 }
49#endif
50 return smallFont;
51 }()));
52
53 syncColors();
54 connect(&m_theme, &Plasma::Theme::themeChanged, this, &PlasmaTheme::syncColors);
55}
56
57PlasmaTheme::~PlasmaTheme()
58{
59}
60
61QIcon PlasmaTheme::iconFromTheme(const QString &name, const QColor &customColor)
62{
63 KIconColors colors(Plasma::Theme::globalPalette());
64 KColorScheme colorScheme(QPalette::Active, KColorScheme::Window, Plasma::Theme::globalColorScheme());
65
66 colors.setPositiveText(colorScheme.foreground(KColorScheme::PositiveText).color().name());
67 colors.setNeutralText(colorScheme.foreground(KColorScheme::NeutralText).color().name());
68 colors.setNegativeText(colorScheme.foreground(KColorScheme::NegativeText).color().name());
69 colors.setActiveText(colorScheme.foreground(KColorScheme::ActiveText).color().name());
70
71 if (customColor != Qt::transparent) {
72 colors.setText(customColor);
73 }
74
75 return KDE::icon(name, colors);
76}
77
78void PlasmaTheme::syncColors()
79{
81 return;
82 }
83
84 Plasma::Theme::ColorGroup group;
85 switch (colorSet()) {
86 case View:
87 group = Plasma::Theme::ViewColorGroup;
88 break;
89 case Button:
90 group = Plasma::Theme::ButtonColorGroup;
91 break;
92 case Tooltip:
93 group = Plasma::Theme::ToolTipColorGroup;
94 break;
95 case Complementary:
96 group = Plasma::Theme::ComplementaryColorGroup;
97 break;
98 case Header:
99 group = Plasma::Theme::HeaderColorGroup;
100 break;
101 case Selection: // Plasma::Theme doesn't have selection group
102 case Window:
103 default:
104 group = Plasma::Theme::NormalColorGroup;
105 }
106
107 // foreground
108 setTextColor(m_theme.color(Plasma::Theme::TextColor, group));
109 setDisabledTextColor(m_theme.color(Plasma::Theme::DisabledTextColor, group));
110 setHighlightedTextColor(m_theme.color(Plasma::Theme::HighlightedTextColor, group));
111 // Plasma::Theme doesn't have ActiveText, use PositiveTextColor
112 setActiveTextColor(m_theme.color(Plasma::Theme::PositiveTextColor, group));
113 setLinkColor(m_theme.color(Plasma::Theme::LinkColor, group));
114 setVisitedLinkColor(m_theme.color(Plasma::Theme::VisitedLinkColor, group));
115 setNegativeTextColor(m_theme.color(Plasma::Theme::NegativeTextColor, group));
116 setNeutralTextColor(m_theme.color(Plasma::Theme::NeutralTextColor, group));
117 setPositiveTextColor(m_theme.color(Plasma::Theme::PositiveTextColor, group));
118
119 // background
120 setBackgroundColor(m_theme.color(Plasma::Theme::BackgroundColor, group));
121 setHighlightColor(m_theme.color(Plasma::Theme::HighlightColor, group));
122 // Plasma::Theme doesn't have AlternateBackground
123 setAlternateBackgroundColor(m_theme.color(Plasma::Theme::BackgroundColor, group));
124
125 // Plasma::Theme doesn't have any different background color type
126 setActiveBackgroundColor(m_theme.color(Plasma::Theme::BackgroundColor, group));
127 setLinkBackgroundColor(m_theme.color(Plasma::Theme::BackgroundColor, group));
128 setVisitedLinkBackgroundColor(m_theme.color(Plasma::Theme::BackgroundColor, group));
129 setNegativeBackgroundColor(m_theme.color(Plasma::Theme::BackgroundColor, group));
130 setNeutralBackgroundColor(m_theme.color(Plasma::Theme::BackgroundColor, group));
131 setPositiveBackgroundColor(m_theme.color(Plasma::Theme::BackgroundColor, group));
132
133 // decoration
134 setHoverColor(m_theme.color(Plasma::Theme::HoverColor, group));
135 setFocusColor(m_theme.color(Plasma::Theme::FocusColor, group));
136}
137
138bool PlasmaTheme::event(QEvent *event)
139{
140 if (event->type() == Kirigami::Platform::PlatformThemeEvents::ColorSetChangedEvent::type) {
141 syncColors();
142 }
143
144 if (event->type() == Kirigami::Platform::PlatformThemeEvents::ColorGroupChangedEvent::type) {
145 syncColors();
146 }
147
148 return PlatformTheme::event(event);
149}
150
151#include "moc_plasmatheme.cpp"
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
void themeChanged()
Emitted when the user changes the theme.
QColor color(ColorRole role, ColorGroup group=NormalColorGroup) const
Returns the text color to be used by items resting on the background.
Definition theme.cpp:255
@ DisabledTextColor
color of disabled text
Definition theme.h:67
@ PositiveTextColor
color of foreground objects with a "positive message" connotation (usually green)
Definition theme.h:64
@ HighlightedTextColor
color contrasting with HighlightColor, to be used for instance with
Definition theme.h:63
@ VisitedLinkColor
color visited clickable links
Definition theme.h:62
@ HighlightColor
the text highlight color to be used by items resting on the background
Definition theme.h:57
@ LinkColor
color for clickable links
Definition theme.h:61
@ NeutralTextColor
color of foreground objects with a "neutral message" connotation (usually yellow)
Definition theme.h:65
@ HoverColor
color for hover effect on view
Definition theme.h:59
@ NegativeTextColor
color of foreground objects with a "negative message" connotation (usually red)
Definition theme.h:66
@ BackgroundColor
the default background color
Definition theme.h:56
@ TextColor
the text color to be used by items resting on the background
Definition theme.h:55
@ FocusColor
color for focus effect on view
Definition theme.h:60
AKONADI_CALENDAR_EXPORT KCalendarCore::Event::Ptr event(const Akonadi::Item &item)
void enabledChanged()
void visibleChanged()
transparent
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 11 2024 12:09:36 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.