2023-12-15 15:48:40 +00:00
|
|
|
// Copyright (C) 2023 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
|
|
|
|
|
|
|
#include "controller.h"
|
2024-01-22 13:02:02 +00:00
|
|
|
#include "laststrikeinfo.h"
|
|
|
|
#include "lightningitemmodel.h"
|
|
|
|
#include "lightningprovider.h"
|
2023-12-15 15:48:40 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
constexpr int NOTIFICATION_RADIUS = 100000; // meters
|
|
|
|
}
|
|
|
|
|
|
|
|
Controller::Controller(QObject *parent)
|
|
|
|
: QObject{parent}
|
|
|
|
{
|
2024-01-16 14:14:12 +00:00
|
|
|
connect(&m_provider, &LightningProvider::dataReady, this, &Controller::onDataReceived);
|
2023-12-15 15:48:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Controller::~Controller()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QAbstractItemModel *Controller::getModel()
|
|
|
|
{
|
2024-01-16 14:14:12 +00:00
|
|
|
return &m_model;
|
2023-12-15 15:48:40 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 13:47:32 +00:00
|
|
|
int Controller::getLastStrikeTime()
|
2023-12-15 15:48:40 +00:00
|
|
|
{
|
2024-01-16 14:14:12 +00:00
|
|
|
return m_lastStrikeInfo.isValid() ? m_lastStrikeInfo.timestamp.count() : -1;
|
2023-12-15 15:48:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double Controller::getLastStrikeDistance()
|
|
|
|
{
|
2024-01-16 14:14:12 +00:00
|
|
|
return m_lastStrikeInfo.isValid() ? m_lastStrikeInfo.distance : -1;
|
2023-12-15 15:48:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double Controller::getLastStrikeDirection()
|
|
|
|
{
|
2024-01-16 14:14:12 +00:00
|
|
|
return m_lastStrikeInfo.isValid() ? m_lastStrikeInfo.direction : -1;
|
2023-12-15 15:48:40 +00:00
|
|
|
}
|
|
|
|
|
2024-01-18 11:45:31 +00:00
|
|
|
bool Controller::isDistanceTimeLayerEnabled() const
|
|
|
|
{
|
|
|
|
return m_distanceTimeLayerEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Controller::updateDistanceTime()
|
|
|
|
{
|
|
|
|
if (!isDistanceTimeLayerEnabled())
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_lastStrikeInfo.invalidate();
|
|
|
|
m_model.getLatestStrikeInfo(m_userLocation, NOTIFICATION_RADIUS, &m_lastStrikeInfo);
|
|
|
|
|
|
|
|
emit lastStrikeInfoUpdated();
|
|
|
|
}
|
|
|
|
|
2024-01-09 09:55:48 +00:00
|
|
|
void Controller::updateDistanceTime(const LightningItemData &data)
|
2023-12-15 15:48:40 +00:00
|
|
|
{
|
2024-01-18 11:45:31 +00:00
|
|
|
if (!isDistanceTimeLayerEnabled())
|
|
|
|
return;
|
|
|
|
|
2024-01-16 14:14:12 +00:00
|
|
|
LastStrikeInfo strikeInfo(data.getDistanceTo(m_userLocation),
|
2024-01-09 09:55:48 +00:00
|
|
|
data.timestamp,
|
2024-01-16 14:14:12 +00:00
|
|
|
data.getDirectionFrom(m_userLocation));
|
2023-12-15 15:48:40 +00:00
|
|
|
if (strikeInfo.distance > NOTIFICATION_RADIUS)
|
|
|
|
return;
|
|
|
|
|
2024-01-16 14:14:12 +00:00
|
|
|
m_lastStrikeInfo = strikeInfo;
|
2023-12-15 15:48:40 +00:00
|
|
|
|
|
|
|
emit lastStrikeInfoUpdated();
|
|
|
|
}
|
|
|
|
|
2024-01-09 09:55:48 +00:00
|
|
|
void Controller::onDataReceived(const LightningItemData &data)
|
2023-12-15 15:48:40 +00:00
|
|
|
{
|
2024-01-16 14:14:12 +00:00
|
|
|
m_model.insertData(data);
|
2023-12-15 15:48:40 +00:00
|
|
|
|
|
|
|
updateDistanceTime(data);
|
|
|
|
}
|
|
|
|
|
2024-01-26 18:31:08 +00:00
|
|
|
void Controller::setUserLocation(const QGeoCoordinate &coordinate)
|
2023-12-15 15:48:40 +00:00
|
|
|
{
|
2024-01-26 18:31:08 +00:00
|
|
|
if (m_userLocation == coordinate)
|
2023-12-15 15:48:40 +00:00
|
|
|
return;
|
2024-01-26 18:31:08 +00:00
|
|
|
m_userLocation = coordinate;
|
2023-12-15 15:48:40 +00:00
|
|
|
|
2024-01-18 11:45:31 +00:00
|
|
|
updateDistanceTime();
|
|
|
|
}
|
2023-12-15 15:48:40 +00:00
|
|
|
|
2024-01-18 11:45:31 +00:00
|
|
|
void Controller::setDistanceTimeLayerEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
if (m_distanceTimeLayerEnabled == enabled)
|
|
|
|
return;
|
|
|
|
m_distanceTimeLayerEnabled = enabled;
|
|
|
|
emit distanceTimeLayerEnabledChanged();
|
|
|
|
|
|
|
|
updateDistanceTime();
|
2023-12-15 15:48:40 +00:00
|
|
|
}
|