File indexing completed on 2024-11-10 04:56:48

0001 /*
0002     KWin - the KDE window manager
0003     This file is part of the KDE project.
0004 
0005     SPDX-FileCopyrightText: 2018 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include "animationsmodel.h"
0011 
0012 #include <KConfigGroup>
0013 
0014 namespace KWin
0015 {
0016 
0017 AnimationsModel::AnimationsModel(QObject *parent)
0018     : EffectsModel(parent)
0019 {
0020     connect(this, &EffectsModel::loaded, this, [this]() {
0021         setAnimationEnabled(modelAnimationEnabled());
0022         setAnimationIndex(modelAnimationIndex());
0023         loadDefaults();
0024     });
0025     connect(this, &AnimationsModel::animationIndexChanged, this, [this]() {
0026         const QModelIndex index_ = index(m_animationIndex, 0);
0027         if (!index_.isValid()) {
0028             return;
0029         }
0030         const bool configurable = index_.data(ConfigurableRole).toBool();
0031         if (configurable != m_currentConfigurable) {
0032             m_currentConfigurable = configurable;
0033             Q_EMIT currentConfigurableChanged();
0034         }
0035     });
0036 }
0037 
0038 bool AnimationsModel::animationEnabled() const
0039 {
0040     return m_animationEnabled;
0041 }
0042 
0043 void AnimationsModel::setAnimationEnabled(bool enabled)
0044 {
0045     if (m_animationEnabled != enabled) {
0046         m_animationEnabled = enabled;
0047         Q_EMIT animationEnabledChanged();
0048     }
0049 }
0050 
0051 int AnimationsModel::animationIndex() const
0052 {
0053     return m_animationIndex;
0054 }
0055 
0056 void AnimationsModel::setAnimationIndex(int index)
0057 {
0058     if (m_animationIndex != index) {
0059         m_animationIndex = index;
0060         Q_EMIT animationIndexChanged();
0061     }
0062 }
0063 
0064 bool AnimationsModel::currentConfigurable() const
0065 {
0066     return m_currentConfigurable;
0067 }
0068 
0069 bool AnimationsModel::defaultAnimationEnabled() const
0070 {
0071     return m_defaultAnimationEnabled;
0072 }
0073 
0074 int AnimationsModel::defaultAnimationIndex() const
0075 {
0076     return m_defaultAnimationIndex;
0077 }
0078 
0079 bool AnimationsModel::shouldStore(const EffectData &data) const
0080 {
0081     return data.untranslatedCategory.contains(
0082         QStringLiteral("Virtual Desktop Switching Animation"), Qt::CaseInsensitive);
0083 }
0084 
0085 EffectsModel::Status AnimationsModel::status(int row) const
0086 {
0087     return Status(data(index(row, 0), static_cast<int>(StatusRole)).toInt());
0088 }
0089 
0090 void AnimationsModel::loadDefaults()
0091 {
0092     for (int i = 0; i < rowCount(); ++i) {
0093         const QModelIndex rowIndex = index(i, 0);
0094         if (rowIndex.data(EnabledByDefaultRole).toBool()) {
0095             m_defaultAnimationEnabled = true;
0096             m_defaultAnimationIndex = i;
0097             Q_EMIT defaultAnimationEnabledChanged();
0098             Q_EMIT defaultAnimationIndexChanged();
0099             break;
0100         }
0101     }
0102 }
0103 
0104 bool AnimationsModel::modelAnimationEnabled() const
0105 {
0106     for (int i = 0; i < rowCount(); ++i) {
0107         if (status(i) != Status::Disabled) {
0108             return true;
0109         }
0110     }
0111 
0112     return false;
0113 }
0114 
0115 int AnimationsModel::modelAnimationIndex() const
0116 {
0117     for (int i = 0; i < rowCount(); ++i) {
0118         if (status(i) != Status::Disabled) {
0119             return i;
0120         }
0121     }
0122 
0123     return 0;
0124 }
0125 
0126 void AnimationsModel::load()
0127 {
0128     EffectsModel::load();
0129 }
0130 
0131 void AnimationsModel::save()
0132 {
0133     for (int i = 0; i < rowCount(); ++i) {
0134         const auto status = (m_animationEnabled && i == m_animationIndex)
0135             ? EffectsModel::Status::Enabled
0136             : EffectsModel::Status::Disabled;
0137         updateEffectStatus(index(i, 0), status);
0138     }
0139 
0140     EffectsModel::save();
0141 }
0142 
0143 void AnimationsModel::defaults()
0144 {
0145     EffectsModel::defaults();
0146     setAnimationEnabled(modelAnimationEnabled());
0147     setAnimationIndex(modelAnimationIndex());
0148 }
0149 
0150 bool AnimationsModel::isDefaults() const
0151 {
0152     // effect at m_animationIndex index may not be the current saved selected effect
0153     const bool enabledByDefault = index(m_animationIndex, 0).data(EnabledByDefaultRole).toBool();
0154     return enabledByDefault;
0155 }
0156 
0157 bool AnimationsModel::needsSave() const
0158 {
0159     KConfigGroup kwinConfig(KSharedConfig::openConfig("kwinrc"), QStringLiteral("Plugins"));
0160 
0161     for (int i = 0; i < rowCount(); ++i) {
0162         const QModelIndex index_ = index(i, 0);
0163         const bool enabledConfig = kwinConfig.readEntry(
0164             index_.data(ServiceNameRole).toString() + QLatin1String("Enabled"),
0165             index_.data(EnabledByDefaultRole).toBool());
0166         const bool enabled = (m_animationEnabled && i == m_animationIndex);
0167 
0168         if (enabled != enabledConfig) {
0169             return true;
0170         }
0171     }
0172 
0173     return false;
0174 }
0175 
0176 }
0177 
0178 #include "moc_animationsmodel.cpp"