From ad7f3674d652b78b6f3947615c29586ce704819b Mon Sep 17 00:00:00 2001 From: Dan Paulat Date: Sun, 16 Oct 2022 23:29:59 -0500 Subject: [PATCH] Alert dialog button logic, update dialog on message receipt --- .../scwx/qt/manager/text_event_manager.cpp | 7 +- scwx-qt/source/scwx/qt/ui/alert_dialog.cpp | 101 +++++++++++++++--- scwx-qt/source/scwx/qt/ui/alert_dialog.hpp | 9 +- .../source/scwx/qt/ui/alert_dock_widget.cpp | 3 +- 4 files changed, 98 insertions(+), 22 deletions(-) diff --git a/scwx-qt/source/scwx/qt/manager/text_event_manager.cpp b/scwx-qt/source/scwx/qt/manager/text_event_manager.cpp index a2da6736..3774f293 100644 --- a/scwx-qt/source/scwx/qt/manager/text_event_manager.cpp +++ b/scwx-qt/source/scwx/qt/manager/text_event_manager.cpp @@ -137,9 +137,10 @@ void TextEventManager::Impl::HandleMessage( } else if (std::find_if(it->second.cbegin(), it->second.cend(), - [=](auto& storedMessage) { - return message->wmo_header() == - storedMessage->wmo_header(); + [=](auto& storedMessage) + { + return *message->wmo_header().get() == + *storedMessage->wmo_header().get(); }) == it->second.cend()) { // If there was a matching event, and this message has not been stored diff --git a/scwx-qt/source/scwx/qt/ui/alert_dialog.cpp b/scwx-qt/source/scwx/qt/ui/alert_dialog.cpp index 57e6800b..e131e0aa 100644 --- a/scwx-qt/source/scwx/qt/ui/alert_dialog.cpp +++ b/scwx-qt/source/scwx/qt/ui/alert_dialog.cpp @@ -4,6 +4,8 @@ #include #include +#include + namespace scwx { namespace qt @@ -14,21 +16,28 @@ namespace ui static const std::string logPrefix_ = "scwx::qt::ui::alert_dialog"; static const auto logger_ = scwx::util::Logger::Create(logPrefix_); -class AlertDialogImpl +class AlertDialogImpl : public QObject { + Q_OBJECT public: explicit AlertDialogImpl(AlertDialog* self) : - self_ {self}, key_ {}, coordinate_ {}, currentIndex_ {0u} + self_ {self}, + goButton_ {nullptr}, + key_ {}, + centroid_ {}, + currentIndex_ {0u} { } ~AlertDialogImpl() = default; void ConnectSignals(); void SelectIndex(size_t newIndex); + void UpdateAlertInfo(); AlertDialog* self_; + QPushButton* goButton_; types::TextEventKey key_; - common::Coordinate coordinate_; + common::Coordinate centroid_; size_t currentIndex_; }; @@ -45,7 +54,7 @@ AlertDialog::AlertDialog(QWidget* parent) : ui->alertText->setFont(monospaceFont); // Add Go button to button box - ui->buttonBox->addButton("&Go", QDialogButtonBox::ActionRole); + p->goButton_ = ui->buttonBox->addButton("&Go", QDialogButtonBox::ActionRole); p->ConnectSignals(); } @@ -55,13 +64,33 @@ AlertDialog::~AlertDialog() delete ui; } -void AlertDialogImpl::ConnectSignals() {} - -bool AlertDialog::SelectAlert(const types::TextEventKey& key, - const common::Coordinate& coordinate) +void AlertDialogImpl::ConnectSignals() { - p->key_ = key; - p->coordinate_ = coordinate; + connect( + &manager::TextEventManager::Instance(), + &manager::TextEventManager::AlertUpdated, + this, + [=](const types::TextEventKey& key) + { + if (key == key_) + { + UpdateAlertInfo(); + } + }, + Qt::QueuedConnection); + connect(goButton_, + &QPushButton::clicked, + this, + [=]() + { + emit self_->MoveMap(centroid_.latitude_, centroid_.longitude_); + self_->close(); + }); +} + +bool AlertDialog::SelectAlert(const types::TextEventKey& key) +{ + p->key_ = key; setWindowTitle(QString::fromStdString(key.ToFullString())); @@ -71,7 +100,7 @@ bool AlertDialog::SelectAlert(const types::TextEventKey& key, return false; } - p->SelectIndex(messages.size() - 1); + p->SelectIndex(messages.size() - 1u); return true; } @@ -92,19 +121,61 @@ void AlertDialogImpl::SelectIndex(size_t newIndex) self_->ui->alertText->setText( QString::fromStdString(messages[currentIndex_]->message_content())); - self_->ui->messageCountLabel->setText( - QObject::tr("%1 of %2").arg(currentIndex_ + 1).arg(messageCount)); - bool firstSelected = (currentIndex_ == 0); - bool lastSelected = (currentIndex_ == messages.size() - 1); + UpdateAlertInfo(); +} + +void AlertDialogImpl::UpdateAlertInfo() +{ + auto messages = manager::TextEventManager::Instance().message_list(key_); + size_t messageCount = messages.size(); + + bool firstSelected = (currentIndex_ == 0u); + bool lastSelected = (currentIndex_ == messageCount - 1u); self_->ui->firstButton->setEnabled(!firstSelected); self_->ui->previousButton->setEnabled(!firstSelected); self_->ui->nextButton->setEnabled(!lastSelected); self_->ui->lastButton->setEnabled(!lastSelected); + + self_->ui->messageCountLabel->setText( + QObject::tr("%1 of %2").arg(currentIndex_ + 1u).arg(messageCount)); + + // Update centroid + auto alertSegment = messages[currentIndex_]->segments().back(); + if (alertSegment->codedLocation_.has_value()) + { + centroid_ = + common::GetCentroid(alertSegment->codedLocation_->coordinates()); + } + + goButton_->setEnabled(centroid_ != common::Coordinate {}); } +void AlertDialog::on_firstButton_clicked() +{ + p->SelectIndex(0); +} + +void AlertDialog::on_previousButton_clicked() +{ + p->SelectIndex(p->currentIndex_ - 1u); +} + +void AlertDialog::on_nextButton_clicked() +{ + p->SelectIndex(p->currentIndex_ + 1u); +} + +void AlertDialog::on_lastButton_clicked() +{ + p->SelectIndex(manager::TextEventManager::Instance().message_count(p->key_) - + 1u); +} + +#include "alert_dialog.moc" + } // namespace ui } // namespace qt } // namespace scwx diff --git a/scwx-qt/source/scwx/qt/ui/alert_dialog.hpp b/scwx-qt/source/scwx/qt/ui/alert_dialog.hpp index 6768beea..bba51f4f 100644 --- a/scwx-qt/source/scwx/qt/ui/alert_dialog.hpp +++ b/scwx-qt/source/scwx/qt/ui/alert_dialog.hpp @@ -27,8 +27,13 @@ public: explicit AlertDialog(QWidget* parent = nullptr); ~AlertDialog(); - bool SelectAlert(const types::TextEventKey& key, - const common::Coordinate& coordinate); + bool SelectAlert(const types::TextEventKey& key); + +public slots: + void on_firstButton_clicked(); + void on_previousButton_clicked(); + void on_nextButton_clicked(); + void on_lastButton_clicked(); signals: void MoveMap(double latitude, double longitude); diff --git a/scwx-qt/source/scwx/qt/ui/alert_dock_widget.cpp b/scwx-qt/source/scwx/qt/ui/alert_dock_widget.cpp index 69350fdf..b8c1cef6 100644 --- a/scwx-qt/source/scwx/qt/ui/alert_dock_widget.cpp +++ b/scwx-qt/source/scwx/qt/ui/alert_dock_widget.cpp @@ -158,8 +158,7 @@ void AlertDockWidgetImpl::ConnectSignals() [=]() { // View alert - alertDialog_->SelectAlert(selectedAlertKey_, - selectedAlertCentroid_); + alertDialog_->SelectAlert(selectedAlertKey_); alertDialog_->show(); }); connect(self_->ui->alertGoButton,