From 7e42b57d73bfba0e8f55bcb599decd4af76878ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Sun, 11 Feb 2018 20:39:39 +0100 Subject: [PATCH] Added coordinates format settings + Fixed units settings issues --- src/GUI/format.cpp | 43 ++++++++++++++++++++++++-- src/GUI/format.h | 8 ++++- src/GUI/gui.cpp | 67 ++++++++++++++++++++++++++++++---------- src/GUI/gui.h | 8 +++++ src/GUI/mapview.cpp | 37 +++++++++++++++++----- src/GUI/mapview.h | 7 +++-- src/GUI/routeitem.cpp | 35 +++++++++++++++++++-- src/GUI/routeitem.h | 4 +++ src/GUI/settings.h | 2 ++ src/GUI/waypointitem.cpp | 20 ++++++++---- src/GUI/waypointitem.h | 5 +-- 11 files changed, 196 insertions(+), 40 deletions(-) diff --git a/src/GUI/format.cpp b/src/GUI/format.cpp index fe2fc63e..1446885b 100644 --- a/src/GUI/format.cpp +++ b/src/GUI/format.cpp @@ -2,6 +2,32 @@ #include "common/coordinates.h" #include "format.h" + +static QString deg2DMS(double val) +{ + int deg = val; + double r1 = val - deg; + + int min = r1 * 60.0; + double r2 = r1 - (min / 60.0); + + double sec = r2 * 3600.0; + + return QString("%1°%2'%3\"").arg(deg).arg(min, 2, 10, QChar('0')) + .arg(sec, 4, 'f', 1, QChar('0')); +} + +static QString deg2DM(double val) +{ + int deg = val; + double r1 = val - deg; + + double min = r1 * 60.0; + + return QString("%1°%2'").arg(deg).arg(min, 6, 'f', 3, QChar('0')); +} + + QString Format::timeSpan(qreal time, bool full) { unsigned h, m, s; @@ -47,11 +73,22 @@ QString Format::elevation(qreal value, Units units) + qApp->translate("Format", "ft"); } -QString Format::coordinates(const Coordinates &value) +QString Format::coordinates(const Coordinates &value, CoordinatesFormat type) { QChar yH = (value.lat() < 0) ? 'S' : 'N'; QChar xH = (value.lon() < 0) ? 'W' : 'E'; - return QString::number(qAbs(value.lat()), 'f', 5) + yH + "," + QChar(0x00A0) - + QString::number(qAbs(value.lon()), 'f', 5) + xH; + switch (type) { + case DegreesMinutes: + return deg2DM(qAbs(value.lat())) + yH + "," + QChar(0x00A0) + + deg2DM(qAbs(value.lon())) + xH; + break; + case DMS: + return deg2DMS(qAbs(value.lat())) + yH + "," + QChar(0x00A0) + + deg2DMS(qAbs(value.lon())) + xH; + break; + default: + return QString::number(qAbs(value.lat()), 'f', 5) + yH + "," + + QChar(0x00A0) + QString::number(qAbs(value.lon()), 'f', 5) + xH; + } } diff --git a/src/GUI/format.h b/src/GUI/format.h index 6bff2d1c..ca891843 100644 --- a/src/GUI/format.h +++ b/src/GUI/format.h @@ -6,12 +6,18 @@ class Coordinates; +enum CoordinatesFormat { + DecimalDegrees, + DegreesMinutes, + DMS +}; + namespace Format { QString timeSpan(qreal time, bool full = true); QString distance(qreal value, Units units); QString elevation(qreal value, Units units); - QString coordinates(const Coordinates &value); + QString coordinates(const Coordinates &value, CoordinatesFormat type); } #endif // FORMAT_H diff --git a/src/GUI/gui.cpp b/src/GUI/gui.cpp index eecb840b..5dcb3d22 100644 --- a/src/GUI/gui.cpp +++ b/src/GUI/gui.cpp @@ -40,7 +40,6 @@ #include "filebrowser.h" #include "cpuarch.h" #include "graphtab.h" -#include "format.h" #include "gui.h" @@ -373,6 +372,23 @@ void GUI::createActions() _imperialUnitsAction->setActionGroup(ag); connect(_imperialUnitsAction, SIGNAL(triggered()), this, SLOT(setImperialUnits())); + ag = new QActionGroup(this); + ag->setExclusive(true); + _decimalDegreesAction = new QAction(tr("Decimal degrees (DD)"), this); + _decimalDegreesAction->setCheckable(true); + _decimalDegreesAction->setActionGroup(ag); + connect(_decimalDegreesAction, SIGNAL(triggered()), this, + SLOT(setDecimalDegrees())); + _degreesMinutesAction = new QAction(tr("Degrees and decimal minutes (DMM)"), + this); + _degreesMinutesAction->setCheckable(true); + _degreesMinutesAction->setActionGroup(ag); + connect(_degreesMinutesAction, SIGNAL(triggered()), this, + SLOT(setDegreesMinutes())); + _DMSAction = new QAction(tr("Degrees, minutes, seconds (DMS)"), this); + _DMSAction->setCheckable(true); + _DMSAction->setActionGroup(ag); + connect(_DMSAction, SIGNAL(triggered()), this, SLOT(setDMS())); _fullscreenAction = new QAction(QIcon(QPixmap(FULLSCREEN_ICON)), tr("Fullscreen mode"), this); _fullscreenAction->setCheckable(true); @@ -463,6 +479,10 @@ void GUI::createMenus() QMenu *unitsMenu = settingsMenu->addMenu(tr("Units")); unitsMenu->addAction(_metricUnitsAction); unitsMenu->addAction(_imperialUnitsAction); + QMenu *coordinatesMenu = settingsMenu->addMenu(tr("Coordinates format")); + coordinatesMenu->addAction(_decimalDegreesAction); + coordinatesMenu->addAction(_degreesMinutesAction); + coordinatesMenu->addAction(_DMSAction); settingsMenu->addSeparator(); settingsMenu->addAction(_showToolbarsAction); settingsMenu->addAction(_fullscreenAction); @@ -1293,6 +1313,11 @@ void GUI::setUnits(Units units) updateStatusBarInfo(); } +void GUI::setCoordinatesFormat(CoordinatesFormat format) +{ + _mapView->setCoordinatesFormat(format); +} + void GUI::setGraphType(GraphType type) { _sliderPos = 0; @@ -1437,11 +1462,15 @@ void GUI::writeSettings() if ((_movingTimeAction->isChecked() ? Moving : Total) != TIME_TYPE_DEFAULT) settings.setValue(TIME_TYPE_SETTING, _movingTimeAction->isChecked() - ? Moving : Total); + ? Moving : Total); if ((_imperialUnitsAction->isChecked() ? Imperial : Metric) != UNITS_DEFAULT) settings.setValue(UNITS_SETTING, _imperialUnitsAction->isChecked() - ? Imperial : Metric); + ? Imperial : Metric); + CoordinatesFormat ct = _DMSAction->isChecked() ? DMS + : _degreesMinutesAction->isChecked() ? DegreesMinutes : DecimalDegrees; + if (ct != COORDINATES_DEFAULT) + settings.setValue(COORDINATES_SETTING, ct); if (_showToolbarsAction->isChecked() != SHOW_TOOLBARS_DEFAULT) settings.setValue(SHOW_TOOLBARS_SETTING, _showToolbarsAction->isChecked()); @@ -1610,20 +1639,24 @@ void GUI::readSettings() settings.beginGroup(SETTINGS_SETTINGS_GROUP); if (settings.value(TIME_TYPE_SETTING, TIME_TYPE_DEFAULT).toInt() - == Moving) { - setTimeType(Moving); - _movingTimeAction->setChecked(true); - } else { - setTimeType(Total); - _totalTimeAction->setChecked(true); - } - if (settings.value(UNITS_SETTING, UNITS_DEFAULT).toInt() == Imperial) { - setUnits(Imperial); - _imperialUnitsAction->setChecked(true); - } else { - setUnits(Metric); - _metricUnitsAction->setChecked(true); - } + == Moving) + _movingTimeAction->activate(QAction::Trigger); + else + _totalTimeAction->activate(QAction::Trigger); + + if (settings.value(UNITS_SETTING, UNITS_DEFAULT).toInt() == Imperial) + _imperialUnitsAction->activate(QAction::Trigger); + else + _metricUnitsAction->activate(QAction::Trigger); + + if (settings.value(COORDINATES_SETTING, COORDINATES_DEFAULT).toInt() == DMS) + _DMSAction->activate(QAction::Trigger); + else if (settings.value(COORDINATES_SETTING, COORDINATES_DEFAULT).toInt() + == DegreesMinutes) + _degreesMinutesAction->activate(QAction::Trigger); + else + _decimalDegreesAction->activate(QAction::Trigger); + if (!settings.value(SHOW_TOOLBARS_SETTING, SHOW_TOOLBARS_DEFAULT).toBool()) showToolbars(false); else diff --git a/src/GUI/gui.h b/src/GUI/gui.h index c27168c7..9d8a09bc 100644 --- a/src/GUI/gui.h +++ b/src/GUI/gui.h @@ -10,6 +10,7 @@ #include "data/poi.h" #include "units.h" #include "timetype.h" +#include "format.h" #include "exportdialog.h" #include "optionsdialog.h" @@ -75,6 +76,9 @@ private slots: void setImperialUnits() {setUnits(Imperial);} void setDistanceGraph() {setGraphType(Distance);} void setTimeGraph() {setGraphType(Time);} + void setDecimalDegrees() {setCoordinatesFormat(DecimalDegrees);} + void setDegreesMinutes() {setCoordinatesFormat(DegreesMinutes);} + void setDMS() {setCoordinatesFormat(DMS);} void sliderPositionChanged(qreal pos); @@ -111,6 +115,7 @@ private: Units units() const; void setTimeType(TimeType type); void setUnits(Units units); + void setCoordinatesFormat(CoordinatesFormat format); void setGraphType(GraphType type); qreal distance() const; @@ -165,6 +170,9 @@ private: QAction *_firstAction; QAction *_metricUnitsAction; QAction *_imperialUnitsAction; + QAction *_decimalDegreesAction; + QAction *_degreesMinutesAction; + QAction *_DMSAction; QAction *_totalTimeAction; QAction *_movingTimeAction; QAction *_nextMapAction; diff --git a/src/GUI/mapview.cpp b/src/GUI/mapview.cpp index d3427f29..bbf9033c 100644 --- a/src/GUI/mapview.cpp +++ b/src/GUI/mapview.cpp @@ -49,6 +49,7 @@ MapView::MapView(Map *map, POI *poi, QWidget *parent) connect(_poi, SIGNAL(pointsChanged()), this, SLOT(updatePOI())); _units = Metric; + _coordinatesFormat = DecimalDegrees; _opacity = 1.0; _backgroundColor = Qt::white; _markerColor = Qt::red; @@ -137,6 +138,7 @@ PathItem *MapView::addRoute(const Route &route) ri->setWidth(_routeWidth); ri->setStyle(_routeStyle); ri->setUnits(_units); + ri->setCoordinatesFormat(_coordinatesFormat); ri->setVisible(_showRoutes); ri->showWaypoints(_showRouteWaypoints); ri->showWaypointLabels(_showWaypointLabels); @@ -162,7 +164,7 @@ void MapView::addWaypoints(const QList &waypoints) wi->setSize(_waypointSize); wi->setColor(_waypointColor); wi->showLabel(_showWaypointLabels); - wi->setUnits(_units); + wi->setToolTipFormat(_units, _coordinatesFormat); wi->setVisible(_showWaypoints); wi->setDigitalZoom(_digitalZoom); _scene->addItem(wi); @@ -348,28 +350,49 @@ void MapView::addPOI(const QList &waypoints) pi->showLabel(_showPOILabels); pi->setVisible(_showPOI); pi->setDigitalZoom(_digitalZoom); + pi->setToolTipFormat(_units, _coordinatesFormat); _scene->addItem(pi); _pois.insert(SearchPointer(&(pi->waypoint())), pi); } } -void MapView::setUnits(enum Units units) +void MapView::setUnits(Units units) { + if (_units == units) + return; + _units = units; - _mapScale->setUnits(units); + _mapScale->setUnits(_units); for (int i = 0; i < _tracks.count(); i++) - _tracks[i]->setUnits(units); + _tracks[i]->setUnits(_units); for (int i = 0; i < _routes.count(); i++) - _routes[i]->setUnits(units); + _routes[i]->setUnits(_units); for (int i = 0; i < _waypoints.size(); i++) - _waypoints.at(i)->setUnits(units); + _waypoints.at(i)->setToolTipFormat(_units, _coordinatesFormat); QHash, WaypointItem*>::const_iterator it; for (it = _pois.constBegin(); it != _pois.constEnd(); it++) - it.value()->setUnits(units); + it.value()->setToolTipFormat(_units, _coordinatesFormat); +} + +void MapView::setCoordinatesFormat(CoordinatesFormat format) +{ + if (_coordinatesFormat == format) + return; + + _coordinatesFormat = format; + + for (int i = 0; i < _waypoints.count(); i++) + _waypoints.at(i)->setToolTipFormat(_units, _coordinatesFormat); + for (int i = 0; i < _routes.count(); i++) + _routes[i]->setCoordinatesFormat(_coordinatesFormat); + + QHash, WaypointItem*>::const_iterator it; + for (it = _pois.constBegin(); it != _pois.constEnd(); it++) + it.value()->setToolTipFormat(_units, _coordinatesFormat); } void MapView::clearMapCache() diff --git a/src/GUI/mapview.h b/src/GUI/mapview.h index 6b594abc..30f286d8 100644 --- a/src/GUI/mapview.h +++ b/src/GUI/mapview.h @@ -9,6 +9,7 @@ #include "data/waypoint.h" #include "searchpointer.h" #include "units.h" +#include "format.h" #include "palette.h" class Data; @@ -34,12 +35,13 @@ public: void setPalette(const Palette &palette); void setPOI(POI *poi); void setMap(Map *map); - void setUnits(enum Units units); void plot(QPainter *painter, const QRectF &target, qreal scale, bool hires); void clear(); + void setUnits(Units units); + void setMarkerColor(const QColor &color); void setTrackWidth(int width); void setRouteWidth(int width); void setTrackStyle(Qt::PenStyle style); @@ -52,7 +54,6 @@ public: void setBackgroundColor(const QColor &color); void useOpenGL(bool use); void useAntiAliasing(bool use); - void setMarkerColor(const QColor &color); public slots: void showMap(bool show); @@ -65,6 +66,7 @@ public slots: void showWaypoints(bool show); void showRouteWaypoints(bool show); void clearMapCache(); + void setCoordinatesFormat(CoordinatesFormat format); private slots: void updatePOI(); @@ -108,6 +110,7 @@ private: POI *_poi; Palette _palette; Units _units; + CoordinatesFormat _coordinatesFormat; qreal _opacity; QColor _backgroundColor; diff --git a/src/GUI/routeitem.cpp b/src/GUI/routeitem.cpp index e8fb9dba..e7069613 100644 --- a/src/GUI/routeitem.cpp +++ b/src/GUI/routeitem.cpp @@ -30,6 +30,8 @@ RouteItem::RouteItem(const Route &route, Map *map, QGraphicsItem *parent) _name = route.name(); _desc = route.description(); + _units = Metric; + _coordinatesFormat = DecimalDegrees; setToolTip(toolTip(Metric)); } @@ -47,9 +49,38 @@ void RouteItem::setMap(Map *map) PathItem::setMap(map); } -void RouteItem::setUnits(enum Units units) +void RouteItem::setUnits(Units units) { - setToolTip(toolTip(units)); + if (_units == units) + return; + + _units = units; + + setToolTip(toolTip(_units)); + + QList childs = childItems(); + for (int i = 0; i < childs.count(); i++) { + if (childs.at(i) != _marker) { + WaypointItem *wi = static_cast(childs.at(i)); + wi->setToolTipFormat(_units, _coordinatesFormat); + } + } +} + +void RouteItem::setCoordinatesFormat(CoordinatesFormat format) +{ + if (_coordinatesFormat == format) + return; + + _coordinatesFormat = format; + + QList childs = childItems(); + for (int i = 0; i < childs.count(); i++) { + if (childs.at(i) != _marker) { + WaypointItem *wi = static_cast(childs.at(i)); + wi->setToolTipFormat(_units, _coordinatesFormat); + } + } } void RouteItem::showWaypoints(bool show) diff --git a/src/GUI/routeitem.h b/src/GUI/routeitem.h index cb1be783..481b4a5b 100644 --- a/src/GUI/routeitem.h +++ b/src/GUI/routeitem.h @@ -4,6 +4,7 @@ #include "data/route.h" #include "pathitem.h" #include "units.h" +#include "format.h" class Map; @@ -17,6 +18,7 @@ public: void setMap(Map *map); void setUnits(Units units); + void setCoordinatesFormat(CoordinatesFormat format); void showWaypoints(bool show); void showWaypointLabels(bool show); @@ -25,6 +27,8 @@ private: QString _name; QString _desc; + Units _units; + CoordinatesFormat _coordinatesFormat; }; #endif // ROUTEITEM_H diff --git a/src/GUI/settings.h b/src/GUI/settings.h index b0f427f6..a73bfaa3 100644 --- a/src/GUI/settings.h +++ b/src/GUI/settings.h @@ -15,6 +15,8 @@ #define TIME_TYPE_DEFAULT Total #define UNITS_SETTING "units" #define UNITS_DEFAULT (IMPERIAL_UNITS() ? Imperial : Metric) +#define COORDINATES_SETTING "coordinates" +#define COORDINATES_DEFAULT DecimalDegrees #define SHOW_TOOLBARS_SETTING "toolbar" #define SHOW_TOOLBARS_DEFAULT true diff --git a/src/GUI/waypointitem.cpp b/src/GUI/waypointitem.cpp index 3c923023..5dd44ad9 100644 --- a/src/GUI/waypointitem.cpp +++ b/src/GUI/waypointitem.cpp @@ -1,7 +1,6 @@ #include #include #include "config.h" -#include "format.h" #include "tooltip.h" #include "waypointitem.h" @@ -11,14 +10,14 @@ #define FS(size) \ ((int)((qreal)size * 1.41)) -QString WaypointItem::toolTip(Units units) +QString WaypointItem::toolTip(Units units, CoordinatesFormat ct) { ToolTip tt; if (!_waypoint.name().isEmpty()) tt.insert(qApp->translate("WaypointItem", "Name"), _waypoint.name()); tt.insert(qApp->translate("WaypointItem", "Coordinates"), - Format::coordinates(_waypoint.coordinates())); + Format::coordinates(_waypoint.coordinates(), ct)); if (!std::isnan(_waypoint.elevation())) tt.insert(qApp->translate("WaypointItem", "Elevation"), Format::elevation(_waypoint.elevation(), units)); @@ -44,7 +43,7 @@ WaypointItem::WaypointItem(const Waypoint &waypoint, Map *map, updateShape(); setPos(map->ll2xy(waypoint.coordinates())); - setToolTip(toolTip(Metric)); + setToolTip(toolTip(Metric, DecimalDegrees)); setCursor(Qt::ArrowCursor); setAcceptHoverEvents(true); } @@ -108,6 +107,9 @@ void WaypointItem::paint(QPainter *painter, void WaypointItem::setSize(int size) { + if (_size == size) + return; + prepareGeometryChange(); _size = size; updateShape(); @@ -115,17 +117,23 @@ void WaypointItem::setSize(int size) void WaypointItem::setColor(const QColor &color) { + if (_color == color) + return; + _color = color; update(); } -void WaypointItem::setUnits(enum Units units) +void WaypointItem::setToolTipFormat(Units units, CoordinatesFormat ct) { - setToolTip(toolTip(units)); + setToolTip(toolTip(units, ct)); } void WaypointItem::showLabel(bool show) { + if (_showLabel == show) + return; + prepareGeometryChange(); _showLabel = show; updateShape(); diff --git a/src/GUI/waypointitem.h b/src/GUI/waypointitem.h index 17abca32..62dfbf66 100644 --- a/src/GUI/waypointitem.h +++ b/src/GUI/waypointitem.h @@ -6,6 +6,7 @@ #include "data/waypoint.h" #include "map/map.h" #include "units.h" +#include "format.h" class WaypointItem : public QGraphicsItem { @@ -15,7 +16,7 @@ public: const Waypoint &waypoint() const {return _waypoint;} void setMap(Map *map) {setPos(map->ll2xy(_waypoint.coordinates()));} - void setUnits(Units units); + void setToolTipFormat(Units units, CoordinatesFormat ct); void setSize(int size); void setColor(const QColor &color); void showLabel(bool show); @@ -31,7 +32,7 @@ private: void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); void updateShape(); - QString toolTip(Units units); + QString toolTip(Units units, CoordinatesFormat ct); QPainterPath _shape; Waypoint _waypoint;