kwin/kcmkwin/kwinrules/ruleitem.cpp
Ismael Asensio 8b9472e0bf [kcm/kwinrules] Fix types property for NET::AllTypesMask
When the user selects all of the types or none of them, the "types"
property must be set to a special value (`NET::AllTypesMask = -1`),
different than the sum of all the flags together.

This re-implements this behaviour as the old KCM, fixing some heuristics
that prevented finding the rule corresponding to the current window.

The enum name that handles this property has been changed to `NetTypes`
to make it more explicit.

BUG: 423214
FIXED-IN: 5.19.3

TEST PLAN:
1. Open the `Application specific settings` on a window via menu
2. Select every "Window Type" (or none of them)
3. Give the rule a different name than the default and save
4. Open it again and check that the same rule is found
2020-06-25 15:44:11 +00:00

225 lines
4.9 KiB
C++

/*
* Copyright (c) 2020 Ismael Asensio <isma.af@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ruleitem.h"
namespace KWin
{
RuleItem::RuleItem(const QString &key,
const RulePolicy::Type policyType,
const RuleItem::Type type,
const QString &name,
const QString &section,
const QIcon &icon,
const QString &description)
: m_key(key)
, m_type(type)
, m_name(name)
, m_section(section)
, m_icon(icon)
, m_description(description)
, m_flags(NoFlags)
, m_enabled(false)
, m_policy(new RulePolicy(policyType))
, m_options(nullptr)
{
reset();
}
RuleItem::~RuleItem()
{
delete m_policy;
delete m_options;
}
void RuleItem::reset()
{
m_enabled = hasFlag(AlwaysEnabled) | hasFlag(StartEnabled);
m_value = typedValue(QVariant(), m_type);
m_suggestedValue = QVariant();
m_policy->resetValue();
if (m_options) {
m_options->resetValue();
}
}
QString RuleItem::key() const
{
return m_key;
}
QString RuleItem::name() const
{
return m_name;
}
QString RuleItem::section() const
{
return m_section;
}
QString RuleItem::iconName() const
{
return m_icon.name();
}
QIcon RuleItem::icon() const
{
return m_icon;
}
QString RuleItem::description() const
{
return m_description;
}
bool RuleItem::isEnabled() const
{
return m_enabled;
}
void RuleItem::setEnabled(bool enabled)
{
m_enabled = enabled | hasFlag(AlwaysEnabled);
}
bool RuleItem::hasFlag(RuleItem::Flags flag) const
{
return m_flags.testFlag(flag);
}
void RuleItem::setFlag(RuleItem::Flags flag, bool active)
{
m_flags.setFlag(flag, active);
}
RuleItem::Type RuleItem::type() const
{
return m_type;
}
QVariant RuleItem::value() const
{
if (m_options && m_type == Option) {
return m_options->value();
}
return m_value;
}
void RuleItem::setValue(QVariant value)
{
if (m_options && m_type == Option) {
m_options->setValue(value);
}
m_value = typedValue(value, m_type);
}
QVariant RuleItem::suggestedValue() const
{
return m_suggestedValue;
}
void RuleItem::setSuggestedValue(QVariant value, bool forceValue)
{
if (forceValue) {
setValue(value);
}
m_suggestedValue = value.isNull() ? QVariant() : typedValue(value, m_type);
}
QVariant RuleItem::options() const
{
if (!m_options) {
return QVariant();
}
return QVariant::fromValue(m_options);
}
void RuleItem::setOptionsData(const QList<OptionsModel::Data> &data)
{
if (!m_options) {
if (m_type != Option && m_type != NetTypes) {
return;
}
m_options = new OptionsModel();
}
m_options->updateModelData(data);
m_options->setValue(m_value);
}
int RuleItem::policy() const
{
return m_policy->value();
}
void RuleItem::setPolicy(int policy)
{
m_policy->setValue(policy);
}
RulePolicy::Type RuleItem::policyType() const
{
return m_policy->type();
}
QVariant RuleItem::policyModel() const
{
return QVariant::fromValue(m_policy);
}
QString RuleItem::policyKey() const
{
return m_policy->policyKey(m_key);
}
QVariant RuleItem::typedValue(const QVariant &value, const RuleItem::Type type)
{
switch (type) {
case Undefined:
case Option:
return value;
case Boolean:
return value.toBool();
case Integer:
case Percentage:
return value.toInt();
case NetTypes: {
const int usefulTypes = value.toInt() & 0x3BF; // remove NET::Override=0x040 (deprecated)
if (usefulTypes == 0 || usefulTypes == 0x3BF) { // if no flags or all of them are selected
return -1; // return NET:AllTypesMask
}
return value.toInt();
}
case Point:
return value.toPoint();
case Size:
return value.toSize();
case String:
return value.toString().trimmed();
case Shortcut:
return value.toString();
}
return value;
}
} //namespace