From c59cdd6efdbf2f946cd750d78bff79f431d68a50 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Fri, 28 Jul 2017 22:01:33 -0700 Subject: Switch new files to unix line endings I thought we had this automated, but Lubomirs commits introduced a few files with dos line endings. This is purely a change of line endings, no other changes. Signed-off-by: Dirk Hohndel --- desktop-widgets/mapwidget.cpp | 204 ++++++++++----------- desktop-widgets/mapwidget.h | 90 +++++----- mobile-widgets/qmlmapwidgethelper.cpp | 322 +++++++++++++++++----------------- mobile-widgets/qmlmapwidgethelper.h | 98 +++++------ qt-models/maplocationmodel.cpp | 276 ++++++++++++++--------------- 5 files changed, 495 insertions(+), 495 deletions(-) diff --git a/desktop-widgets/mapwidget.cpp b/desktop-widgets/mapwidget.cpp index 0b7624371..adda11c26 100644 --- a/desktop-widgets/mapwidget.cpp +++ b/desktop-widgets/mapwidget.cpp @@ -1,102 +1,102 @@ -// SPDX-License-Identifier: GPL-2.0 -#include -#include -#include -#include - -#include "mapwidget.h" -#include "core/dive.h" -#include "core/divesite.h" -#include "mobile-widgets/qmlmapwidgethelper.h" -#include "qt-models/maplocationmodel.h" -#include "mainwindow.h" -#include "divelistview.h" - -static bool skipReload = false; - -MapWidget *MapWidget::m_instance = NULL; - -MapWidget::MapWidget(QWidget *parent) : QQuickWidget(parent) -{ - qmlRegisterType("org.subsurfacedivelog.mobile", 1, 0, "MapWidgetHelper"); - qmlRegisterType("org.subsurfacedivelog.mobile", 1, 0, "MapLocationModel"); - qmlRegisterType("org.subsurfacedivelog.mobile", 1, 0, "MapLocation"); - - setSource(QUrl(QStringLiteral("qrc:/MapWidget.qml"))); - setResizeMode(QQuickWidget::SizeRootObjectToView); - - m_rootItem = qobject_cast(rootObject()); - m_mapHelper = rootObject()->findChild(); - connect(m_mapHelper, SIGNAL(selectedDivesChanged(QList)), - this, SLOT(selectedDivesChanged(QList))); - connect(m_mapHelper, SIGNAL(coordinatesChanged()), - this, SLOT(coordinatesChangedLocal())); -} - -void MapWidget::centerOnDiveSite(struct dive_site *ds) -{ - if (!skipReload) - m_mapHelper->centerOnDiveSite(ds); -} - -void MapWidget::centerOnIndex(const QModelIndex& idx) -{ - struct dive_site *ds = get_dive_site_by_uuid(idx.model()->index(idx.row(), 0).data().toInt()); - if (!ds || !dive_site_has_gps_location(ds)) - centerOnDiveSite(&displayed_dive_site); - else - centerOnDiveSite(ds); -} - -void MapWidget::repopulateLabels() -{ - m_mapHelper->reloadMapLocations(); -} - -void MapWidget::reload() -{ - setEditMode(false); - if (!skipReload) - m_mapHelper->reloadMapLocations(); -} - -void MapWidget::setEditMode(bool editMode) -{ - m_mapHelper->setEditMode(editMode); -} - -void MapWidget::endGetDiveCoordinates() -{ - setEditMode(false); -} - -void MapWidget::prepareForGetDiveCoordinates() -{ - setEditMode(true); -} - -void MapWidget::selectedDivesChanged(QList list) -{ - skipReload = true; - MainWindow::instance()->dive_list()->unselectDives(); - if (!list.empty()) - MainWindow::instance()->dive_list()->selectDives(list); - skipReload = false; -} - -void MapWidget::coordinatesChangedLocal() -{ - emit coordinatesChanged(); -} - -MapWidget::~MapWidget() -{ - m_instance = NULL; -} - -MapWidget *MapWidget::instance() -{ - if (m_instance == NULL) - m_instance = new MapWidget(); - return m_instance; -} +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include + +#include "mapwidget.h" +#include "core/dive.h" +#include "core/divesite.h" +#include "mobile-widgets/qmlmapwidgethelper.h" +#include "qt-models/maplocationmodel.h" +#include "mainwindow.h" +#include "divelistview.h" + +static bool skipReload = false; + +MapWidget *MapWidget::m_instance = NULL; + +MapWidget::MapWidget(QWidget *parent) : QQuickWidget(parent) +{ + qmlRegisterType("org.subsurfacedivelog.mobile", 1, 0, "MapWidgetHelper"); + qmlRegisterType("org.subsurfacedivelog.mobile", 1, 0, "MapLocationModel"); + qmlRegisterType("org.subsurfacedivelog.mobile", 1, 0, "MapLocation"); + + setSource(QUrl(QStringLiteral("qrc:/MapWidget.qml"))); + setResizeMode(QQuickWidget::SizeRootObjectToView); + + m_rootItem = qobject_cast(rootObject()); + m_mapHelper = rootObject()->findChild(); + connect(m_mapHelper, SIGNAL(selectedDivesChanged(QList)), + this, SLOT(selectedDivesChanged(QList))); + connect(m_mapHelper, SIGNAL(coordinatesChanged()), + this, SLOT(coordinatesChangedLocal())); +} + +void MapWidget::centerOnDiveSite(struct dive_site *ds) +{ + if (!skipReload) + m_mapHelper->centerOnDiveSite(ds); +} + +void MapWidget::centerOnIndex(const QModelIndex& idx) +{ + struct dive_site *ds = get_dive_site_by_uuid(idx.model()->index(idx.row(), 0).data().toInt()); + if (!ds || !dive_site_has_gps_location(ds)) + centerOnDiveSite(&displayed_dive_site); + else + centerOnDiveSite(ds); +} + +void MapWidget::repopulateLabels() +{ + m_mapHelper->reloadMapLocations(); +} + +void MapWidget::reload() +{ + setEditMode(false); + if (!skipReload) + m_mapHelper->reloadMapLocations(); +} + +void MapWidget::setEditMode(bool editMode) +{ + m_mapHelper->setEditMode(editMode); +} + +void MapWidget::endGetDiveCoordinates() +{ + setEditMode(false); +} + +void MapWidget::prepareForGetDiveCoordinates() +{ + setEditMode(true); +} + +void MapWidget::selectedDivesChanged(QList list) +{ + skipReload = true; + MainWindow::instance()->dive_list()->unselectDives(); + if (!list.empty()) + MainWindow::instance()->dive_list()->selectDives(list); + skipReload = false; +} + +void MapWidget::coordinatesChangedLocal() +{ + emit coordinatesChanged(); +} + +MapWidget::~MapWidget() +{ + m_instance = NULL; +} + +MapWidget *MapWidget::instance() +{ + if (m_instance == NULL) + m_instance = new MapWidget(); + return m_instance; +} diff --git a/desktop-widgets/mapwidget.h b/desktop-widgets/mapwidget.h index 2d33ee85b..d104da1a3 100644 --- a/desktop-widgets/mapwidget.h +++ b/desktop-widgets/mapwidget.h @@ -1,45 +1,45 @@ -// SPDX-License-Identifier: GPL-2.0 -#ifndef MAPWIDGET_H -#define MAPWIDGET_H - -#include -#include - -#undef IGNORE - -class QResizeEvent; -class QQuickItem; -class MapWidgetHelper; -struct dive_site; - -class MapWidget : public QQuickWidget { - - Q_OBJECT - -public: - MapWidget(QWidget *parent = NULL); - ~MapWidget(); - - static MapWidget *instance(); - void reload(); - -signals: - void coordinatesChanged(); - -public slots: - void centerOnDiveSite(struct dive_site *); - void centerOnIndex(const QModelIndex& idx); - void endGetDiveCoordinates(); - void repopulateLabels(); - void prepareForGetDiveCoordinates(); - void selectedDivesChanged(QList); - void coordinatesChangedLocal(); - -private: - void setEditMode(bool editMode); - static MapWidget *m_instance; - QQuickItem *m_rootItem; - MapWidgetHelper *m_mapHelper; -}; - -#endif // MAPWIDGET_H +// SPDX-License-Identifier: GPL-2.0 +#ifndef MAPWIDGET_H +#define MAPWIDGET_H + +#include +#include + +#undef IGNORE + +class QResizeEvent; +class QQuickItem; +class MapWidgetHelper; +struct dive_site; + +class MapWidget : public QQuickWidget { + + Q_OBJECT + +public: + MapWidget(QWidget *parent = NULL); + ~MapWidget(); + + static MapWidget *instance(); + void reload(); + +signals: + void coordinatesChanged(); + +public slots: + void centerOnDiveSite(struct dive_site *); + void centerOnIndex(const QModelIndex& idx); + void endGetDiveCoordinates(); + void repopulateLabels(); + void prepareForGetDiveCoordinates(); + void selectedDivesChanged(QList); + void coordinatesChangedLocal(); + +private: + void setEditMode(bool editMode); + static MapWidget *m_instance; + QQuickItem *m_rootItem; + MapWidgetHelper *m_mapHelper; +}; + +#endif // MAPWIDGET_H diff --git a/mobile-widgets/qmlmapwidgethelper.cpp b/mobile-widgets/qmlmapwidgethelper.cpp index 22e8b28c6..c439fb131 100644 --- a/mobile-widgets/qmlmapwidgethelper.cpp +++ b/mobile-widgets/qmlmapwidgethelper.cpp @@ -1,161 +1,161 @@ -// SPDX-License-Identifier: GPL-2.0 -#include -#include -#include -#include - -#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 locationNameMap; - m_mapLocationModel->clear(); - MapLocation *location; - QVector 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(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(); -} +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include + +#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 locationNameMap; + m_mapLocationModel->clear(); + MapLocation *location; + QVector 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(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(); +} diff --git a/mobile-widgets/qmlmapwidgethelper.h b/mobile-widgets/qmlmapwidgethelper.h index 3c6f6a1c8..6cba3dc91 100644 --- a/mobile-widgets/qmlmapwidgethelper.h +++ b/mobile-widgets/qmlmapwidgethelper.h @@ -1,49 +1,49 @@ -// SPDX-License-Identifier: GPL-2.0 -#ifndef QMLMAPWIDGETHELPER_H -#define QMLMAPWIDGETHELPER_H - -#include - -class QGeoCoordinate; -class MapLocationModel; -class MapLocation; -struct dive_site; - -class MapWidgetHelper : public QObject { - - Q_OBJECT - Q_PROPERTY(QObject *map MEMBER m_map) - Q_PROPERTY(MapLocationModel *model MEMBER m_mapLocationModel NOTIFY modelChanged) - Q_PROPERTY(bool editMode READ editMode WRITE setEditMode NOTIFY editModeChanged) - -public: - explicit MapWidgetHelper(QObject *parent = NULL); - - void centerOnDiveSite(struct dive_site *); - void reloadMapLocations(); - Q_INVOKABLE void copyToClipboardCoordinates(QGeoCoordinate coord, bool formatTraditional); - Q_INVOKABLE void calculateSmallCircleRadius(QGeoCoordinate coord); - Q_INVOKABLE void updateCurrentDiveSiteCoordinates(quint32 uuid, QGeoCoordinate coord); - bool editMode(); - void setEditMode(bool editMode); - -private: - QObject *m_map; - MapLocationModel *m_mapLocationModel; - qreal m_smallCircleRadius; - QList m_selectedDiveIds; - bool m_editMode; - -private slots: - void selectedLocationChanged(MapLocation *); - -signals: - void modelChanged(); - void editModeChanged(); - void selectedDivesChanged(QList list); - void coordinatesChanged(); -}; - -extern "C" const char *printGPSCoords(int lat, int lon); - -#endif +// SPDX-License-Identifier: GPL-2.0 +#ifndef QMLMAPWIDGETHELPER_H +#define QMLMAPWIDGETHELPER_H + +#include + +class QGeoCoordinate; +class MapLocationModel; +class MapLocation; +struct dive_site; + +class MapWidgetHelper : public QObject { + + Q_OBJECT + Q_PROPERTY(QObject *map MEMBER m_map) + Q_PROPERTY(MapLocationModel *model MEMBER m_mapLocationModel NOTIFY modelChanged) + Q_PROPERTY(bool editMode READ editMode WRITE setEditMode NOTIFY editModeChanged) + +public: + explicit MapWidgetHelper(QObject *parent = NULL); + + void centerOnDiveSite(struct dive_site *); + void reloadMapLocations(); + Q_INVOKABLE void copyToClipboardCoordinates(QGeoCoordinate coord, bool formatTraditional); + Q_INVOKABLE void calculateSmallCircleRadius(QGeoCoordinate coord); + Q_INVOKABLE void updateCurrentDiveSiteCoordinates(quint32 uuid, QGeoCoordinate coord); + bool editMode(); + void setEditMode(bool editMode); + +private: + QObject *m_map; + MapLocationModel *m_mapLocationModel; + qreal m_smallCircleRadius; + QList m_selectedDiveIds; + bool m_editMode; + +private slots: + void selectedLocationChanged(MapLocation *); + +signals: + void modelChanged(); + void editModeChanged(); + void selectedDivesChanged(QList list); + void coordinatesChanged(); +}; + +extern "C" const char *printGPSCoords(int lat, int lon); + +#endif diff --git a/qt-models/maplocationmodel.cpp b/qt-models/maplocationmodel.cpp index bc53d6c0b..9666ca009 100644 --- a/qt-models/maplocationmodel.cpp +++ b/qt-models/maplocationmodel.cpp @@ -1,138 +1,138 @@ -// SPDX-License-Identifier: GPL-2.0 -#include "maplocationmodel.h" - -const char *MapLocation::PROPERTY_NAME_COORDINATE = "coordinate"; -const char *MapLocation::PROPERTY_NAME_UUID = "uuid"; -const char *MapLocation::PROPERTY_NAME_NAME = "name"; - -MapLocation::MapLocation() -{ -} - -MapLocation::MapLocation(quint32 uuid, QGeoCoordinate coord, QString name) : - m_uuid(uuid), m_coordinate(coord), m_name(name) -{ -} - -QVariant MapLocation::getRole(int role) const -{ - switch (role) { - case Roles::RoleUuid: - return QVariant::fromValue(m_uuid); - case Roles::RoleCoordinate: - return QVariant::fromValue(m_coordinate); - case Roles::RoleName: - return QVariant::fromValue(m_name); - default: - return QVariant(); - } -} - -QGeoCoordinate MapLocation::coordinate() -{ - return m_coordinate; -} - -void MapLocation::setCoordinate(QGeoCoordinate coord) -{ - m_coordinate = coord; - emit coordinateChanged(); -} - -quint32 MapLocation::uuid() -{ - return m_uuid; -} - -MapLocationModel::MapLocationModel(QObject *parent) : QAbstractListModel(parent) -{ - m_roles[MapLocation::Roles::RoleUuid] = MapLocation::PROPERTY_NAME_UUID; - m_roles[MapLocation::Roles::RoleCoordinate] = MapLocation::PROPERTY_NAME_COORDINATE; - m_roles[MapLocation::Roles::RoleName] = MapLocation::PROPERTY_NAME_NAME; -} - -MapLocationModel::~MapLocationModel() -{ - clear(); -} - -QVariant MapLocationModel::data(const QModelIndex & index, int role) const -{ - if (index.row() < 0 || index.row() >= m_mapLocations.size()) - return QVariant(); - - return m_mapLocations.at(index.row())->getRole(role); -} - -QHash MapLocationModel::roleNames() const -{ - return m_roles; -} - -int MapLocationModel::rowCount(const QModelIndex &parent) const -{ - Q_UNUSED(parent); - return m_mapLocations.size(); -} - -int MapLocationModel::count() -{ - return m_mapLocations.size(); -} - -MapLocation *MapLocationModel::get(int row) -{ - if (row < 0 || row >= m_mapLocations.size()) - return NULL; - return m_mapLocations.at(row); -} - -void MapLocationModel::add(MapLocation *location) -{ - beginInsertRows(QModelIndex(), m_mapLocations.size(), m_mapLocations.size()); - m_mapLocations.append(location); - endInsertRows(); -} - -void MapLocationModel::addList(QVector list) -{ - if (!list.size()) - return; - beginInsertRows(QModelIndex(), m_mapLocations.size(), m_mapLocations.size() + list.size() - 1); - m_mapLocations.append(list); - endInsertRows(); -} - -void MapLocationModel::clear() -{ - if (!m_mapLocations.size()) - return; - beginRemoveRows(QModelIndex(), 0, m_mapLocations.size() - 1); - qDeleteAll(m_mapLocations); - m_mapLocations.clear(); - endRemoveRows(); -} - -void MapLocationModel::setSelectedUuid(QVariant uuid, QVariant fromClick) -{ - m_selectedUuid = qvariant_cast(uuid); - const bool fromClickBool = qvariant_cast(fromClick); - emit selectedUuidChanged(); - if (fromClickBool) - emit selectedLocationChanged(getMapLocationForUuid(m_selectedUuid)); -} - -quint32 MapLocationModel::selectedUuid() -{ - return m_selectedUuid; -} - -MapLocation *MapLocationModel::getMapLocationForUuid(quint32 uuid) -{ - MapLocation *location; - foreach(location, m_mapLocations) { - if (uuid == location->uuid()) - return location; - } - return NULL; -} +// SPDX-License-Identifier: GPL-2.0 +#include "maplocationmodel.h" + +const char *MapLocation::PROPERTY_NAME_COORDINATE = "coordinate"; +const char *MapLocation::PROPERTY_NAME_UUID = "uuid"; +const char *MapLocation::PROPERTY_NAME_NAME = "name"; + +MapLocation::MapLocation() +{ +} + +MapLocation::MapLocation(quint32 uuid, QGeoCoordinate coord, QString name) : + m_uuid(uuid), m_coordinate(coord), m_name(name) +{ +} + +QVariant MapLocation::getRole(int role) const +{ + switch (role) { + case Roles::RoleUuid: + return QVariant::fromValue(m_uuid); + case Roles::RoleCoordinate: + return QVariant::fromValue(m_coordinate); + case Roles::RoleName: + return QVariant::fromValue(m_name); + default: + return QVariant(); + } +} + +QGeoCoordinate MapLocation::coordinate() +{ + return m_coordinate; +} + +void MapLocation::setCoordinate(QGeoCoordinate coord) +{ + m_coordinate = coord; + emit coordinateChanged(); +} + +quint32 MapLocation::uuid() +{ + return m_uuid; +} + +MapLocationModel::MapLocationModel(QObject *parent) : QAbstractListModel(parent) +{ + m_roles[MapLocation::Roles::RoleUuid] = MapLocation::PROPERTY_NAME_UUID; + m_roles[MapLocation::Roles::RoleCoordinate] = MapLocation::PROPERTY_NAME_COORDINATE; + m_roles[MapLocation::Roles::RoleName] = MapLocation::PROPERTY_NAME_NAME; +} + +MapLocationModel::~MapLocationModel() +{ + clear(); +} + +QVariant MapLocationModel::data(const QModelIndex & index, int role) const +{ + if (index.row() < 0 || index.row() >= m_mapLocations.size()) + return QVariant(); + + return m_mapLocations.at(index.row())->getRole(role); +} + +QHash MapLocationModel::roleNames() const +{ + return m_roles; +} + +int MapLocationModel::rowCount(const QModelIndex &parent) const +{ + Q_UNUSED(parent); + return m_mapLocations.size(); +} + +int MapLocationModel::count() +{ + return m_mapLocations.size(); +} + +MapLocation *MapLocationModel::get(int row) +{ + if (row < 0 || row >= m_mapLocations.size()) + return NULL; + return m_mapLocations.at(row); +} + +void MapLocationModel::add(MapLocation *location) +{ + beginInsertRows(QModelIndex(), m_mapLocations.size(), m_mapLocations.size()); + m_mapLocations.append(location); + endInsertRows(); +} + +void MapLocationModel::addList(QVector list) +{ + if (!list.size()) + return; + beginInsertRows(QModelIndex(), m_mapLocations.size(), m_mapLocations.size() + list.size() - 1); + m_mapLocations.append(list); + endInsertRows(); +} + +void MapLocationModel::clear() +{ + if (!m_mapLocations.size()) + return; + beginRemoveRows(QModelIndex(), 0, m_mapLocations.size() - 1); + qDeleteAll(m_mapLocations); + m_mapLocations.clear(); + endRemoveRows(); +} + +void MapLocationModel::setSelectedUuid(QVariant uuid, QVariant fromClick) +{ + m_selectedUuid = qvariant_cast(uuid); + const bool fromClickBool = qvariant_cast(fromClick); + emit selectedUuidChanged(); + if (fromClickBool) + emit selectedLocationChanged(getMapLocationForUuid(m_selectedUuid)); +} + +quint32 MapLocationModel::selectedUuid() +{ + return m_selectedUuid; +} + +MapLocation *MapLocationModel::getMapLocationForUuid(quint32 uuid) +{ + MapLocation *location; + foreach(location, m_mapLocations) { + if (uuid == location->uuid()) + return location; + } + return NULL; +} -- cgit v1.2.3-70-g09d2