From d421660f91d56ac434860a1deff17eeeacbc7e26 Mon Sep 17 00:00:00 2001 From: "Lubomir I. Ivanov" Date: Mon, 7 Aug 2017 02:04:23 +0300 Subject: mapwidgethelper: support tracking of all selected dive sites MapWidgetHelper::centerOnDiveSite() now checks if more than one dive sites are selected and finds the most top-left and bottom-right ones in the coordinate system to form a rectangle. It also supports the special cases where a selected dive site does not have coordinates or the case where only a single dive site with GPS coordinates are selected. TODO: implement mapwidget.qml::centerOnRectangle() This QML function will receive a QGeoCoordinate based rectangle which has to be centered in the viewport with animation. Signed-off-by: Lubomir I. Ivanov --- mobile-widgets/qmlmapwidgethelper.cpp | 66 +++++++++++++++++++++++++++++++---- 1 file changed, 59 insertions(+), 7 deletions(-) (limited to 'mobile-widgets/qmlmapwidgethelper.cpp') diff --git a/mobile-widgets/qmlmapwidgethelper.cpp b/mobile-widgets/qmlmapwidgethelper.cpp index c439fb131..954a83ebf 100644 --- a/mobile-widgets/qmlmapwidgethelper.cpp +++ b/mobile-widgets/qmlmapwidgethelper.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "qmlmapwidgethelper.h" #include "core/dive.h" @@ -21,16 +22,67 @@ MapWidgetHelper::MapWidgetHelper(QObject *parent) : QObject(parent) void MapWidgetHelper::centerOnDiveSite(struct dive_site *ds) { - if (!dive_site_has_gps_location(ds)) { + int idx; + struct dive *dive; + QVector selDS; + QVector selGC; + QGeoCoordinate dsCoord; + + for_each_dive (idx, dive) { + struct dive_site *dss = get_dive_site_for_dive(dive); + if (!dive_site_has_gps_location(dss) || !dive->selected) + continue; + // only store dive sites with GPS + selDS.append(dss); + selGC.append(QGeoCoordinate(dss->latitude.udeg * 0.000001, + dss->longitude.udeg * 0.000001)); + } + if (!dive_site_has_gps_location(ds) && !selDS.size()) { + // only a single dive site with no GPS selected m_mapLocationModel->setSelectedUuid(ds ? ds->uuid : 0, false); QMetaObject::invokeMethod(m_map, "deselectMapLocation"); - return; + + } else if (selDS.size() == 1) { + // a single dive site with GPS selected + ds = selDS.at(0); + m_mapLocationModel->setSelectedUuid(ds->uuid, false); + dsCoord.setLatitude(ds->latitude.udeg * 0.000001); + dsCoord.setLongitude(ds->longitude.udeg * 0.000001); + QMetaObject::invokeMethod(m_map, "centerOnCoordinate", Q_ARG(QVariant, QVariant::fromValue(dsCoord))); + } else if (selDS.size() > 1) { + /* more than one dive sites with GPS selected. + * find the most top-left and bottom-right dive sites on the map coordinate system. */ + ds = selDS.at(0); + m_mapLocationModel->setSelectedUuid(ds->uuid, false); + qreal minLat = 0.0, minLon = 0.0, maxLat = 0.0, maxLon = 0.0; + bool start = true; + foreach(QGeoCoordinate gc, selGC) { + qreal lat = gc.latitude(); + qreal lon = gc.longitude(); + 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))); } - 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() -- cgit v1.2.3-70-g09d2