mirror of
https://github.com/ciphervance/supercell-wx.git
synced 2025-10-30 21:00:05 +00:00
Radar Product Model population
This commit is contained in:
parent
4c4c93cad1
commit
efc6dc1413
5 changed files with 120 additions and 21 deletions
|
|
@ -1,6 +1,8 @@
|
|||
#include <scwx/qt/model/radar_product_model.hpp>
|
||||
#include <scwx/qt/manager/radar_product_manager.hpp>
|
||||
#include <scwx/qt/manager/radar_product_manager_notifier.hpp>
|
||||
#include <scwx/util/logger.hpp>
|
||||
#include <scwx/util/time.hpp>
|
||||
|
||||
namespace scwx
|
||||
{
|
||||
|
|
@ -17,14 +19,15 @@ class RadarProductModelImpl : public QObject
|
|||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit RadarProductModelImpl();
|
||||
explicit RadarProductModelImpl(RadarProductModel* self);
|
||||
~RadarProductModelImpl() = default;
|
||||
|
||||
RadarProductModel* self_;
|
||||
std::shared_ptr<TreeItem> rootItem_;
|
||||
};
|
||||
|
||||
RadarProductModel::RadarProductModel(QObject* parent) :
|
||||
TreeModel(parent), p(std::make_unique<RadarProductModelImpl>())
|
||||
TreeModel(parent), p(std::make_unique<RadarProductModelImpl>(this))
|
||||
{
|
||||
}
|
||||
RadarProductModel::~RadarProductModel() = default;
|
||||
|
|
@ -34,18 +37,69 @@ const std::shared_ptr<TreeItem> RadarProductModel::root_item() const
|
|||
return p->rootItem_;
|
||||
}
|
||||
|
||||
RadarProductModelImpl::RadarProductModelImpl() :
|
||||
RadarProductModelImpl::RadarProductModelImpl(RadarProductModel* self) :
|
||||
self_ {self},
|
||||
rootItem_ {std::make_shared<TreeItem>(
|
||||
std::vector<QVariant> {QObject::tr("Name"), QObject::tr("Info")})}
|
||||
{
|
||||
connect(&manager::RadarProductManagerNotifier::Instance(),
|
||||
&manager::RadarProductManagerNotifier::RadarProductManagerCreated,
|
||||
this,
|
||||
[=](const std::string& radarSite)
|
||||
{
|
||||
rootItem_->AppendChild(new TreeItem(
|
||||
std::vector<QVariant> {QString::fromStdString(radarSite)}));
|
||||
});
|
||||
connect(
|
||||
&manager::RadarProductManagerNotifier::Instance(),
|
||||
&manager::RadarProductManagerNotifier::RadarProductManagerCreated,
|
||||
this,
|
||||
[=](const std::string& radarSite)
|
||||
{
|
||||
TreeItem* radarSiteItem =
|
||||
new TreeItem({QString::fromStdString(radarSite)});
|
||||
|
||||
rootItem_->AppendChild(radarSiteItem);
|
||||
|
||||
connect(
|
||||
manager::RadarProductManager::Instance(radarSite).get(),
|
||||
&manager::RadarProductManager::NewDataAvailable,
|
||||
this,
|
||||
[=](common::RadarProductGroup group,
|
||||
const std::string& product,
|
||||
std::chrono::system_clock::time_point latestTime)
|
||||
{
|
||||
const QString groupName {QString::fromStdString(
|
||||
common::GetRadarProductGroupName(group))};
|
||||
|
||||
// Find existing group item (e.g., Level 2, Level 3)
|
||||
TreeItem* groupItem = radarSiteItem->FindChild(0, groupName);
|
||||
|
||||
if (groupItem == nullptr)
|
||||
{
|
||||
groupItem = new TreeItem({groupName});
|
||||
radarSiteItem->AppendChild(groupItem);
|
||||
}
|
||||
|
||||
TreeItem* productItem = nullptr;
|
||||
|
||||
if (group == common::RadarProductGroup::Level2)
|
||||
{
|
||||
// Level 2 items are not separated by product
|
||||
productItem = groupItem;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Find existing product item (e.g., N0B, N0Q)
|
||||
const QString productName {QString::fromStdString(product)};
|
||||
productItem = groupItem->FindChild(0, productName);
|
||||
|
||||
if (productItem == nullptr)
|
||||
{
|
||||
productItem = new TreeItem({productName});
|
||||
groupItem->AppendChild(productItem);
|
||||
}
|
||||
}
|
||||
|
||||
// Create leaf item for product time
|
||||
productItem->AppendChild(new TreeItem {
|
||||
QString::fromStdString(util::TimeString(latestTime))});
|
||||
},
|
||||
Qt::QueuedConnection);
|
||||
},
|
||||
Qt::QueuedConnection);
|
||||
}
|
||||
|
||||
#include "radar_product_model.moc"
|
||||
|
|
|
|||
|
|
@ -28,6 +28,11 @@ TreeItem::TreeItem(const std::vector<QVariant>& data, TreeItem* parent) :
|
|||
{
|
||||
}
|
||||
|
||||
TreeItem::TreeItem(std::initializer_list<QVariant> data, TreeItem* parent) :
|
||||
TreeItem(std::vector<QVariant> {data}, parent)
|
||||
{
|
||||
}
|
||||
|
||||
TreeItem::~TreeItem() {}
|
||||
|
||||
TreeItem::TreeItem(TreeItem&&) noexcept = default;
|
||||
|
|
@ -39,6 +44,24 @@ void TreeItem::AppendChild(TreeItem* item)
|
|||
p->childItems_.push_back(item);
|
||||
}
|
||||
|
||||
TreeItem* TreeItem::FindChild(int column, const QVariant& data)
|
||||
{
|
||||
auto it = std::find_if(p->childItems_.begin(),
|
||||
p->childItems_.end(),
|
||||
[&](auto& item) {
|
||||
return (column < item->column_count() &&
|
||||
item->data(column) == data);
|
||||
});
|
||||
|
||||
TreeItem* item = nullptr;
|
||||
if (it != p->childItems_.end())
|
||||
{
|
||||
item = *it;
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
const TreeItem* TreeItem::child(int row) const
|
||||
{
|
||||
const TreeItem* item = nullptr;
|
||||
|
|
@ -51,6 +74,23 @@ const TreeItem* TreeItem::child(int row) const
|
|||
return item;
|
||||
}
|
||||
|
||||
TreeItem* TreeItem::child(int row)
|
||||
{
|
||||
TreeItem* item = nullptr;
|
||||
|
||||
if (0 <= row && row < p->childItems_.size())
|
||||
{
|
||||
item = p->childItems_[row];
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
std::vector<TreeItem*> TreeItem::children()
|
||||
{
|
||||
return p->childItems_;
|
||||
}
|
||||
|
||||
int TreeItem::child_count() const
|
||||
{
|
||||
return static_cast<int>(p->childItems_.size());
|
||||
|
|
|
|||
|
|
@ -17,6 +17,8 @@ class TreeItem
|
|||
public:
|
||||
explicit TreeItem(const std::vector<QVariant>& data,
|
||||
TreeItem* parent = nullptr);
|
||||
explicit TreeItem(std::initializer_list<QVariant> data,
|
||||
TreeItem* parent = nullptr);
|
||||
virtual ~TreeItem();
|
||||
|
||||
TreeItem(const TreeItem&) = delete;
|
||||
|
|
@ -25,14 +27,17 @@ public:
|
|||
TreeItem(TreeItem&&) noexcept;
|
||||
TreeItem& operator=(TreeItem&&) noexcept;
|
||||
|
||||
void AppendChild(TreeItem* child);
|
||||
void AppendChild(TreeItem* child);
|
||||
TreeItem* FindChild(int column, const QVariant& data);
|
||||
|
||||
const TreeItem* child(int row) const;
|
||||
int child_count() const;
|
||||
int column_count() const;
|
||||
QVariant data(int column) const;
|
||||
int row() const;
|
||||
const TreeItem* parent_item() const;
|
||||
const TreeItem* child(int row) const;
|
||||
TreeItem* child(int row);
|
||||
std::vector<TreeItem*> children();
|
||||
int child_count() const;
|
||||
int column_count() const;
|
||||
QVariant data(int column) const;
|
||||
int row() const;
|
||||
const TreeItem* parent_item() const;
|
||||
|
||||
private:
|
||||
class Impl;
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ public:
|
|||
};
|
||||
|
||||
TreeModel::TreeModel(QObject* parent) :
|
||||
QAbstractTableModel(parent), p(std::make_unique<TreeModelImpl>())
|
||||
QAbstractItemModel(parent), p(std::make_unique<TreeModelImpl>())
|
||||
{
|
||||
}
|
||||
TreeModel::~TreeModel() = default;
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
#include <scwx/qt/model/tree_item.hpp>
|
||||
|
||||
#include <QAbstractTableModel>
|
||||
#include <QAbstractItemModel>
|
||||
|
||||
namespace scwx
|
||||
{
|
||||
|
|
@ -13,7 +13,7 @@ namespace model
|
|||
|
||||
class TreeModelImpl;
|
||||
|
||||
class TreeModel : public QAbstractTableModel
|
||||
class TreeModel : public QAbstractItemModel
|
||||
{
|
||||
public:
|
||||
explicit TreeModel(QObject* parent = nullptr);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue