Commit 66ee8985 authored by Ismael Asensio's avatar Ismael Asensio
Browse files

kcm/kwinrules: Allow to select multiple activities

This commit addresses the UI part

BUG: 430499
parent fc1553e8
...@@ -69,15 +69,6 @@ int OptionsModel::indexOf(QVariant value) const ...@@ -69,15 +69,6 @@ int OptionsModel::indexOf(QVariant value) const
return -1; return -1;
} }
QString OptionsModel::textOfValue(QVariant value) const
{
int index = indexOf(value);
if (index < 0 || index >= m_data.count()) {
return QString();
}
return m_data.at(index).text;
}
QVariant OptionsModel::value() const QVariant OptionsModel::value() const
{ {
if (m_data.isEmpty()) { if (m_data.isEmpty()) {
......
...@@ -56,7 +56,7 @@ public: ...@@ -56,7 +56,7 @@ public:
void updateModelData(const QList<Data> &data); void updateModelData(const QList<Data> &data);
Q_INVOKABLE int indexOf(QVariant value) const; Q_INVOKABLE int indexOf(QVariant value) const;
Q_INVOKABLE QString textOfValue(QVariant value) const;
int selectedIndex() const; int selectedIndex() const;
signals: signals:
......
...@@ -18,6 +18,9 @@ QQC2.ComboBox { ...@@ -18,6 +18,9 @@ QQC2.ComboBox {
valueRole: "value" valueRole: "value"
property bool multipleChoice: false property bool multipleChoice: false
// If `useFlagsValue` is true, `selectionMask` will be composed using the item values.
// Otherwise, it will use the item indexes.
property bool useFlagsValue: false
property int selectionMask: 0 property int selectionMask: 0
currentIndex: multipleChoice ? -1 : model.selectedIndex currentIndex: multipleChoice ? -1 : model.selectedIndex
...@@ -31,8 +34,9 @@ QQC2.ComboBox { ...@@ -31,8 +34,9 @@ QQC2.ComboBox {
case 0: case 0:
return i18n("None selected"); return i18n("None selected");
case 1: case 1:
var selectedValue = selectionMask.toString(2).length - 1; let selectedBit = selectionMask.toString(2).length - 1;
return model.textOfValue(selectedValue); let selectedIndex = (useFlagsValue) ? model.indexOf(selectedBit) : selectedBit
return model.data(model.index(selectedIndex, 0), Qt.DisplayRole);
case count: case count:
return i18n("All selected"); return i18n("All selected");
} }
...@@ -47,9 +51,10 @@ QQC2.ComboBox { ...@@ -47,9 +51,10 @@ QQC2.ComboBox {
QQC2.CheckBox { QQC2.CheckBox {
id: itemSelection id: itemSelection
visible: multipleChoice visible: multipleChoice
checked: (selectionMask & (1 << value)) readonly property int bit: (useFlagsValue) ? value : index
checked: (selectionMask & (1 << bit))
onToggled: { onToggled: {
selectionMask = (selectionMask & ~(1 << value)) | (checked << value); selectionMask = (selectionMask & ~(1 << bit)) | (checked << bit);
activated(index); activated(index);
} }
} }
......
...@@ -34,6 +34,7 @@ Loader { ...@@ -34,6 +34,7 @@ Loader {
case RuleItem.Point: return coordinateEditor case RuleItem.Point: return coordinateEditor
case RuleItem.Size: return coordinateEditor case RuleItem.Size: return coordinateEditor
case RuleItem.Shortcut: return shortcutEditor case RuleItem.Shortcut: return shortcutEditor
case RuleItem.OptionList: return optionListEditor
default: return emptyEditor default: return emptyEditor
} }
} }
...@@ -104,6 +105,7 @@ Loader { ...@@ -104,6 +105,7 @@ Loader {
flat: true flat: true
model: ruleOptions model: ruleOptions
multipleChoice: true multipleChoice: true
useFlagsValue: true
// Filter the provided value with the options mask // Filter the provided value with the options mask
selectionMask: ruleValue & optionsMask selectionMask: ruleValue & optionsMask
onActivated: { onActivated: {
...@@ -112,6 +114,33 @@ Loader { ...@@ -112,6 +114,33 @@ Loader {
} }
} }
Component {
id: optionListEditor
OptionsComboBox {
flat: true
model: ruleOptions
multipleChoice: true
selectionMask: {
let mask = 0
for (let i = 0; i < count; i++) {
if (ruleValue.includes(model.data(model.index(i,0), Qt.UserRole))) {
mask += 1 << i
}
}
return mask
}
onActivated: {
let selectionList = []
for (let i = 0; i < count; i++) {
if (selectionMask & (1 << i)) {
selectionList.push(model.data(model.index(i,0), Qt.UserRole))
}
}
valueEditor.valueEdited(selectionList);
}
}
}
Component { Component {
id: percentageEditor id: percentageEditor
RowLayout { RowLayout {
......
...@@ -140,10 +140,10 @@ QVariant RuleItem::options() const ...@@ -140,10 +140,10 @@ QVariant RuleItem::options() const
void RuleItem::setOptionsData(const QList<OptionsModel::Data> &data) void RuleItem::setOptionsData(const QList<OptionsModel::Data> &data)
{ {
if (m_type != Option && m_type != OptionList && m_type != NetTypes) {
return;
}
if (!m_options) { if (!m_options) {
if (m_type != Option && m_type != NetTypes) {
return;
}
m_options = new OptionsModel(); m_options = new OptionsModel();
} }
m_options->updateModelData(data); m_options->updateModelData(data);
...@@ -215,6 +215,8 @@ QVariant RuleItem::typedValue(const QVariant &value) const ...@@ -215,6 +215,8 @@ QVariant RuleItem::typedValue(const QVariant &value) const
return value.toString().trimmed(); return value.toString().trimmed();
case Shortcut: case Shortcut:
return value.toString(); return value.toString();
case OptionList:
return value.toStringList();
} }
return value; return value;
} }
......
...@@ -31,7 +31,8 @@ public: ...@@ -31,7 +31,8 @@ public:
Percentage, Percentage,
Point, Point,
Size, Size,
Shortcut Shortcut,
OptionList,
}; };
Q_ENUM(Type) Q_ENUM(Type)
......
...@@ -448,8 +448,8 @@ void RulesModel::populateRuleList() ...@@ -448,8 +448,8 @@ void RulesModel::populateRuleList()
m_activities = new KActivities::Consumer(this); m_activities = new KActivities::Consumer(this);
auto activity = addRule(new RuleItem(QLatin1String("activity"), auto activity = addRule(new RuleItem(QLatin1String("activity"),
RulePolicy::SetRule, RuleItem::Option, RulePolicy::SetRule, RuleItem::OptionList,
i18n("Activity"), i18n("Size & Position"), i18n("Activities"), i18n("Size & Position"),
QIcon::fromTheme("activities"))); QIcon::fromTheme("activities")));
activity->setOptionsData(activitiesModelData()); activity->setOptionsData(activitiesModelData());
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment