Plasma-framework

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 <KConfigGroup>
17#include <KIconColors>
18
19PlasmaTheme::PlasmaTheme(QObject *parent)
20 : PlatformTheme(parent)
21{
22 setSupportsIconColoring(true);
23
24 auto parentItem = qobject_cast<QQuickItem *>(parent);
25 if (parentItem) {
26 connect(parentItem, &QQuickItem::enabledChanged, this, &PlasmaTheme::syncColors);
27 connect(parentItem, &QQuickItem::visibleChanged, this, [this, parentItem] {
28 if (!parentItem->isVisible()) {
29 return;
30 }
31 syncColors();
32 });
33 }
34
35 setDefaultFont(qGuiApp->font());
36
37 KSharedConfigPtr ptr = KSharedConfig::openConfig();
38 KConfigGroup general(ptr->group(QStringLiteral("general")));
39
40 setSmallFont(general.readEntry("smallestReadableFont", []() {
41 auto smallFont = qApp->font();
42#ifndef Q_OS_WIN
43 if (smallFont.pixelSize() != -1) {
44 smallFont.setPixelSize(smallFont.pixelSize() - 2);
45 } else {
46 smallFont.setPointSize(smallFont.pointSize() - 2);
47 }
48#endif
49 return smallFont;
50 }()));
51
52 syncColors();
53 connect(&m_theme, &Plasma::Theme::themeChanged, this, &PlasmaTheme::syncColors);
54}
55
56PlasmaTheme::~PlasmaTheme()
57{
58}
59
60QIcon PlasmaTheme::iconFromTheme(const QString &name, const QColor &customColor)
61{
62 if (customColor != Qt::transparent) {
63 KIconColors colors;
64 colors.setText(customColor);
65 return KDE::icon(name, colors);
66 } else {
67 return KDE::icon(name);
68 }
69}
70
71void PlasmaTheme::syncColors()
72{
74 return;
75 }
76
77 Plasma::Theme::ColorGroup group;
78 switch (colorSet()) {
79 case View:
80 group = Plasma::Theme::ViewColorGroup;
81 break;
82 case Button:
83 group = Plasma::Theme::ButtonColorGroup;
84 break;
85 case Tooltip:
86 group = Plasma::Theme::ToolTipColorGroup;
87 break;
88 case Complementary:
89 group = Plasma::Theme::ComplementaryColorGroup;
90 break;
91 case Header:
92 group = Plasma::Theme::HeaderColorGroup;
93 break;
94 case Selection: // Plasma::Theme doesn't have selection group
95 case Window:
96 default:
97 group = Plasma::Theme::NormalColorGroup;
98 }
99
100 // foreground
101 setTextColor(m_theme.color(Plasma::Theme::TextColor, group));
102 setDisabledTextColor(m_theme.color(Plasma::Theme::DisabledTextColor, group));
103 setHighlightedTextColor(m_theme.color(Plasma::Theme::HighlightedTextColor, group));
104 // Plasma::Theme doesn't have ActiveText, use PositiveTextColor
105 setActiveTextColor(m_theme.color(Plasma::Theme::PositiveTextColor, group));
106 setLinkColor(m_theme.color(Plasma::Theme::LinkColor, group));
107 setVisitedLinkColor(m_theme.color(Plasma::Theme::VisitedLinkColor, group));
108 setNegativeTextColor(m_theme.color(Plasma::Theme::NegativeTextColor, group));
109 setNeutralTextColor(m_theme.color(Plasma::Theme::NeutralTextColor, group));
110 setPositiveTextColor(m_theme.color(Plasma::Theme::PositiveTextColor, group));
111
112 // background
113 setBackgroundColor(m_theme.color(Plasma::Theme::BackgroundColor, group));
114 setHighlightColor(m_theme.color(Plasma::Theme::HighlightColor, group));
115 // Plasma::Theme doesn't have AlternateBackground
116 setAlternateBackgroundColor(m_theme.color(Plasma::Theme::BackgroundColor, group));
117
118 // Plasma::Theme doesn't have any different background color type
119 setActiveBackgroundColor(m_theme.color(Plasma::Theme::BackgroundColor, group));
120 setLinkBackgroundColor(m_theme.color(Plasma::Theme::BackgroundColor, group));
121 setVisitedLinkBackgroundColor(m_theme.color(Plasma::Theme::BackgroundColor, group));
122 setNegativeBackgroundColor(m_theme.color(Plasma::Theme::BackgroundColor, group));
123 setNeutralBackgroundColor(m_theme.color(Plasma::Theme::BackgroundColor, group));
124 setPositiveBackgroundColor(m_theme.color(Plasma::Theme::BackgroundColor, group));
125
126 // decoration
127 setHoverColor(m_theme.color(Plasma::Theme::HoverColor, group));
128 setFocusColor(m_theme.color(Plasma::Theme::FocusColor, group));
129}
130
131bool PlasmaTheme::event(QEvent *event)
132{
133 if (event->type() == Kirigami::Platform::PlatformThemeEvents::ColorSetChangedEvent::type) {
134 syncColors();
135 }
136
137 if (event->type() == Kirigami::Platform::PlatformThemeEvents::ColorGroupChangedEvent::type) {
138 syncColors();
139 }
140
141 return PlatformTheme::event(event);
142}
143
144#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:246
@ 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 May 17 2024 11:54:11 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.