Changeset View
Changeset View
Standalone View
Standalone View
kcmkwin/kwineffects/kcm.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2019 Vlad Zagorodniy <vladzzag@gmail.com> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or modify | ||||
5 | * it under the terms of the GNU General Public License as published by | ||||
6 | * the Free Software Foundation; either version 2 of the License, or | ||||
7 | * (at your option) any later version. | ||||
8 | * | ||||
9 | * This program is distributed in the hope that it will be useful, | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
12 | * GNU General Public License for more details. | ||||
13 | * | ||||
14 | * You should have received a copy of the GNU General Public License | ||||
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
16 | */ | ||||
17 | | ||||
18 | #include "kcm.h" | ||||
19 | #include "effectmodel.h" | ||||
20 | #include "effectsfilterproxymodel.h" | ||||
21 | | ||||
22 | #include <KAboutData> | ||||
23 | #include <KCModule> | ||||
24 | #include <KLocalizedString> | ||||
25 | #include <KNS3/DownloadDialog> | ||||
26 | #include <KPluginFactory> | ||||
27 | #include <KPluginTrader> | ||||
28 | | ||||
29 | #include <QDialogButtonBox> | ||||
30 | #include <QPushButton> | ||||
31 | #include <QQuickWindow> | ||||
32 | #include <QVBoxLayout> | ||||
33 | #include <QWindow> | ||||
34 | | ||||
35 | K_PLUGIN_FACTORY_WITH_JSON(DesktopEffectsKCMFactory, | ||||
36 | "kcm_kwin_effects.json", | ||||
37 | registerPlugin<KWin::DesktopEffectsKCM>();) | ||||
38 | | ||||
39 | namespace KWin | ||||
40 | { | ||||
41 | | ||||
42 | DesktopEffectsKCM::DesktopEffectsKCM(QObject *parent, const QVariantList &args) | ||||
43 | : KQuickAddons::ConfigModule(parent, args) | ||||
44 | , m_model(new EffectModel(this)) | ||||
45 | { | ||||
46 | qmlRegisterType<EffectsFilterProxyModel>("org.kde.private.kcms.kwin.effects", 1, 0, "EffectsFilterProxyModel"); | ||||
47 | | ||||
48 | auto about = new KAboutData( | ||||
49 | QStringLiteral("kcm_kwin_effects"), | ||||
50 | i18n("Configure Desktop Effects"), | ||||
51 | QStringLiteral("2.0"), | ||||
52 | QString(), | ||||
53 | KAboutLicense::GPL | ||||
54 | ); | ||||
55 | about->addAuthor(i18n("Vlad Zagorodniy"), QString(), QStringLiteral("vladzzag@gmail.com")); | ||||
56 | setAboutData(about); | ||||
57 | | ||||
58 | setButtons(Apply | Default); | ||||
59 | | ||||
60 | connect(m_model, &EffectModel::dataChanged, this, &DesktopEffectsKCM::updateNeedsSave); | ||||
61 | } | ||||
62 | | ||||
63 | DesktopEffectsKCM::~DesktopEffectsKCM() | ||||
64 | { | ||||
65 | } | ||||
66 | | ||||
67 | QAbstractItemModel *DesktopEffectsKCM::effectsModel() const | ||||
68 | { | ||||
69 | return m_model; | ||||
70 | } | ||||
71 | | ||||
72 | void DesktopEffectsKCM::load() | ||||
73 | { | ||||
74 | m_model->load(); | ||||
75 | setNeedsSave(false); | ||||
76 | } | ||||
77 | | ||||
78 | void DesktopEffectsKCM::save() | ||||
79 | { | ||||
80 | m_model->save(); | ||||
81 | setNeedsSave(false); | ||||
82 | } | ||||
83 | | ||||
84 | void DesktopEffectsKCM::defaults() | ||||
85 | { | ||||
86 | m_model->defaults(); | ||||
87 | updateNeedsSave(); | ||||
88 | } | ||||
89 | | ||||
90 | void DesktopEffectsKCM::openGHNS(QQuickItem *context) | ||||
91 | { | ||||
92 | QPointer<KNS3::DownloadDialog> dialog = new KNS3::DownloadDialog(QStringLiteral("kwineffect.knsrc")); | ||||
93 | dialog->setWindowTitle(i18n("Download New Desktop Effects")); | ||||
94 | dialog->winId(); | ||||
95 | | ||||
96 | if (context && context->window()) { | ||||
97 | dialog->windowHandle()->setTransientParent(context->window()); | ||||
98 | } | ||||
99 | | ||||
100 | if (dialog->exec() == QDialog::Accepted) { | ||||
101 | if (!dialog->changedEntries().isEmpty()) { | ||||
102 | m_model->load(); | ||||
103 | } | ||||
104 | } | ||||
105 | | ||||
106 | delete dialog; | ||||
107 | } | ||||
108 | | ||||
109 | static KCModule *findBinaryConfig(const QString &pluginId, QObject *parent) | ||||
110 | { | ||||
111 | return KPluginTrader::createInstanceFromQuery<KCModule>( | ||||
112 | QStringLiteral("kwin/effects/configs/"), | ||||
113 | QString(), | ||||
114 | QStringLiteral("'%1' in [X-KDE-ParentComponents]").arg(pluginId), | ||||
115 | parent | ||||
116 | ); | ||||
117 | } | ||||
118 | | ||||
119 | static KCModule *findScriptedConfig(const QString &pluginId, QObject *parent) | ||||
120 | { | ||||
121 | const auto offers = KPluginTrader::self()->query( | ||||
122 | QStringLiteral("kwin/effects/configs/"), | ||||
123 | QString(), | ||||
124 | QStringLiteral("[X-KDE-Library] == 'kcm_kwin4_genericscripted'") | ||||
125 | ); | ||||
126 | | ||||
127 | if (offers.isEmpty()) { | ||||
128 | return nullptr; | ||||
129 | } | ||||
130 | | ||||
131 | const KPluginInfo &generic = offers.first(); | ||||
132 | KPluginLoader loader(generic.libraryPath()); | ||||
133 | KPluginFactory *factory = loader.factory(); | ||||
134 | if (!factory) { | ||||
135 | return nullptr; | ||||
136 | } | ||||
137 | | ||||
138 | return factory->create<KCModule>(pluginId, parent); | ||||
139 | } | ||||
140 | | ||||
141 | void DesktopEffectsKCM::configure(const QString &pluginId, QQuickItem *context) | ||||
142 | { | ||||
143 | const QModelIndex idx = m_model->findByPluginId(pluginId); | ||||
144 | if (!idx.isValid()) { | ||||
145 | return; | ||||
146 | } | ||||
147 | | ||||
148 | QPointer<QDialog> dialog = new QDialog(); | ||||
149 | | ||||
150 | KCModule *module = idx.data(EffectModel::ScriptedRole).toBool() | ||||
151 | ? findScriptedConfig(pluginId, dialog) | ||||
152 | : findBinaryConfig(pluginId, dialog); | ||||
153 | if (!module) { | ||||
154 | delete dialog; | ||||
155 | return; | ||||
156 | } | ||||
157 | | ||||
158 | dialog->setWindowTitle(idx.data(EffectModel::NameRole).toString()); | ||||
159 | dialog->winId(); | ||||
160 | | ||||
161 | auto buttons = new QDialogButtonBox( | ||||
162 | QDialogButtonBox::Ok | | ||||
163 | QDialogButtonBox::Cancel | | ||||
164 | QDialogButtonBox::RestoreDefaults, | ||||
165 | dialog | ||||
166 | ); | ||||
167 | connect(buttons, &QDialogButtonBox::accepted, dialog, &QDialog::accept); | ||||
168 | connect(buttons, &QDialogButtonBox::rejected, dialog, &QDialog::reject); | ||||
169 | connect(buttons->button(QDialogButtonBox::RestoreDefaults), &QPushButton::clicked, | ||||
170 | module, &KCModule::defaults); | ||||
171 | | ||||
172 | auto layout = new QVBoxLayout(dialog); | ||||
173 | layout->addWidget(module); | ||||
174 | layout->addWidget(buttons); | ||||
175 | | ||||
176 | if (context && context->window()) { | ||||
177 | dialog->windowHandle()->setTransientParent(context->window()); | ||||
178 | } | ||||
179 | | ||||
180 | dialog->exec(); | ||||
181 | | ||||
182 | delete dialog; | ||||
183 | } | ||||
184 | | ||||
185 | void DesktopEffectsKCM::updateNeedsSave() | ||||
186 | { | ||||
187 | setNeedsSave(m_model->needsSave()); | ||||
188 | } | ||||
189 | | ||||
190 | } // namespace KWin | ||||
191 | | ||||
192 | #include "kcm.moc" |