// SPDX-License-Identifier: GPL-2.0 #include #include #include #include #include "qmlmapwidgethelper.h" #include "core/divesite.h" #include "core/qthelper.h" #include "qt-models/maplocationmodel.h" #ifndef SUBSURFACE_MOBILE #include "qt-models/filtermodels.h" #endif #define SMALL_CIRCLE_RADIUS_PX 26.0 MapWidgetHelper::MapWidgetHelper(QObject *parent) : QObject(parent) { m_mapLocationModel = new MapLocationModel(this); m_smallCircleRadius = SMALL_CIRCLE_RADIUS_PX; m_map = nullptr; m_editMode = false; connect(m_mapLocationModel, SIGNAL(selectedLocationChanged(MapLocation *)), this, SLOT(selectedLocationChanged(MapLocation *))); } QGeoCoordinate MapWidgetHelper::getCoordinates(struct dive_site *ds) { if (!dive_site_has_gps_location(ds)) return QGeoCoordinate(0.0, 0.0); return QGeoCoordinate(ds->location.lat.udeg * 0.000001, ds->location.lon.udeg * 0.000001); } void MapWidgetHelper::centerOnDiveSite(struct dive_site *ds) { if (!dive_site_has_gps_location(ds)) { // dive site with no GPS m_mapLocationModel->setSelected(ds, false); QMetaObject::invokeMethod(m_map, "deselectMapLocation"); } else { // dive site with GPS m_mapLocationModel->setSelected(ds, false); QGeoCoordinate dsCoord (ds->location.lat.udeg * 0.000001, ds->location.lon.udeg * 0.000001); QMetaObject::invokeMethod(m_map, "centerOnCoordinate", Q_ARG(QVariant, QVariant::fromValue(dsCoord))); } } void MapWidgetHelper::centerOnSelectedDiveSite() { QVector selDS = m_mapLocationModel->selectedDs(); QVector selGC; if (selDS.isEmpty()) { // no selected dives with GPS coordinates QMetaObject::invokeMethod(m_map, "deselectMapLocation"); } else if (selDS.size() == 1) { QGeoCoordinate dsCoord (selDS[0]->location.lat.udeg * 0.000001, selDS[0]->location.lon.udeg * 0.000001); QMetaObject::invokeMethod(m_map, "centerOnCoordinate", Q_ARG(QVariant, QVariant::fromValue(dsCoord))); } else { /* more than one dive sites with GPS selected. * find the most top-left and bottom-right dive sites on the map coordinate system. */ qreal minLat = 0.0, minLon = 0.0, maxLat = 0.0, maxLon = 0.0; bool start = true; for(struct dive_site *dss: selDS) { qreal lat = dss->location.lat.udeg * 0.000001; qreal lon = dss->location.lon.udeg * 0.000001; if (start) { minLat = maxLat = lat; minLon = maxLon = lon; start = false; continue; } if (lat < minLat) minLat = lat; else if (lat > maxLat) maxLat = lat; if (lon < minLon) minLon = lon; else if (lon > maxLon) maxLon = lon; } // pass rectangle coordinates to QML QGeoCoordinate coordTopLeft(minLat, minLon); QGeoCoordinate coordBottomRight(maxLat, maxLon); QGeoCoordinate coordCenter(minLat + (maxLat - minLat) * 0.5, minLon + (maxLon - minLon) * 0.5); QMetaObject::invokeMethod(m_map, "centerOnRectangle", Q_ARG(QVariant, QVariant::fromValue(coordTopLeft)), Q_ARG(QVariant, QVariant::fromValue(coordBottomRight)), Q_ARG(QVariant, QVariant::fromValue(coordCenter))); } } void MapWidgetHelper::reloadMapLocations() { #ifndef SUBSURFACE_MOBILE // The filter being set to dive site is the signal that we are in dive site edit mode. // This is the case when either the dive site edit tab or the dive site list tab are active. if (MultiFilterSortModel::instance()->diveSiteMode()) enterEditMode(); else exitEditMode(); #endif m_mapLocationModel->reload(); } void MapWidgetHelper::selectedLocationChanged(MapLocation *location) { int idx; struct dive *dive; QList selectedDiveIds; 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; #ifndef SUBSURFACE_MOBILE const qreal latitude = ds->location.lat.udeg * 0.000001; const qreal longitude = ds->location.lon.udeg * 0.000001; QGeoCoordinate dsCoord(latitude, longitude); if (locationCoord.distanceTo(dsCoord) < m_smallCircleRadius) selectedDiveIds.append(idx); } #else // the mobile version doesn't support multi-dive selection if (ds == location->divesite()) selectedDiveIds.append(dive->id); // use id here instead of index } int last; // get latest dive chronologically if (!selectedDiveIds.isEmpty()) { last = selectedDiveIds.last(); selectedDiveIds.clear(); selectedDiveIds.append(last); } #endif emit selectedDivesChanged(selectedDiveIds); } void MapWidgetHelper::selectVisibleLocations() { int idx; struct dive *dive; QList selectedDiveIds; 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->location.lat.udeg * 0.000001; const qreal longitude = ds->location.lon.udeg * 0.000001; QGeoCoordinate dsCoord(latitude, longitude); QPointF point; QMetaObject::invokeMethod(m_map, "fromCoordinate", Q_RETURN_ARG(QPointF, point), Q_ARG(QGeoCoordinate, dsCoord)); if (!qIsNaN(point.x())) #ifndef SUBSURFACE_MOBILE // indexes on desktop selectedDiveIds.append(idx); } #else // use id on mobile instead of index selectedDiveIds.append(dive->id); } int last; // get latest dive chronologically if (!selectedDiveIds.isEmpty()) { last = selectedDiveIds.last(); selectedDiveIds.clear(); selectedDiveIds.append(last); } #endif emit selectedDivesChanged(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)); 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)); m_smallCircleRadius = coord2.distanceTo(coord); } static location_t mk_location(QGeoCoordinate coord) { return create_location(coord.latitude(), coord.longitude()); } void MapWidgetHelper::copyToClipboardCoordinates(QGeoCoordinate coord, bool formatTraditional) { bool savep = prefs.coordinates_traditional; prefs.coordinates_traditional = formatTraditional; location_t location = mk_location(coord); QApplication::clipboard()->setText(printGPSCoords(&location), QClipboard::Clipboard); prefs.coordinates_traditional = savep; } void MapWidgetHelper::updateCurrentDiveSiteCoordinatesFromMap(struct dive_site *ds, QGeoCoordinate coord) { MapLocation *loc = m_mapLocationModel->getMapLocation(ds); if (loc) loc->setCoordinate(coord); location_t location = mk_location(coord); emit coordinatesChanged(ds, location); } void MapWidgetHelper::updateDiveSiteCoordinates(struct dive_site *ds, const location_t &location) { if (!ds) return; const qreal latitude_r = location.lat.udeg * 0.000001; const qreal longitude_r = location.lon.udeg * 0.000001; QGeoCoordinate coord(latitude_r, longitude_r); m_mapLocationModel->updateMapLocationCoordinates(ds, coord); QMetaObject::invokeMethod(m_map, "centerOnCoordinate", Q_ARG(QVariant, QVariant::fromValue(coord))); } void MapWidgetHelper::exitEditMode() { if (!m_editMode) return; m_editMode = false; emit editModeChanged(); } void MapWidgetHelper::enterEditMode() { if (m_editMode) return; m_editMode = true; // if divesite of the first selected dive doesn't exist in the model, add a new MapLocation. const QVector selDs = m_mapLocationModel->selectedDs(); if (!selDs.isEmpty() && ! m_mapLocationModel->getMapLocation(selDs[0])) { // If the dive site doesn't have a GPS location, use the centre of the map QGeoCoordinate coord = has_location(&selDs[0]->location) ? getCoordinates(selDs[0]) : m_map->property("center").value(); m_mapLocationModel->add(new MapLocation(selDs[0], coord, QString(selDs[0]->name))); } emit editModeChanged(); } QString MapWidgetHelper::pluginObject() { QString str; str += "import QtQuick 2.0;"; str += "import QtLocation 5.3;"; str += "Plugin {"; str += " id: mapPlugin;"; str += " name: 'googlemaps';"; str += " PluginParameter { name: 'googlemaps.maps.language'; value: '%lang%' }"; str += " PluginParameter { name: 'googlemaps.cachefolder'; value: '%cacheFolder%' }"; str += " Component.onCompleted: {"; str += " if (availableServiceProviders.indexOf(name) === -1) {"; str += " console.warn('MapWidget.qml: cannot find a plugin named: ' + name);"; str += " }"; str += " }"; str += "}"; QString lang = uiLanguage(NULL).replace('_', '-'); str.replace("%lang%", lang); QString cacheFolder = QString(system_default_directory()).append("/googlemaps"); str.replace("%cacheFolder%", cacheFolder.replace("\\", "/")); return str; }