KWin
Loading...
Searching...
No Matches
kcm.cpp
Go to the documentation of this file.
1/*
2 SPDX-FileCopyrightText: 2019 Valerio Pilo <vpilo@coldshock.net>
3 SPDX-FileCopyrightText: 2019 Cyril Rossi <cyril.rossi@enioka.com>
4
5 SPDX-License-Identifier: LGPL-2.0-only
6*/
7#include "kcm.h"
8
9#include <config-kwin.h>
10
12#include "decorationmodel.h"
13
14#include <KConfigGroup>
15#include <KLocalizedString>
16#include <KPluginFactory>
17
18#include <QDBusConnection>
19#include <QDBusMessage>
20#include <QDebug>
21#include <QQuickItem>
22#include <QQuickWindow>
23#include <QSortFilterProxyModel>
24
25#include "kwindecorationdata.h"
26#include "kwindecorationsettings.h"
27#include "utils.h"
28
29K_PLUGIN_FACTORY_WITH_JSON(KCMKWinDecorationFactory, "kcm_kwindecoration.json", registerPlugin<KCMKWinDecoration>(); registerPlugin<KWinDecorationData>();)
30
31Q_DECLARE_METATYPE(KDecoration2::BorderSize)
32
33namespace
34{
35const KDecoration2::BorderSize s_defaultRecommendedBorderSize = KDecoration2::BorderSize::Normal;
36}
37
38KCMKWinDecoration::KCMKWinDecoration(QObject *parent, const KPluginMetaData &metaData)
39 : KQuickManagedConfigModule(parent, metaData)
40 , m_themesModel(new KDecoration2::Configuration::DecorationsModel(this))
41 , m_proxyThemesModel(new QSortFilterProxyModel(this))
42 , m_leftButtonsModel(new KDecoration2::Preview::ButtonsModel(DecorationButtonsList(), this))
43 , m_rightButtonsModel(new KDecoration2::Preview::ButtonsModel(DecorationButtonsList(), this))
44 , m_availableButtonsModel(new KDecoration2::Preview::ButtonsModel(this))
45 , m_data(new KWinDecorationData(this))
46{
47 setButtons(Apply | Default | Help);
48 qmlRegisterAnonymousType<QAbstractListModel>("org.kde.kwin.KWinDecoration", 1);
49 qmlRegisterAnonymousType<QSortFilterProxyModel>("org.kde.kwin.KWinDecoration", 1);
50 qmlRegisterAnonymousType<KWinDecorationSettings>("org.kde.kwin.KWinDecoration", 1);
51 m_proxyThemesModel->setSourceModel(m_themesModel);
52 m_proxyThemesModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
53 m_proxyThemesModel->setSortCaseSensitivity(Qt::CaseInsensitive);
54 m_proxyThemesModel->sort(0);
55
56 connect(m_proxyThemesModel, &QSortFilterProxyModel::rowsInserted, this, &KCMKWinDecoration::themeChanged);
57 connect(m_proxyThemesModel, &QSortFilterProxyModel::rowsRemoved, this, &KCMKWinDecoration::themeChanged);
58 connect(m_proxyThemesModel, &QSortFilterProxyModel::modelReset, this, &KCMKWinDecoration::themeChanged);
59
60 connect(m_data->settings(), &KWinDecorationSettings::themeChanged, this, &KCMKWinDecoration::themeChanged);
61 connect(m_data->settings(), &KWinDecorationSettings::borderSizeChanged, this, &KCMKWinDecoration::borderSizeChanged);
62
63 connect(m_data->settings(), &KWinDecorationSettings::borderSizeAutoChanged, this, &KCMKWinDecoration::borderIndexChanged);
66
67 connect(this, &KCMKWinDecoration::themeChanged, this, [this]() {
68 if (m_data->settings()->borderSizeAuto()) {
69 setBorderSize(recommendedBorderSize());
70 }
71 });
72
73 connect(m_leftButtonsModel, &QAbstractItemModel::rowsInserted, this, &KCMKWinDecoration::onLeftButtonsChanged);
74 connect(m_leftButtonsModel, &QAbstractItemModel::rowsMoved, this, &KCMKWinDecoration::onLeftButtonsChanged);
75 connect(m_leftButtonsModel, &QAbstractItemModel::rowsRemoved, this, &KCMKWinDecoration::onLeftButtonsChanged);
76 connect(m_leftButtonsModel, &QAbstractItemModel::modelReset, this, &KCMKWinDecoration::onLeftButtonsChanged);
77
78 connect(m_rightButtonsModel, &QAbstractItemModel::rowsInserted, this, &KCMKWinDecoration::onRightButtonsChanged);
79 connect(m_rightButtonsModel, &QAbstractItemModel::rowsMoved, this, &KCMKWinDecoration::onRightButtonsChanged);
80 connect(m_rightButtonsModel, &QAbstractItemModel::rowsRemoved, this, &KCMKWinDecoration::onRightButtonsChanged);
81 connect(m_rightButtonsModel, &QAbstractItemModel::modelReset, this, &KCMKWinDecoration::onRightButtonsChanged);
82
83 connect(this, &KCMKWinDecoration::borderSizeChanged, this, &KCMKWinDecoration::settingsChanged);
84
85 // Update the themes when the color scheme or a theme's settings change
86 QDBusConnection::sessionBus()
87 .connect(QString(), QStringLiteral("/KWin"), QStringLiteral("org.kde.KWin"), QStringLiteral("reloadConfig"),
88 this, SLOT(reloadKWinSettings()));
89
90 QMetaObject::invokeMethod(m_themesModel, &KDecoration2::Configuration::DecorationsModel::init, Qt::QueuedConnection);
91}
92
93KWinDecorationSettings *KCMKWinDecoration::settings() const
94{
95 return m_data->settings();
96}
97
99{
100 QMetaObject::invokeMethod(m_themesModel, &KDecoration2::Configuration::DecorationsModel::init, Qt::QueuedConnection);
101}
102
104{
105 KQuickManagedConfigModule::load();
106
107 m_leftButtonsModel->replace(Utils::buttonsFromString(settings()->buttonsOnLeft()));
108 m_rightButtonsModel->replace(Utils::buttonsFromString(settings()->buttonsOnRight()));
109
110 setBorderSize(borderSizeIndexFromString(settings()->borderSize()));
111
112 Q_EMIT themeChanged();
113}
114
116{
117 if (!settings()->borderSizeAuto()) {
118 settings()->setBorderSize(borderSizeIndexToString(m_borderSizeIndex));
119 } else {
120 settings()->setBorderSize(settings()->defaultBorderSizeValue());
121 }
122
123 KQuickManagedConfigModule::save();
124
125 // Send a signal to all kwin instances
126 QDBusMessage message = QDBusMessage::createSignal(QStringLiteral("/KWin"),
127 QStringLiteral("org.kde.KWin"),
128 QStringLiteral("reloadConfig"));
129 QDBusConnection::sessionBus().send(message);
130}
131
133{
134 KQuickManagedConfigModule::defaults();
135
137
138 m_leftButtonsModel->replace(Utils::buttonsFromString(settings()->buttonsOnLeft()));
139 m_rightButtonsModel->replace(Utils::buttonsFromString(settings()->buttonsOnRight()));
140}
141
142void KCMKWinDecoration::onLeftButtonsChanged()
143{
144 settings()->setButtonsOnLeft(Utils::buttonsToString(m_leftButtonsModel->buttons()));
145}
146
147void KCMKWinDecoration::onRightButtonsChanged()
148{
149 settings()->setButtonsOnRight(Utils::buttonsToString(m_rightButtonsModel->buttons()));
150}
151
152QSortFilterProxyModel *KCMKWinDecoration::themesModel() const
153{
154 return m_proxyThemesModel;
155}
156
157QAbstractListModel *KCMKWinDecoration::leftButtonsModel()
158{
159 return m_leftButtonsModel;
160}
161
162QAbstractListModel *KCMKWinDecoration::rightButtonsModel()
163{
164 return m_rightButtonsModel;
165}
166
168{
169 return m_availableButtonsModel;
170}
171
173{
174 // Use index 0 for borderSizeAuto == true
175 // The rest of indexes get offset by 1
176 QStringList model = Utils::getBorderSizeNames().values();
177 model.insert(0, i18nc("%1 is the name of a border size", "Theme's default (%1)", model.at(recommendedBorderSize())));
178 return model;
179}
180
182{
183 return settings()->borderSizeAuto() ? 0 : m_borderSizeIndex + 1;
184}
185
187{
188 const bool borderAuto = (index == 0);
189 settings()->setBorderSizeAuto(borderAuto);
190 setBorderSize(borderAuto ? recommendedBorderSize() : index - 1);
191}
192
194{
195 return m_borderSizeIndex;
196}
197
199{
201 const QModelIndex proxyIndex = m_proxyThemesModel->index(theme(), 0);
202 if (proxyIndex.isValid()) {
203 const QModelIndex index = m_proxyThemesModel->mapToSource(proxyIndex);
204 if (index.isValid()) {
205 QVariant ret = m_themesModel->data(index, DecoRole::RecommendedBorderSizeRole);
206 return Utils::getBorderSizeNames().keys().indexOf(Utils::stringToBorderSize(ret.toString()));
207 }
208 }
209 return Utils::getBorderSizeNames().keys().indexOf(s_defaultRecommendedBorderSize);
210}
211
213{
214 return m_proxyThemesModel->mapFromSource(m_themesModel->findDecoration(settings()->pluginName(), settings()->theme())).row();
215}
216
218{
219 if (m_borderSizeIndex != index) {
220 m_borderSizeIndex = index;
221 Q_EMIT borderSizeChanged();
222 }
223}
224
225void KCMKWinDecoration::setBorderSize(KDecoration2::BorderSize size)
226{
227 settings()->setBorderSize(Utils::borderSizeToString(size));
228}
229
231{
232 QModelIndex dataIndex = m_proxyThemesModel->index(index, 0);
233 if (dataIndex.isValid()) {
234 settings()->setTheme(m_proxyThemesModel->data(dataIndex, KDecoration2::Configuration::DecorationsModel::ThemeNameRole).toString());
235 settings()->setPluginName(m_proxyThemesModel->data(dataIndex, KDecoration2::Configuration::DecorationsModel::PluginNameRole).toString());
236 Q_EMIT themeChanged();
237 }
238}
239
240bool KCMKWinDecoration::isSaveNeeded() const
241{
242 return !settings()->borderSizeAuto() && borderSizeIndexFromString(settings()->borderSize()) != m_borderSizeIndex;
243}
244
245int KCMKWinDecoration::borderSizeIndexFromString(const QString &size) const
246{
247 return Utils::getBorderSizeNames().keys().indexOf(Utils::stringToBorderSize(size));
248}
249
250QString KCMKWinDecoration::borderSizeIndexToString(int index) const
251{
252 return Utils::borderSizeToString(Utils::getBorderSizeNames().keys().at(index));
253}
254
255#include "kcm.moc"
256#include "moc_kcm.cpp"
void defaults() override
Definition kcm.cpp:132
int borderSize
Definition kcm.h:41
KCMKWinDecoration(QObject *parent, const KPluginMetaData &metaData)
Definition kcm.cpp:38
KWinDecorationSettings * settings
Definition kcm.h:37
void setBorderIndex(int index)
Definition kcm.cpp:186
QAbstractListModel * leftButtonsModel
Definition kcm.h:44
void save() override
Definition kcm.cpp:115
void setTheme(int index)
Definition kcm.cpp:230
int recommendedBorderSize
Definition kcm.h:42
void reloadKWinSettings()
Definition kcm.cpp:98
QSortFilterProxyModel * themesModel
Definition kcm.h:38
void borderIndexChanged()
QAbstractListModel * availableButtonsModel
Definition kcm.h:46
void load() override
Definition kcm.cpp:103
QStringList borderSizesModel
Definition kcm.h:39
void setBorderSize(int index)
Definition kcm.cpp:217
QAbstractListModel * rightButtonsModel
Definition kcm.h:45
int borderIndex
Definition kcm.h:40
void borderSizeChanged()
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
QModelIndex findDecoration(const QString &pluginName, const QString &themeName=QString()) const
void replace(const QList< DecorationButtonType > &buttons)
QList< DecorationButtonType > buttons() const
K_PLUGIN_FACTORY_WITH_JSON(KCMKWinDecorationFactory, "kcm_kwindecoration.json", registerPlugin< KCMKWinDecoration >();registerPlugin< KWinDecorationData >();) namespace
Definition kcm.cpp:29
Q_DECLARE_METATYPE(KWin::SwitchEvent::State)
DecorationButtonsList buttonsFromString(const QString &buttons)
Definition utils.cpp:69
const QMap< KDecoration2::BorderSize, QString > & getBorderSizeNames()
Definition utils.cpp:102
QString borderSizeToString(KDecoration2::BorderSize size)
Definition utils.cpp:97
KDecoration2::BorderSize stringToBorderSize(const QString &name)
Definition utils.cpp:87
QString buttonsToString(const DecorationButtonsList &buttons)
Definition utils.cpp:53
QList< KDecoration2::DecorationButtonType > DecorationButtonsList
Definition utils.h:14