Merge PR #482: Add more "by name" thing methods and c++11 list_initializers for lists

pull/487/head
Jenkins nymea 2021-12-11 00:31:10 +01:00
commit b8e58511b9
5 changed files with 34 additions and 1 deletions

View File

@ -225,6 +225,12 @@ QVariant Thing::paramValue(const ParamTypeId &paramTypeId) const
return QVariant();
}
QVariant Thing::paramValue(const QString &paramName) 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 &paramTypeId, const QVariant &value)
{
@ -238,6 +244,12 @@ void Thing::setParamValue(const ParamTypeId &paramTypeId, const QVariant &value)
m_params = params;
}
void Thing::setParamValue(const QString &paramName, 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 &params)
emit eventTriggered(Event(eventTypeId, m_id, params));
}
/*! Emits an event from this thing to the system. */
void Thing::emitEvent(const QString &eventName, const ParamList &params)
{
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
{

View File

@ -119,7 +119,9 @@ public:
void setParams(const ParamList &params);
QVariant paramValue(const ParamTypeId &paramTypeId) const;
QVariant paramValue(const QString &paramName) const;
void setParamValue(const ParamTypeId &paramName, const QVariant &value);
void setParamValue(const QString &paramName, const QVariant &value);
Q_INVOKABLE ParamList settings() const;
Q_INVOKABLE bool hasSetting(const ParamTypeId &paramTypeId) 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 &params = ParamList());
void emitEvent(const QString &eventName, const ParamList &params = ParamList());
signals:
void stateValueChanged(const StateTypeId &stateTypeId, const QVariant &value, const QVariant &minValue, const QVariant &maxValue);

View File

@ -61,6 +61,7 @@ class LIBNYMEA_EXPORT Interfaces: public QList<Interface>
public:
Interfaces() = default;
Interfaces(const QList<Interface> &other);
Interfaces(std::initializer_list<Interface> args):QList(args) {}
Interface findByName(const QString &name);
};

View File

@ -68,6 +68,7 @@ class LIBNYMEA_EXPORT ParamList: public QList<Param>
public:
ParamList();
ParamList(const QList<Param> &other);
ParamList(std::initializer_list<Param> args):QList(args) {}
Q_INVOKABLE QVariant get(int index);
Q_INVOKABLE void put(const QVariant &variant);
bool hasParam(const ParamTypeId &paramTypeId) const;

View File

@ -84,6 +84,7 @@ class States: public QList<State>
public:
States();
States(const QList<State> &other);
States(std::initializer_list<State> args):QList(args) {}
Q_INVOKABLE QVariant get(int index) const;
Q_INVOKABLE void put(const QVariant &variant);
Q_INVOKABLE QVariant stateValue(const StateTypeId &stateTypeId);