From 6633e0911f9bcca73604cecbbbb6f9c8961e727b Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Fri, 21 Dec 2018 00:31:04 +0100 Subject: [PATCH] Don't ask the user to confirm exiting Edit rules page if there was no change --- libnymea-common/types/calendaritem.cpp | 12 +++++++++ libnymea-common/types/calendaritem.h | 1 + libnymea-common/types/calendaritems.cpp | 13 ++++++++++ libnymea-common/types/calendaritems.h | 2 ++ libnymea-common/types/eventdescriptor.cpp | 13 ++++++++++ libnymea-common/types/eventdescriptor.h | 1 + libnymea-common/types/eventdescriptors.cpp | 17 ++++++++++++ libnymea-common/types/eventdescriptors.h | 2 ++ libnymea-common/types/paramdescriptor.cpp | 13 ++++++++++ libnymea-common/types/paramdescriptor.h | 1 + libnymea-common/types/paramdescriptors.cpp | 13 ++++++++++ libnymea-common/types/paramdescriptors.h | 2 ++ libnymea-common/types/rule.cpp | 22 ++++++++++++++++ libnymea-common/types/rule.h | 3 +++ libnymea-common/types/ruleaction.cpp | 14 ++++++++++ libnymea-common/types/ruleaction.h | 1 + libnymea-common/types/ruleactionparam.cpp | 14 ++++++++++ libnymea-common/types/ruleactionparam.h | 1 + libnymea-common/types/ruleactionparams.cpp | 13 ++++++++++ libnymea-common/types/ruleactionparams.h | 2 ++ libnymea-common/types/ruleactions.cpp | 13 ++++++++++ libnymea-common/types/ruleactions.h | 1 + libnymea-common/types/statedescriptor.cpp | 15 +++++++++++ libnymea-common/types/statedescriptor.h | 1 + libnymea-common/types/stateevaluator.cpp | 12 +++++++++ libnymea-common/types/stateevaluator.h | 1 + libnymea-common/types/stateevaluators.cpp | 13 ++++++++++ libnymea-common/types/stateevaluators.h | 2 ++ libnymea-common/types/timedescriptor.cpp | 11 ++++++++ libnymea-common/types/timedescriptor.h | 2 ++ libnymea-common/types/timeeventitem.cpp | 12 +++++++++ libnymea-common/types/timeeventitem.h | 1 + libnymea-common/types/timeeventitems.cpp | 13 ++++++++++ libnymea-common/types/timeeventitems.h | 1 + nymea-app/ui/magic/EditRulePage.qml | 30 +++++++++++++++++++--- 35 files changed, 284 insertions(+), 4 deletions(-) diff --git a/libnymea-common/types/calendaritem.cpp b/libnymea-common/types/calendaritem.cpp index e2c87956..875a6576 100644 --- a/libnymea-common/types/calendaritem.cpp +++ b/libnymea-common/types/calendaritem.cpp @@ -1,6 +1,7 @@ #include "calendaritem.h" #include "repeatingoption.h" +#include CalendarItem::CalendarItem(QObject *parent) : QObject(parent) { @@ -60,3 +61,14 @@ CalendarItem *CalendarItem::clone() const ret->m_startTime = this->m_startTime; return ret; } + +#define COMPARE(a, b) if (a != b) { qDebug() << a << "!=" << b; return false; } +#define COMPARE_PTR(a, b) if (!a->operator==(b)) { qDebug() << a << "!=" << b; return false; } +bool CalendarItem::operator ==(CalendarItem *other) const +{ + COMPARE(m_dateTime, other->dateTime()); + COMPARE(m_startTime, other->startTime()); + COMPARE(m_duration, other->duration()); + COMPARE(m_repeatingOption, other->repeatingOption()); + return true; +} diff --git a/libnymea-common/types/calendaritem.h b/libnymea-common/types/calendaritem.h index 46f8d8e9..f2049bd0 100644 --- a/libnymea-common/types/calendaritem.h +++ b/libnymea-common/types/calendaritem.h @@ -29,6 +29,7 @@ public: RepeatingOption* repeatingOption() const; CalendarItem* clone() const; + bool operator==(CalendarItem* other) const; signals: void durationChanged(); diff --git a/libnymea-common/types/calendaritems.cpp b/libnymea-common/types/calendaritems.cpp index 23d6d2b6..243a1daa 100644 --- a/libnymea-common/types/calendaritems.cpp +++ b/libnymea-common/types/calendaritems.cpp @@ -50,3 +50,16 @@ CalendarItem *CalendarItems::get(int index) const } return m_list.at(index); } + +bool CalendarItems::operator==(CalendarItems *other) const +{ + if (rowCount() != other->rowCount()) { + return false; + } + for (int i = 0; i < rowCount(); i++) { + if (!get(i)->operator==(other->get(i))) { + return false; + } + } + return true; +} diff --git a/libnymea-common/types/calendaritems.h b/libnymea-common/types/calendaritems.h index 9b4e279e..48517de3 100644 --- a/libnymea-common/types/calendaritems.h +++ b/libnymea-common/types/calendaritems.h @@ -21,6 +21,8 @@ public: Q_INVOKABLE CalendarItem* createNewCalendarItem() const; Q_INVOKABLE CalendarItem* get(int index) const; + bool operator==(CalendarItems *other) const; + signals: void countChanged(); diff --git a/libnymea-common/types/eventdescriptor.cpp b/libnymea-common/types/eventdescriptor.cpp index 930f7965..e9e0b46f 100644 --- a/libnymea-common/types/eventdescriptor.cpp +++ b/libnymea-common/types/eventdescriptor.cpp @@ -1,4 +1,5 @@ #include "eventdescriptor.h" +#include EventDescriptor::EventDescriptor(QObject *parent) : QObject(parent) { @@ -74,3 +75,15 @@ EventDescriptor *EventDescriptor::clone() const } return ret; } + +#define COMPARE(a, b) if (a != b) { qDebug() << a << "!=" << b; return false; } +#define COMPARE_PTR(a, b) if (!a->operator==(b)) { qDebug() << a << "!=" << b; return false; } +bool EventDescriptor::operator==(EventDescriptor *other) const +{ + COMPARE(m_deviceId, other->deviceId()); + COMPARE(m_eventTypeId, other->eventTypeId()); + COMPARE(m_interfaceName, other->interfaceName()); + COMPARE(m_interfaceEvent, other->interfaceEvent()); + COMPARE_PTR(m_paramDescriptors, other->paramDescriptors()); + return true; +} diff --git a/libnymea-common/types/eventdescriptor.h b/libnymea-common/types/eventdescriptor.h index 0153b815..5cadc7be 100644 --- a/libnymea-common/types/eventdescriptor.h +++ b/libnymea-common/types/eventdescriptor.h @@ -35,6 +35,7 @@ public: ParamDescriptors* paramDescriptors() const; EventDescriptor* clone() const; + bool operator==(EventDescriptor* other) const; signals: void deviceIdChanged(); diff --git a/libnymea-common/types/eventdescriptors.cpp b/libnymea-common/types/eventdescriptors.cpp index 056df5e3..6370b274 100644 --- a/libnymea-common/types/eventdescriptors.cpp +++ b/libnymea-common/types/eventdescriptors.cpp @@ -1,6 +1,8 @@ #include "eventdescriptors.h" #include "eventdescriptor.h" +#include + EventDescriptors::EventDescriptors(QObject *parent) : QAbstractListModel(parent) { @@ -61,3 +63,18 @@ void EventDescriptors::removeEventDescriptor(int index) endRemoveRows(); emit countChanged(); } + +bool EventDescriptors::operator==(EventDescriptors *other) const +{ + qDebug() << "EventDescriptors comparison"; + if (rowCount() != other->rowCount()) { + qDebug() << "EventDescriptors count not matching"; + return false; + } + for (int i = 0; i < rowCount(); i++) { + if (!get(i)->operator==(other->get(i))) { + return false; + } + } + return true; +} diff --git a/libnymea-common/types/eventdescriptors.h b/libnymea-common/types/eventdescriptors.h index 87b661d1..8ba5bb2e 100644 --- a/libnymea-common/types/eventdescriptors.h +++ b/libnymea-common/types/eventdescriptors.h @@ -26,6 +26,8 @@ public: Q_INVOKABLE void addEventDescriptor(EventDescriptor *eventDescriptor); Q_INVOKABLE void removeEventDescriptor(int index); + bool operator==(EventDescriptors* other) const; + signals: void countChanged(); diff --git a/libnymea-common/types/paramdescriptor.cpp b/libnymea-common/types/paramdescriptor.cpp index 6eb0f1f7..31b55e23 100644 --- a/libnymea-common/types/paramdescriptor.cpp +++ b/libnymea-common/types/paramdescriptor.cpp @@ -1,5 +1,7 @@ #include "paramdescriptor.h" +#include + ParamDescriptor::ParamDescriptor(QObject *parent) : Param(parent) { @@ -40,3 +42,14 @@ ParamDescriptor *ParamDescriptor::clone() const ret->setOperatorType(this->operatorType()); return ret; } + +#define COMPARE(a, b) if (a != b) { qDebug() << a << "!=" << b; return false; } +#define COMPARE_PTR(a, b) if (!a->operator==(b)) { qDebug() << a << "!=" << b; return false; } +bool ParamDescriptor::operator==(ParamDescriptor *other) const +{ + COMPARE(m_paramTypeId, other->paramTypeId()); + COMPARE(m_paramName, other->paramName()); + COMPARE(m_value, other->value()); + COMPARE(m_operator, other->operatorType()); + return true; +} diff --git a/libnymea-common/types/paramdescriptor.h b/libnymea-common/types/paramdescriptor.h index 33ae0d4f..3c142b10 100644 --- a/libnymea-common/types/paramdescriptor.h +++ b/libnymea-common/types/paramdescriptor.h @@ -28,6 +28,7 @@ public: void setOperatorType(ValueOperator operatorType); ParamDescriptor* clone() const; + bool operator==(ParamDescriptor *other) const; signals: void paramNameChanged(); diff --git a/libnymea-common/types/paramdescriptors.cpp b/libnymea-common/types/paramdescriptors.cpp index ee4c0d72..fd0df0ee 100644 --- a/libnymea-common/types/paramdescriptors.cpp +++ b/libnymea-common/types/paramdescriptors.cpp @@ -98,3 +98,16 @@ void ParamDescriptors::clear() endResetModel(); emit countChanged(); } + +bool ParamDescriptors::operator==(ParamDescriptors *other) const +{ + if (rowCount() != other->rowCount()) { + return false; + } + for (int i = 0; i < rowCount(); i++) { + if (!get(i)->operator==(other->get(i))) { + return false; + } + } + return true; +} diff --git a/libnymea-common/types/paramdescriptors.h b/libnymea-common/types/paramdescriptors.h index d3c62696..07406243 100644 --- a/libnymea-common/types/paramdescriptors.h +++ b/libnymea-common/types/paramdescriptors.h @@ -42,6 +42,8 @@ public: Q_INVOKABLE void setParamDescriptorByName(const QString ¶mName, const QVariant &value, ValueOperator operatorType); Q_INVOKABLE void clear(); + bool operator==(ParamDescriptors *other) const; + signals: void countChanged(); diff --git a/libnymea-common/types/rule.cpp b/libnymea-common/types/rule.cpp index 8c656a0f..0e96cb81 100644 --- a/libnymea-common/types/rule.cpp +++ b/libnymea-common/types/rule.cpp @@ -159,6 +159,28 @@ Rule *Rule::clone() const return ret; } +bool Rule::compare(Rule *other) const +{ + qDebug() << "comparing rule" << this << "to" << other; + return this->operator==(other); +} + +#define COMPARE(a, b) if (a != b) { qDebug() << a << "!=" << b; return false; } +#define COMPARE_PTR(a, b) if (!a && !b) return true; if (!a || !b) return false; if (!a->operator==(b)) { qDebug() << a << "!=" << b; return false; } +bool Rule::operator==(Rule *other) const +{ + COMPARE(m_id, other->id()); + COMPARE(m_name, other->name()); + COMPARE(m_enabled, other->enabled()); + COMPARE(m_executable, other->executable()); + COMPARE_PTR(m_eventDescriptors, other->eventDescriptors()); + COMPARE_PTR(m_stateEvaluator, other->stateEvaluator()); + COMPARE_PTR(m_actions, other->actions()); + COMPARE_PTR(m_exitActions, other->exitActions()); + COMPARE_PTR(m_timeDescriptor, other->timeDescriptor()); + return true; +} + QDebug operator <<(QDebug &dbg, Rule *rule) { dbg << rule->name() << " (Enabled:" << rule->enabled() << "Active:" << rule->active() << ")" << endl; diff --git a/libnymea-common/types/rule.h b/libnymea-common/types/rule.h index db0da471..6619c411 100644 --- a/libnymea-common/types/rule.h +++ b/libnymea-common/types/rule.h @@ -52,6 +52,9 @@ public: Q_INVOKABLE Rule *clone() const; + Q_INVOKABLE bool compare(Rule* other) const; + bool operator==(Rule *other) const; + signals: void nameChanged(); void enabledChanged(); diff --git a/libnymea-common/types/ruleaction.cpp b/libnymea-common/types/ruleaction.cpp index 4c78cbf1..acb2b997 100644 --- a/libnymea-common/types/ruleaction.cpp +++ b/libnymea-common/types/ruleaction.cpp @@ -3,6 +3,8 @@ #include "ruleactionparam.h" #include "ruleactionparams.h" +#include + RuleAction::RuleAction(QObject *parent) : QObject(parent) { m_ruleActionParams = new RuleActionParams(this); @@ -77,3 +79,15 @@ RuleAction *RuleAction::clone() const } return ret; } + +#define COMPARE(a, b) if (a != b) { qDebug() << a << "!=" << b; return false; } +#define COMPARE_PTR(a, b) if (!a->operator==(b)) { qDebug() << a << "!=" << b; return false; } +bool RuleAction::operator==(RuleAction *other) const +{ + COMPARE(m_deviceId, other->deviceId()); + COMPARE(m_actionTypeId, other->actionTypeId()); + COMPARE(m_interfaceName, other->interfaceName()); + COMPARE(m_interfaceAction, other->interfaceAction()); + COMPARE_PTR(m_ruleActionParams, other->ruleActionParams()); + return true; +} diff --git a/libnymea-common/types/ruleaction.h b/libnymea-common/types/ruleaction.h index d9d85614..9b31c5ec 100644 --- a/libnymea-common/types/ruleaction.h +++ b/libnymea-common/types/ruleaction.h @@ -33,6 +33,7 @@ public: RuleActionParams* ruleActionParams() const; RuleAction *clone() const; + bool operator==(RuleAction *other) const; signals: void deviceIdChanged(); diff --git a/libnymea-common/types/ruleactionparam.cpp b/libnymea-common/types/ruleactionparam.cpp index 1c0ede94..e9c6bd92 100644 --- a/libnymea-common/types/ruleactionparam.cpp +++ b/libnymea-common/types/ruleactionparam.cpp @@ -1,5 +1,7 @@ #include "ruleactionparam.h" +#include + RuleActionParam::RuleActionParam(const QString ¶mName, const QVariant &value, QObject *parent): Param(parent), m_paramName(paramName) @@ -61,3 +63,15 @@ RuleActionParam *RuleActionParam::clone() const ret->setEventParamTypeId(eventParamTypeId()); return ret; } + +#define COMPARE(a, b) if (a != b) { qDebug() << a << "!=" << b; return false; } +#define COMPARE_PTR(a, b) if (!a->operator==(b)) { qDebug() << a << "!=" << b; return false; } +bool RuleActionParam::operator==(RuleActionParam *other) const +{ + COMPARE(m_paramTypeId, other->paramTypeId()); + COMPARE(m_paramName, other->paramName()); + COMPARE(m_eventTypeId, other->eventTypeId()); + COMPARE(m_eventParamTypeId, other->eventParamTypeId()); + COMPARE(m_value, other->value()); + return true; +} diff --git a/libnymea-common/types/ruleactionparam.h b/libnymea-common/types/ruleactionparam.h index e25ee6ea..6d79bc9a 100644 --- a/libnymea-common/types/ruleactionparam.h +++ b/libnymea-common/types/ruleactionparam.h @@ -27,6 +27,7 @@ public: void setEventParamTypeId(const QString &eventParamTypeId); RuleActionParam* clone() const; + bool operator==(RuleActionParam *other) const; signals: void paramNameChanged(); void eventTypeIdChanged(); diff --git a/libnymea-common/types/ruleactionparams.cpp b/libnymea-common/types/ruleactionparams.cpp index cc18816f..0e40495e 100644 --- a/libnymea-common/types/ruleactionparams.cpp +++ b/libnymea-common/types/ruleactionparams.cpp @@ -98,3 +98,16 @@ RuleActionParam *RuleActionParams::get(int index) const { return m_list.at(index); } + +bool RuleActionParams::operator==(RuleActionParams *other) const +{ + if (rowCount() != other->rowCount()) { + return false; + } + for (int i = 0; i < rowCount(); i++) { + if (!get(i)->operator==(other->get(i))) { + return false; + } + } + return true; +} diff --git a/libnymea-common/types/ruleactionparams.h b/libnymea-common/types/ruleactionparams.h index 03a18120..89444ecd 100644 --- a/libnymea-common/types/ruleactionparams.h +++ b/libnymea-common/types/ruleactionparams.h @@ -31,6 +31,8 @@ public: Q_INVOKABLE void setRuleActionParamEvent(const QString ¶mTypeId, const QString &eventTypeId, const QString &eventParamTypeId); Q_INVOKABLE RuleActionParam* get(int index) const; + bool operator==(RuleActionParams *other) const; + signals: void countChanged(); diff --git a/libnymea-common/types/ruleactions.cpp b/libnymea-common/types/ruleactions.cpp index 4d6301b9..ca135ead 100644 --- a/libnymea-common/types/ruleactions.cpp +++ b/libnymea-common/types/ruleactions.cpp @@ -48,3 +48,16 @@ RuleAction *RuleActions::createNewRuleAction() const { return new RuleAction(); } + +bool RuleActions::operator==(RuleActions *other) const +{ + if (rowCount() != other->rowCount()) { + return false; + } + for (int i = 0; i < rowCount(); i++) { + if (!get(i)->operator==(other->get(i))) { + return false; + } + } + return true; +} diff --git a/libnymea-common/types/ruleactions.h b/libnymea-common/types/ruleactions.h index c052d3f2..d7c346f1 100644 --- a/libnymea-common/types/ruleactions.h +++ b/libnymea-common/types/ruleactions.h @@ -21,6 +21,7 @@ public: Q_INVOKABLE RuleAction* get(int index) const; Q_INVOKABLE RuleAction* createNewRuleAction() const; + bool operator==(RuleActions *other) const; signals: void countChanged(); diff --git a/libnymea-common/types/statedescriptor.cpp b/libnymea-common/types/statedescriptor.cpp index 102c4a84..05deaa4f 100644 --- a/libnymea-common/types/statedescriptor.cpp +++ b/libnymea-common/types/statedescriptor.cpp @@ -1,5 +1,7 @@ #include "statedescriptor.h" +#include + StateDescriptor::StateDescriptor(const QUuid &deviceId, const QUuid &stateTypeId, StateDescriptor::ValueOperator valueOperator, const QVariant &value, QObject *parent): QObject(parent), m_deviceId(deviceId), @@ -110,3 +112,16 @@ StateDescriptor *StateDescriptor::clone() const ret->setInterfaceState(interfaceState()); return ret; } + +#define COMPARE(a, b) if (a != b) { qDebug() << a << "!=" << b; return false; } +#define COMPARE_PTR(a, b) if (!a->operator==(b)) { qDebug() << a << "!=" << b; return false; } +bool StateDescriptor::operator==(StateDescriptor *other) const +{ + COMPARE(m_deviceId, other->deviceId()); + COMPARE(m_stateTypeId, other->stateTypeId()); + COMPARE(m_interfaceName, other->interfaceName()); + COMPARE(m_interfaceState, other->interfaceState()); + COMPARE(m_operator, other->valueOperator()); + COMPARE(m_value, other->value()); + return true; +} diff --git a/libnymea-common/types/statedescriptor.h b/libnymea-common/types/statedescriptor.h index 796e7a74..791c6acc 100644 --- a/libnymea-common/types/statedescriptor.h +++ b/libnymea-common/types/statedescriptor.h @@ -49,6 +49,7 @@ public: void setValue(const QVariant &value); StateDescriptor* clone() const; + bool operator==(StateDescriptor *other) const; signals: void deviceIdChanged(); diff --git a/libnymea-common/types/stateevaluator.cpp b/libnymea-common/types/stateevaluator.cpp index ba0c4de2..e1f1140c 100644 --- a/libnymea-common/types/stateevaluator.cpp +++ b/libnymea-common/types/stateevaluator.cpp @@ -2,6 +2,8 @@ #include "stateevaluators.h" #include "statedescriptor.h" +#include + StateEvaluator::StateEvaluator(QObject *parent) : QObject(parent) { m_childEvaluators = new StateEvaluators(this); @@ -75,3 +77,13 @@ StateEvaluator *StateEvaluator::clone() const } return ret; } + +#define COMPARE(a, b) if (a != b) { qDebug() << a << "!=" << b; return false; } +#define COMPARE_PTR(a, b) if (!a->operator==(b)) { qDebug() << a << "!=" << b; return false; } +bool StateEvaluator::operator==(StateEvaluator *other) const +{ + COMPARE(m_operator, other->stateOperator()); + COMPARE_PTR(m_stateDescriptor, other->stateDescriptor()); + COMPARE_PTR(m_childEvaluators, other->childEvaluators()); + return true; +} diff --git a/libnymea-common/types/stateevaluator.h b/libnymea-common/types/stateevaluator.h index 0d830580..1c145551 100644 --- a/libnymea-common/types/stateevaluator.h +++ b/libnymea-common/types/stateevaluator.h @@ -34,6 +34,7 @@ public: Q_INVOKABLE StateEvaluator* addChildEvaluator(); StateEvaluator* clone() const; + bool operator==(StateEvaluator *other) const; signals: void stateOperatorChanged(); diff --git a/libnymea-common/types/stateevaluators.cpp b/libnymea-common/types/stateevaluators.cpp index 46c9ca62..ea1d8482 100644 --- a/libnymea-common/types/stateevaluators.cpp +++ b/libnymea-common/types/stateevaluators.cpp @@ -56,3 +56,16 @@ void StateEvaluators::remove(int index) { take(index)->deleteLater(); } + +bool StateEvaluators::operator==(StateEvaluators *other) const +{ + if (rowCount() != other->rowCount()) { + return false; + } + for (int i = 0; i < rowCount(); i++) { + if (!get(i)->operator==(other->get(i))) { + return false; + } + } + return true; +} diff --git a/libnymea-common/types/stateevaluators.h b/libnymea-common/types/stateevaluators.h index f2646fc8..07c52e09 100644 --- a/libnymea-common/types/stateevaluators.h +++ b/libnymea-common/types/stateevaluators.h @@ -26,6 +26,8 @@ public: // StateEvaluator will be deleted Q_INVOKABLE void remove(int index); + bool operator==(StateEvaluators *other) const; + signals: void countChanged(); diff --git a/libnymea-common/types/timedescriptor.cpp b/libnymea-common/types/timedescriptor.cpp index ac342634..8bb4796a 100644 --- a/libnymea-common/types/timedescriptor.cpp +++ b/libnymea-common/types/timedescriptor.cpp @@ -3,6 +3,8 @@ #include "timeeventitems.h" #include "calendaritems.h" +#include + TimeDescriptor::TimeDescriptor(QObject *parent) : QObject(parent) { m_timeEventItems = new TimeEventItems(this); @@ -18,3 +20,12 @@ CalendarItems *TimeDescriptor::calendarItems() const { return m_calendarItems; } + +#define COMPARE(a, b) if (a != b) { qDebug() << a << "!=" << b; return false; } +#define COMPARE_PTR(a, b) if (!a->operator==(b)) { qDebug() << a << "!=" << b; return false; } +bool TimeDescriptor::operator==(TimeDescriptor *other) const +{ + COMPARE_PTR(m_timeEventItems, other->timeEventItems()); + COMPARE_PTR(m_calendarItems, other->calendarItems()); + return true; +} diff --git a/libnymea-common/types/timedescriptor.h b/libnymea-common/types/timedescriptor.h index 28366664..cdb95ace 100644 --- a/libnymea-common/types/timedescriptor.h +++ b/libnymea-common/types/timedescriptor.h @@ -18,6 +18,8 @@ public: TimeEventItems* timeEventItems() const; CalendarItems* calendarItems() const; + + bool operator==(TimeDescriptor* other) const; signals: public slots: diff --git a/libnymea-common/types/timeeventitem.cpp b/libnymea-common/types/timeeventitem.cpp index 67cacaf5..d08fb480 100644 --- a/libnymea-common/types/timeeventitem.cpp +++ b/libnymea-common/types/timeeventitem.cpp @@ -2,6 +2,8 @@ #include "repeatingoption.h" +#include + TimeEventItem::TimeEventItem(QObject *parent): QObject(parent), m_repeatingOption(new RepeatingOption(this)) @@ -47,3 +49,13 @@ TimeEventItem *TimeEventItem::clone() const ret->m_repeatingOption = this->m_repeatingOption; return ret; } + +#define COMPARE(a, b) if (a != b) { qDebug() << a << "!=" << b; return false; } +#define COMPARE_PTR(a, b) if (!a->operator==(b)) { qDebug() << a << "!=" << b; return false; } +bool TimeEventItem::operator==(TimeEventItem *other) const +{ + COMPARE(m_time, other->time()); + COMPARE(m_dateTime, other->dateTime()); + COMPARE(m_repeatingOption, other->repeatingOption()); + return true; +} diff --git a/libnymea-common/types/timeeventitem.h b/libnymea-common/types/timeeventitem.h index 9e481993..4a372201 100644 --- a/libnymea-common/types/timeeventitem.h +++ b/libnymea-common/types/timeeventitem.h @@ -26,6 +26,7 @@ public: RepeatingOption* repeatingOption() const; TimeEventItem* clone() const; + bool operator==(TimeEventItem *other) const; signals: void dateTimeChanged(); diff --git a/libnymea-common/types/timeeventitems.cpp b/libnymea-common/types/timeeventitems.cpp index 2f560cc8..44d08d12 100644 --- a/libnymea-common/types/timeeventitems.cpp +++ b/libnymea-common/types/timeeventitems.cpp @@ -53,3 +53,16 @@ TimeEventItem *TimeEventItems::createNewTimeEventItem() const { return new TimeEventItem(); } + +bool TimeEventItems::operator==(TimeEventItems *other) const +{ + if (rowCount() != other->rowCount()) { + return false; + } + for (int i = 0; i < rowCount(); i++) { + if (!get(i)->operator==(other->get(i))) { + return false; + } + } + return true; +} diff --git a/libnymea-common/types/timeeventitems.h b/libnymea-common/types/timeeventitems.h index 96c30d2c..4ae0be96 100644 --- a/libnymea-common/types/timeeventitems.h +++ b/libnymea-common/types/timeeventitems.h @@ -21,6 +21,7 @@ public: Q_INVOKABLE TimeEventItem* get(int index) const; Q_INVOKABLE TimeEventItem* createNewTimeEventItem() const; + bool operator==(TimeEventItems *other) const; signals: void countChanged(); diff --git a/nymea-app/ui/magic/EditRulePage.qml b/nymea-app/ui/magic/EditRulePage.qml index b10cd98b..706589f8 100644 --- a/nymea-app/ui/magic/EditRulePage.qml +++ b/nymea-app/ui/magic/EditRulePage.qml @@ -7,8 +7,8 @@ import Nymea 1.0 Page { id: root - property var rule: null - property var initialDeviceToBeAdded: null + property Rule rule: null + property Device initialDeviceToBeAdded: null property bool busy: false @@ -27,7 +27,23 @@ Page { signal cancel(); Component.onCompleted: print("+++ created editrulepage") - Component.onDestruction: print("--- destroying editrulepage") + Component.onDestruction: { + print("--- destroying editrulepage") + d.backupRule.destroy(); + } + + onRuleChanged: d.createRuleBackup(); + QtObject { + id: d + property Rule backupRule: null + + function createRuleBackup() { + if (backupRule !== null) { + backupRule.destroy(); + } + backupRule = root.rule.clone(); + } + } function addEventDescriptor(interfaceMode) { if (interfaceMode === undefined) { @@ -208,6 +224,12 @@ Page { header: GuhHeader { text: root.rule.name.length === 0 ? qsTr("Add new magic") : qsTr("Edit %1").arg(root.rule.name) onBackPressed: { + if (root.rule.compare(d.backupRule)) { + print("Rule has not been changed. Exiting EditRulePage"); + root.cancel(); + return; + } + print("Rule has changed. Asking for cancellation dialog") var component = Qt.createComponent(Qt.resolvedUrl("../components/MeaDialog.qml")); var popup = component.createObject(root, {headerIcon: "../images/question.svg", title: qsTr("Cancel?"), @@ -349,7 +371,7 @@ Page { } } Repeater { - model: ["light-on", "light-off", "alarm-clock", "media-play", "network-secure", "notification", "sensors", "shutter-10", "attention", "eye"] + model: ["light-on", "light-off", "alarm-clock", "media-playback-start", "network-secure", "notification", "sensors", "shutter/shutter-050", "attention", "eye"] delegate: Item { Layout.fillWidth: true Layout.preferredHeight: app.iconSize + app.margins