Radar product model cleanup

This commit is contained in:
Dan Paulat 2022-10-10 00:49:49 -05:00
parent 254fbbeb67
commit 782d61e5f0
3 changed files with 67 additions and 81 deletions

View file

@ -22,6 +22,8 @@ public:
explicit RadarProductModelImpl(RadarProductModel* self); explicit RadarProductModelImpl(RadarProductModel* self);
~RadarProductModelImpl() = default; ~RadarProductModelImpl() = default;
void AppendRow(TreeItem* parent, TreeItem* child);
RadarProductModel* self_; RadarProductModel* self_;
std::shared_ptr<TreeItem> rootItem_; std::shared_ptr<TreeItem> rootItem_;
}; };
@ -37,6 +39,18 @@ const std::shared_ptr<TreeItem> RadarProductModel::root_item() const
return p->rootItem_; 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) : RadarProductModelImpl::RadarProductModelImpl(RadarProductModel* self) :
self_ {self}, self_ {self},
rootItem_ {std::make_shared<TreeItem>( rootItem_ {std::make_shared<TreeItem>(
@ -57,16 +71,8 @@ RadarProductModelImpl::RadarProductModelImpl(RadarProductModel* self) :
if (radarSiteItem == nullptr) 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}); radarSiteItem = new TreeItem({radarSiteName});
rootItem_->AppendChild(radarSiteItem); AppendRow(rootItem_.get(), radarSiteItem);
self_->endInsertRows();
} }
connect( connect(
@ -86,17 +92,8 @@ RadarProductModelImpl::RadarProductModelImpl(RadarProductModel* self) :
if (groupItem == nullptr) if (groupItem == nullptr)
{ {
// Existing group item was not found, create it // 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}); groupItem = new TreeItem({groupName});
radarSiteItem->AppendChild(groupItem); AppendRow(radarSiteItem, groupItem);
self_->endInsertRows();
} }
TreeItem* productItem = nullptr; TreeItem* productItem = nullptr;
@ -115,32 +112,15 @@ RadarProductModelImpl::RadarProductModelImpl(RadarProductModel* self) :
if (productItem == nullptr) if (productItem == nullptr)
{ {
// Existing product item was not found, create it // 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}); productItem = new TreeItem({productName});
groupItem->AppendChild(productItem); AppendRow(groupItem, productItem);
self_->endInsertRows();
} }
} }
// Create leaf item for product time // Create leaf item for product time
const QModelIndex productItemIndex = AppendRow(productItem,
self_->createIndex(productItem->row(), 0, productItem); new TreeItem {QString::fromStdString(
const int productItemChildren = productItem->child_count(); util::TimeString(latestTime))});
self_->beginInsertRows(
productItemIndex, productItemChildren, productItemChildren);
productItem->AppendChild(new TreeItem {
QString::fromStdString(util::TimeString(latestTime))});
self_->endInsertRows();
}, },
Qt::QueuedConnection); Qt::QueuedConnection);
}); });

View file

@ -12,46 +12,30 @@ static const std::string logPrefix_ = "scwx::qt::model::tree_model";
class TreeModelImpl class TreeModelImpl
{ {
public: public:
explicit TreeModelImpl() = default; explicit TreeModelImpl(TreeModel* self) : self_ {self} {};
~TreeModelImpl() = default; ~TreeModelImpl() = default;
const TreeItem* item(const QModelIndex& index) const;
TreeItem* item(const QModelIndex& index);
TreeModel* self_;
}; };
TreeModel::TreeModel(QObject* parent) : TreeModel::TreeModel(QObject* parent) :
QAbstractItemModel(parent), p(std::make_unique<TreeModelImpl>()) QAbstractItemModel(parent), p(std::make_unique<TreeModelImpl>(this))
{ {
} }
TreeModel::~TreeModel() = default; TreeModel::~TreeModel() = default;
int TreeModel::rowCount(const QModelIndex& parent) const int TreeModel::rowCount(const QModelIndex& parent) const
{ {
const TreeItem* parentItem; const TreeItem* parentItem = p->item(parent);
return parentItem ? parentItem->child_count() : 0;
if (parent.isValid())
{
parentItem = static_cast<const TreeItem*>(parent.constInternalPointer());
}
else
{
parentItem = root_item().get();
} }
return parentItem->child_count(); int TreeModel::columnCount(const QModelIndex& /* parent */) const
}
int TreeModel::columnCount(const QModelIndex& parent) const
{ {
const TreeItem* parentItem; return root_item()->column_count();
if (parent.isValid())
{
parentItem = static_cast<const TreeItem*>(parent.constInternalPointer());
}
else
{
parentItem = root_item().get();
}
return parentItem->column_count();
} }
QVariant TreeModel::data(const QModelIndex& index, int role) const QVariant TreeModel::data(const QModelIndex& index, int role) const
@ -61,7 +45,7 @@ QVariant TreeModel::data(const QModelIndex& index, int role) const
return QVariant(); return QVariant();
} }
const TreeItem* item = static_cast<const TreeItem*>(index.internalPointer()); const TreeItem* item = p->item(index);
return item->data(index.column()); return item->data(index.column());
} }
@ -96,20 +80,15 @@ TreeModel::headerData(int section, Qt::Orientation orientation, int role) const
QModelIndex QModelIndex
TreeModel::index(int row, int column, const QModelIndex& parent) const TreeModel::index(int row, int column, const QModelIndex& parent) const
{ {
if (!hasIndex(row, column, parent)) if (parent.isValid() && parent.column() != 0)
{ {
return QModelIndex(); return QModelIndex();
} }
const TreeItem* parentItem; const TreeItem* parentItem = p->item(parent);
if (parentItem == nullptr)
if (!parent.isValid())
{ {
parentItem = root_item().get(); return QModelIndex();
}
else
{
parentItem = static_cast<const TreeItem*>(parent.constInternalPointer());
} }
const TreeItem* childItem = parentItem->child(row); const TreeItem* childItem = parentItem->child(row);
@ -128,9 +107,8 @@ QModelIndex TreeModel::parent(const QModelIndex& index) const
return QModelIndex(); return QModelIndex();
} }
const TreeItem* childItem = const TreeItem* childItem = p->item(index);
static_cast<const TreeItem*>(index.constInternalPointer()); const TreeItem* parentItem = childItem ? childItem->parent_item() : nullptr;
const TreeItem* parentItem = childItem->parent_item();
if (parentItem == root_item().get() || parentItem == 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); return createIndex(parentItem->row(), 0, parentItem);
} }
const TreeItem* TreeModelImpl::item(const QModelIndex& index) const
{
if (index.isValid())
{
const TreeItem* item =
static_cast<const TreeItem*>(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<TreeItem*>(index.internalPointer());
if (item != nullptr)
{
return item;
}
}
return self_->root_item().get();
}
} // namespace model } // namespace model
} // namespace qt } // namespace qt
} // namespace scwx } // namespace scwx

View file

@ -37,6 +37,7 @@ protected:
virtual const std::shared_ptr<TreeItem> root_item() const = 0; virtual const std::shared_ptr<TreeItem> root_item() const = 0;
private: private:
friend class TreeModelImpl;
std::unique_ptr<TreeModelImpl> p; std::unique_ptr<TreeModelImpl> p;
}; };