1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
// SPDX-License-Identifier: GPL-2.0
#include <QApplication>
#include <QClipboard>
#include <QGeoCoordinate>
#include <QDebug>
#include "qmlmapwidgethelper.h"
#include "core/dive.h"
#include "core/divesite.h"
#include "qt-models/maplocationmodel.h"
#define MIN_DISTANCE_BETWEEN_DIVE_SITES_M 50.0
#define SMALL_CIRCLE_RADIUS_PX 26.0
MapWidgetHelper::MapWidgetHelper(QObject *parent) : QObject(parent)
{
m_mapLocationModel = new MapLocationModel(this);
connect(m_mapLocationModel, SIGNAL(selectedLocationChanged(MapLocation *)),
this, SLOT(selectedLocationChanged(MapLocation *)));
}
void MapWidgetHelper::centerOnDiveSite(struct dive_site *ds)
{
if (!dive_site_has_gps_location(ds)) {
m_mapLocationModel->setSelectedUuid(ds ? ds->uuid : 0, false);
QMetaObject::invokeMethod(m_map, "deselectMapLocation");
return;
}
m_mapLocationModel->setSelectedUuid(ds->uuid, false);
const qreal latitude = ds->latitude.udeg * 0.000001;
const qreal longitude = ds->longitude.udeg * 0.000001;
QGeoCoordinate dsCoord(latitude, longitude);
QMetaObject::invokeMethod(m_map, "centerOnCoordinate", Q_ARG(QVariant, QVariant::fromValue(dsCoord)));
}
void MapWidgetHelper::reloadMapLocations()
{
struct dive_site *ds;
int idx;
QMap<QString, MapLocation *> locationNameMap;
m_mapLocationModel->clear();
MapLocation *location;
QVector<MapLocation *> locationList;
qreal latitude, longitude;
if (displayed_dive_site.uuid && dive_site_has_gps_location(&displayed_dive_site)) {
latitude = displayed_dive_site.latitude.udeg * 0.000001;
longitude = displayed_dive_site.longitude.udeg * 0.000001;
location = new MapLocation(displayed_dive_site.uuid, QGeoCoordinate(latitude, longitude),
QString(displayed_dive_site.name));
locationList.append(location);
locationNameMap[QString(displayed_dive_site.name)] = location;
}
for_each_dive_site(idx, ds) {
if (!dive_site_has_gps_location(ds) || ds->uuid == displayed_dive_site.uuid)
continue;
latitude = ds->latitude.udeg * 0.000001;
longitude = ds->longitude.udeg * 0.000001;
QGeoCoordinate dsCoord(latitude, longitude);
QString name(ds->name);
// don't add dive locations with the same name, unless they are
// at least MIN_DISTANCE_BETWEEN_DIVE_SITES_M apart
if (locationNameMap[name]) {
MapLocation *existingLocation = locationNameMap[name];
QGeoCoordinate coord = qvariant_cast<QGeoCoordinate>(existingLocation->getRole(MapLocation::Roles::RoleCoordinate));
if (dsCoord.distanceTo(coord) < MIN_DISTANCE_BETWEEN_DIVE_SITES_M)
continue;
}
location = new MapLocation(ds->uuid, dsCoord, name);
locationList.append(location);
locationNameMap[name] = location;
}
m_mapLocationModel->addList(locationList);
}
void MapWidgetHelper::selectedLocationChanged(MapLocation *location)
{
int idx;
struct dive *dive;
m_selectedDiveIds.clear();
QGeoCoordinate locationCoord = location->coordinate();
for_each_dive (idx, dive) {
struct dive_site *ds = get_dive_site_for_dive(dive);
if (!dive_site_has_gps_location(ds))
continue;
const qreal latitude = ds->latitude.udeg * 0.000001;
const qreal longitude = ds->longitude.udeg * 0.000001;
QGeoCoordinate dsCoord(latitude, longitude);
if (locationCoord.distanceTo(dsCoord) < m_smallCircleRadius)
m_selectedDiveIds.append(idx);
}
emit selectedDivesChanged(m_selectedDiveIds);
}
/*
* Based on a 2D Map widget circle with center "coord" and radius SMALL_CIRCLE_RADIUS_PX,
* obtain a "small circle" with radius m_smallCircleRadius in meters:
* https://en.wikipedia.org/wiki/Circle_of_a_sphere
*
* The idea behind this circle is to be able to select multiple nearby dives, when clicking on
* the map. This code can be in QML, but it is in C++ instead for performance reasons.
*
* This can be made faster with an exponential regression [a * exp(b * x)], with a pretty
* decent R-squared, but it becomes bound to map provider zoom level mappings and the
* SMALL_CIRCLE_RADIUS_PX value, which makes the code hard to maintain.
*/
void MapWidgetHelper::calculateSmallCircleRadius(QGeoCoordinate coord)
{
QPointF point;
QMetaObject::invokeMethod(m_map, "fromCoordinate", Q_RETURN_ARG(QPointF, point),
Q_ARG(QGeoCoordinate, coord), Q_ARG(bool, false));
QPointF point2(point.x() + SMALL_CIRCLE_RADIUS_PX, point.y());
QGeoCoordinate coord2;
QMetaObject::invokeMethod(m_map, "toCoordinate", Q_RETURN_ARG(QGeoCoordinate, coord2),
Q_ARG(QPointF, point2), Q_ARG(bool, false));
m_smallCircleRadius = coord2.distanceTo(coord);
}
void MapWidgetHelper::copyToClipboardCoordinates(QGeoCoordinate coord, bool formatTraditional)
{
bool savep = prefs.coordinates_traditional;
prefs.coordinates_traditional = formatTraditional;
const int lat = llrint(1000000.0 * coord.latitude());
const int lon = llrint(1000000.0 * coord.longitude());
const char *coordinates = printGPSCoords(lat, lon);
QApplication::clipboard()->setText(QString(coordinates), QClipboard::Clipboard);
free((void *)coordinates);
prefs.coordinates_traditional = savep;
}
void MapWidgetHelper::updateCurrentDiveSiteCoordinates(quint32 uuid, QGeoCoordinate coord)
{
MapLocation *loc = m_mapLocationModel->getMapLocationForUuid(uuid);
if (loc)
loc->setCoordinate(coord);
displayed_dive_site.latitude.udeg = llrint(coord.latitude() * 1000000.0);
displayed_dive_site.longitude.udeg = llrint(coord.longitude() * 1000000.0);
emit coordinatesChanged();
}
bool MapWidgetHelper::editMode()
{
return m_editMode;
}
void MapWidgetHelper::setEditMode(bool editMode)
{
m_editMode = editMode;
MapLocation *exists = m_mapLocationModel->getMapLocationForUuid(displayed_dive_site.uuid);
// if divesite uuid doesn't exist in the model, add a new MapLocation.
if (editMode && !exists) {
QGeoCoordinate coord(0.0, 0.0);
m_mapLocationModel->add(new MapLocation(displayed_dive_site.uuid, coord,
QString(displayed_dive_site.name)));
QMetaObject::invokeMethod(m_map, "centerOnCoordinate",
Q_ARG(QVariant, QVariant::fromValue(coord)));
}
emit editModeChanged();
}
|