File indexing completed on 2024-11-10 04:57:18

0001 /*
0002     SPDX-FileCopyrightText: 2023 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "scripting/scriptedquicksceneeffect.h"
0008 #include "main.h"
0009 
0010 #include <KConfigGroup>
0011 #include <KConfigLoader>
0012 
0013 #include <QFile>
0014 
0015 namespace KWin
0016 {
0017 
0018 ScriptedQuickSceneEffect::ScriptedQuickSceneEffect()
0019 {
0020     m_visibleTimer.setSingleShot(true);
0021     connect(&m_visibleTimer, &QTimer::timeout, this, [this]() {
0022         setRunning(false);
0023     });
0024 }
0025 
0026 ScriptedQuickSceneEffect::~ScriptedQuickSceneEffect()
0027 {
0028 }
0029 
0030 void ScriptedQuickSceneEffect::reconfigure(ReconfigureFlags flags)
0031 {
0032     m_configLoader->load();
0033     Q_EMIT m_configLoader->configChanged();
0034 }
0035 
0036 int ScriptedQuickSceneEffect::requestedEffectChainPosition() const
0037 {
0038     return m_requestedEffectChainPosition;
0039 }
0040 
0041 void ScriptedQuickSceneEffect::setMetaData(const KPluginMetaData &metaData)
0042 {
0043     m_requestedEffectChainPosition = metaData.value(QStringLiteral("X-KDE-Ordering"), 50);
0044 
0045     KConfigGroup cg = kwinApp()->config()->group(QStringLiteral("Effect-%1").arg(metaData.pluginId()));
0046     const QString configFilePath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QLatin1String("kwin/effects/") + metaData.pluginId() + QLatin1String("/contents/config/main.xml"));
0047     if (configFilePath.isNull()) {
0048         m_configLoader = new KConfigLoader(cg, nullptr, this);
0049     } else {
0050         QFile xmlFile(configFilePath);
0051         m_configLoader = new KConfigLoader(cg, &xmlFile, this);
0052         m_configLoader->load();
0053     }
0054 
0055     m_configuration = new KConfigPropertyMap(m_configLoader, this);
0056     connect(m_configLoader, &KConfigLoader::configChanged, this, &ScriptedQuickSceneEffect::configurationChanged);
0057 }
0058 
0059 bool ScriptedQuickSceneEffect::isVisible() const
0060 {
0061     return m_isVisible;
0062 }
0063 
0064 void ScriptedQuickSceneEffect::setVisible(bool visible)
0065 {
0066     if (m_isVisible == visible) {
0067         return;
0068     }
0069     m_isVisible = visible;
0070 
0071     if (m_isVisible) {
0072         m_visibleTimer.stop();
0073         setRunning(true);
0074     } else {
0075         // Delay setRunning(false) to avoid destroying views while still executing JS code.
0076         m_visibleTimer.start();
0077     }
0078 
0079     Q_EMIT visibleChanged();
0080 }
0081 
0082 KConfigPropertyMap *ScriptedQuickSceneEffect::configuration() const
0083 {
0084     return m_configuration;
0085 }
0086 
0087 QQmlListProperty<QObject> ScriptedQuickSceneEffect::data()
0088 {
0089     return QQmlListProperty<QObject>(this, nullptr,
0090                                      data_append,
0091                                      data_count,
0092                                      data_at,
0093                                      data_clear);
0094 }
0095 
0096 void ScriptedQuickSceneEffect::data_append(QQmlListProperty<QObject> *objects, QObject *object)
0097 {
0098     if (!object) {
0099         return;
0100     }
0101 
0102     ScriptedQuickSceneEffect *effect = static_cast<ScriptedQuickSceneEffect *>(objects->object);
0103     if (!effect->m_children.contains(object)) {
0104         object->setParent(effect);
0105         effect->m_children.append(object);
0106     }
0107 }
0108 
0109 qsizetype ScriptedQuickSceneEffect::data_count(QQmlListProperty<QObject> *objects)
0110 {
0111     ScriptedQuickSceneEffect *effect = static_cast<ScriptedQuickSceneEffect *>(objects->object);
0112     return effect->m_children.count();
0113 }
0114 
0115 QObject *ScriptedQuickSceneEffect::data_at(QQmlListProperty<QObject> *objects, qsizetype index)
0116 {
0117     ScriptedQuickSceneEffect *effect = static_cast<ScriptedQuickSceneEffect *>(objects->object);
0118     return effect->m_children.value(index);
0119 }
0120 
0121 void ScriptedQuickSceneEffect::data_clear(QQmlListProperty<QObject> *objects)
0122 {
0123     ScriptedQuickSceneEffect *effect = static_cast<ScriptedQuickSceneEffect *>(objects->object);
0124     while (!effect->m_children.isEmpty()) {
0125         QObject *child = effect->m_children.takeLast();
0126         child->setParent(nullptr);
0127     }
0128 }
0129 
0130 } // namespace KWin
0131 
0132 #include "moc_scriptedquicksceneeffect.cpp"