Skip to content

Commit

Permalink
[feature][gps] Make location label in GPS toolbar a button which
Browse files Browse the repository at this point in the history
shows a drop down menu allowing users to select which GPS information
they want to show in the toolbar
  • Loading branch information
nyalldawson committed Nov 8, 2022
1 parent b436282 commit ef97a7f
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 15 deletions.
8 changes: 8 additions & 0 deletions src/app/gps/qgsappgpsconnection.cpp
Expand Up @@ -65,6 +65,14 @@ QgsPoint QgsAppGpsConnection::lastValidLocation() const
return QgsPoint();
}

QgsGpsInformation QgsAppGpsConnection::lastInformation() const
{
if ( mConnection )
return mConnection->currentGPSInformation();
else
return QgsGpsInformation();
}

void QgsAppGpsConnection::connectGps()
{
QString port;
Expand Down
5 changes: 5 additions & 0 deletions src/app/gps/qgsappgpsconnection.h
Expand Up @@ -68,6 +68,11 @@ class APP_EXPORT QgsAppGpsConnection : public QObject
*/
QgsPoint lastValidLocation() const;

/**
* Returns the last received GPS information.
*/
QgsGpsInformation lastInformation() const;

public slots:

/**
Expand Down
108 changes: 95 additions & 13 deletions src/app/gps/qgsgpstoolbar.cpp
Expand Up @@ -26,6 +26,7 @@
#include "qgsprojectgpssettings.h"
#include "qgsmaplayermodel.h"
#include "qgsmaplayerproxymodel.h"
#include "qgsgpsconnection.h"

#include <QLabel>
#include <QToolButton>
Expand Down Expand Up @@ -122,7 +123,8 @@ QgsGpsToolBar::QgsGpsToolBar( QgsAppGpsConnection *connection, QgsMapCanvas *can
addAction( mShowInfoAction );

connect( mConnection, &QgsAppGpsConnection::positionChanged, this, &QgsGpsToolBar::updateLocationLabel );
updateLocationLabel( mConnection->lastValidLocation() );
connect( mConnection, &QgsAppGpsConnection::stateChanged, this, &QgsGpsToolBar::updateLocationLabel );
updateLocationLabel();

QToolButton *settingsButton = new QToolButton();
settingsButton->setAutoRaise( true );
Expand All @@ -149,8 +151,8 @@ QgsGpsToolBar::QgsGpsToolBar( QgsAppGpsConnection *connection, QgsMapCanvas *can
mRecenterAction->setEnabled( false );
mCreateFeatureAction->setEnabled( false );
mAddTrackVertexAction->setEnabled( false );
delete mLocationLabel;
mLocationLabel = nullptr;
delete mLocationButton;
mLocationButton = nullptr;
break;
case Qgis::GpsConnectionStatus::Connecting:
whileBlocking( mConnectAction )->setChecked( true );
Expand All @@ -160,8 +162,8 @@ QgsGpsToolBar::QgsGpsToolBar( QgsAppGpsConnection *connection, QgsMapCanvas *can
mRecenterAction->setEnabled( false );
mCreateFeatureAction->setEnabled( false );
mAddTrackVertexAction->setEnabled( false );
delete mLocationLabel;
mLocationLabel = nullptr;
delete mLocationButton;
mLocationButton = nullptr;
break;
case Qgis::GpsConnectionStatus::Connected:
whileBlocking( mConnectAction )->setChecked( true );
Expand Down Expand Up @@ -199,22 +201,56 @@ void QgsGpsToolBar::setResetTrackButtonEnabled( bool enabled )
mResetFeatureAction->setEnabled( enabled );
}

void QgsGpsToolBar::updateLocationLabel( const QgsPoint &point )
void QgsGpsToolBar::updateLocationLabel()
{
const QgsPoint point = mConnection->lastValidLocation();
if ( point.isEmpty() )
{
delete mLocationLabel;
mLocationLabel = nullptr;
delete mLocationButton;
mLocationButton = nullptr;
}
else
{
if ( !mLocationLabel )
if ( !mLocationButton )
{
mLocationLabel = new QLabel();
insertWidget( mSettingsMenuAction, mLocationLabel );
createLocationWidget();
}
const QString pos = QgsCoordinateUtils::formatCoordinateForProject( QgsProject::instance(), point, QgsCoordinateReferenceSystem(), 8 );
mLocationLabel->setText( pos );

const QgsGpsInformation information = mConnection->lastInformation();

const Qgis::GpsInformationComponents visibleComponents = settingShowInToolbar.value();

QStringList parts;
for ( Qgis::GpsInformationComponent component :
{
Qgis::GpsInformationComponent::Location,
Qgis::GpsInformationComponent::Altitude,
Qgis::GpsInformationComponent::Bearing,
Qgis::GpsInformationComponent::GroundSpeed,
} )
{
if ( visibleComponents & component )
{
const QVariant value = information.componentValue( component );
switch ( component )
{
case Qgis::GpsInformationComponent::Location:
parts << QgsCoordinateUtils::formatCoordinateForProject( QgsProject::instance(), point, QgsCoordinateReferenceSystem(), 8 );
break;
case Qgis::GpsInformationComponent::Altitude:
parts << tr( "%1 m" ).arg( value.toDouble( ) );
break;
case Qgis::GpsInformationComponent::GroundSpeed:
parts << tr( "%1 km/h" ).arg( value.toDouble( ) );
break;
case Qgis::GpsInformationComponent::Bearing:
parts << QString::number( value.toDouble( ) ) + QChar( 176 );
break;
}
}
}

mLocationButton->setText( parts.join( ' ' ) );
}

// this is necessary to ensure that the toolbar in floating mode correctly resizes to fit the label!
Expand Down Expand Up @@ -337,3 +373,49 @@ void QgsGpsToolBar::destinationMenuAboutToShow()
}
}

void QgsGpsToolBar::createLocationWidget()
{
mLocationButton = new QToolButton();
mLocationButton->setToolTip( tr( "Current GPS Location" ) );

QMenu *locationMenu = new QMenu( mLocationButton );

const Qgis::GpsInformationComponents visibleComponents = settingShowInToolbar.value();

for ( const auto &it : std::vector< std::pair< Qgis::GpsInformationComponent, QString> >
{
{ 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" ) }
} )
{
const Qgis::GpsInformationComponent component = it.first;
QAction *showComponentAction = new QAction( it.second, locationMenu );
showComponentAction->setCheckable( true );
showComponentAction->setData( QVariant::fromValue( component ) );
showComponentAction->setChecked( visibleComponents & component );
locationMenu->addAction( showComponentAction );

connect( showComponentAction, &QAction::toggled, this, [ = ]( bool checked )
{
const Qgis::GpsInformationComponents currentVisibleComponents = settingShowInToolbar.value();
if ( checked )
{
settingShowInToolbar.setValue( currentVisibleComponents | component );
}
else
{
settingShowInToolbar.setValue( currentVisibleComponents & ~( static_cast< int >( component ) ) );
}
updateLocationLabel();
} );
}

mLocationButton->setMenu( locationMenu );
mLocationButton->setAutoRaise( true );
mLocationButton->setPopupMode( QToolButton::ToolButtonPopupMode::InstantPopup );

insertWidget( mSettingsMenuAction, mLocationButton );
}

12 changes: 10 additions & 2 deletions src/app/gps/qgsgpstoolbar.h
Expand Up @@ -20,6 +20,7 @@
#include <QPointer>

#include "qgscoordinatereferencesystem.h"
#include "qgssettingsentryenumflag.h"

class QgsAppGpsConnection;
class QgsMapCanvas;
Expand All @@ -28,12 +29,16 @@ class QgsVectorLayer;
class QgsMapLayerProxyModel;
class QToolButton;



class QgsGpsToolBar : public QToolBar
{
Q_OBJECT

public:

static const inline QgsSettingsEntryEnumFlag<Qgis::GpsInformationComponents> settingShowInToolbar = QgsSettingsEntryEnumFlag<Qgis::GpsInformationComponents>( QStringLiteral( "show-in-toolbar" ), QgsSettings::Prefix::GPS, Qgis::GpsInformationComponent::Location, QStringLiteral( "GPS information components to show in GPS toolbar" ) );

QgsGpsToolBar( QgsAppGpsConnection *connection, QgsMapCanvas *canvas, QWidget *parent = nullptr );

QAction *showInfoAction() { return mShowInfoAction; }
Expand All @@ -51,12 +56,14 @@ class QgsGpsToolBar : public QToolBar

private slots:

void updateLocationLabel( const QgsPoint &point );
void updateLocationLabel();
void destinationLayerChanged( QgsVectorLayer *lyr );
void destinationMenuAboutToShow();

private:

void createLocationWidget();

QgsAppGpsConnection *mConnection = nullptr;
QgsMapCanvas *mCanvas = nullptr;
QAction *mConnectAction = nullptr;
Expand All @@ -65,12 +72,13 @@ class QgsGpsToolBar : public QToolBar
QAction *mAddTrackVertexAction = nullptr;
QAction *mCreateFeatureAction = nullptr;
QAction *mResetFeatureAction = nullptr;
QAction *mSettingsMenuAction = nullptr;

QToolButton *mDestinationLayerButton = nullptr;

QMenu *mDestinationLayerMenu = nullptr;

QLabel *mLocationLabel = nullptr;
QPointer< QToolButton > mLocationButton;

QgsCoordinateReferenceSystem mWgs84CRS;
bool mEnableAddVertexButton = true;
Expand Down
3 changes: 3 additions & 0 deletions src/app/qgssettingsregistryapp.cpp
Expand Up @@ -32,6 +32,7 @@
#include "qgsappgpsdigitizing.h"
#include "qgsgpscanvasbridge.h"
#include "qgsgpsmarker.h"
#include "qgsgpstoolbar.h"

QgsSettingsRegistryApp::QgsSettingsRegistryApp()
: QgsSettingsRegistry()
Expand Down Expand Up @@ -72,6 +73,8 @@ QgsSettingsRegistryApp::QgsSettingsRegistryApp()

addSettingsEntry( &QgsAppGpsDigitizing::settingTrackLineSymbol );

addSettingsEntry( &QgsGpsToolBar::settingShowInToolbar );

addSettingsEntry( &QgsGpsMarker::settingLocationMarkerSymbol );
addSettingsEntry( &QgsGpsMarker::settingShowLocationMarker );
addSettingsEntry( &QgsGpsMarker::settingRotateLocationMarker );
Expand Down

0 comments on commit ef97a7f

Please sign in to comment.