KWin
Loading...
Searching...
No Matches
optionsmodel.h
Go to the documentation of this file.
1/*
2 SPDX-FileCopyrightText: 2020 Ismael Asensio <isma.af@gmail.com>
3
4 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
5*/
6
7#pragma once
8
9#include <rules.h>
10
11#include <QAbstractListModel>
12#include <QIcon>
13#include <QVariant>
14
15namespace KWin
16{
17
18class OptionsModel : public QAbstractListModel
19{
20 Q_OBJECT
22 Q_PROPERTY(int allOptionsMask READ allOptionsMask NOTIFY modelUpdated)
23 Q_PROPERTY(int useFlags READ useFlags CONSTANT)
24
25public:
27 ValueRole = Qt::UserRole,
29 OptionTypeRole, // The type of an option item, defaults to NormalOption
31 };
32 Q_ENUM(OptionsRole)
33
39 Q_ENUM(OptionType)
40
41 struct Data
42 {
43 Data(const QVariant &value, const QString &text, const QIcon &icon = {}, const QString &description = {}, OptionType optionType = NormalOption)
44 : value(value)
45 , text(text)
46 , icon(icon)
47 , description(description)
48 , optionType(optionType)
49 {
50 }
51 Data(const QVariant &value, const QString &text, const QString &description)
52 : value(value)
53 , text(text)
54 , description(description)
55 {
56 }
57
58 QVariant value;
59 QString text;
60 QIcon icon;
61 QString description;
63 };
64
65public:
66 OptionsModel(QList<Data> data = {}, bool useFlags = false)
67 : QAbstractListModel()
68 , m_data(data)
69 , m_index(0)
71
72 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
73 QHash<int, QByteArray> roleNames() const override;
74 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
75
76 QVariant value() const;
77 void setValue(QVariant value);
78 void resetValue();
79
80 bool useFlags() const;
81 QVariant allValues() const;
82 uint allOptionsMask() const;
83
84 void updateModelData(const QList<Data> &data);
85
86 Q_INVOKABLE int indexOf(const QVariant &value) const;
87 Q_INVOKABLE QString textOfValue(const QVariant &value) const;
88 int selectedIndex() const;
89 uint bitMask(int index) const;
90
91Q_SIGNALS:
92 void selectedIndexChanged(int index);
94
95public:
96 QList<Data> m_data;
97
98protected:
99 int m_index = 0;
100 bool m_useFlags = false;
101};
102
104{
105public:
112
113public:
115 : OptionsModel(policyOptions(type))
116 , m_type(type){};
117
118 Type type() const;
119 int value() const;
120 QString policyKey(const QString &key) const;
121
122private:
123 static QList<Data> policyOptions(RulePolicy::Type type);
124
125private:
126 Type m_type;
127};
128
129} // namespace
void selectedIndexChanged(int index)
QHash< int, QByteArray > roleNames() const override
uint bitMask(int index) const
void setValue(QVariant value)
Q_INVOKABLE int indexOf(const QVariant &value) const
OptionsModel(QList< Data > data={}, bool useFlags=false)
QVariant allValues() const
Q_INVOKABLE QString textOfValue(const QVariant &value) const
int rowCount(const QModelIndex &parent=QModelIndex()) const override
QList< Data > m_data
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
QVariant value() const
void updateModelData(const QList< Data > &data)
RulePolicy(Type type)
Type type() const
QString policyKey(const QString &key) const
Data(const QVariant &value, const QString &text, const QIcon &icon={}, const QString &description={}, OptionType optionType=NormalOption)
Data(const QVariant &value, const QString &text, const QString &description)