Refactor Alert Dock Widget to its own class

This commit is contained in:
Dan Paulat 2022-10-16 00:16:35 -05:00
parent 1c5e0d51b7
commit f8021b00bf
6 changed files with 323 additions and 132 deletions

View file

@ -0,0 +1,139 @@
#include "alert_dock_widget.hpp"
#include "ui_alert_dock_widget.h"
#include <scwx/qt/manager/text_event_manager.hpp>
#include <scwx/qt/model/alert_model.hpp>
#include <scwx/qt/types/qt_types.hpp>
#include <scwx/util/logger.hpp>
#include <QSortFilterProxyModel>
namespace scwx
{
namespace qt
{
namespace ui
{
static const std::string logPrefix_ = "scwx::qt::ui::alert_dock_widget";
static const auto logger_ = scwx::util::Logger::Create(logPrefix_);
class AlertDockWidgetImpl : QObject
{
Q_OBJECT
public:
explicit AlertDockWidgetImpl(AlertDockWidget* self) :
self_ {self},
alertModel_ {std::make_unique<model::AlertModel>()},
proxyModel_ {std::make_unique<QSortFilterProxyModel>()},
mapPosition_ {},
mapUpdateDeferred_ {false}
{
proxyModel_->setSourceModel(alertModel_.get());
proxyModel_->setSortRole(types::SortRole);
proxyModel_->setFilterCaseSensitivity(Qt::CaseInsensitive);
proxyModel_->setFilterKeyColumn(-1);
}
~AlertDockWidgetImpl() = default;
void ConnectSignals();
AlertDockWidget* self_;
std::unique_ptr<model::AlertModel> alertModel_;
std::unique_ptr<QSortFilterProxyModel> proxyModel_;
scwx::common::Coordinate mapPosition_;
bool mapUpdateDeferred_;
};
AlertDockWidget::AlertDockWidget(QWidget* parent) :
QDockWidget(parent),
p {std::make_unique<AlertDockWidgetImpl>(this)},
ui(new Ui::AlertDockWidget)
{
ui->setupUi(this);
ui->alertView->setModel(p->proxyModel_.get());
ui->alertSettings->addAction(ui->actionActiveAlerts);
p->ConnectSignals();
}
AlertDockWidget::~AlertDockWidget()
{
delete ui;
}
void AlertDockWidget::showEvent(QShowEvent* event)
{
if (p->mapUpdateDeferred_)
{
p->alertModel_->HandleMapUpdate(p->mapPosition_.latitude_,
p->mapPosition_.longitude_);
p->mapUpdateDeferred_ = false;
}
QDockWidget::showEvent(event);
}
void AlertDockWidget::HandleMapUpdate(double latitude, double longitude)
{
p->mapPosition_ = {latitude, longitude};
if (isVisible())
{
p->alertModel_->HandleMapUpdate(latitude, longitude);
}
else
{
p->mapUpdateDeferred_ = true;
}
}
void AlertDockWidgetImpl::ConnectSignals()
{
connect(self_->ui->alertFilter,
&QLineEdit::textChanged,
proxyModel_.get(),
&QSortFilterProxyModel::setFilterWildcard);
connect(&manager::TextEventManager::Instance(),
&manager::TextEventManager::AlertUpdated,
alertModel_.get(),
&model::AlertModel::HandleAlert,
Qt::QueuedConnection);
connect(self_->ui->alertView->selectionModel(),
&QItemSelectionModel::selectionChanged,
this,
[=](const QItemSelection& selected, const QItemSelection& deselected)
{
if (selected.size() == 0 && deselected.size() == 0)
{
// Items which stay selected but change their index are not
// included in selected and deselected. Thus, this signal might
// be emitted with both selected and deselected empty, if only
// the indices of selected items change.
return;
}
});
connect(self_->ui->alertViewButton,
&QPushButton::clicked,
this,
[]()
{
// TODO: View alert
});
connect(self_->ui->alertGoButton,
&QPushButton::clicked,
this,
[]()
{
// TODO: Go to alert
});
}
#include "alert_dock_widget.moc"
} // namespace ui
} // namespace qt
} // namespace scwx

View file

@ -0,0 +1,41 @@
#pragma once
#include <QDockWidget>
namespace Ui
{
class AlertDockWidget;
}
namespace scwx
{
namespace qt
{
namespace ui
{
class AlertDockWidgetImpl;
class AlertDockWidget : public QDockWidget
{
Q_OBJECT
public:
explicit AlertDockWidget(QWidget* parent = nullptr);
~AlertDockWidget();
protected:
void showEvent(QShowEvent*) override;
public slots:
void HandleMapUpdate(double latitude, double longitude);
private:
friend class AlertDockWidgetImpl;
std::unique_ptr<AlertDockWidgetImpl> p;
Ui::AlertDockWidget* ui;
};
} // namespace ui
} // namespace qt
} // namespace scwx

View file

@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>AlertDockWidget</class>
<widget class="QDockWidget" name="AlertDockWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Alerts</string>
</property>
<widget class="QWidget" name="dockWidgetContents">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTreeView" name="alertView">
<property name="alternatingRowColors">
<bool>true</bool>
</property>
<property name="indentation">
<number>0</number>
</property>
<property name="sortingEnabled">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QFrame" name="frame_2">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QLineEdit" name="alertFilter">
<property name="placeholderText">
<string>Filter</string>
</property>
<property name="clearButtonEnabled">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QToolButton" name="alertSettings">
<property name="toolTip">
<string>Settings</string>
</property>
<property name="text">
<string>...</string>
</property>
<property name="icon">
<iconset resource="../../../../scwx-qt.qrc">
<normaloff>:/res/icons/font-awesome-6/sliders-solid.svg</normaloff>:/res/icons/font-awesome-6/sliders-solid.svg</iconset>
</property>
<property name="popupMode">
<enum>QToolButton::InstantPopup</enum>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="alertViewButton">
<property name="text">
<string>&amp;View</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="alertGoButton">
<property name="text">
<string>&amp;Go</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<action name="actionActiveAlerts">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>&amp;Active Alerts</string>
</property>
</action>
</widget>
<resources>
<include location="../../../../scwx-qt.qrc"/>
</resources>
<connections/>
</ui>