From 262e490b9b6bd97f062ba9183e2a015c7d052691 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Mon, 8 Nov 2021 01:07:37 +0100 Subject: [PATCH] Add more "by name" thing methods and c++11 list_initializers for lists --- libnymea/integrations/thing.cpp | 28 +++++++++++++++++++++++++++- libnymea/integrations/thing.h | 4 ++++ libnymea/types/interface.h | 1 + libnymea/types/param.h | 1 + libnymea/types/state.h | 1 + 5 files changed, 34 insertions(+), 1 deletion(-) diff --git a/libnymea/integrations/thing.cpp b/libnymea/integrations/thing.cpp index eb4e5118..5c95b6c6 100644 --- a/libnymea/integrations/thing.cpp +++ b/libnymea/integrations/thing.cpp @@ -225,6 +225,12 @@ QVariant Thing::paramValue(const ParamTypeId ¶mTypeId) const return QVariant(); } +QVariant Thing::paramValue(const QString ¶mName) const +{ + ParamTypeId paramTypeId = m_thingClass.paramTypes().findByName(paramName).id(); + return paramValue(paramTypeId); +} + /*! Sets the \a value of the \l{Param} with the given \a paramTypeId. */ void Thing::setParamValue(const ParamTypeId ¶mTypeId, const QVariant &value) { @@ -238,6 +244,12 @@ void Thing::setParamValue(const ParamTypeId ¶mTypeId, const QVariant &value) m_params = params; } +void Thing::setParamValue(const QString ¶mName, const QVariant &value) +{ + ParamTypeId paramTypeId = m_thingClass.paramTypes().findByName(paramName).id(); + setParamValue(paramTypeId, value); +} + ParamList Thing::settings() const { return m_settings; @@ -321,7 +333,7 @@ void Thing::setStates(const States &states) m_states = states; } -/*! Returns true, a \l{State} with the given \a stateTypeId exists for this thing. */ +/*! Returns true, a \l{State} with the state given by \a stateTypeId exists for this thing. */ bool Thing::hasState(const StateTypeId &stateTypeId) const { foreach (const State &state, m_states) { @@ -332,6 +344,13 @@ bool Thing::hasState(const StateTypeId &stateTypeId) const return false; } +/*! Finds the \l{State} matching the given \a stateTypeId in this thing and returns the current value. */ +bool Thing::hasState(const QString &stateName) const +{ + StateTypeId stateTypeId = m_thingClass.stateTypes().findByName(stateName).id(); + return hasState(stateTypeId); +} + /*! Finds the \l{State} matching the given \a stateTypeId in this thing and returns the current value. */ QVariant Thing::stateValue(const StateTypeId &stateTypeId) const { @@ -604,6 +623,13 @@ void Thing::emitEvent(const EventTypeId &eventTypeId, const ParamList ¶ms) emit eventTriggered(Event(eventTypeId, m_id, params)); } +/*! Emits an event from this thing to the system. */ +void Thing::emitEvent(const QString &eventName, const ParamList ¶ms) +{ + EventTypeId eventTypeId = m_thingClass.eventTypes().findByName(eventName).id(); + emit eventTriggered(Event(eventTypeId, m_id, params)); +} + /*! Returns true if this thing has been auto-created (not created by the user) */ bool Thing::autoCreated() const { diff --git a/libnymea/integrations/thing.h b/libnymea/integrations/thing.h index 2b4d39ed..e0d0f2f7 100644 --- a/libnymea/integrations/thing.h +++ b/libnymea/integrations/thing.h @@ -119,7 +119,9 @@ public: void setParams(const ParamList ¶ms); QVariant paramValue(const ParamTypeId ¶mTypeId) const; + QVariant paramValue(const QString ¶mName) const; void setParamValue(const ParamTypeId ¶mName, const QVariant &value); + void setParamValue(const QString ¶mName, const QVariant &value); Q_INVOKABLE ParamList settings() const; Q_INVOKABLE bool hasSetting(const ParamTypeId ¶mTypeId) const; @@ -132,6 +134,7 @@ public: States states() const; bool hasState(const StateTypeId &stateTypeId) const; + bool hasState(const QString &stateName) const; void setStates(const States &states); Q_INVOKABLE QVariant stateValue(const StateTypeId &stateTypeId) const; @@ -163,6 +166,7 @@ public: public slots: void emitEvent(const EventTypeId &eventTypeId, const ParamList ¶ms = ParamList()); + void emitEvent(const QString &eventName, const ParamList ¶ms = ParamList()); signals: void stateValueChanged(const StateTypeId &stateTypeId, const QVariant &value, const QVariant &minValue, const QVariant &maxValue); diff --git a/libnymea/types/interface.h b/libnymea/types/interface.h index e4164483..8715fdf0 100644 --- a/libnymea/types/interface.h +++ b/libnymea/types/interface.h @@ -61,6 +61,7 @@ class LIBNYMEA_EXPORT Interfaces: public QList public: Interfaces() = default; Interfaces(const QList &other); + Interfaces(std::initializer_list args):QList(args) {} Interface findByName(const QString &name); }; diff --git a/libnymea/types/param.h b/libnymea/types/param.h index eef5b471..f1d480b7 100644 --- a/libnymea/types/param.h +++ b/libnymea/types/param.h @@ -68,6 +68,7 @@ class LIBNYMEA_EXPORT ParamList: public QList public: ParamList(); ParamList(const QList &other); + ParamList(std::initializer_list args):QList(args) {} Q_INVOKABLE QVariant get(int index); Q_INVOKABLE void put(const QVariant &variant); bool hasParam(const ParamTypeId ¶mTypeId) const; diff --git a/libnymea/types/state.h b/libnymea/types/state.h index 84f444cd..8f799bb8 100644 --- a/libnymea/types/state.h +++ b/libnymea/types/state.h @@ -84,6 +84,7 @@ class States: public QList public: States(); States(const QList &other); + States(std::initializer_list args):QList(args) {} Q_INVOKABLE QVariant get(int index) const; Q_INVOKABLE void put(const QVariant &variant); Q_INVOKABLE QVariant stateValue(const StateTypeId &stateTypeId);