1 /*
2 KWin - the KDE window manager
3 This file is part of the KDE project.
4
5 SPDX-FileCopyrightText: 2018 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
6
7 SPDX-License-Identifier: GPL-2.0-or-later
8 */
9
10 #include "animationsmodel.h"
11
12 #include <KConfigGroup>
13
14 namespace KWin
15 {
16
AnimationsModel(QObject * parent)17 AnimationsModel::AnimationsModel(QObject *parent)
18 : EffectsModel(parent)
19 {
20 connect(this, &EffectsModel::loaded, this,
21 [this] {
22 setAnimationEnabled(modelAnimationEnabled());
23 setAnimationIndex(modelAnimationIndex());
24 loadDefaults();
25 }
26 );
27 connect(this, &AnimationsModel::animationIndexChanged, this,
28 [this] {
29 const QModelIndex index_ = index(m_animationIndex, 0);
30 if (!index_.isValid()) {
31 return;
32 }
33 const bool configurable = index_.data(ConfigurableRole).toBool();
34 if (configurable != m_currentConfigurable) {
35 m_currentConfigurable = configurable;
36 Q_EMIT currentConfigurableChanged();
37 }
38 }
39 );
40 }
41
animationEnabled() const42 bool AnimationsModel::animationEnabled() const
43 {
44 return m_animationEnabled;
45 }
46
setAnimationEnabled(bool enabled)47 void AnimationsModel::setAnimationEnabled(bool enabled)
48 {
49 if (m_animationEnabled != enabled) {
50 m_animationEnabled = enabled;
51 Q_EMIT animationEnabledChanged();
52 }
53 }
54
animationIndex() const55 int AnimationsModel::animationIndex() const
56 {
57 return m_animationIndex;
58 }
59
setAnimationIndex(int index)60 void AnimationsModel::setAnimationIndex(int index)
61 {
62 if (m_animationIndex != index) {
63 m_animationIndex = index;
64 Q_EMIT animationIndexChanged();
65 }
66 }
67
currentConfigurable() const68 bool AnimationsModel::currentConfigurable() const
69 {
70 return m_currentConfigurable;
71 }
72
defaultAnimationEnabled() const73 bool AnimationsModel::defaultAnimationEnabled() const
74 {
75 return m_defaultAnimationEnabled;
76 }
77
defaultAnimationIndex() const78 int AnimationsModel::defaultAnimationIndex() const
79 {
80 return m_defaultAnimationIndex;
81 }
82
shouldStore(const EffectData & data) const83 bool AnimationsModel::shouldStore(const EffectData &data) const
84 {
85 return data.untranslatedCategory.contains(
86 QStringLiteral("Virtual Desktop Switching Animation"), Qt::CaseInsensitive);
87 }
88
status(int row) const89 EffectsModel::Status AnimationsModel::status(int row) const
90 {
91 return Status(data(index(row, 0), static_cast<int>(StatusRole)).toInt());
92 }
93
loadDefaults()94 void AnimationsModel::loadDefaults()
95 {
96 for (int i = 0; i < rowCount(); ++i) {
97 const QModelIndex rowIndex = index(i, 0);
98 if (rowIndex.data(EnabledByDefaultRole).toBool()) {
99 m_defaultAnimationEnabled = true;
100 m_defaultAnimationIndex = i;
101 Q_EMIT defaultAnimationEnabledChanged();
102 Q_EMIT defaultAnimationIndexChanged();
103 break;
104 }
105 }
106 }
107
modelAnimationEnabled() const108 bool AnimationsModel::modelAnimationEnabled() const
109 {
110 for (int i = 0; i < rowCount(); ++i) {
111 if (status(i) != Status::Disabled) {
112 return true;
113 }
114 }
115
116 return false;
117 }
118
modelAnimationIndex() const119 int AnimationsModel::modelAnimationIndex() const
120 {
121 for (int i = 0; i < rowCount(); ++i) {
122 if (status(i) != Status::Disabled) {
123 return i;
124 }
125 }
126
127 return 0;
128 }
129
load()130 void AnimationsModel::load()
131 {
132 EffectsModel::load();
133 }
134
save()135 void AnimationsModel::save()
136 {
137 for (int i = 0; i < rowCount(); ++i) {
138 const auto status = (m_animationEnabled && i == m_animationIndex)
139 ? EffectsModel::Status::Enabled
140 : EffectsModel::Status::Disabled;
141 updateEffectStatus(index(i, 0), status);
142 }
143
144 EffectsModel::save();
145 }
146
defaults()147 void AnimationsModel::defaults()
148 {
149 EffectsModel::defaults();
150 setAnimationEnabled(modelAnimationEnabled());
151 setAnimationIndex(modelAnimationIndex());
152 }
153
isDefaults() const154 bool AnimationsModel::isDefaults() const
155 {
156 // effect at m_animationIndex index may not be the current saved selected effect
157 const bool enabledByDefault = index(m_animationIndex, 0).data(EnabledByDefaultRole).toBool();
158 return enabledByDefault;
159 }
160
needsSave() const161 bool AnimationsModel::needsSave() const
162 {
163 KConfigGroup kwinConfig(KSharedConfig::openConfig("kwinrc"), "Plugins");
164
165 for (int i = 0; i < rowCount(); ++i) {
166 const QModelIndex index_ = index(i, 0);
167 const bool enabledConfig = kwinConfig.readEntry(
168 index_.data(ServiceNameRole).toString() + QLatin1String("Enabled"),
169 index_.data(EnabledByDefaultRole).toBool()
170 );
171 const bool enabled = (m_animationEnabled && i == m_animationIndex);
172
173 if (enabled != enabledConfig) {
174 return true;
175 }
176 }
177
178 return false;
179 }
180
181 }
182