2017-07-29 05:01:33 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#include "maplocationmodel.h"
|
2019-05-09 19:33:01 +00:00
|
|
|
#include "divelocationmodel.h"
|
2018-10-26 15:03:54 +00:00
|
|
|
#include "core/divesite.h"
|
2019-11-17 17:13:55 +00:00
|
|
|
#include "core/divefilter.h"
|
core: introduce divelog structure
The parser API was very annoying, as a number of tables
to-be-filled were passed in as pointers. The goal of this
commit is to collect all these tables in a single struct.
This should make it (more or less) clear what is actually
written into the divelog files.
Moreover, it should now be rather easy to search for
instances, where the global logfile is accessed (and it
turns out that there are many!).
The divelog struct does not contain the tables as substructs,
but only collects pointers. The idea is that the "divelog.h"
file can be included without all the other files describing
the numerous tables.
To make it easier to use from C++ parts of the code, the
struct implements a constructor and a destructor. Sadly,
we can't use smart pointers, since the pointers are accessed
from C code. Therfore the constructor and destructor are
quite complex.
The whole commit is large, but was mostly an automatic
conversion.
One oddity of note: the divelog structure also contains
the "autogroup" flag, since that is saved in the divelog.
This actually fixes a bug: Before, when importing dives
from a different log, the autogroup flag was overwritten.
This was probably not intended and does not happen anymore.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2022-11-08 20:31:08 +00:00
|
|
|
#include "core/divelog.h"
|
2024-05-11 11:21:53 +00:00
|
|
|
#include "core/range.h"
|
2022-08-17 00:17:04 +00:00
|
|
|
#include "core/settings/qPrefDisplay.h"
|
2020-11-15 03:21:16 +00:00
|
|
|
#if !defined(SUBSURFACE_MOBILE) && !defined(SUBSURFACE_DOWNLOADER)
|
2019-05-01 21:33:45 +00:00
|
|
|
#include "qt-models/filtermodels.h"
|
2019-08-30 13:25:59 +00:00
|
|
|
#include "desktop-widgets/mapwidget.h"
|
2019-05-01 21:33:45 +00:00
|
|
|
#endif
|
2024-05-11 11:21:53 +00:00
|
|
|
#include <map>
|
2017-07-29 05:01:33 +00:00
|
|
|
|
2019-05-01 21:33:45 +00:00
|
|
|
#define MIN_DISTANCE_BETWEEN_DIVE_SITES_M 50.0
|
|
|
|
|
2022-08-17 00:14:09 +00:00
|
|
|
// MKW If "Map Short Names" preference is set, only return the last component
|
|
|
|
// of the full dive site name.
|
|
|
|
// Example:
|
|
|
|
// Japan/Izu Peninsula/Atami/Chinsen-Aft
|
|
|
|
// Short name: Chinsen-Aft
|
2024-05-04 15:18:08 +00:00
|
|
|
static QString siteMapDisplayName(const std::string &sitename)
|
2022-08-17 00:14:09 +00:00
|
|
|
{
|
|
|
|
const char Separator = '/';
|
2024-05-04 15:18:08 +00:00
|
|
|
QString fullname = QString::fromStdString(sitename);
|
2022-08-17 00:14:09 +00:00
|
|
|
|
2024-01-16 16:39:19 +00:00
|
|
|
if (!qPrefDisplay::map_short_names() )
|
2022-08-17 00:17:04 +00:00
|
|
|
return fullname;
|
|
|
|
|
|
|
|
QString name = fullname.section(Separator, -1).trimmed();
|
2024-01-16 16:39:19 +00:00
|
|
|
if (name.isEmpty())
|
|
|
|
name = std::move(fullname);
|
2022-08-17 00:14:09 +00:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-31 22:18:15 +00:00
|
|
|
MapLocation::MapLocation(struct dive_site *dsIn, QGeoCoordinate coordIn, QString nameIn, bool selectedIn) :
|
|
|
|
divesite(dsIn), coordinate(coordIn), name(nameIn), selected(selectedIn)
|
2017-07-29 05:01:33 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-08-30 13:25:59 +00:00
|
|
|
// Check whether we are in divesite-edit mode. This doesn't
|
|
|
|
// exist on mobile. And on desktop we have to access the MapWidget.
|
|
|
|
// Simplify this!
|
|
|
|
static bool inEditMode()
|
|
|
|
{
|
2020-11-15 03:21:16 +00:00
|
|
|
#if defined(SUBSURFACE_MOBILE) || defined(SUBSURFACE_DOWNLOADER)
|
2019-08-30 13:25:59 +00:00
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
return MapWidget::instance()->editMode();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-07-29 05:01:33 +00:00
|
|
|
QVariant MapLocation::getRole(int role) const
|
|
|
|
{
|
|
|
|
switch (role) {
|
2019-09-01 14:38:48 +00:00
|
|
|
case RoleDivesite:
|
2019-08-31 22:18:15 +00:00
|
|
|
return QVariant::fromValue(divesite);
|
2019-09-01 14:38:48 +00:00
|
|
|
case RoleCoordinate:
|
2019-08-31 22:18:15 +00:00
|
|
|
return QVariant::fromValue(coordinate);
|
2019-09-01 14:38:48 +00:00
|
|
|
case RoleName:
|
2019-08-31 22:18:15 +00:00
|
|
|
return QVariant::fromValue(name);
|
2019-09-01 14:38:48 +00:00
|
|
|
case RolePixmap:
|
2019-08-31 22:18:15 +00:00
|
|
|
return selected ? QString("qrc:///dive-location-marker-selected-icon") :
|
2019-08-30 13:25:59 +00:00
|
|
|
inEditMode() ? QString("qrc:///dive-location-marker-inactive-icon") :
|
|
|
|
QString("qrc:///dive-location-marker-icon");
|
2019-09-01 14:38:48 +00:00
|
|
|
case RoleZ:
|
2019-08-31 22:18:15 +00:00
|
|
|
return selected ? 1 : 0;
|
2019-09-01 14:38:48 +00:00
|
|
|
case RoleIsSelected:
|
2019-08-31 22:18:15 +00:00
|
|
|
return QVariant::fromValue(selected);
|
2017-07-29 05:01:33 +00:00
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-01 22:09:59 +00:00
|
|
|
MapLocationModel::MapLocationModel(QObject *parent) : QAbstractListModel(parent)
|
2018-10-26 15:03:54 +00:00
|
|
|
{
|
2019-05-09 19:33:01 +00:00
|
|
|
connect(&diveListNotifier, &DiveListNotifier::diveSiteChanged, this, &MapLocationModel::diveSiteChanged);
|
2017-07-29 05:01:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MapLocationModel::~MapLocationModel()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant MapLocationModel::data(const QModelIndex & index, int role) const
|
|
|
|
{
|
2024-05-11 11:21:53 +00:00
|
|
|
if (index.row() < 0 || index.row() >= (int)m_mapLocations.size())
|
2017-07-29 05:01:33 +00:00
|
|
|
return QVariant();
|
|
|
|
|
2024-05-11 11:21:53 +00:00
|
|
|
return m_mapLocations[index.row()].getRole(role);
|
2017-07-29 05:01:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray> MapLocationModel::roleNames() const
|
|
|
|
{
|
2019-08-31 21:17:04 +00:00
|
|
|
QHash<int, QByteArray> roles;
|
2019-09-01 14:38:48 +00:00
|
|
|
roles[MapLocation::RoleDivesite] = "divesite";
|
|
|
|
roles[MapLocation::RoleCoordinate] = "coordinate";
|
|
|
|
roles[MapLocation::RoleName] = "name";
|
|
|
|
roles[MapLocation::RolePixmap] = "pixmap";
|
|
|
|
roles[MapLocation::RoleZ] = "z";
|
|
|
|
roles[MapLocation::RoleIsSelected] = "isSelected";
|
2019-08-31 21:17:04 +00:00
|
|
|
return roles;
|
2017-07-29 05:01:33 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 15:53:42 +00:00
|
|
|
int MapLocationModel::rowCount(const QModelIndex&) const
|
2017-07-29 05:01:33 +00:00
|
|
|
{
|
2024-05-11 11:21:53 +00:00
|
|
|
return (int)m_mapLocations.size();
|
2017-07-29 05:01:33 +00:00
|
|
|
}
|
|
|
|
|
2024-05-11 11:21:53 +00:00
|
|
|
/* UNUSED?
|
2017-07-29 05:01:33 +00:00
|
|
|
void MapLocationModel::add(MapLocation *location)
|
|
|
|
{
|
2024-05-11 11:21:53 +00:00
|
|
|
beginInsertRows(QModelIndex(), (int)m_mapLocations.size(), (int)m_mapLocations.size());
|
2017-07-29 05:01:33 +00:00
|
|
|
m_mapLocations.append(location);
|
|
|
|
endInsertRows();
|
|
|
|
}
|
2024-05-11 11:21:53 +00:00
|
|
|
*/
|
2017-07-29 05:01:33 +00:00
|
|
|
|
2024-05-11 11:21:53 +00:00
|
|
|
const std::vector<dive_site *> &MapLocationModel::selectedDs() const
|
2019-05-03 21:16:40 +00:00
|
|
|
{
|
|
|
|
return m_selectedDs;
|
|
|
|
}
|
|
|
|
|
2024-05-11 09:47:45 +00:00
|
|
|
static bool hasVisibleDive(const dive_site &ds)
|
2019-05-08 19:35:49 +00:00
|
|
|
{
|
2024-05-11 09:47:45 +00:00
|
|
|
return std::any_of(ds.dives.begin(), ds.dives.end(),
|
2019-05-08 19:35:49 +00:00
|
|
|
[] (const dive *d) { return !d->hidden_by_filter; });
|
|
|
|
}
|
|
|
|
|
2024-05-11 09:47:45 +00:00
|
|
|
static bool hasSelectedDive(const dive_site &ds)
|
2017-07-29 05:01:33 +00:00
|
|
|
{
|
2024-05-11 09:47:45 +00:00
|
|
|
return std::any_of(ds.dives.begin(), ds.dives.end(),
|
2019-05-08 19:35:49 +00:00
|
|
|
[] (const dive *d) { return d->selected; });
|
|
|
|
}
|
2019-05-01 21:33:45 +00:00
|
|
|
|
2019-08-30 14:51:59 +00:00
|
|
|
void MapLocationModel::selectionChanged()
|
|
|
|
{
|
2024-05-11 11:21:53 +00:00
|
|
|
if (m_mapLocations.empty())
|
2019-08-30 14:51:59 +00:00
|
|
|
return;
|
2024-05-11 11:21:53 +00:00
|
|
|
for(MapLocation &m: m_mapLocations)
|
|
|
|
m.selected = range_contains(m_selectedDs, m.divesite);
|
|
|
|
emit dataChanged(createIndex(0, 0), createIndex((int)m_mapLocations.size() - 1, 0));
|
2019-08-30 14:51:59 +00:00
|
|
|
}
|
|
|
|
|
2019-05-08 20:15:01 +00:00
|
|
|
void MapLocationModel::reload(QObject *map)
|
2019-05-08 19:35:49 +00:00
|
|
|
{
|
2019-05-01 21:33:45 +00:00
|
|
|
beginResetModel();
|
2017-07-29 05:01:33 +00:00
|
|
|
|
|
|
|
m_mapLocations.clear();
|
2019-05-01 22:09:59 +00:00
|
|
|
m_selectedDs.clear();
|
2019-05-01 21:33:45 +00:00
|
|
|
|
2024-05-11 11:21:53 +00:00
|
|
|
std::map<QString, size_t> locationNameMap;
|
2019-05-01 21:33:45 +00:00
|
|
|
|
2020-11-15 03:21:16 +00:00
|
|
|
#if defined(SUBSURFACE_MOBILE) || defined(SUBSURFACE_DOWNLOADER)
|
2019-05-01 21:33:45 +00:00
|
|
|
bool diveSiteMode = false;
|
|
|
|
#else
|
|
|
|
// In dive site mode (that is when either editing a dive site or on
|
|
|
|
// the dive site tab), we want to show all dive sites, not only those
|
2019-05-03 21:37:42 +00:00
|
|
|
// of the non-hidden dives. Moreover, the selected dive sites are those
|
|
|
|
// that we filter for.
|
2019-11-17 17:13:55 +00:00
|
|
|
bool diveSiteMode = DiveFilter::instance()->diveSiteMode();
|
2019-05-03 21:37:42 +00:00
|
|
|
if (diveSiteMode)
|
2019-11-17 17:13:55 +00:00
|
|
|
m_selectedDs = DiveFilter::instance()->filteredDiveSites();
|
2019-05-01 21:33:45 +00:00
|
|
|
#endif
|
2024-06-08 14:30:24 +00:00
|
|
|
for (const auto &ds: divelog.sites) {
|
2019-05-08 20:15:01 +00:00
|
|
|
QGeoCoordinate dsCoord;
|
2019-05-08 19:35:49 +00:00
|
|
|
|
|
|
|
// Don't show dive sites of hidden dives, unless we're in dive site edit mode.
|
2024-05-11 09:47:45 +00:00
|
|
|
if (!diveSiteMode && !hasVisibleDive(*ds))
|
2019-05-01 21:33:45 +00:00
|
|
|
continue;
|
2024-05-11 09:47:45 +00:00
|
|
|
if (!dive_site_has_gps_location(ds.get())) {
|
2019-05-08 20:15:01 +00:00
|
|
|
// Dive sites that do not have a gps location are not shown in normal mode.
|
|
|
|
// In dive-edit mode, selected sites are placed at the center of the map,
|
|
|
|
// so that the user can drag them somewhere without having to enter coordinates.
|
2024-05-11 11:21:53 +00:00
|
|
|
if (!diveSiteMode || !range_contains(m_selectedDs, ds.get()) || !map)
|
2019-05-08 20:15:01 +00:00
|
|
|
continue;
|
|
|
|
dsCoord = map->property("center").value<QGeoCoordinate>();
|
|
|
|
} else {
|
|
|
|
qreal latitude = ds->location.lat.udeg * 0.000001;
|
|
|
|
qreal longitude = ds->location.lon.udeg * 0.000001;
|
|
|
|
dsCoord = QGeoCoordinate(latitude, longitude);
|
|
|
|
}
|
2024-05-11 11:21:53 +00:00
|
|
|
if (!diveSiteMode && hasSelectedDive(*ds) && !range_contains(m_selectedDs, ds.get()))
|
|
|
|
m_selectedDs.push_back(ds.get());
|
2022-08-17 00:14:09 +00:00
|
|
|
QString name = siteMapDisplayName(ds->name);
|
2019-05-08 19:35:49 +00:00
|
|
|
if (!diveSiteMode) {
|
|
|
|
// don't add dive locations with the same name, unless they are
|
|
|
|
// at least MIN_DISTANCE_BETWEEN_DIVE_SITES_M apart
|
2024-05-11 11:21:53 +00:00
|
|
|
auto it = locationNameMap.find(name);
|
|
|
|
if (it != locationNameMap.end()) {
|
|
|
|
const MapLocation &existingLocation = m_mapLocations[it->second];
|
|
|
|
QGeoCoordinate coord = existingLocation.coordinate;
|
2019-05-08 19:35:49 +00:00
|
|
|
if (dsCoord.distanceTo(coord) < MIN_DISTANCE_BETWEEN_DIVE_SITES_M)
|
|
|
|
continue;
|
|
|
|
}
|
2019-05-01 21:33:45 +00:00
|
|
|
}
|
2024-05-11 11:21:53 +00:00
|
|
|
bool selected = range_contains(m_selectedDs, ds.get());
|
|
|
|
m_mapLocations.emplace_back(ds.get(), dsCoord, name, selected);
|
2019-07-27 20:30:09 +00:00
|
|
|
if (!diveSiteMode)
|
2024-05-11 11:21:53 +00:00
|
|
|
locationNameMap[name] = m_mapLocations.size() - 1;
|
2019-05-01 21:33:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
endResetModel();
|
2017-07-29 05:01:33 +00:00
|
|
|
}
|
|
|
|
|
2019-08-30 10:38:25 +00:00
|
|
|
void MapLocationModel::setSelected(struct dive_site *ds)
|
2018-10-26 15:03:54 +00:00
|
|
|
{
|
2019-05-01 22:09:59 +00:00
|
|
|
m_selectedDs.clear();
|
2019-08-30 10:38:25 +00:00
|
|
|
if (ds)
|
2024-05-11 11:21:53 +00:00
|
|
|
m_selectedDs.push_back(ds);
|
2018-10-26 15:03:54 +00:00
|
|
|
}
|
|
|
|
|
2024-05-11 11:21:53 +00:00
|
|
|
void MapLocationModel::setSelected(const std::vector<dive_site *> &divesites)
|
2019-08-30 15:38:54 +00:00
|
|
|
{
|
|
|
|
m_selectedDs = divesites;
|
|
|
|
}
|
|
|
|
|
2018-10-26 15:03:54 +00:00
|
|
|
MapLocation *MapLocationModel::getMapLocation(const struct dive_site *ds)
|
2017-07-29 05:01:33 +00:00
|
|
|
{
|
2024-05-11 11:21:53 +00:00
|
|
|
for (MapLocation &location: m_mapLocations) {
|
|
|
|
if (location.divesite == ds)
|
|
|
|
return &location;
|
2017-07-29 05:01:33 +00:00
|
|
|
}
|
2022-08-17 01:20:11 +00:00
|
|
|
return nullptr;
|
2017-07-29 05:01:33 +00:00
|
|
|
}
|
2017-11-09 16:43:21 +00:00
|
|
|
|
2019-05-09 19:33:01 +00:00
|
|
|
void MapLocationModel::diveSiteChanged(struct dive_site *ds, int field)
|
2017-11-09 16:43:21 +00:00
|
|
|
{
|
2019-05-09 19:33:01 +00:00
|
|
|
// Find dive site
|
2024-05-11 11:21:53 +00:00
|
|
|
auto it = std::find_if(m_mapLocations.begin(), m_mapLocations.end(),
|
|
|
|
[ds](auto &entry) { return entry.divesite == ds; });
|
|
|
|
if (it == m_mapLocations.end())
|
2019-05-09 19:33:01 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
switch (field) {
|
|
|
|
case LocationInformationModel::LOCATION:
|
|
|
|
if (has_location(&ds->location)) {
|
|
|
|
const qreal latitude_r = ds->location.lat.udeg * 0.000001;
|
|
|
|
const qreal longitude_r = ds->location.lon.udeg * 0.000001;
|
|
|
|
QGeoCoordinate coord(latitude_r, longitude_r);
|
2024-05-11 11:21:53 +00:00
|
|
|
it->coordinate = coord;
|
2017-11-09 16:43:21 +00:00
|
|
|
}
|
2019-05-09 19:33:01 +00:00
|
|
|
break;
|
|
|
|
case LocationInformationModel::NAME:
|
2024-05-11 11:21:53 +00:00
|
|
|
it->name = siteMapDisplayName(ds->name);
|
2019-05-09 19:33:01 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2017-11-09 16:43:21 +00:00
|
|
|
}
|
2019-05-09 19:33:01 +00:00
|
|
|
|
2024-05-11 11:21:53 +00:00
|
|
|
int row = static_cast<int>(it - m_mapLocations.begin());
|
2019-05-09 19:33:01 +00:00
|
|
|
emit dataChanged(createIndex(row, 0), createIndex(row, 0));
|
2017-11-09 16:43:21 +00:00
|
|
|
}
|