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("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 | } | ||||
76 | | ||||
77 | void DesktopEffectsKCM::save() | ||||
78 | { | ||||
79 | m_model->save(); | ||||
80 | setNeedsSave(false); | ||||
81 | } | ||||
82 | | ||||
83 | void DesktopEffectsKCM::defaults() | ||||
84 | { | ||||
85 | m_model->defaults(); | ||||
86 | updateNeedsSave(); | ||||
87 | } | ||||
88 | | ||||
89 | void DesktopEffectsKCM::openGHNS(QQuickItem *context) | ||||
90 | { | ||||
91 | QPointer<KNS3::DownloadDialog> dialog = new KNS3::DownloadDialog(QStringLiteral("kwineffect.knsrc")); | ||||
92 | dialog->setWindowTitle(i18n("Download New Desktop Effects")); | ||||
93 | dialog->winId(); | ||||
94 | | ||||
95 | if (context && context->window()) { | ||||
96 | dialog->windowHandle()->setTransientParent(context->window()); | ||||
97 | } | ||||
98 | | ||||
99 | if (dialog->exec() == QDialog::Accepted) { | ||||
100 | if (!dialog->changedEntries().isEmpty()) { | ||||
101 | m_model->load(); | ||||
102 | } | ||||
103 | } | ||||
104 | | ||||
105 | delete dialog; | ||||
106 | } | ||||
107 | | ||||
108 | static KCModule *findBinaryConfig(const QString &pluginId, QObject *parent) | ||||
109 | { | ||||
110 | return KPluginTrader::createInstanceFromQuery<KCModule>( | ||||
111 | QStringLiteral("kwin/effects/configs/"), | ||||
112 | QString(), | ||||
113 | QStringLiteral("'%1' in [X-KDE-ParentComponents]").arg(pluginId), | ||||
114 | parent | ||||
115 | ); | ||||
116 | } | ||||
117 | | ||||
118 | static KCModule *findScriptedConfig(const QString &pluginId, QObject *parent) | ||||
119 | { | ||||
120 | const auto offers = KPluginTrader::self()->query( | ||||
121 | QStringLiteral("kwin/effects/configs/"), | ||||
122 | QString(), | ||||
123 | QStringLiteral("[X-KDE-Library] == 'kcm_kwin4_genericscripted'") | ||||
124 | ); | ||||
125 | | ||||
126 | if (offers.isEmpty()) { | ||||
127 | return nullptr; | ||||
128 | } | ||||
129 | | ||||
130 | const KPluginInfo &generic = offers.first(); | ||||
131 | KPluginLoader loader(generic.libraryPath()); | ||||
132 | KPluginFactory *factory = loader.factory(); | ||||
133 | if (!factory) { | ||||
134 | return nullptr; | ||||
135 | } | ||||
136 | | ||||
137 | return factory->create<KCModule>(pluginId, parent); | ||||
138 | } | ||||
139 | | ||||
140 | void DesktopEffectsKCM::configure(const QString &pluginId, QQuickItem *context) | ||||
141 | { | ||||
142 | const QModelIndex idx = m_model->findByPluginId(pluginId); | ||||
143 | if (!idx.isValid()) { | ||||
144 | return; | ||||
145 | } | ||||
146 | | ||||
147 | QPointer<QDialog> dialog = new QDialog(); | ||||
148 | | ||||
149 | KCModule *module = idx.data(EffectModel::ScriptedRole).toBool() | ||||
150 | ? findScriptedConfig(pluginId, dialog) | ||||
151 | : findBinaryConfig(pluginId, dialog); | ||||
152 | if (!module) { | ||||
153 | delete dialog; | ||||
154 | return; | ||||
155 | } | ||||
156 | | ||||
157 | dialog->setWindowTitle(idx.data(EffectModel::NameRole).toString()); | ||||
158 | dialog->winId(); | ||||
159 | | ||||
160 | auto buttons = new QDialogButtonBox( | ||||
161 | QDialogButtonBox::Ok | | ||||
162 | QDialogButtonBox::Cancel | | ||||
163 | QDialogButtonBox::RestoreDefaults, | ||||
164 | dialog | ||||
165 | ); | ||||
166 | connect(buttons, &QDialogButtonBox::accepted, dialog, &QDialog::accept); | ||||
167 | connect(buttons, &QDialogButtonBox::rejected, dialog, &QDialog::reject); | ||||
168 | connect(buttons->button(QDialogButtonBox::RestoreDefaults), &QPushButton::clicked, | ||||
169 | module, &KCModule::defaults); | ||||
170 | | ||||
171 | auto layout = new QVBoxLayout(dialog); | ||||
172 | layout->addWidget(module); | ||||
173 | layout->addWidget(buttons); | ||||
174 | | ||||
175 | if (context && context->window()) { | ||||
176 | dialog->windowHandle()->setTransientParent(context->window()); | ||||
177 | } | ||||
178 | | ||||
179 | dialog->exec(); | ||||
180 | | ||||
181 | delete dialog; | ||||
182 | } | ||||
183 | | ||||
184 | void DesktopEffectsKCM::updateNeedsSave() | ||||
185 | { | ||||
186 | setNeedsSave(m_model->needsSave()); | ||||
187 | } | ||||
188 | | ||||
189 | } // namespace KWin | ||||
190 | | ||||
191 | #include "kcm.moc" |