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

0001 /*
0002     SPDX-FileCopyrightText: 2020 Ismael Asensio <isma.af@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #pragma once
0008 
0009 #include <rules.h>
0010 
0011 #include <QAbstractListModel>
0012 #include <QIcon>
0013 #include <QVariant>
0014 
0015 namespace KWin
0016 {
0017 
0018 class OptionsModel : public QAbstractListModel
0019 {
0020     Q_OBJECT
0021     Q_PROPERTY(int selectedIndex READ selectedIndex NOTIFY selectedIndexChanged)
0022     Q_PROPERTY(int allOptionsMask READ allOptionsMask NOTIFY modelUpdated)
0023     Q_PROPERTY(int useFlags READ useFlags CONSTANT)
0024 
0025 public:
0026     enum OptionsRole {
0027         ValueRole = Qt::UserRole,
0028         IconNameRole,
0029         OptionTypeRole, // The type of an option item, defaults to NormalOption
0030         BitMaskRole,
0031     };
0032     Q_ENUM(OptionsRole)
0033 
0034     enum OptionType {
0035         NormalOption = 0, /**< Normal option */
0036         ExclusiveOption, /**< An exclusive option, so all other option items are deselected when this one is selected */
0037         SelectAllOption, /**< All option items are selected when this option item is selected */
0038     };
0039     Q_ENUM(OptionType)
0040 
0041     struct Data
0042     {
0043         Data(const QVariant &value, const QString &text, const QIcon &icon = {}, const QString &description = {}, OptionType optionType = NormalOption)
0044             : value(value)
0045             , text(text)
0046             , icon(icon)
0047             , description(description)
0048             , optionType(optionType)
0049         {
0050         }
0051         Data(const QVariant &value, const QString &text, const QString &description)
0052             : value(value)
0053             , text(text)
0054             , description(description)
0055         {
0056         }
0057 
0058         QVariant value;
0059         QString text;
0060         QIcon icon;
0061         QString description;
0062         OptionType optionType = NormalOption;
0063     };
0064 
0065 public:
0066     OptionsModel(QList<Data> data = {}, bool useFlags = false)
0067         : QAbstractListModel()
0068         , m_data(data)
0069         , m_index(0)
0070         , m_useFlags(useFlags){};
0071 
0072     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0073     QHash<int, QByteArray> roleNames() const override;
0074     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0075 
0076     QVariant value() const;
0077     void setValue(QVariant value);
0078     void resetValue();
0079 
0080     bool useFlags() const;
0081     QVariant allValues() const;
0082     uint allOptionsMask() const;
0083 
0084     void updateModelData(const QList<Data> &data);
0085 
0086     Q_INVOKABLE int indexOf(const QVariant &value) const;
0087     Q_INVOKABLE QString textOfValue(const QVariant &value) const;
0088     int selectedIndex() const;
0089     uint bitMask(int index) const;
0090 
0091 Q_SIGNALS:
0092     void selectedIndexChanged(int index);
0093     void modelUpdated();
0094 
0095 public:
0096     QList<Data> m_data;
0097 
0098 protected:
0099     int m_index = 0;
0100     bool m_useFlags = false;
0101 };
0102 
0103 class RulePolicy : public OptionsModel
0104 {
0105 public:
0106     enum Type {
0107         NoPolicy,
0108         StringMatch,
0109         SetRule,
0110         ForceRule
0111     };
0112 
0113 public:
0114     RulePolicy(Type type)
0115         : OptionsModel(policyOptions(type))
0116         , m_type(type){};
0117 
0118     Type type() const;
0119     int value() const;
0120     QString policyKey(const QString &key) const;
0121 
0122 private:
0123     static QList<Data> policyOptions(RulePolicy::Type type);
0124 
0125 private:
0126     Type m_type;
0127 };
0128 
0129 } // namespace