File indexing completed on 2024-11-10 04:56:46
0001 /* 0002 KWin - the KDE window manager 0003 This file is part of the KDE project. 0004 0005 SPDX-FileCopyrightText: 2013 Antonis Tsiapaliokas <kok3rs@gmail.com> 0006 SPDX-FileCopyrightText: 2018 Vlad Zahorodnii <vlad.zahorodnii@kde.org> 0007 0008 SPDX-License-Identifier: GPL-2.0-or-later 0009 */ 0010 0011 #pragma once 0012 0013 #include <kwin_export.h> 0014 0015 #include <KSharedConfig> 0016 0017 #include <QAbstractItemModel> 0018 #include <QString> 0019 #include <QUrl> 0020 #include <QWindow> 0021 0022 namespace KWin 0023 { 0024 0025 class KWIN_EXPORT EffectsModel : public QAbstractItemModel 0026 { 0027 Q_OBJECT 0028 0029 public: 0030 /** 0031 * This enum type is used to specify data roles. 0032 */ 0033 enum AdditionalRoles { 0034 /** 0035 * The user-friendly name of the effect. 0036 */ 0037 NameRole = Qt::UserRole + 1, 0038 /** 0039 * The description of the effect. 0040 */ 0041 DescriptionRole, 0042 /** 0043 * The name of the effect's author. If there are several authors, they 0044 * will be comma separated. 0045 */ 0046 AuthorNameRole, 0047 /** 0048 * The email of the effect's author. If there are several authors, the 0049 * emails will be comma separated. 0050 */ 0051 AuthorEmailRole, 0052 /** 0053 * The license of the effect. 0054 */ 0055 LicenseRole, 0056 /** 0057 * The version of the effect. 0058 */ 0059 VersionRole, 0060 /** 0061 * The category of the effect. 0062 */ 0063 CategoryRole, 0064 /** 0065 * The service name(plugin name) of the effect. 0066 */ 0067 ServiceNameRole, 0068 /** 0069 * The icon name of the effect. 0070 */ 0071 IconNameRole, 0072 /** 0073 * Whether the effect is enabled or disabled. 0074 */ 0075 StatusRole, 0076 /** 0077 * Link to a video demonstration of the effect. 0078 */ 0079 VideoRole, 0080 /** 0081 * Link to the home page of the effect. 0082 */ 0083 WebsiteRole, 0084 /** 0085 * Whether the effect is supported. 0086 */ 0087 SupportedRole, 0088 /** 0089 * The exclusive group of the effect. 0090 */ 0091 ExclusiveRole, 0092 /** 0093 * Whether the effect is internal. 0094 */ 0095 InternalRole, 0096 /** 0097 * Whether the effect has a KCM. 0098 */ 0099 ConfigurableRole, 0100 /** 0101 * Whether the effect is enabled by default. 0102 */ 0103 EnabledByDefaultRole, 0104 /** 0105 * Id of the effect's config module, empty if the effect has no config. 0106 */ 0107 ConfigModuleRole, 0108 /** 0109 * Whether the effect has a function to determine if the effect is enabled by default. 0110 */ 0111 EnabledByDefaultFunctionRole, 0112 }; 0113 0114 /** 0115 * This enum type is used to specify the status of a given effect. 0116 */ 0117 enum class Status { 0118 /** 0119 * The effect is disabled. 0120 */ 0121 Disabled = Qt::Unchecked, 0122 /** 0123 * An enable function is used to determine whether the effect is enabled. 0124 * For example, such function can be useful to disable the blur effect 0125 * when running in a virtual machine. 0126 */ 0127 EnabledUndeterminded = Qt::PartiallyChecked, 0128 /** 0129 * The effect is enabled. 0130 */ 0131 Enabled = Qt::Checked 0132 }; 0133 0134 explicit EffectsModel(QObject *parent = nullptr); 0135 0136 // Reimplemented from QAbstractItemModel. 0137 QHash<int, QByteArray> roleNames() const override; 0138 QModelIndex index(int row, int column, const QModelIndex &parent = {}) const override; 0139 QModelIndex parent(const QModelIndex &child) const override; 0140 int rowCount(const QModelIndex &parent = {}) const override; 0141 int columnCount(const QModelIndex &parent = {}) const override; 0142 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; 0143 bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; 0144 0145 /** 0146 * Changes the status of a given effect. 0147 * 0148 * @param rowIndex An effect represented by the given index. 0149 * @param effectState The new state. 0150 * @note In order to actually apply the change, you have to call save(). 0151 */ 0152 void updateEffectStatus(const QModelIndex &rowIndex, Status effectState); 0153 0154 /** 0155 * This enum type is used to specify load options. 0156 */ 0157 enum class LoadOptions { 0158 None, 0159 /** 0160 * Do not discard unsaved changes when reloading the model. 0161 */ 0162 KeepDirty 0163 }; 0164 0165 /** 0166 * Loads effects. 0167 * 0168 * You have to call this method in order to populate the model. 0169 */ 0170 void load(LoadOptions options = LoadOptions::None); 0171 0172 /** 0173 * Saves status of each modified effect. 0174 */ 0175 void save(); 0176 0177 /** 0178 * Resets the status of each effect to the default state. 0179 * 0180 * @note In order to actually apply the change, you have to call save(). 0181 */ 0182 void defaults(); 0183 0184 /** 0185 * Whether the status of each effect is its default state. 0186 */ 0187 bool isDefaults() const; 0188 0189 /** 0190 * Whether the model has unsaved changes. 0191 */ 0192 bool needsSave() const; 0193 0194 /** 0195 * Finds an effect with the given plugin id. 0196 */ 0197 QModelIndex findByPluginId(const QString &pluginId) const; 0198 0199 /** 0200 * Shows a configuration dialog for a given effect. 0201 * 0202 * @param index An effect represented by the given index. 0203 * @param transientParent The transient parent of the configuration dialog. 0204 */ 0205 void requestConfigure(const QModelIndex &index, QWindow *transientParent); 0206 0207 Q_SIGNALS: 0208 /** 0209 * This signal is emitted when the model is loaded or reloaded. 0210 * 0211 * @see load 0212 */ 0213 void loaded(); 0214 0215 protected: 0216 struct EffectData 0217 { 0218 QString name; 0219 QString description; 0220 QString authorName; 0221 QString authorEmail; 0222 QString license; 0223 QString version; 0224 QString untranslatedCategory; 0225 QString category; 0226 QString serviceName; 0227 QString iconName; 0228 Status status; 0229 Status originalStatus; 0230 bool enabledByDefault; 0231 bool enabledByDefaultFunction; 0232 QUrl video; 0233 QUrl website; 0234 bool supported; 0235 QString exclusiveGroup; 0236 bool internal; 0237 bool changed = false; 0238 QString configModule; 0239 QVariantList configArgs; 0240 }; 0241 0242 /** 0243 * Returns whether the given effect should be stored in the model. 0244 * 0245 * @param data The effect. 0246 * @returns @c true if the effect should be stored, otherwise @c false. 0247 */ 0248 virtual bool shouldStore(const EffectData &data) const; 0249 0250 private: 0251 void loadBuiltInEffects(const KConfigGroup &kwinConfig); 0252 void loadJavascriptEffects(const KConfigGroup &kwinConfig); 0253 void loadPluginEffects(const KConfigGroup &kwinConfig); 0254 0255 QList<EffectData> m_effects; 0256 QList<EffectData> m_pendingEffects; 0257 int m_lastSerial = -1; 0258 0259 Q_DISABLE_COPY(EffectsModel) 0260 }; 0261 0262 }