From 491a33794f714b42a8b3fcf5690a20947bca63f4 Mon Sep 17 00:00:00 2001 From: AdenKoperczak Date: Sun, 6 Oct 2024 12:18:22 -0400 Subject: [PATCH] Updated MarkerModel to update correctly on add/remove --- scwx-qt/source/scwx/qt/model/marker_model.cpp | 19 ++++++++++++++++++- scwx-qt/source/scwx/qt/model/marker_model.hpp | 1 + 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/scwx-qt/source/scwx/qt/model/marker_model.cpp b/scwx-qt/source/scwx/qt/model/marker_model.cpp index 6dbd46bc..3f137c8c 100644 --- a/scwx-qt/source/scwx/qt/model/marker_model.cpp +++ b/scwx-qt/source/scwx/qt/model/marker_model.cpp @@ -42,6 +42,11 @@ MarkerModel::MarkerModel(QObject* parent) : &manager::MarkerManager::MarkerAdded, this, &MarkerModel::HandleMarkerAdded); + + connect(p->markerManager_.get(), + &manager::MarkerManager::MarkerRemoved, + this, + &MarkerModel::HandleMarkerRemoved); } MarkerModel::~MarkerModel() = default; @@ -240,8 +245,8 @@ bool MarkerModel::setData(const QModelIndex& index, void MarkerModel::HandleMarkerAdded() { - QModelIndex topLeft = createIndex(0, kFirstColumn); const int newIndex = static_cast(p->markerManager_->marker_count() - 1); + QModelIndex topLeft = createIndex(newIndex, kFirstColumn); QModelIndex bottomRight = createIndex(newIndex, kLastColumn); beginInsertRows(QModelIndex(), newIndex, newIndex); @@ -250,6 +255,18 @@ void MarkerModel::HandleMarkerAdded() Q_EMIT dataChanged(topLeft, bottomRight); } +void MarkerModel::HandleMarkerRemoved(size_t index) +{ + const int removedIndex = static_cast(index); + QModelIndex topLeft = createIndex(removedIndex, kFirstColumn); + QModelIndex bottomRight = createIndex(removedIndex, kLastColumn); + + beginRemoveRows(QModelIndex(), removedIndex, removedIndex); + endRemoveRows(); + + Q_EMIT dataChanged(topLeft, bottomRight); +} + } // namespace model } // namespace qt } // namespace scwx diff --git a/scwx-qt/source/scwx/qt/model/marker_model.hpp b/scwx-qt/source/scwx/qt/model/marker_model.hpp index a3d23546..9c640238 100644 --- a/scwx-qt/source/scwx/qt/model/marker_model.hpp +++ b/scwx-qt/source/scwx/qt/model/marker_model.hpp @@ -40,6 +40,7 @@ public: public slots: void HandleMarkerAdded(); + void HandleMarkerRemoved(size_t index); private: class Impl;