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
24{
25 // do nothing if we have the proper platform theme already
26 if (QGuiApplicationPrivate::platformTheme() && QGuiApplicationPrivate::platformTheme()->name() == QLatin1String("kde")) {
27 return;
28 }
29
30 // get config, with fallback to kdeglobals
31 const auto config = KSharedConfig::openConfig();
32
33 // enforce the style configured by the user, with kdeglobals fallback
34 // if not set or the style is not there, use Breeze
35 QString styleToUse = KConfigGroup(config, QStringLiteral("KDE")).readEntry("widgetStyle", QString());
36 if (styleToUse.isEmpty() || !QApplication::setStyle(styleToUse)) {
37 styleToUse = QStringLiteral("breeze");
38 QApplication::setStyle(styleToUse);
39 }
40}
41
43{
44 // if we are running with our application theme, just return a disabled & hidden action
45 // there we just follow the Plasma theme
46 if (QGuiApplicationPrivate::platformTheme() && QGuiApplicationPrivate::platformTheme()->name() == QLatin1String("kde")) {
47 QAction *a = new QAction(parent);
48 a->setEnabled(false);
49 a->setVisible(false);
50 return a;
51 }
52
53 // else: provide style chooser
54
55 // get config, without fallback to kdeglobals
57 const QString styleWeUse = KConfigGroup(config, QStringLiteral("KDE")).readEntry("widgetStyle", QString());
58
59 // build menu with default to reset setting and all known styles
60 KActionMenu *menu = new KActionMenu(QIcon::fromTheme(QStringLiteral("preferences-desktop-theme-applications")), i18n("Application Style"), parent);
61 QActionGroup *group = new QActionGroup(menu);
62 const QStringList styles = QStringList() << QString() << QStyleFactory::keys();
63 for (const QString &style : styles) {
64 QAction *action = new QAction(style.isEmpty() ? i18n("Default") : style, menu);
65 action->setData(style);
66 action->setActionGroup(group);
67 action->setCheckable(true);
68 if (style.toLower() == styleWeUse.toLower()) {
69 action->setChecked(true);
70 }
71 menu->addAction(action);
72 }
73
74 // toggle style change
75 QObject::connect(group, &QActionGroup::triggered, group, [](QAction *action) {
76 const QString styleToUse = action->data().toString();
77 const auto config = KSharedConfig::openConfig();
78 if (styleToUse.isEmpty()) {
79 KConfigGroup(config, QStringLiteral("KDE")).deleteEntry("widgetStyle");
80 } else {
81 KConfigGroup(config, QStringLiteral("KDE")).writeEntry("widgetStyle", styleToUse);
82 }
83 initStyle();
84 });
85
86 return menu;
87}
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...)
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-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:07:46 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.