From 782d61e5f02f16be5f9f940f8287168bad65db3b Mon Sep 17 00:00:00 2001 From: Dan Paulat Date: Mon, 10 Oct 2022 00:49:49 -0500 Subject: [PATCH] Radar product model cleanup --- .../scwx/qt/model/radar_product_model.cpp | 60 +++++-------- scwx-qt/source/scwx/qt/model/tree_model.cpp | 87 ++++++++++--------- scwx-qt/source/scwx/qt/model/tree_model.hpp | 1 + 3 files changed, 67 insertions(+), 81 deletions(-) diff --git a/scwx-qt/source/scwx/qt/model/radar_product_model.cpp b/scwx-qt/source/scwx/qt/model/radar_product_model.cpp index e2e158b0..9af76a52 100644 --- a/scwx-qt/source/scwx/qt/model/radar_product_model.cpp +++ b/scwx-qt/source/scwx/qt/model/radar_product_model.cpp @@ -22,6 +22,8 @@ public: explicit RadarProductModelImpl(RadarProductModel* self); ~RadarProductModelImpl() = default; + void AppendRow(TreeItem* parent, TreeItem* child); + RadarProductModel* self_; std::shared_ptr rootItem_; }; @@ -37,6 +39,18 @@ const std::shared_ptr RadarProductModel::root_item() const return p->rootItem_; } +void RadarProductModelImpl::AppendRow(TreeItem* parent, TreeItem* child) +{ + const QModelIndex parentIndex = self_->createIndex(parent->row(), 0, parent); + const int childCount = parent->child_count(); + const int first = childCount; + const int last = childCount; + + self_->beginInsertRows(parentIndex, first, last); + parent->AppendChild(child); + self_->endInsertRows(); +} + RadarProductModelImpl::RadarProductModelImpl(RadarProductModel* self) : self_ {self}, rootItem_ {std::make_shared( @@ -57,16 +71,8 @@ RadarProductModelImpl::RadarProductModelImpl(RadarProductModel* self) : if (radarSiteItem == nullptr) { - const QModelIndex rootIndex = - self_->createIndex(rootItem_->row(), 0, rootItem_.get()); - const int rootChildren = rootItem_->child_count(); - - self_->beginInsertRows(rootIndex, rootChildren, rootChildren); - radarSiteItem = new TreeItem({radarSiteName}); - rootItem_->AppendChild(radarSiteItem); - - self_->endInsertRows(); + AppendRow(rootItem_.get(), radarSiteItem); } connect( @@ -86,17 +92,8 @@ RadarProductModelImpl::RadarProductModelImpl(RadarProductModel* self) : if (groupItem == nullptr) { // Existing group item was not found, create it - const QModelIndex radarSiteIndex = - self_->createIndex(radarSiteItem->row(), 0, radarSiteItem); - const int radarSiteChildren = radarSiteItem->child_count(); - - self_->beginInsertRows( - radarSiteIndex, radarSiteChildren, radarSiteChildren); - groupItem = new TreeItem({groupName}); - radarSiteItem->AppendChild(groupItem); - - self_->endInsertRows(); + AppendRow(radarSiteItem, groupItem); } TreeItem* productItem = nullptr; @@ -115,32 +112,15 @@ RadarProductModelImpl::RadarProductModelImpl(RadarProductModel* self) : if (productItem == nullptr) { // Existing product item was not found, create it - const QModelIndex groupItemIndex = - self_->createIndex(groupItem->row(), 0, groupItem); - const int groupItemChildren = groupItem->child_count(); - - self_->beginInsertRows( - groupItemIndex, groupItemChildren, groupItemChildren); - productItem = new TreeItem({productName}); - groupItem->AppendChild(productItem); - - self_->endInsertRows(); + AppendRow(groupItem, productItem); } } // Create leaf item for product time - const QModelIndex productItemIndex = - self_->createIndex(productItem->row(), 0, productItem); - const int productItemChildren = productItem->child_count(); - - self_->beginInsertRows( - productItemIndex, productItemChildren, productItemChildren); - - productItem->AppendChild(new TreeItem { - QString::fromStdString(util::TimeString(latestTime))}); - - self_->endInsertRows(); + AppendRow(productItem, + new TreeItem {QString::fromStdString( + util::TimeString(latestTime))}); }, Qt::QueuedConnection); }); diff --git a/scwx-qt/source/scwx/qt/model/tree_model.cpp b/scwx-qt/source/scwx/qt/model/tree_model.cpp index e2be6382..e24585a3 100644 --- a/scwx-qt/source/scwx/qt/model/tree_model.cpp +++ b/scwx-qt/source/scwx/qt/model/tree_model.cpp @@ -12,46 +12,30 @@ static const std::string logPrefix_ = "scwx::qt::model::tree_model"; class TreeModelImpl { public: - explicit TreeModelImpl() = default; - ~TreeModelImpl() = default; + explicit TreeModelImpl(TreeModel* self) : self_ {self} {}; + ~TreeModelImpl() = default; + + const TreeItem* item(const QModelIndex& index) const; + TreeItem* item(const QModelIndex& index); + + TreeModel* self_; }; TreeModel::TreeModel(QObject* parent) : - QAbstractItemModel(parent), p(std::make_unique()) + QAbstractItemModel(parent), p(std::make_unique(this)) { } TreeModel::~TreeModel() = default; int TreeModel::rowCount(const QModelIndex& parent) const { - const TreeItem* parentItem; - - if (parent.isValid()) - { - parentItem = static_cast(parent.constInternalPointer()); - } - else - { - parentItem = root_item().get(); - } - - return parentItem->child_count(); + const TreeItem* parentItem = p->item(parent); + return parentItem ? parentItem->child_count() : 0; } -int TreeModel::columnCount(const QModelIndex& parent) const +int TreeModel::columnCount(const QModelIndex& /* parent */) const { - const TreeItem* parentItem; - - if (parent.isValid()) - { - parentItem = static_cast(parent.constInternalPointer()); - } - else - { - parentItem = root_item().get(); - } - - return parentItem->column_count(); + return root_item()->column_count(); } QVariant TreeModel::data(const QModelIndex& index, int role) const @@ -61,7 +45,7 @@ QVariant TreeModel::data(const QModelIndex& index, int role) const return QVariant(); } - const TreeItem* item = static_cast(index.internalPointer()); + const TreeItem* item = p->item(index); return item->data(index.column()); } @@ -96,20 +80,15 @@ TreeModel::headerData(int section, Qt::Orientation orientation, int role) const QModelIndex TreeModel::index(int row, int column, const QModelIndex& parent) const { - if (!hasIndex(row, column, parent)) + if (parent.isValid() && parent.column() != 0) { return QModelIndex(); } - const TreeItem* parentItem; - - if (!parent.isValid()) + const TreeItem* parentItem = p->item(parent); + if (parentItem == nullptr) { - parentItem = root_item().get(); - } - else - { - parentItem = static_cast(parent.constInternalPointer()); + return QModelIndex(); } const TreeItem* childItem = parentItem->child(row); @@ -128,9 +107,8 @@ QModelIndex TreeModel::parent(const QModelIndex& index) const return QModelIndex(); } - const TreeItem* childItem = - static_cast(index.constInternalPointer()); - const TreeItem* parentItem = childItem->parent_item(); + const TreeItem* childItem = p->item(index); + const TreeItem* parentItem = childItem ? childItem->parent_item() : nullptr; if (parentItem == root_item().get() || parentItem == nullptr) { @@ -140,6 +118,33 @@ QModelIndex TreeModel::parent(const QModelIndex& index) const return createIndex(parentItem->row(), 0, parentItem); } +const TreeItem* TreeModelImpl::item(const QModelIndex& index) const +{ + if (index.isValid()) + { + const TreeItem* item = + static_cast(index.constInternalPointer()); + if (item != nullptr) + { + return item; + } + } + return self_->root_item().get(); +} + +TreeItem* TreeModelImpl::item(const QModelIndex& index) +{ + if (index.isValid()) + { + TreeItem* item = static_cast(index.internalPointer()); + if (item != nullptr) + { + return item; + } + } + return self_->root_item().get(); +} + } // namespace model } // namespace qt } // namespace scwx diff --git a/scwx-qt/source/scwx/qt/model/tree_model.hpp b/scwx-qt/source/scwx/qt/model/tree_model.hpp index 6c85c299..eae47254 100644 --- a/scwx-qt/source/scwx/qt/model/tree_model.hpp +++ b/scwx-qt/source/scwx/qt/model/tree_model.hpp @@ -37,6 +37,7 @@ protected: virtual const std::shared_ptr root_item() const = 0; private: + friend class TreeModelImpl; std::unique_ptr p; };