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 #include "ruleitem.h"
0008 
0009 namespace KWin
0010 {
0011 
0012 RuleItem::RuleItem(const QString &key,
0013                    const RulePolicy::Type policyType,
0014                    const RuleItem::Type type,
0015                    const QString &name,
0016                    const QString &section,
0017                    const QIcon &icon,
0018                    const QString &description)
0019     : m_key(key)
0020     , m_type(type)
0021     , m_name(name)
0022     , m_section(section)
0023     , m_icon(icon)
0024     , m_description(description)
0025     , m_flags(NoFlags)
0026     , m_enabled(false)
0027     , m_policy(std::make_unique<RulePolicy>(policyType))
0028 {
0029     reset();
0030 }
0031 
0032 void RuleItem::reset()
0033 {
0034     m_enabled = hasFlag(AlwaysEnabled) || hasFlag(StartEnabled);
0035     m_value = typedValue(QVariant());
0036     m_suggestedValue = QVariant();
0037     m_policy->resetValue();
0038     if (m_options) {
0039         m_options->resetValue();
0040     }
0041 }
0042 
0043 QString RuleItem::key() const
0044 {
0045     return m_key;
0046 }
0047 
0048 QString RuleItem::name() const
0049 {
0050     return m_name;
0051 }
0052 
0053 QString RuleItem::section() const
0054 {
0055     return m_section;
0056 }
0057 
0058 QString RuleItem::iconName() const
0059 {
0060     return m_icon.name();
0061 }
0062 
0063 QIcon RuleItem::icon() const
0064 {
0065     return m_icon;
0066 }
0067 
0068 QString RuleItem::description() const
0069 {
0070     return m_description;
0071 }
0072 
0073 bool RuleItem::isEnabled() const
0074 {
0075     return m_enabled;
0076 }
0077 
0078 void RuleItem::setEnabled(bool enabled)
0079 {
0080     m_enabled = (enabled && !hasFlag(SuggestionOnly)) || hasFlag(AlwaysEnabled);
0081 }
0082 
0083 bool RuleItem::hasFlag(RuleItem::Flags flag) const
0084 {
0085     return m_flags.testFlag(flag);
0086 }
0087 
0088 void RuleItem::setFlag(RuleItem::Flags flag, bool active)
0089 {
0090     m_flags.setFlag(flag, active);
0091 }
0092 
0093 RuleItem::Type RuleItem::type() const
0094 {
0095     return m_type;
0096 }
0097 
0098 QVariant RuleItem::value() const
0099 {
0100     if (m_options && m_type == Option) {
0101         return m_options->value();
0102     }
0103     return m_value;
0104 }
0105 
0106 void RuleItem::setValue(QVariant value)
0107 {
0108     if (m_options && m_type == Option) {
0109         m_options->setValue(value);
0110     }
0111     m_value = typedValue(value);
0112 }
0113 
0114 QVariant RuleItem::suggestedValue() const
0115 {
0116     return m_suggestedValue;
0117 }
0118 
0119 void RuleItem::setSuggestedValue(QVariant value)
0120 {
0121     m_suggestedValue = value.isNull() ? QVariant() : typedValue(value);
0122 }
0123 
0124 QVariant RuleItem::options() const
0125 {
0126     if (!m_options) {
0127         return QVariant();
0128     }
0129     return QVariant::fromValue(m_options.get());
0130 }
0131 
0132 void RuleItem::setOptionsData(const QList<OptionsModel::Data> &data)
0133 {
0134     if (m_type != Option && m_type != OptionList && m_type != NetTypes) {
0135         return;
0136     }
0137     if (!m_options) {
0138         m_options = std::make_unique<OptionsModel>(QList<OptionsModel::Data>{}, m_type == NetTypes);
0139     }
0140     m_options->updateModelData(data);
0141     m_options->setValue(m_value);
0142 }
0143 
0144 int RuleItem::policy() const
0145 {
0146     return m_policy->value();
0147 }
0148 
0149 void RuleItem::setPolicy(int policy)
0150 {
0151     m_policy->setValue(policy);
0152 }
0153 
0154 RulePolicy::Type RuleItem::policyType() const
0155 {
0156     return m_policy->type();
0157 }
0158 
0159 QVariant RuleItem::policyModel() const
0160 {
0161     return QVariant::fromValue(m_policy.get());
0162 }
0163 
0164 QString RuleItem::policyKey() const
0165 {
0166     return m_policy->policyKey(m_key);
0167 }
0168 
0169 QVariant RuleItem::typedValue(const QVariant &value) const
0170 {
0171     switch (type()) {
0172     case Undefined:
0173     case Option:
0174         return value;
0175     case Boolean:
0176         return value.toBool();
0177     case Integer:
0178     case Percentage:
0179         return value.toInt();
0180     case NetTypes: {
0181         const uint typesMask = m_options ? value.toUInt() & m_options->allOptionsMask() : 0; // filter by the allowed mask in the model
0182         if (typesMask == 0 || typesMask == m_options->allOptionsMask()) { // if no types or all of them are selected
0183             return 0U - 1; // return an all active mask (NET:AllTypesMask)
0184         }
0185         return typesMask;
0186     }
0187     case Point: {
0188         const QPoint point = value.toPoint();
0189         return (point == invalidPoint) ? QPoint(0, 0) : point;
0190     }
0191     case Size:
0192         return value.toSize();
0193     case String:
0194         if (value.type() == QVariant::StringList && !value.toStringList().isEmpty()) {
0195             return value.toStringList().at(0).trimmed();
0196         }
0197         return value.toString().trimmed();
0198     case Shortcut:
0199         return value.toString();
0200     case OptionList:
0201         return value.toStringList();
0202     }
0203     return value;
0204 }
0205 
0206 } // namespace
0207 
0208 #include "moc_ruleitem.cpp"