Skip to content

Commit

Permalink
Add menu to allow users to select destination layer for GPS features
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 7, 2022
1 parent 7ea1224 commit 57baa86
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/app/gps/qgsgpstoolbar.cpp
Expand Up @@ -24,6 +24,8 @@
#include "qgsappgpssettingsmenu.h"
#include "qgsapplication.h"
#include "qgsprojectgpssettings.h"
#include "qgsmaplayermodel.h"
#include "qgsmaplayerproxymodel.h"

#include <QLabel>
#include <QToolButton>
Expand Down Expand Up @@ -79,6 +81,21 @@ QgsGpsToolBar::QgsGpsToolBar( QgsAppGpsConnection *connection, QgsMapCanvas *can

addSeparator();

mDestinationLayerModel = new QgsMapLayerProxyModel( this );
mDestinationLayerModel->setProject( QgsProject::instance() );
mDestinationLayerModel->setFilters( QgsMapLayerProxyModel::Filter::HasGeometry | QgsMapLayerProxyModel::Filter::WritableLayer );

mDestinationLayerMenu = new QMenu( this );
connect( mDestinationLayerMenu, &QMenu::aboutToShow, this, &QgsGpsToolBar::destinationMenuAboutToShow );

QToolButton *destinationLayerButton = new QToolButton();
destinationLayerButton->setAutoRaise( true );
destinationLayerButton->setToolTip( tr( "Set destination layer for GPS digitized features" ) );
destinationLayerButton->setMenu( mDestinationLayerMenu );
destinationLayerButton->setPopupMode( QToolButton::InstantPopup );
destinationLayerButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionOptions.svg" ) ) );
addWidget( destinationLayerButton );

mAddTrackVertexAction = new QAction( tr( "Add Track Vertex" ), this );
mAddTrackVertexAction->setToolTip( tr( "Add vertex to GPS track using current GPS location" ) );
mAddTrackVertexAction->setEnabled( false );
Expand Down Expand Up @@ -269,3 +286,42 @@ void QgsGpsToolBar::layerEditStateChanged()
{
updateCloseFeatureButton( mLastLayer );
}

void QgsGpsToolBar::destinationMenuAboutToShow()
{
mDestinationLayerMenu->clear();

const QString currentLayerId = QgsProject::instance()->gpsSettings()->destinationLayer() ?
QgsProject::instance()->gpsSettings()->destinationLayer()->id() : QString();

for ( int row = 0; row < mDestinationLayerModel->rowCount(); ++row )
{
const QModelIndex index = mDestinationLayerModel->index( row, 0 );

QAction *layerAction = new QAction( index.data( Qt::DisplayRole ).toString(), mDestinationLayerMenu );
layerAction->setToolTip( index.data( Qt::ToolTipRole ).toString() );
layerAction->setIcon( index.data( Qt::DecorationRole ).value< QIcon >() );
layerAction->setCheckable( true );

const QString actionLayerId = index.data( QgsMapLayerModel::ItemDataRole::LayerIdRole ).toString();

if ( actionLayerId == currentLayerId )
layerAction->setChecked( true );

connect( layerAction, &QAction::toggled, this, [ = ]( bool checked )
{
if ( checked )
{
QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( QgsProject::instance()->mapLayer( actionLayerId ) );
if ( layer != QgsProject::instance()->gpsSettings()->destinationLayer() )
{
QgsProject::instance()->gpsSettings()->setDestinationLayer( layer );
QgsProject::instance()->setDirty();
}
}
} );

mDestinationLayerMenu->addAction( layerAction );
}
}

6 changes: 6 additions & 0 deletions src/app/gps/qgsgpstoolbar.h
Expand Up @@ -25,6 +25,7 @@ class QgsAppGpsConnection;
class QgsMapCanvas;
class QLabel;
class QgsVectorLayer;
class QgsMapLayerProxyModel;

class QgsGpsToolBar : public QToolBar
{
Expand Down Expand Up @@ -52,6 +53,7 @@ class QgsGpsToolBar : public QToolBar
void updateLocationLabel( const QgsPoint &point );
void updateCloseFeatureButton( QgsVectorLayer *lyr );
void layerEditStateChanged();
void destinationMenuAboutToShow();

private:

Expand All @@ -64,12 +66,16 @@ class QgsGpsToolBar : public QToolBar
QAction *mAddFeatureAction = nullptr;
QAction *mResetFeatureAction = nullptr;

QMenu *mDestinationLayerMenu = nullptr;

QLabel *mLocationLabel = nullptr;

QgsCoordinateReferenceSystem mWgs84CRS;
bool mEnableAddVertexButton = true;

QPointer< QgsVectorLayer > mLastLayer;

QgsMapLayerProxyModel *mDestinationLayerModel = nullptr;
};

#endif // QGSGPSTOOLBAR_H

0 comments on commit 57baa86

Please sign in to comment.