From a369a1689d61d517db7926da7350bddcb216600f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Sat, 6 Feb 2021 15:23:02 +0100 Subject: [PATCH] Added position date display option Closes #330 --- gpxsee.pro | 2 ++ src/GUI/gui.cpp | 15 ++++++++ src/GUI/gui.h | 1 + src/GUI/mapview.cpp | 13 +++++++ src/GUI/mapview.h | 3 +- src/GUI/markerinfoitem.cpp | 73 ++++++++++++++++++++++++++++++++++++++ src/GUI/markerinfoitem.h | 27 ++++++++++++++ src/GUI/pathitem.cpp | 21 +++++++++++ src/GUI/pathitem.h | 6 ++++ src/GUI/routeitem.h | 1 + src/GUI/settings.h | 2 ++ src/GUI/trackitem.h | 1 + 12 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 src/GUI/markerinfoitem.cpp create mode 100644 src/GUI/markerinfoitem.h diff --git a/gpxsee.pro b/gpxsee.pro index 8e606d5b..0859f1a2 100644 --- a/gpxsee.pro +++ b/gpxsee.pro @@ -22,6 +22,7 @@ HEADERS += src/common/config.h \ src/GUI/mapaction.h \ src/GUI/mapitem.h \ src/GUI/marginswidget.h \ + src/GUI/markerinfoitem.h \ src/GUI/planeitem.h \ src/GUI/popup.h \ src/common/garmin.h \ @@ -214,6 +215,7 @@ SOURCES += src/main.cpp \ src/GUI/axislabelitem.cpp \ src/GUI/mapitem.cpp \ src/GUI/marginswidget.cpp \ + src/GUI/markerinfoitem.cpp \ src/GUI/popup.cpp \ src/common/coordinates.cpp \ src/common/rectc.cpp \ diff --git a/src/GUI/gui.cpp b/src/GUI/gui.cpp index 419780ff..6ad95ed7 100644 --- a/src/GUI/gui.cpp +++ b/src/GUI/gui.cpp @@ -379,6 +379,11 @@ void GUI::createActions() _showMarkersAction->setCheckable(true); connect(_showMarkersAction, SIGNAL(triggered(bool)), _mapView, SLOT(showMarkers(bool))); + _showMarkerInfoAction = new QAction(tr("Position date/time"), this); + _showMarkerInfoAction->setMenuRole(QAction::NoRole); + _showMarkerInfoAction->setCheckable(true); + connect(_showMarkerInfoAction, SIGNAL(triggered(bool)), _mapView, + SLOT(showMarkerInfo(bool))); // Graph actions _showGraphsAction = new QAction(QIcon(SHOW_GRAPHS_ICON), tr("Show graphs"), @@ -564,6 +569,7 @@ void GUI::createMenus() displayMenu->addAction(_showRouteWaypointsAction); displayMenu->addAction(_showTicksAction); displayMenu->addAction(_showMarkersAction); + displayMenu->addAction(_showMarkerInfoAction); dataMenu->addSeparator(); dataMenu->addAction(_showTracksAction); dataMenu->addAction(_showRoutesAction); @@ -1018,6 +1024,7 @@ void GUI::openOptions() _mapView->setTimeZone(options.timeZone.zone()); _dateRange.first = _dateRange.first.toTimeZone(options.timeZone.zone()); _dateRange.second = _dateRange.second.toTimeZone(options.timeZone.zone()); + reload = true; } if (reload) @@ -2006,6 +2013,9 @@ void GUI::writeSettings() if (_showMarkersAction->isChecked() != SHOW_MARKERS_DEFAULT) settings.setValue(SHOW_MARKERS_SETTING, _showMarkersAction->isChecked()); + if (_showMarkerInfoAction->isChecked() != SHOW_MARKER_INFO_DEFAULT) + settings.setValue(SHOW_MARKER_INFO_SETTING, + _showMarkerInfoAction->isChecked()); settings.endGroup(); settings.beginGroup(PDF_EXPORT_SETTINGS_GROUP); @@ -2303,6 +2313,11 @@ void GUI::readSettings() _mapView->showMarkers(false); else _showMarkersAction->setChecked(true); + if (settings.value(SHOW_MARKER_INFO_SETTING, + SHOW_MARKER_INFO_DEFAULT).toBool()) { + _mapView->showMarkerInfo(true); + _showMarkerInfoAction->setChecked(true); + } settings.endGroup(); settings.beginGroup(PDF_EXPORT_SETTINGS_GROUP); diff --git a/src/GUI/gui.h b/src/GUI/gui.h index fba257bb..a22e6a06 100644 --- a/src/GUI/gui.h +++ b/src/GUI/gui.h @@ -206,6 +206,7 @@ private: QAction *_showAreasAction; QAction *_showRouteWaypointsAction; QAction *_showMarkersAction; + QAction *_showMarkerInfoAction; QAction *_showTicksAction; QAction *_showCoordinatesAction; QAction *_openOptionsAction; diff --git a/src/GUI/mapview.cpp b/src/GUI/mapview.cpp index 89cb022a..573464f9 100644 --- a/src/GUI/mapview.cpp +++ b/src/GUI/mapview.cpp @@ -93,6 +93,7 @@ MapView::MapView(Map *map, POI *poi, QWidget *parent) _overlapPOIs = true; _showRouteWaypoints = true; _showMarkers = true; + _showMarkerInfo = false; _showPathTicks = false; _trackWidth = 3; _routeWidth = 3; @@ -141,6 +142,7 @@ PathItem *MapView::addTrack(const Track &track) ti->setDigitalZoom(_digitalZoom); ti->setMarkerColor(_markerColor); ti->showMarker(_showMarkers); + ti->showMarkerInfo(_showMarkerInfo); ti->showTicks(_showPathTicks); _scene->addItem(ti); @@ -169,6 +171,7 @@ PathItem *MapView::addRoute(const Route &route) ri->setDigitalZoom(_digitalZoom); ri->setMarkerColor(_markerColor); ri->showMarker(_showMarkers); + ri->showMarkerInfo(_showMarkerInfo); ri->showTicks(_showPathTicks); _scene->addItem(ri); @@ -805,6 +808,16 @@ void MapView::showMarkers(bool show) _routes.at(i)->showMarker(show); } +void MapView::showMarkerInfo(bool show) +{ + _showMarkerInfo = show; + + for (int i = 0; i < _tracks.size(); i++) + _tracks.at(i)->showMarkerInfo(show); + for (int i = 0; i < _routes.size(); i++) + _routes.at(i)->showMarkerInfo(show); +} + void MapView::showTicks(bool show) { _showPathTicks = show; diff --git a/src/GUI/mapview.h b/src/GUI/mapview.h index d4030280..ada95e4d 100644 --- a/src/GUI/mapview.h +++ b/src/GUI/mapview.h @@ -95,6 +95,7 @@ public slots: void showCoordinates(bool show); void showTicks(bool show); void showMarkers(bool show); + void showMarkerInfo(bool show); void showOverlappedPOIs(bool show); void showWaypointLabels(bool show); void showTracks(bool show); @@ -161,7 +162,7 @@ private: bool _showMap, _showTracks, _showRoutes, _showAreas, _showWaypoints, _showWaypointLabels, _showPOI, _showPOILabels, _showRouteWaypoints, - _showMarkers, _showPathTicks; + _showMarkers, _showMarkerInfo, _showPathTicks; bool _overlapPOIs; int _trackWidth, _routeWidth, _areaWidth; Qt::PenStyle _trackStyle, _routeStyle, _areaStyle; diff --git a/src/GUI/markerinfoitem.cpp b/src/GUI/markerinfoitem.cpp new file mode 100644 index 00000000..076cf72b --- /dev/null +++ b/src/GUI/markerinfoitem.cpp @@ -0,0 +1,73 @@ +#include +#include +#include +#include "font.h" +#include "markerinfoitem.h" + + +#define OFFSET 7 + +MarkerInfoItem::MarkerInfoItem(QGraphicsItem *parent) : QGraphicsItem(parent) +{ + _color = Qt::red; + + _font.setPixelSize(FONT_SIZE); + _font.setFamily(FONT_FAMILY); +} + +void MarkerInfoItem::setDate(const QDateTime &date) +{ + prepareGeometryChange(); + + QLocale l; + _date = l.toString(date.date(), QLocale::ShortFormat); + _time = l.toString(date.time(), QLocale::ShortFormat); + + updateBoundingRect(); +} + +void MarkerInfoItem::updateBoundingRect() +{ + QFontMetrics fm(_font); + + qreal width = qMax(fm.boundingRect(_date).width(), + fm.boundingRect(_time).width()); + qreal height = 2 * fm.height() - 2*fm.descent(); + + _boundingRect = QRectF(-OFFSET/2, -height/2, width + 1.5*OFFSET, height); +} + +void MarkerInfoItem::paint(QPainter *painter, const QStyleOptionGraphicsItem + *option, QWidget *widget) +{ + Q_UNUSED(option); + Q_UNUSED(widget); + + QFontMetrics fm(_font); + QRectF ry(OFFSET, -fm.height() + fm.descent(), + fm.boundingRect(_time).width(), fm.height() - fm.descent()); + QRectF rx(OFFSET, 0, fm.boundingRect(_date).width(), fm.height() + - fm.descent()); + + painter->setPen(Qt::NoPen); + QColor bc(painter->background().color()); + bc.setAlpha(196); + painter->setBrush(QBrush(bc)); + painter->drawRect(ry); + painter->drawRect(rx); + painter->setBrush(Qt::NoBrush); + + painter->setFont(_font); + painter->setPen(_color); + + painter->drawText(OFFSET, -fm.descent()/2, _date); + painter->drawText(OFFSET, fm.height() - fm.descent()*1.5, _time); + + //painter->drawRect(boundingRect()); +} + +void MarkerInfoItem::setColor(const QColor &color) +{ + _color = color; + update(); +} diff --git a/src/GUI/markerinfoitem.h b/src/GUI/markerinfoitem.h new file mode 100644 index 00000000..be07dd86 --- /dev/null +++ b/src/GUI/markerinfoitem.h @@ -0,0 +1,27 @@ +#ifndef MARKERINFOITEM_H +#define MARKERINFOITEM_H + +#include + +class MarkerInfoItem : public QGraphicsItem +{ +public: + MarkerInfoItem(QGraphicsItem *parent = 0); + + QRectF boundingRect() const {return _boundingRect;} + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, + QWidget *widget); + + void setDate(const QDateTime &date); + void setColor(const QColor &color); + +private: + void updateBoundingRect(); + + QString _date, _time; + QRectF _boundingRect; + QColor _color; + QFont _font; +}; + +#endif // MARKERINFOITEM_H diff --git a/src/GUI/pathitem.cpp b/src/GUI/pathitem.cpp index 54d15267..5876eca1 100644 --- a/src/GUI/pathitem.cpp +++ b/src/GUI/pathitem.cpp @@ -8,6 +8,7 @@ #include "popup.h" #include "graphitem.h" #include "markeritem.h" +#include "markerinfoitem.h" #include "pathitem.h" @@ -52,6 +53,8 @@ PathItem::PathItem(const Path &path, Map *map, QGraphicsItem *parent) _marker = new MarkerItem(this); _marker->setZValue(1); _marker->setPos(position(_markerDistance)); + _markerInfo = new MarkerInfoItem(_marker); + _markerInfo->setVisible(false); setCursor(Qt::ArrowCursor); setAcceptHoverEvents(true); @@ -278,13 +281,26 @@ void PathItem::setMarkerPosition(qreal pos) if (isValid(pp)) { _marker->setVisible(_showMarker); _marker->setPos(pp); + setMarkerInfo(pos); } else _marker->setVisible(false); } +void PathItem::setMarkerInfo(qreal pos) +{ + qreal time = _graph + ? (_graph->graphType() == Time) ? pos : _graph->timeAtDistance(pos) + : NAN; + QDateTime d(date()); + + if (!std::isnan(time) && d.isValid()) + _markerInfo->setDate(d.addSecs(time).toTimeZone(_timeZone)); +} + void PathItem::setMarkerColor(const QColor &color) { _marker->setColor(color); + _markerInfo->setColor(color); } void PathItem::hover(bool hover) @@ -309,6 +325,11 @@ void PathItem::showMarker(bool show) _marker->setVisible(show && isValid(position(_markerDistance))); } +void PathItem::showMarkerInfo(bool show) +{ + _markerInfo->setVisible(show); +} + qreal PathItem::xInM() const { return (_units == Nautical) ? NMIINM : (_units == Imperial) ? MIINM : KMINM; diff --git a/src/GUI/pathitem.h b/src/GUI/pathitem.h index 9a589803..a4cda50c 100644 --- a/src/GUI/pathitem.h +++ b/src/GUI/pathitem.h @@ -12,6 +12,7 @@ class Map; class PathTickItem; class GraphItem; class MarkerItem; +class MarkerInfoItem; class PathItem : public QObject, public GraphicsItem { @@ -26,6 +27,8 @@ public: void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); + virtual QDateTime date() const = 0; + const Path &path() const {return _path;} void addGraph(GraphItem *graph); @@ -39,6 +42,7 @@ public: void setDigitalZoom(int zoom); void setMarkerColor(const QColor &color); void showMarker(bool show); + void showMarkerInfo(bool show); void showTicks(bool show); void setMarkerPosition(qreal pos); @@ -68,6 +72,7 @@ private: void updatePainterPath(); void updateShape(); void addSegment(const Coordinates &c1, const Coordinates &c2); + void setMarkerInfo(qreal pos); qreal xInM() const; unsigned tickSize() const; @@ -87,6 +92,7 @@ private: bool _showTicks; MarkerItem *_marker; + MarkerInfoItem *_markerInfo; QVector _ticks; }; diff --git a/src/GUI/routeitem.h b/src/GUI/routeitem.h index 28f91409..8c815e8a 100644 --- a/src/GUI/routeitem.h +++ b/src/GUI/routeitem.h @@ -21,6 +21,7 @@ public: void showWaypointLabels(bool show); QString info() const; + QDateTime date() const {return QDateTime();} private: QString _name; diff --git a/src/GUI/settings.h b/src/GUI/settings.h index c851bff6..9c873554 100644 --- a/src/GUI/settings.h +++ b/src/GUI/settings.h @@ -65,6 +65,8 @@ #define SHOW_WAYPOINT_LABELS_DEFAULT true #define SHOW_MARKERS_SETTING "positionMarkers" #define SHOW_MARKERS_DEFAULT true +#define SHOW_MARKER_INFO_SETTING "markerInfo" +#define SHOW_MARKER_INFO_DEFAULT false #define PDF_EXPORT_SETTINGS_GROUP "Export" #define PAPER_ORIENTATION_SETTING "orientation" diff --git a/src/GUI/trackitem.h b/src/GUI/trackitem.h index 3c4219c0..34f94f55 100644 --- a/src/GUI/trackitem.h +++ b/src/GUI/trackitem.h @@ -16,6 +16,7 @@ public: TrackItem(const Track &track, Map *map, QGraphicsItem *parent = 0); QString info() const; + QDateTime date() const {return _date;} private: QString _name;