Skip to content

Commit

Permalink
show current layer CRS in the layer context menu and allow to change it
Browse files Browse the repository at this point in the history
directly to one of the recently used CRSs (fix #13882)
  • Loading branch information
alexbruy authored and nyalldawson committed Aug 2, 2020
1 parent 48619cf commit 92bf631
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
45 changes: 40 additions & 5 deletions src/app/qgsapplayertreeviewmenuprovider.cpp
Expand Up @@ -328,16 +328,39 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()
if ( !layer->isInScaleRange( mCanvas->scale() ) )
menu->addAction( tr( "Zoom to &Visible Scale" ), QgisApp::instance(), &QgisApp::zoomToLayerScale );

QMenu *menuSetCRS = new QMenu( tr( "Set CRS" ), menu );
// set layer crs
QAction *actionSetLayerCrs = new QAction( tr( "Set Layer CRS…" ), menuSetCRS );
connect( actionSetLayerCrs, &QAction::triggered, QgisApp::instance(), &QgisApp::setLayerCrs );
menuSetCRS->addAction( actionSetLayerCrs );
QMenu *menuSetCRS = new QMenu( tr( "Layer CRS" ), menu );
QAction *actionCurrentCrs = new QAction( layer->crs().authid(), menuSetCRS );
actionCurrentCrs->setEnabled( false );
menuSetCRS->addAction( actionCurrentCrs );
// assign layer crs to project
QAction *actionSetProjectCrs = new QAction( tr( "Set &Project CRS from Layer" ), menuSetCRS );
connect( actionSetProjectCrs, &QAction::triggered, QgisApp::instance(), &QgisApp::setProjectCrsFromLayer );
menuSetCRS->addAction( actionSetProjectCrs );

const QList< QgsCoordinateReferenceSystem> recentProjections = QgsCoordinateReferenceSystem::recentCoordinateReferenceSystems();
if ( !recentProjections.isEmpty() )
{
menuSetCRS->addSeparator();
int i = 0;
for ( const QgsCoordinateReferenceSystem &crs : recentProjections )
{
QAction *action = menuSetCRS->addAction( crs.authid() );
action->setProperty( "layerId", layer->id() );
action->setProperty( "crs", crs.authid() );
i++;
if ( i == 2 )
break;
}
}
// Connect once for the entire submenu.
connect( menuSetCRS, &QMenu::triggered, this, &QgsAppLayerTreeViewMenuProvider::setLayerCrs );

// set layer crs
menuSetCRS->addSeparator();
QAction *actionSetLayerCrs = new QAction( tr( "Set Layer CRS…" ), menuSetCRS );
connect( actionSetLayerCrs, &QAction::triggered, QgisApp::instance(), &QgisApp::setLayerCrs );
menuSetCRS->addAction( actionSetLayerCrs );

menu->addMenu( menuSetCRS );
}

Expand Down Expand Up @@ -997,3 +1020,15 @@ bool QgsAppLayerTreeViewMenuProvider::removeActionEnabled()
}
return true;
}

void QgsAppLayerTreeViewMenuProvider::setLayerCrs( QAction *action )
{
QString layerId = action->property( "layerId" ).toString();
QgsMapLayer *layer = QgsProject::instance()->mapLayer( layerId );
if ( !layer )
return;

QString authid = action->property( "crs" ).toString();
layer->setCrs( QgsCoordinateReferenceSystem( authid ), true );
layer->triggerRepaint();
}
3 changes: 2 additions & 1 deletion src/app/qgsapplayertreeviewmenuprovider.h
Expand Up @@ -71,7 +71,8 @@ class QgsAppLayerTreeViewMenuProvider : public QObject, public QgsLayerTreeViewM
void copySymbolLegendNodeSymbol( const QString &layerId, const QString &ruleKey );
void pasteSymbolLegendNodeSymbol( const QString &layerId, const QString &ruleKey );
void setSymbolLegendNodeColor( const QColor &color );

// Set layer CRS corresponding to the text of the given action
void setLayerCrs( QAction *action );
private:
bool removeActionEnabled();
};
Expand Down

0 comments on commit 92bf631

Please sign in to comment.