From aec937aa97f07433f77d286ac6a6b8ebf508658b Mon Sep 17 00:00:00 2001 From: AdenKoperczak Date: Thu, 10 Oct 2024 08:38:33 -0400 Subject: [PATCH] add more handling to ensure that MarkerModel stays up to date with MarkerManager --- .../source/scwx/qt/manager/marker_manager.cpp | 3 ++ .../source/scwx/qt/manager/marker_manager.hpp | 2 ++ scwx-qt/source/scwx/qt/model/marker_model.cpp | 31 +++++++++++++++++++ scwx-qt/source/scwx/qt/model/marker_model.hpp | 2 ++ 4 files changed, 38 insertions(+) diff --git a/scwx-qt/source/scwx/qt/manager/marker_manager.cpp b/scwx-qt/source/scwx/qt/manager/marker_manager.cpp index d74fe09f..19aa6a94 100644 --- a/scwx-qt/source/scwx/qt/manager/marker_manager.cpp +++ b/scwx-qt/source/scwx/qt/manager/marker_manager.cpp @@ -181,6 +181,8 @@ MarkerManager::MarkerManager() : p(std::make_unique(this)) // Read Marker settings on startup main::Application::WaitForInitialization(); p->ReadMarkerSettings(); + + Q_EMIT MarkersInitialized(p->markerRecords_.size()); } catch (const std::exception& ex) { @@ -219,6 +221,7 @@ void MarkerManager::set_marker(size_t index, const types::MarkerInfo& marker) std::shared_ptr& markerRecord = p->markerRecords_[index]; markerRecord->markerInfo_ = marker; + Q_EMIT MarkerChanged(index); Q_EMIT MarkersUpdated(); } diff --git a/scwx-qt/source/scwx/qt/manager/marker_manager.hpp b/scwx-qt/source/scwx/qt/manager/marker_manager.hpp index 4fb81457..2f073ab7 100644 --- a/scwx-qt/source/scwx/qt/manager/marker_manager.hpp +++ b/scwx-qt/source/scwx/qt/manager/marker_manager.hpp @@ -30,7 +30,9 @@ public: static std::shared_ptr Instance(); signals: + void MarkersInitialized(size_t count); void MarkersUpdated(); + void MarkerChanged(size_t index); void MarkerAdded(); void MarkerRemoved(size_t index); diff --git a/scwx-qt/source/scwx/qt/model/marker_model.cpp b/scwx-qt/source/scwx/qt/model/marker_model.cpp index 04a65623..5ad00a56 100644 --- a/scwx-qt/source/scwx/qt/model/marker_model.cpp +++ b/scwx-qt/source/scwx/qt/model/marker_model.cpp @@ -38,11 +38,22 @@ public: MarkerModel::MarkerModel(QObject* parent) : QAbstractTableModel(parent), p(std::make_unique()) { + + connect(p->markerManager_.get(), + &manager::MarkerManager::MarkersInitialized, + this, + &MarkerModel::HandleMarkersInitialized); + connect(p->markerManager_.get(), &manager::MarkerManager::MarkerAdded, this, &MarkerModel::HandleMarkerAdded); + connect(p->markerManager_.get(), + &manager::MarkerManager::MarkerChanged, + this, + &MarkerModel::HandleMarkerChanged); + connect(p->markerManager_.get(), &manager::MarkerManager::MarkerRemoved, this, @@ -235,6 +246,17 @@ bool MarkerModel::setData(const QModelIndex& index, return result; } +void MarkerModel::HandleMarkersInitialized(size_t count) +{ + QModelIndex topLeft = createIndex(0, kFirstColumn); + QModelIndex bottomRight = createIndex(count - 1, kLastColumn); + + beginInsertRows(QModelIndex(), 0, count - 1); + endInsertRows(); + + Q_EMIT dataChanged(topLeft, bottomRight); +} + void MarkerModel::HandleMarkerAdded() { const int newIndex = static_cast(p->markerManager_->marker_count() - 1); @@ -247,6 +269,15 @@ void MarkerModel::HandleMarkerAdded() Q_EMIT dataChanged(topLeft, bottomRight); } +void MarkerModel::HandleMarkerChanged(size_t index) +{ + const int changedIndex = static_cast(index); + QModelIndex topLeft = createIndex(changedIndex, kFirstColumn); + QModelIndex bottomRight = createIndex(changedIndex, kLastColumn); + + Q_EMIT dataChanged(topLeft, bottomRight); +} + void MarkerModel::HandleMarkerRemoved(size_t index) { const int removedIndex = static_cast(index); diff --git a/scwx-qt/source/scwx/qt/model/marker_model.hpp b/scwx-qt/source/scwx/qt/model/marker_model.hpp index 9c640238..c93526b1 100644 --- a/scwx-qt/source/scwx/qt/model/marker_model.hpp +++ b/scwx-qt/source/scwx/qt/model/marker_model.hpp @@ -39,7 +39,9 @@ public: public slots: + void HandleMarkersInitialized(size_t count); void HandleMarkerAdded(); + void HandleMarkerChanged(size_t index); void HandleMarkerRemoved(size_t index); private: