From 143900a19a31b5bd2e40a07359d621c4bce02b05 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Tue, 13 Mar 2018 18:09:34 +0100 Subject: [PATCH] more work on ruleactionparams and rules in general --- libnymea-common/types/rules.cpp | 20 +++ libnymea-common/types/rules.h | 3 + mea/rulemanager.cpp | 1 + mea/ui/MagicPage.qml | 2 +- mea/ui/magic/SelectRuleActionParamsPage.qml | 162 ++++++++++-------- mea/ui/main.qml | 2 +- .../ParamDescriptorDelegateBase.qml | 5 +- 7 files changed, 114 insertions(+), 81 deletions(-) diff --git a/libnymea-common/types/rules.cpp b/libnymea-common/types/rules.cpp index 149f3793..d2982acf 100644 --- a/libnymea-common/types/rules.cpp +++ b/libnymea-common/types/rules.cpp @@ -1,6 +1,8 @@ #include "rules.h" #include "rule.h" +#include + Rules::Rules(QObject *parent) : QAbstractListModel(parent) { @@ -51,6 +53,9 @@ void Rules::insert(Rule *rule) rule->setParent(this); beginInsertRows(QModelIndex(), m_list.count(), m_list.count()); m_list.append(rule); + connect(rule, &Rule::enabledChanged, this, &Rules::ruleChanged); + connect(rule, &Rule::activeChanged, this, &Rules::ruleChanged); + connect(rule, &Rule::nameChanged, this, &Rules::ruleChanged); endInsertRows(); emit countChanged(); } @@ -85,3 +90,18 @@ Rule *Rules::getRule(const QUuid &ruleId) const } return nullptr; } + +void Rules::ruleChanged() +{ + Rule *rule = dynamic_cast(sender()); + if (!rule) { + return; + } + int idx = m_list.indexOf(rule); + if (idx < 0) { + qDebug() << "Rule not found in list. Discarding changed event."; + return; + } + QModelIndex modelIndex = index(idx); + emit dataChanged(modelIndex, modelIndex, {RoleActive, RoleEnabled, RoleName}); +} diff --git a/libnymea-common/types/rules.h b/libnymea-common/types/rules.h index 693d2b60..60d76aa2 100644 --- a/libnymea-common/types/rules.h +++ b/libnymea-common/types/rules.h @@ -33,6 +33,9 @@ public: signals: void countChanged(); +private slots: + void ruleChanged(); + private: QList m_list; }; diff --git a/mea/rulemanager.cpp b/mea/rulemanager.cpp index da69c4ba..208a7ffa 100644 --- a/mea/rulemanager.cpp +++ b/mea/rulemanager.cpp @@ -112,6 +112,7 @@ void RuleManager::getRulesReply(const QVariantMap ¶ms) qWarning() << "Error getting rules:" << params.value("error").toString(); return; } + qDebug() << "Get Rules reply" << params; foreach (const QVariant &ruleDescriptionVariant, params.value("params").toMap().value("ruleDescriptions").toList()) { QUuid ruleId = ruleDescriptionVariant.toMap().value("id").toUuid(); QString name = ruleDescriptionVariant.toMap().value("name").toString(); diff --git a/mea/ui/MagicPage.qml b/mea/ui/MagicPage.qml index d9851b5e..7a947b1c 100644 --- a/mea/ui/MagicPage.qml +++ b/mea/ui/MagicPage.qml @@ -56,7 +56,7 @@ Page { height: app.iconSize width: height name: "../images/magic.svg" - color: !model.enabled ? "gray" : (model.active ? "red" : app.guhAccent) + color: !model.enabled ? "red" : (model.active ? app.guhAccent : "grey") } Label { diff --git a/mea/ui/magic/SelectRuleActionParamsPage.qml b/mea/ui/magic/SelectRuleActionParamsPage.qml index 0a54b284..570f8afa 100644 --- a/mea/ui/magic/SelectRuleActionParamsPage.qml +++ b/mea/ui/magic/SelectRuleActionParamsPage.qml @@ -26,95 +26,105 @@ Page { onBackPressed: root.backPressed(); } - ColumnLayout { + Flickable { anchors.fill: parent - Repeater { - id: delegateRepeater - model: root.actionType.paramTypes - delegate: ColumnLayout { - Layout.fillWidth: true - property string type: { - if (staticParamRadioButton.checked) { - return "static" - } - if (eventParamRadioButton.checked) { - return "event" - } - return "" - } + contentHeight: contentCol.height - property alias paramType: paramDelegate.paramType - property alias value: paramDelegate.value - property alias eventType: eventDescriptorParamsFilterModel.eventType - property alias eventParamTypeId: eventDescriptorParamsFilterModel.paramTypeId + ColumnLayout { + id: contentCol + width: parent.width - RadioButton { - id: staticParamRadioButton - text: qsTr("Use static value as parameter") - checked: true - } - ParamDelegate { - id: paramDelegate + Repeater { + id: delegateRepeater + model: root.actionType.paramTypes + delegate: ColumnLayout { Layout.fillWidth: true - paramType: root.actionType.paramTypes.get(index) - enabled: staticParamRadioButton.checked - } + property string type: { + if (staticParamRadioButton.checked) { + return "static" + } + if (eventParamRadioButton.checked) { + return "event" + } + return "" + } - RadioButton { - id: eventParamRadioButton - text: qsTr("Use event parameter") - visible: eventParamsComboBox.count > 0 - } - ComboBox { - id: eventParamsComboBox - Layout.fillWidth: true - Layout.margins: app.margins - enabled: eventParamRadioButton.checked - visible: count > 0 - Component.onCompleted: currentIndex = 0; - model: EventDescriptorParamsFilterModel { - id: eventDescriptorParamsFilterModel - eventDescriptor: root.rule.eventDescriptors.count === 1 ? root.rule.eventDescriptors.get(0) : null - property var device: Engine.deviceManager.devices.getDevice(eventDescriptor.deviceId) - property var deviceClass: Engine.deviceManager.deviceClasses.getDeviceClass(device.deviceClassId) - property var eventType: deviceClass.eventTypes.getEventType(eventDescriptor.eventTypeId) - property var paramDescriptor: eventDescriptorParamsFilterModel.eventType.paramTypes.get(eventParamsComboBox.currentIndex) - property var paramTypeId: paramDescriptor.id + property alias paramType: paramDelegate.paramType + property alias value: paramDelegate.value + property alias eventType: eventDescriptorParamsFilterModel.eventType + property alias eventParamTypeId: eventDescriptorParamsFilterModel.paramTypeId + + RadioButton { + id: staticParamRadioButton + text: qsTr("Use static value as parameter") + checked: true } - delegate: ItemDelegate { - width: parent.width - text: eventDescriptorParamsFilterModel.device.name + " - " + eventDescriptorParamsFilterModel.eventType.displayName + " - " + eventDescriptorParamsFilterModel.eventType.paramTypes.getParamType(model.id).displayName + ParamDelegate { + id: paramDelegate + Layout.fillWidth: true + paramType: root.actionType.paramTypes.get(index) + enabled: staticParamRadioButton.checked } - contentItem: Label { - id: eventParamsComboBoxContentItem - anchors.fill: parent - anchors.margins: app.margins - text: eventDescriptorParamsFilterModel.device.name + " - " + eventDescriptorParamsFilterModel.eventType.displayName + " - " + eventDescriptorParamsFilterModel.paramDescriptor.displayName + + RadioButton { + id: eventParamRadioButton + text: qsTr("Use event parameter") + visible: eventParamsComboBox.count > 0 } + ComboBox { + id: eventParamsComboBox + Layout.fillWidth: true + Layout.margins: app.margins + enabled: eventParamRadioButton.checked + visible: count > 0 + Component.onCompleted: currentIndex = 0; + model: EventDescriptorParamsFilterModel { + id: eventDescriptorParamsFilterModel + eventDescriptor: root.rule.eventDescriptors.count === 1 ? root.rule.eventDescriptors.get(0) : null + property var device: Engine.deviceManager.devices.getDevice(eventDescriptor.deviceId) + property var deviceClass: Engine.deviceManager.deviceClasses.getDeviceClass(device.deviceClassId) + property var eventType: deviceClass.eventTypes.getEventType(eventDescriptor.eventTypeId) + property var paramDescriptor: eventDescriptorParamsFilterModel.eventType.paramTypes.get(eventParamsComboBox.currentIndex) + property var paramTypeId: paramDescriptor.id + } + delegate: ItemDelegate { + width: parent.width + text: eventDescriptorParamsFilterModel.device.name + " - " + eventDescriptorParamsFilterModel.eventType.displayName + " - " + eventDescriptorParamsFilterModel.eventType.paramTypes.getParamType(model.id).displayName + } + contentItem: Label { + id: eventParamsComboBoxContentItem + anchors.fill: parent + anchors.margins: app.margins + text: eventDescriptorParamsFilterModel.device.name + " - " + eventDescriptorParamsFilterModel.eventType.displayName + " - " + eventDescriptorParamsFilterModel.paramDescriptor.displayName + } + } + + ThinDivider {} } } - } - Item { - Layout.fillWidth: true - Layout.fillHeight: true - } - Button { - text: "OK" - Layout.fillWidth: true - Layout.margins: app.margins - onClicked: { - var params = []; - for (var i = 0; i < delegateRepeater.count; i++) { - var paramDelegate = delegateRepeater.itemAt(i); - if (paramDelegate.type === "static") { - root.ruleAction.ruleActionParams.setRuleActionParam(paramDelegate.paramType.id, paramDelegate.value) - } else if (paramDelegate.type === "event") { - print("adding event based rule action param", paramDelegate.paramType.id, paramDelegate.eventType.id, paramDelegate.eventParamTypeId) - root.ruleAction.ruleActionParams.setRuleActionParamEvent(paramDelegate.paramType.id, paramDelegate.eventType.id, paramDelegate.eventParamTypeId) + Item { + Layout.fillWidth: true + Layout.fillHeight: true + } + Button { + text: "OK" + Layout.fillWidth: true + Layout.margins: app.margins + onClicked: { + var params = []; + for (var i = 0; i < delegateRepeater.count; i++) { + var paramDelegate = delegateRepeater.itemAt(i); + if (paramDelegate.type === "static") { + root.ruleAction.ruleActionParams.setRuleActionParam(paramDelegate.paramType.id, paramDelegate.value) + } else if (paramDelegate.type === "event") { + print("adding event based rule action param", paramDelegate.paramType.id, paramDelegate.eventType.id, paramDelegate.eventParamTypeId) + root.ruleAction.ruleActionParams.setRuleActionParamEvent(paramDelegate.paramType.id, paramDelegate.eventType.id, paramDelegate.eventParamTypeId) + } } + root.completed() } - root.completed() } } } + } diff --git a/mea/ui/main.qml b/mea/ui/main.qml index 09d645b4..e628fce3 100644 --- a/mea/ui/main.qml +++ b/mea/ui/main.qml @@ -142,7 +142,7 @@ ApplicationWindow { return Qt.resolvedUrl("images/mediaplayer-app-symbolic.svg") case "button": case "longpressbutton": - case "multibutton": + case "simplemultibutton": case "longpressmultibutton": return Qt.resolvedUrl("images/system-shutdown.svg") case "weather": diff --git a/mea/ui/paramdescriptordelegates/ParamDescriptorDelegateBase.qml b/mea/ui/paramdescriptordelegates/ParamDescriptorDelegateBase.qml index 63bd5e42..80e68275 100644 --- a/mea/ui/paramdescriptordelegates/ParamDescriptorDelegateBase.qml +++ b/mea/ui/paramdescriptordelegates/ParamDescriptorDelegateBase.qml @@ -61,7 +61,7 @@ ItemDelegate { Layout.fillWidth: true sourceComponent: { - print("Datatye is:", paramType.type, paramType.minValue, paramType.maxValue) + print("Datatye is:", paramType.type, paramType.minValue, paramType.maxValue, paramType.allowedValues) switch (paramType.type.toLowerCase()) { case "bool": return boolComponent; @@ -158,8 +158,7 @@ ItemDelegate { id: comboBoxComponent ComboBox { model: paramType.allowedValues - currentIndex: root.paramType.value - onActivated: { + onCurrentIndexChanged: { root.value = paramType.allowedValues[index] } }