2017-04-27 18:25:32 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "qt-models/gpslistmodel.h"
|
cleanup: invert control-flow when resetting the core structures
To reset the core data structures, the mobile and desktop UIs
were calling into the dive-list models, which then reset the
core data structures, themselves and the unrelated
locationinformation model. The UI code then reset various other
things, such as the TankInformation model or the map. . This was
unsatisfying from a control-flow perspective, as the models should
display the core data, not act on it. Moreover, this meant lots
of intricate intermodule-dependencies.
Thus, straighten up the control flow: give the C core the
possibility to send a "all data reset" event. And do that
in those functions that reset the core data structures.
Let each module react to this event by itself. This removes
inter-module dependencies. For example, the MainWindow now
doesn't have to reset the TankInfoModel or the MapWidget.
Then, to reset the core data structures, let the UI code
simply directly call the respective core functions.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-05-04 22:12:36 +00:00
|
|
|
#include "core/subsurface-qt/divelistnotifier.h"
|
2018-06-03 20:15:19 +00:00
|
|
|
#include "core/qthelper.h"
|
2016-01-09 22:07:58 +00:00
|
|
|
#include <QVector>
|
2016-01-07 08:08:00 +00:00
|
|
|
|
2019-09-28 21:23:57 +00:00
|
|
|
GpsListModel::GpsListModel()
|
2016-01-07 08:08:00 +00:00
|
|
|
{
|
cleanup: invert control-flow when resetting the core structures
To reset the core data structures, the mobile and desktop UIs
were calling into the dive-list models, which then reset the
core data structures, themselves and the unrelated
locationinformation model. The UI code then reset various other
things, such as the TankInformation model or the map. . This was
unsatisfying from a control-flow perspective, as the models should
display the core data, not act on it. Moreover, this meant lots
of intricate intermodule-dependencies.
Thus, straighten up the control flow: give the C core the
possibility to send a "all data reset" event. And do that
in those functions that reset the core data structures.
Let each module react to this event by itself. This removes
inter-module dependencies. For example, the MainWindow now
doesn't have to reset the TankInfoModel or the MapWidget.
Then, to reset the core data structures, let the UI code
simply directly call the respective core functions.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-05-04 22:12:36 +00:00
|
|
|
connect(&diveListNotifier, &DiveListNotifier::dataReset, this, &GpsListModel::update);
|
2016-01-07 08:08:00 +00:00
|
|
|
}
|
|
|
|
|
2019-09-27 23:26:54 +00:00
|
|
|
void GpsListModel::update()
|
2016-01-08 15:11:49 +00:00
|
|
|
{
|
2021-01-01 19:04:55 +00:00
|
|
|
GpsLocation *glp = GpsLocation::instance();
|
|
|
|
if (!glp)
|
|
|
|
return;
|
|
|
|
QVector<gpsTracker> trackers = QVector<gpsTracker>::fromList(glp->currentGPSInfo().values());
|
2016-01-08 15:11:49 +00:00
|
|
|
beginResetModel();
|
|
|
|
m_gpsFixes = trackers;
|
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
2016-01-07 08:08:00 +00:00
|
|
|
void GpsListModel::clear()
|
|
|
|
{
|
|
|
|
if (m_gpsFixes.count()) {
|
|
|
|
beginRemoveRows(QModelIndex(), 0, m_gpsFixes.count() - 1);
|
|
|
|
m_gpsFixes.clear();
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-21 15:53:42 +00:00
|
|
|
int GpsListModel::rowCount(const QModelIndex&) const
|
2016-01-07 08:08:00 +00:00
|
|
|
{
|
|
|
|
return m_gpsFixes.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant GpsListModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (index.row() < 0 || index.row() > m_gpsFixes.count())
|
|
|
|
return QVariant();
|
|
|
|
|
2016-01-08 15:11:49 +00:00
|
|
|
const gpsTracker > = m_gpsFixes[index.row()];
|
2016-01-07 08:08:00 +00:00
|
|
|
|
|
|
|
if (role == GpsDateRole)
|
2016-01-08 15:11:49 +00:00
|
|
|
return get_short_dive_date_string(gt.when);
|
2016-01-09 07:13:30 +00:00
|
|
|
else if (role == GpsWhenRole)
|
|
|
|
return gt.when;
|
2016-01-07 08:08:00 +00:00
|
|
|
else if (role == GpsNameRole)
|
2016-01-08 15:11:49 +00:00
|
|
|
return gt.name;
|
2016-01-07 08:08:00 +00:00
|
|
|
else if (role == GpsLatitudeRole)
|
2018-10-20 18:12:15 +00:00
|
|
|
return QString::number(gt.location.lat.udeg / 1000000.0, 'f', 6);
|
2016-01-07 08:08:00 +00:00
|
|
|
else if (role == GpsLongitudeRole)
|
2018-10-20 18:12:15 +00:00
|
|
|
return QString::number(gt.location.lon.udeg / 1000000.0, 'f', 6);
|
2016-01-07 08:08:00 +00:00
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray> GpsListModel::roleNames() const
|
|
|
|
{
|
|
|
|
QHash<int, QByteArray> roles;
|
2016-01-09 07:13:30 +00:00
|
|
|
roles[GpsDateRole] = "date";
|
|
|
|
roles[GpsWhenRole] = "when";
|
2016-01-07 08:08:00 +00:00
|
|
|
roles[GpsNameRole] = "name";
|
|
|
|
roles[GpsLatitudeRole] = "latitude";
|
|
|
|
roles[GpsLongitudeRole] = "longitude";
|
|
|
|
return roles;
|
|
|
|
}
|
2019-09-27 23:26:54 +00:00
|
|
|
|
|
|
|
GpsListModel *GpsListModel::instance()
|
|
|
|
{
|
2019-09-28 21:23:57 +00:00
|
|
|
static GpsListModel self;
|
|
|
|
return &self;
|
2019-09-27 23:26:54 +00:00
|
|
|
}
|