Skip to content

Commit

Permalink
Allow track length and distance from start to be shown in floating
Browse files Browse the repository at this point in the history
GPS toolbar
  • Loading branch information
nyalldawson committed Nov 11, 2022
1 parent 187da08 commit 5c6c63d
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 10 deletions.
4 changes: 3 additions & 1 deletion python/core/auto_additions/qgis.py
Expand Up @@ -905,7 +905,9 @@
Qgis.GpsInformationComponent.Altitude.__doc__ = "Altitude/elevation above or below the mean sea level"
Qgis.GpsInformationComponent.GroundSpeed.__doc__ = "Ground speed"
Qgis.GpsInformationComponent.Bearing.__doc__ = "Bearing measured in degrees clockwise from true north to the direction of travel"
Qgis.GpsInformationComponent.__doc__ = 'GPS information component.\n\n.. versionadded:: 3.30\n\n' + '* ``Location``: ' + Qgis.GpsInformationComponent.Location.__doc__ + '\n' + '* ``Altitude``: ' + Qgis.GpsInformationComponent.Altitude.__doc__ + '\n' + '* ``GroundSpeed``: ' + Qgis.GpsInformationComponent.GroundSpeed.__doc__ + '\n' + '* ``Bearing``: ' + Qgis.GpsInformationComponent.Bearing.__doc__
Qgis.GpsInformationComponent.TotalTrackLength.__doc__ = "Total distance of current GPS track (available from QGIS app library only)"
Qgis.GpsInformationComponent.TrackDistanceFromStart.__doc__ = "Direct distance from first vertex in current GPS track to last vertex (available from QGIS app library only)"
Qgis.GpsInformationComponent.__doc__ = 'GPS information component.\n\n.. versionadded:: 3.30\n\n' + '* ``Location``: ' + Qgis.GpsInformationComponent.Location.__doc__ + '\n' + '* ``Altitude``: ' + Qgis.GpsInformationComponent.Altitude.__doc__ + '\n' + '* ``GroundSpeed``: ' + Qgis.GpsInformationComponent.GroundSpeed.__doc__ + '\n' + '* ``Bearing``: ' + Qgis.GpsInformationComponent.Bearing.__doc__ + '\n' + '* ``TotalTrackLength``: ' + Qgis.GpsInformationComponent.TotalTrackLength.__doc__ + '\n' + '* ``TrackDistanceFromStart``: ' + Qgis.GpsInformationComponent.TrackDistanceFromStart.__doc__
# --
Qgis.GpsInformationComponent.baseClass = Qgis
Qgis.GpsInformationComponents.baseClass = Qgis
Expand Down
2 changes: 2 additions & 0 deletions python/core/auto_generated/qgis.sip.in
Expand Up @@ -620,6 +620,8 @@ The development version
Altitude,
GroundSpeed,
Bearing,
TotalTrackLength,
TrackDistanceFromStart,
};

typedef QFlags<Qgis::GpsInformationComponent> GpsInformationComponents;
Expand Down
2 changes: 1 addition & 1 deletion src/app/gps/qgsappgpsdigitizing.cpp
Expand Up @@ -109,7 +109,7 @@ double QgsAppGpsDigitizing::totalTrackLength() const
return mDa.measureLine( points );
}

double QgsAppGpsDigitizing::trackDirectLength() const
double QgsAppGpsDigitizing::trackDistanceFromStart() const
{
if ( mCaptureListWgs84.empty() )
return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/app/gps/qgsappgpsdigitizing.h
Expand Up @@ -62,7 +62,7 @@ class APP_EXPORT QgsAppGpsDigitizing: public QObject
*
* The returned length is calculated using ellipsoidal calculations.
*/
double trackDirectLength() const;
double trackDistanceFromStart() const;

/**
* Returns the distance area calculator used to calculate track lengths.
Expand Down
2 changes: 1 addition & 1 deletion src/app/gps/qgsgpsinformationwidget.cpp
Expand Up @@ -296,7 +296,7 @@ void QgsGpsInformationWidget::gpsConnected()
void QgsGpsInformationWidget::updateTrackInformation()
{
const double totalTrackLength = mDigitizing->totalTrackLength();
const double directTrackLength = mDigitizing->trackDirectLength();
const double directTrackLength = mDigitizing->trackDistanceFromStart();

const QgsSettings settings;
const bool keepBaseUnit = settings.value( QStringLiteral( "qgis/measure/keepbaseunit" ), true ).toBool();
Expand Down
38 changes: 37 additions & 1 deletion src/app/gps/qgsgpstoolbar.cpp
Expand Up @@ -27,6 +27,8 @@
#include "qgsmaplayermodel.h"
#include "qgsmaplayerproxymodel.h"
#include "qgsgpsconnection.h"
#include "qgsappgpsdigitizing.h"
#include "qgsunittypes.h"

#include <QLabel>
#include <QToolButton>
Expand Down Expand Up @@ -190,6 +192,13 @@ QgsGpsToolBar::QgsGpsToolBar( QgsAppGpsConnection *connection, QgsMapCanvas *can
setFixedWidth( sizeHint().width() );
}

void QgsGpsToolBar::setGpsDigitizing( QgsAppGpsDigitizing *digitizing )
{
mDigitizing = digitizing;
connect( mDigitizing, &QgsAppGpsDigitizing::distanceAreaChanged, this, &QgsGpsToolBar::updateLocationLabel );
connect( mDigitizing, &QgsAppGpsDigitizing::trackChanged, this, &QgsGpsToolBar::updateLocationLabel );
}

void QgsGpsToolBar::setAddVertexButtonEnabled( bool enabled )
{
mEnableAddVertexButton = enabled;
Expand Down Expand Up @@ -227,6 +236,8 @@ void QgsGpsToolBar::updateLocationLabel()
Qgis::GpsInformationComponent::Altitude,
Qgis::GpsInformationComponent::Bearing,
Qgis::GpsInformationComponent::GroundSpeed,
Qgis::GpsInformationComponent::TotalTrackLength,
Qgis::GpsInformationComponent::TrackDistanceFromStart,
} )
{
if ( visibleComponents & component )
Expand All @@ -246,6 +257,28 @@ void QgsGpsToolBar::updateLocationLabel()
case Qgis::GpsInformationComponent::Bearing:
parts << QString::number( value.toDouble( ) ) + QChar( 176 );
break;

case Qgis::GpsInformationComponent::TotalTrackLength:
case Qgis::GpsInformationComponent::TrackDistanceFromStart:
{
if ( mDigitizing )
{
const double measurement = component == Qgis::GpsInformationComponent::TotalTrackLength
? mDigitizing->totalTrackLength()
: mDigitizing->trackDistanceFromStart();

const QgsSettings settings;
const bool keepBaseUnit = settings.value( QStringLiteral( "qgis/measure/keepbaseunit" ), true ).toBool();
const int decimalPlaces = settings.value( QStringLiteral( "qgis/measure/decimalplaces" ), 3 ).toInt();

if ( measurement > 0 )
parts << mDigitizing->distanceArea().formatDistance( measurement, decimalPlaces, mDigitizing->distanceArea().lengthUnits(), keepBaseUnit );
else
parts << QStringLiteral( "0%1" ).arg( QgsUnitTypes::toAbbreviatedString( mDigitizing->distanceArea().lengthUnits() ) );
}

break;
}
}
}
}
Expand Down Expand Up @@ -387,7 +420,10 @@ void QgsGpsToolBar::createLocationWidget()
{ Qgis::GpsInformationComponent::Location, tr( "Show Location" ) },
{ Qgis::GpsInformationComponent::Altitude, tr( "Show Altitude" ) },
{ Qgis::GpsInformationComponent::GroundSpeed, tr( "Show Ground Speed" ) },
{ Qgis::GpsInformationComponent::Bearing, tr( "Show Bearing" ) }
{ Qgis::GpsInformationComponent::Bearing, tr( "Show Bearing" ) },
{ Qgis::GpsInformationComponent::TotalTrackLength, tr( "Show Total Track Length" ) },
{ Qgis::GpsInformationComponent::TrackDistanceFromStart, tr( "Show Distance from Start of Track" ) }

} )
{
const Qgis::GpsInformationComponent component = it.first;
Expand Down
6 changes: 5 additions & 1 deletion src/app/gps/qgsgpstoolbar.h
Expand Up @@ -28,7 +28,7 @@ class QLabel;
class QgsVectorLayer;
class QgsMapLayerProxyModel;
class QToolButton;

class QgsAppGpsDigitizing;


class QgsGpsToolBar : public QToolBar
Expand All @@ -43,6 +43,8 @@ class QgsGpsToolBar : public QToolBar

QAction *showInfoAction() { return mShowInfoAction; }

void setGpsDigitizing( QgsAppGpsDigitizing *digitizing );

signals:

void addVertexClicked();
Expand All @@ -66,6 +68,8 @@ class QgsGpsToolBar : public QToolBar

QgsAppGpsConnection *mConnection = nullptr;
QgsMapCanvas *mCanvas = nullptr;
QPointer< QgsAppGpsDigitizing > mDigitizing;

QAction *mConnectAction = nullptr;
QAction *mRecenterAction = nullptr;
QAction *mShowInfoAction = nullptr;
Expand Down
2 changes: 2 additions & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -1408,6 +1408,8 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipBadLayers
connect( mGpsToolBar, &QgsGpsToolBar::addVertexClicked, mGpsDigitizing, &QgsAppGpsDigitizing::addVertex );
connect( mGpsToolBar, &QgsGpsToolBar::resetFeatureClicked, mGpsDigitizing, &QgsAppGpsDigitizing::resetTrack );

mGpsToolBar->setGpsDigitizing( mGpsDigitizing );

mGpsCanvasBridge = new QgsGpsCanvasBridge( mGpsConnection, mMapCanvas );
mGpsCanvasBridge->setLocationMarkerVisible( mGpsSettingsMenu->locationMarkerVisible() );
mGpsCanvasBridge->setBearingLineVisible( mGpsSettingsMenu->bearingLineVisible() );
Expand Down
4 changes: 4 additions & 0 deletions src/core/gps/qgsgpsconnection.cpp
Expand Up @@ -140,6 +140,10 @@ QVariant QgsGpsInformation::componentValue( Qgis::GpsInformationComponent compon
return speed;
case Qgis::GpsInformationComponent::Bearing:
return std::isnan( direction ) ? QVariant() : direction;

case Qgis::GpsInformationComponent::TotalTrackLength:
case Qgis::GpsInformationComponent::TrackDistanceFromStart:
return QVariant(); // not available
}
BUILTIN_UNREACHABLE
}
Expand Down
2 changes: 2 additions & 0 deletions src/core/qgis.h
Expand Up @@ -1012,6 +1012,8 @@ class CORE_EXPORT Qgis
Altitude = 1 << 1, //!< Altitude/elevation above or below the mean sea level
GroundSpeed = 1 << 2, //!< Ground speed
Bearing = 1 << 3, //!< Bearing measured in degrees clockwise from true north to the direction of travel
TotalTrackLength = 1 << 4, //!< Total distance of current GPS track (available from QGIS app library only)
TrackDistanceFromStart = 1 << 5, //!< Direct distance from first vertex in current GPS track to last vertex (available from QGIS app library only)
};

/**
Expand Down
8 changes: 4 additions & 4 deletions tests/src/app/testqgsgpsintegration.cpp
Expand Up @@ -462,7 +462,7 @@ void TestQgsGpsIntegration::testTrackDistance()
QCOMPARE( spy.count(), 1 );

QCOMPARE( gpsDigitizing.totalTrackLength(), 0 );
QCOMPARE( gpsDigitizing.trackDirectLength(), 0 );
QCOMPARE( gpsDigitizing.trackDistanceFromStart(), 0 );

info.latitude = 46;
info.longitude = 100;
Expand All @@ -474,11 +474,11 @@ void TestQgsGpsIntegration::testTrackDistance()
QgsProject::instance()->setCrs( QgsCoordinateReferenceSystem( "EPSG:3857" ) );
QCOMPARE( QgsProject::instance()->ellipsoid(), QStringLiteral( "NONE" ) );
QGSCOMPARENEAR( gpsDigitizing.totalTrackLength(), 1, 0.01 );
QGSCOMPARENEAR( gpsDigitizing.trackDirectLength(), 1, 0.01 );
QGSCOMPARENEAR( gpsDigitizing.trackDistanceFromStart(), 1, 0.01 );
QgsProject::instance()->setEllipsoid( QStringLiteral( "EPSG:7030" ) );
QCOMPARE( QgsProject::instance()->ellipsoid(), QStringLiteral( "EPSG:7030" ) );
QGSCOMPARENEAR( gpsDigitizing.totalTrackLength(), 111141.548, 1 );
QGSCOMPARENEAR( gpsDigitizing.trackDirectLength(), 111141.548, 1 );
QGSCOMPARENEAR( gpsDigitizing.trackDistanceFromStart(), 111141.548, 1 );

info.latitude = 46;
info.longitude = 101;
Expand All @@ -487,7 +487,7 @@ void TestQgsGpsIntegration::testTrackDistance()
gpsDigitizing.addVertex();
QCOMPARE( spy.count(), 3 );
QGSCOMPARENEAR( gpsDigitizing.totalTrackLength(), 188604.338, 1 );
QGSCOMPARENEAR( gpsDigitizing.trackDirectLength(), 135869.0912, 1 );
QGSCOMPARENEAR( gpsDigitizing.trackDistanceFromStart(), 135869.0912, 1 );

gpsDigitizing.resetTrack();
QCOMPARE( spy.count(), 4 );
Expand Down

0 comments on commit 5c6c63d

Please sign in to comment.