Handle selection of items in the alert view

This commit is contained in:
Dan Paulat 2022-10-16 00:49:00 -05:00
parent f8021b00bf
commit c117078335
5 changed files with 60 additions and 4 deletions

View file

@ -66,6 +66,25 @@ AlertModel::AlertModel(QObject* parent) :
} }
AlertModel::~AlertModel() = default; AlertModel::~AlertModel() = default;
types::TextEventKey AlertModel::key(const QModelIndex& index) const
{
return index.isValid() ? p->textEventKeys_[index.row()] :
types::TextEventKey {};
}
common::Coordinate AlertModel::centroid(const types::TextEventKey& key) const
{
common::Coordinate centroid {};
const auto& it = p->centroidMap_.find(key);
if (it != p->centroidMap_.cend())
{
centroid = it->second;
}
return centroid;
}
int AlertModel::rowCount(const QModelIndex& parent) const int AlertModel::rowCount(const QModelIndex& parent) const
{ {
return parent.isValid() ? 0 : p->textEventKeys_.size(); return parent.isValid() ? 0 : p->textEventKeys_.size();

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <scwx/qt/types/text_event_key.hpp> #include <scwx/qt/types/text_event_key.hpp>
#include <scwx/common/geographic.hpp>
#include <memory> #include <memory>
@ -21,6 +22,9 @@ public:
explicit AlertModel(QObject* parent = nullptr); explicit AlertModel(QObject* parent = nullptr);
~AlertModel(); ~AlertModel();
types::TextEventKey key(const QModelIndex& index) const;
common::Coordinate centroid(const types::TextEventKey& key) const;
int rowCount(const QModelIndex& parent = QModelIndex()) const override; int rowCount(const QModelIndex& parent = QModelIndex()) const override;
int columnCount(const QModelIndex& parent = QModelIndex()) const override; int columnCount(const QModelIndex& parent = QModelIndex()) const override;

View file

@ -15,10 +15,10 @@ static const std::string logPrefix_ = "scwx::qt::types::text_event_key";
std::string TextEventKey::ToString() const std::string TextEventKey::ToString() const
{ {
return std::format("{}, {}, {}, {}", return std::format("{}.{}.{}.{}",
officeId_, officeId_,
awips::GetPhenomenonText(phenomenon_), awips::GetPhenomenonCode(phenomenon_),
awips::GetSignificanceText(significance_), awips::GetSignificanceCode(significance_),
etn_); etn_);
} }

View file

@ -11,6 +11,7 @@ namespace types
struct TextEventKey struct TextEventKey
{ {
TextEventKey() : TextEventKey(awips::PVtec {}) {}
TextEventKey(const awips::PVtec& pvtec) : TextEventKey(const awips::PVtec& pvtec) :
officeId_ {pvtec.office_id()}, officeId_ {pvtec.office_id()},
phenomenon_ {pvtec.phenomenon()}, phenomenon_ {pvtec.phenomenon()},

View file

@ -27,7 +27,9 @@ public:
alertModel_ {std::make_unique<model::AlertModel>()}, alertModel_ {std::make_unique<model::AlertModel>()},
proxyModel_ {std::make_unique<QSortFilterProxyModel>()}, proxyModel_ {std::make_unique<QSortFilterProxyModel>()},
mapPosition_ {}, mapPosition_ {},
mapUpdateDeferred_ {false} mapUpdateDeferred_ {false},
selectedAlertKey_ {},
selectedAlertCentroid_ {}
{ {
proxyModel_->setSourceModel(alertModel_.get()); proxyModel_->setSourceModel(alertModel_.get());
proxyModel_->setSortRole(types::SortRole); proxyModel_->setSortRole(types::SortRole);
@ -44,6 +46,9 @@ public:
scwx::common::Coordinate mapPosition_; scwx::common::Coordinate mapPosition_;
bool mapUpdateDeferred_; bool mapUpdateDeferred_;
types::TextEventKey selectedAlertKey_;
common::Coordinate selectedAlertCentroid_;
}; };
AlertDockWidget::AlertDockWidget(QWidget* parent) : AlertDockWidget::AlertDockWidget(QWidget* parent) :
@ -57,6 +62,9 @@ AlertDockWidget::AlertDockWidget(QWidget* parent) :
ui->alertSettings->addAction(ui->actionActiveAlerts); ui->alertSettings->addAction(ui->actionActiveAlerts);
ui->alertViewButton->setEnabled(false);
ui->alertGoButton->setEnabled(false);
p->ConnectSignals(); p->ConnectSignals();
} }
@ -115,6 +123,30 @@ void AlertDockWidgetImpl::ConnectSignals()
// the indices of selected items change. // the indices of selected items change.
return; return;
} }
bool itemSelected = selected.size() > 0;
bool itemHasCoordinates = false;
if (itemSelected)
{
QModelIndex selectedIndex =
proxyModel_->mapToSource(selected[0].indexes()[0]);
selectedAlertKey_ = alertModel_->key(selectedIndex);
selectedAlertCentroid_ =
alertModel_->centroid(selectedAlertKey_);
itemHasCoordinates =
selectedAlertCentroid_ != common::Coordinate {};
}
else
{
selectedAlertKey_ = {};
selectedAlertCentroid_ = {};
}
self_->ui->alertViewButton->setEnabled(itemSelected);
self_->ui->alertGoButton->setEnabled(itemHasCoordinates);
logger_->debug("Selected: {}", selectedAlertKey_.ToString());
}); });
connect(self_->ui->alertViewButton, connect(self_->ui->alertViewButton,
&QPushButton::clicked, &QPushButton::clicked,