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 #include "ruleitem.h"
8 
9 
10 namespace KWin
11 {
12 
RuleItem(const QString & key,const RulePolicy::Type policyType,const RuleItem::Type type,const QString & name,const QString & section,const QIcon & icon,const QString & description)13 RuleItem::RuleItem(const QString &key,
14                    const RulePolicy::Type policyType,
15                    const RuleItem::Type type,
16                    const QString &name,
17                    const QString &section,
18                    const QIcon &icon,
19                    const QString &description)
20     : m_key(key)
21     , m_type(type)
22     , m_name(name)
23     , m_section(section)
24     , m_icon(icon)
25     , m_description(description)
26     , m_flags(NoFlags)
27     , m_enabled(false)
28     , m_policy(new RulePolicy(policyType))
29     , m_options(nullptr)
30     , m_optionsMask(0U - 1)
31 {
32     reset();
33 }
34 
~RuleItem()35 RuleItem::~RuleItem()
36 {
37     delete m_policy;
38     delete m_options;
39 }
40 
reset()41 void RuleItem::reset()
42 {
43     m_enabled = hasFlag(AlwaysEnabled) | hasFlag(StartEnabled);
44     m_value = typedValue(QVariant());
45     m_suggestedValue = QVariant();
46     m_policy->resetValue();
47     if (m_options) {
48         m_options->resetValue();
49     }
50 }
51 
key() const52 QString RuleItem::key() const
53 {
54     return m_key;
55 }
56 
name() const57 QString RuleItem::name() const
58 {
59     return m_name;
60 }
61 
section() const62 QString RuleItem::section() const
63 {
64     return m_section;
65 }
66 
iconName() const67 QString RuleItem::iconName() const
68 {
69     return m_icon.name();
70 }
71 
icon() const72 QIcon RuleItem::icon() const
73 {
74     return m_icon;
75 }
76 
description() const77 QString RuleItem::description() const
78 {
79     return m_description;
80 }
81 
isEnabled() const82 bool RuleItem::isEnabled() const
83 {
84     return m_enabled;
85 }
86 
setEnabled(bool enabled)87 void RuleItem::setEnabled(bool enabled)
88 {
89     m_enabled = (enabled && !hasFlag(SuggestionOnly)) || hasFlag(AlwaysEnabled);
90 }
91 
hasFlag(RuleItem::Flags flag) const92 bool RuleItem::hasFlag(RuleItem::Flags flag) const
93 {
94     return m_flags.testFlag(flag);
95 }
96 
setFlag(RuleItem::Flags flag,bool active)97 void RuleItem::setFlag(RuleItem::Flags flag, bool active)
98 {
99     m_flags.setFlag(flag, active);
100 }
101 
type() const102 RuleItem::Type RuleItem::type() const
103 {
104     return m_type;
105 }
106 
value() const107 QVariant RuleItem::value() const
108 {
109     if (m_options && m_type == Option) {
110         return m_options->value();
111     }
112     return m_value;
113 }
114 
setValue(QVariant value)115 void RuleItem::setValue(QVariant value)
116 {
117     if (m_options && m_type == Option) {
118         m_options->setValue(value);
119     }
120     m_value = typedValue(value);
121 }
122 
suggestedValue() const123 QVariant RuleItem::suggestedValue() const
124 {
125     return m_suggestedValue;
126 }
127 
setSuggestedValue(QVariant value)128 void RuleItem::setSuggestedValue(QVariant value)
129 {
130     m_suggestedValue = value.isNull() ? QVariant() : typedValue(value);
131 }
132 
options() const133 QVariant RuleItem::options() const
134 {
135     if (!m_options) {
136         return QVariant();
137     }
138     return QVariant::fromValue(m_options);
139 }
140 
setOptionsData(const QList<OptionsModel::Data> & data)141 void RuleItem::setOptionsData(const QList<OptionsModel::Data> &data)
142 {
143     if (m_type != Option && m_type != OptionList && m_type != NetTypes) {
144         return;
145     }
146     if (!m_options) {
147         m_options = new OptionsModel();
148     }
149     m_options->updateModelData(data);
150     m_options->setValue(m_value);
151 
152     if (m_type == NetTypes) {
153         m_optionsMask = 0;
154         for (const OptionsModel::Data &dataItem : data) {
155             m_optionsMask += 1 << dataItem.value.toUInt();
156         }
157     }
158 }
159 
optionsMask() const160 uint RuleItem::optionsMask() const
161 {
162     return m_optionsMask;
163 }
164 
policy() const165 int RuleItem::policy() const
166 {
167     return m_policy->value();
168 }
169 
setPolicy(int policy)170 void RuleItem::setPolicy(int policy)
171 {
172     m_policy->setValue(policy);
173 }
174 
policyType() const175 RulePolicy::Type RuleItem::policyType() const
176 {
177     return m_policy->type();
178 }
179 
policyModel() const180 QVariant RuleItem::policyModel() const
181 {
182     return QVariant::fromValue(m_policy);
183 }
184 
policyKey() const185 QString RuleItem::policyKey() const
186 {
187     return m_policy->policyKey(m_key);
188 }
189 
typedValue(const QVariant & value) const190 QVariant RuleItem::typedValue(const QVariant &value) const
191 {
192     switch (type()) {
193         case Undefined:
194         case Option:
195             return value;
196         case Boolean:
197             return value.toBool();
198         case Integer:
199         case Percentage:
200             return value.toInt();
201         case NetTypes: {
202             const uint typesMask = value.toUInt() & optionsMask();  // filter by the allowed mask in the model
203             if (typesMask == 0 || typesMask == optionsMask()) {     // if no types or all of them are selected
204                 return 0U - 1;                                      // return an all active mask (NET:AllTypesMask)
205             }
206             return typesMask;
207         }
208         case Point: {
209             const QPoint point = value.toPoint();
210             return (point == invalidPoint) ? QPoint(0, 0) : point;
211         }
212         case Size:
213             return value.toSize();
214         case String:
215             if (value.type() == QVariant::StringList && !value.toStringList().isEmpty()) {
216                 return value.toStringList().at(0).trimmed();
217             }
218             return value.toString().trimmed();
219         case Shortcut:
220             return value.toString();
221         case OptionList:
222             return value.toStringList();
223     }
224     return value;
225 }
226 
227 }   //namespace
228 
229