diff --git a/libnymea-app/libnymea-app-core.h b/libnymea-app/libnymea-app-core.h index f70dd878..89a2d782 100644 --- a/libnymea-app/libnymea-app-core.h +++ b/libnymea-app/libnymea-app-core.h @@ -128,6 +128,7 @@ #include "zigbee/zigbeenetwork.h" #include "zigbee/zigbeenetworks.h" #include "applogcontroller.h" +#include "tagwatcher.h" #include @@ -266,6 +267,7 @@ void registerQmlTypes() { qmlRegisterType(uri, 1, 0, "TagsProxyModel"); qmlRegisterType(uri, 1, 0, "TagListModel"); qmlRegisterType(uri, 1, 0, "TagListProxyModel"); + qmlRegisterType(uri, 1, 0, "TagWatcher"); qmlRegisterType(uri, 1, 0, "BtWiFiSetup"); qmlRegisterType(uri, 1, 0, "BluetoothDiscovery"); diff --git a/libnymea-app/libnymea-app.pri b/libnymea-app/libnymea-app.pri index 6a475eeb..ff45df9b 100644 --- a/libnymea-app/libnymea-app.pri +++ b/libnymea-app/libnymea-app.pri @@ -21,6 +21,7 @@ INCLUDEPATH += \ SOURCES += \ $$PWD/models/scriptsproxymodel.cpp \ + $$PWD/tagwatcher.cpp \ $${PWD}/logging.cpp \ $${PWD}/applogcontroller.cpp \ $${PWD}/wifisetup/btwifisetup.cpp \ @@ -167,6 +168,7 @@ SOURCES += \ HEADERS += \ $$PWD/models/scriptsproxymodel.h \ + $$PWD/tagwatcher.h \ $${PWD}/logging.h \ $${PWD}/applogcontroller.h \ $${PWD}/wifisetup/btwifisetup.h \ diff --git a/libnymea-app/tagsmanager.cpp b/libnymea-app/tagsmanager.cpp index fb7d19e6..0620d956 100644 --- a/libnymea-app/tagsmanager.cpp +++ b/libnymea-app/tagsmanager.cpp @@ -34,9 +34,6 @@ #include -#include "logging.h" -NYMEA_LOGGING_CATEGORY(dcTags, "Tags") - TagsManager::TagsManager(JsonRpcClient *jsonClient, QObject *parent): JsonHandler(parent), m_jsonClient(jsonClient), diff --git a/libnymea-app/tagwatcher.cpp b/libnymea-app/tagwatcher.cpp new file mode 100644 index 00000000..dee3f455 --- /dev/null +++ b/libnymea-app/tagwatcher.cpp @@ -0,0 +1,117 @@ +#include "tagwatcher.h" +#include "types/tag.h" + +TagWatcher::TagWatcher(QObject *parent) : QObject(parent) +{ + +} + +Tags *TagWatcher::tags() const +{ + return m_tags; +} + +void TagWatcher::setTags(Tags *tags) +{ + if (m_tags != tags) { + if (m_tags) { + disconnect(m_tags, &Tags::countChanged, this, &TagWatcher::update); + } + m_tags = tags; + emit tagsChanged(); + + if (m_tags) { + connect(m_tags, &Tags::countChanged, this, &TagWatcher::update); + } + update(); + } +} + +QUuid TagWatcher::thingId() const +{ + return m_thingId; +} + +void TagWatcher::setThingId(const QUuid &thingId) +{ + if (m_thingId != thingId) { + m_thingId = thingId; + emit thingIdChanged(); + update(); + } +} + +QUuid TagWatcher::ruleId() const +{ + return m_ruleId; +} + +void TagWatcher::setRuleId(const QUuid &ruleId) +{ + if (m_ruleId != ruleId) { + m_ruleId = ruleId; + emit ruleIdChanged(); + update(); + } +} + +QString TagWatcher::tagId() const +{ + return m_tagId; +} + +void TagWatcher::setTagId(const QString &tagId) +{ + if (m_tagId != tagId) { + m_tagId = tagId; + emit tagIdChanged(); + update(); + } +} + +Tag *TagWatcher::tag() const +{ + return m_tag; +} + +void TagWatcher::update() +{ + qCDebug(dcTags) << "Updating tag for watcher:" << m_tags << m_thingId << m_tagId; + if (!m_tags) { + updateTag(nullptr); + return; + } + + if (m_thingId.isNull() && m_ruleId.isNull()) { + updateTag(nullptr); + return; + } + + if (m_tagId.isEmpty()) { + updateTag(nullptr); + return; + } + + Tag *tag = nullptr; + for (int i = 0; i < m_tags->rowCount(); i++) { + Tag *t = m_tags->get(i); + if (t->tagId() != m_tagId) { + continue; + } + if (t->thingId() != m_thingId && t->ruleId() != m_ruleId) { + continue; + } + tag = t; + break; + } + + updateTag(tag); +} + +void TagWatcher::updateTag(Tag *tag) +{ + if (m_tag != tag) { + m_tag = tag; + emit tagChanged(); + } +} diff --git a/libnymea-app/tagwatcher.h b/libnymea-app/tagwatcher.h new file mode 100644 index 00000000..c4988f72 --- /dev/null +++ b/libnymea-app/tagwatcher.h @@ -0,0 +1,53 @@ +#ifndef TAGWATCHER_H +#define TAGWATCHER_H + +#include +#include + +#include "types/tags.h" + +class TagWatcher : public QObject +{ + Q_OBJECT + Q_PROPERTY(Tags* tags READ tags WRITE setTags NOTIFY tagsChanged) + Q_PROPERTY(QUuid thingId READ thingId WRITE setThingId NOTIFY thingIdChanged) + Q_PROPERTY(QUuid ruleId READ ruleId WRITE setRuleId NOTIFY ruleIdChanged) + Q_PROPERTY(QString tagId READ tagId WRITE setTagId NOTIFY tagIdChanged) + Q_PROPERTY(Tag* tag READ tag NOTIFY tagChanged) +public: + explicit TagWatcher(QObject *parent = nullptr); + + Tags* tags() const; + void setTags(Tags *tags); + + QUuid thingId() const; + void setThingId(const QUuid &thingId); + + QUuid ruleId() const; + void setRuleId(const QUuid &ruleId); + + QString tagId() const; + void setTagId(const QString &tagId); + + Tag* tag() const; + +signals: + void tagsChanged(); + void tagIdChanged(); + void thingIdChanged(); + void ruleIdChanged(); + void tagChanged(); + +private slots: + void update(); + void updateTag(Tag *tag); + +private: + Tags* m_tags = nullptr; + QUuid m_thingId; + QUuid m_ruleId; + QString m_tagId; + Tag *m_tag = nullptr; +}; + +#endif // TAGWATCHER_H diff --git a/libnymea-app/types/tag.cpp b/libnymea-app/types/tag.cpp index 1de78786..11e8a597 100644 --- a/libnymea-app/types/tag.cpp +++ b/libnymea-app/types/tag.cpp @@ -32,6 +32,9 @@ #include +#include "logging.h" +NYMEA_LOGGING_CATEGORY(dcTags, "Tags") + Tag::Tag(const QString &tagId, const QString &value, QObject *parent): QObject(parent), m_tagId(tagId), diff --git a/libnymea-app/types/tag.h b/libnymea-app/types/tag.h index b78b6638..fb42e40c 100644 --- a/libnymea-app/types/tag.h +++ b/libnymea-app/types/tag.h @@ -33,6 +33,9 @@ #include #include +#include + +Q_DECLARE_LOGGING_CATEGORY(dcTags) class Tag : public QObject {