diff --git a/libnymea-app/models/taglistmodel.cpp b/libnymea-app/models/taglistmodel.cpp index 55781995..e8b610fc 100644 --- a/libnymea-app/models/taglistmodel.cpp +++ b/libnymea-app/models/taglistmodel.cpp @@ -104,10 +104,6 @@ bool TagListModel::containsValue(const QString &tagValue) void TagListModel::update() { - beginResetModel(); - qDeleteAll(m_list); - m_list.clear(); - for (int i = 0; i < m_tagsProxy->rowCount(); i++) { Tag *tag = m_tagsProxy->get(i); @@ -120,11 +116,33 @@ void TagListModel::update() } if (!found) { Tag *t = new Tag(tag->tagId(), tag->value(), this); + t->setThingId(tag->thingId()); + t->setRuleId(tag->ruleId()); + beginInsertRows(QModelIndex(), m_list.count(), m_list.count()); m_list.append(t); + endInsertRows(); } } - endResetModel(); + QMutableListIterator it(m_list); + while (it.hasNext()) { + Tag *tag = it.next(); + bool found = false; + for (int i = 0; i < m_tagsProxy->rowCount(); i++) { + Tag *tagInSource = m_tagsProxy->get(i); + if (tag->tagId() == tagInSource->tagId() && tag->value() == tagInSource->value()) { + found = true; + break; + } + } + if (!found) { + int idx = m_list.indexOf(tag); + beginRemoveRows(QModelIndex(), idx, idx); + m_list.at(idx)->deleteLater(); + it.remove(); + endRemoveRows(); + } + } emit countChanged(); } diff --git a/libnymea-app/types/tag.cpp b/libnymea-app/types/tag.cpp index 11e8a597..b6d0186f 100644 --- a/libnymea-app/types/tag.cpp +++ b/libnymea-app/types/tag.cpp @@ -81,3 +81,8 @@ void Tag::setValue(const QString &value) emit valueChanged(); } } + +bool Tag::equals(Tag *other) const +{ + return m_tagId == other->tagId() && m_thingId == other->thingId() && m_ruleId == other->ruleId() && m_value == other->value(); +} diff --git a/libnymea-app/types/tag.h b/libnymea-app/types/tag.h index fb42e40c..ed3e7822 100644 --- a/libnymea-app/types/tag.h +++ b/libnymea-app/types/tag.h @@ -59,6 +59,8 @@ public: QString value() const; void setValue(const QString &value); + bool equals(Tag *other) const; + signals: void valueChanged();