KConfigWidgets

kstylemanager.cpp
1/*
2 SPDX-FileCopyrightText: 2024 Christoph Cullmann <cullmann@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "kstylemanager.h"
8
9#include <KActionMenu>
10#include <KConfigGroup>
11#include <KLocalizedString>
12#include <KSharedConfig>
13
14#include <QAction>
15#include <QActionGroup>
16#include <QApplication>
17#include <QStyle>
18#include <QStyleFactory>
19
20#include <private/qguiapplication_p.h>
21#include <qpa/qplatformtheme.h>
22
23static bool supported()
24{
25 // do nothing if an explicit style has been requested
26 if (!QGuiApplicationPrivate::styleOverride.isEmpty()) {
27 return false;
28 }
29
30 // do nothing if we have the proper platform theme already
31 if (QGuiApplicationPrivate::platformTheme() && QGuiApplicationPrivate::platformTheme()->name() == QLatin1String("kde")) {
32 return false;
33 }
34
35 return true;
36}
37
39{
40 if (!supported()) {
41 return;
42 }
43
44 // get config, with fallback to kdeglobals
45 const auto config = KSharedConfig::openConfig();
46
47 // enforce the style configured by the user, with kdeglobals fallback
48 // if not set or the style is not there, use Breeze
49 QString styleToUse = KConfigGroup(config, QStringLiteral("KDE")).readEntry("widgetStyle", QString());
50 if (styleToUse.isEmpty() || !QApplication::setStyle(styleToUse)) {
51 styleToUse = QStringLiteral("breeze");
52 QApplication::setStyle(styleToUse);
53 }
54}
55
57{
58 // if we are running with our application theme, just return a disabled & hidden action
59 // there we just follow the Plasma theme
60 if (!supported()) {
61 QAction *a = new QAction(parent);
62 a->setEnabled(false);
63 a->setVisible(false);
64 return a;
65 }
66
67 // else: provide style chooser
68
69 // get config, without fallback to kdeglobals
71 const QString styleWeUse = KConfigGroup(config, QStringLiteral("KDE")).readEntry("widgetStyle", QString());
72
73 // build menu with default to reset setting and all known styles
74 KActionMenu *menu = new KActionMenu(QIcon::fromTheme(QStringLiteral("preferences-desktop-theme-applications")), i18n("Application Style"), parent);
75 QActionGroup *group = new QActionGroup(menu);
76 const QStringList styles = QStringList() << QString() << QStyleFactory::keys();
77 for (const QString &style : styles) {
78 QAction *action = new QAction(style.isEmpty() ? i18n("Default") : style, menu);
79 action->setData(style);
80 action->setActionGroup(group);
81 action->setCheckable(true);
82 if (style.toLower() == styleWeUse.toLower()) {
83 action->setChecked(true);
84 }
85 menu->addAction(action);
86 }
87
88 // toggle style change
89 QObject::connect(group, &QActionGroup::triggered, group, [](QAction *action) {
90 const QString styleToUse = action->data().toString();
91 const auto config = KSharedConfig::openConfig();
92 if (styleToUse.isEmpty()) {
93 KConfigGroup(config, QStringLiteral("KDE")).deleteEntry("widgetStyle");
94 } else {
95 KConfigGroup(config, QStringLiteral("KDE")).writeEntry("widgetStyle", styleToUse);
96 }
97 initStyle();
98 });
99
100 return menu;
101}
void addAction(QAction *action)
void deleteEntry(const char *key, WriteConfigFlags pFlags=Normal)
void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags=Normal)
QString readEntry(const char *key, const char *aDefault=nullptr) const
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
QString i18n(const char *text, const TYPE &arg...)
QString name(StandardAction id)
void initStyle()
Enforces the style configured by the user with fallback to the Breeze style.
QAction * createConfigureAction(QObject *parent=nullptr)
Creates an action to configure the current used style.
void setCheckable(bool)
void setChecked(bool)
QVariant data() const const
void setEnabled(bool)
void setActionGroup(QActionGroup *group)
void setData(const QVariant &data)
void setVisible(bool)
QStyle * setStyle(const QString &style)
QIcon fromTheme(const QString &name)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool isEmpty() const const
QString toLower() const const
QStringList keys()
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Feb 28 2025 12:01:09 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.