diff --git a/python/core/auto_generated/qgsproject.sip.in b/python/core/auto_generated/qgsproject.sip.in index 6636b3eb920..aef8b9ade4b 100644 --- a/python/core/auto_generated/qgsproject.sip.in +++ b/python/core/auto_generated/qgsproject.sip.in @@ -720,6 +720,7 @@ Retrieve a pointer to a registered layer by layer ID. .. seealso:: :py:func:`mapLayers` %End + QList mapLayersByName( const QString &layerName ) const; %Docstring Retrieve a list of matching registered layers by layer name. diff --git a/src/app/locator/qgsinbuiltlocatorfilters.cpp b/src/app/locator/qgsinbuiltlocatorfilters.cpp index 95fb13fd4cd..082d1a93d7c 100644 --- a/src/app/locator/qgsinbuiltlocatorfilters.cpp +++ b/src/app/locator/qgsinbuiltlocatorfilters.cpp @@ -317,7 +317,7 @@ void QgsActiveLayerFeaturesLocatorFilter::triggerResult( const QgsLocatorResult QVariantList dataList = result.userData.toList(); QgsFeatureId id = dataList.at( 0 ).toLongLong(); QString layerId = dataList.at( 1 ).toString(); - QgsVectorLayer *layer = qobject_cast< QgsVectorLayer *>( QgsProject::instance()->mapLayer( layerId ) ); + QgsVectorLayer *layer = QgsProject::instance()->mapLayer( layerId ); if ( !layer ) return; @@ -431,7 +431,7 @@ void QgsAllLayersFeaturesLocatorFilter::triggerResultFromAction( const QgsLocato QVariantList dataList = result.userData.toList(); QgsFeatureId fid = dataList.at( 0 ).toLongLong(); QString layerId = dataList.at( 1 ).toString(); - QgsVectorLayer *layer = qobject_cast< QgsVectorLayer *>( QgsProject::instance()->mapLayer( layerId ) ); + QgsVectorLayer *layer = QgsProject::instance()->mapLayer( layerId ); if ( !layer ) return; diff --git a/src/app/qgsapplayertreeviewmenuprovider.cpp b/src/app/qgsapplayertreeviewmenuprovider.cpp index 162dee3dae4..1b03d086fc2 100644 --- a/src/app/qgsapplayertreeviewmenuprovider.cpp +++ b/src/app/qgsapplayertreeviewmenuprovider.cpp @@ -682,7 +682,7 @@ void QgsAppLayerTreeViewMenuProvider::editVectorSymbol() return; QString layerId = action->property( "layerId" ).toString(); - QgsVectorLayer *layer = dynamic_cast( QgsProject::instance()->mapLayer( layerId ) ); + QgsVectorLayer *layer = QgsProject::instance()->mapLayer( layerId ); if ( !layer ) return; @@ -712,7 +712,7 @@ void QgsAppLayerTreeViewMenuProvider::setVectorSymbolColor( const QColor &color return; QString layerId = action->property( "layerId" ).toString(); - QgsVectorLayer *layer = qobject_cast( QgsProject::instance()->mapLayer( layerId ) ); + QgsVectorLayer *layer = QgsProject::instance()->mapLayer( layerId ); if ( !layer ) return; @@ -805,7 +805,7 @@ void QgsAppLayerTreeViewMenuProvider::setSymbolLegendNodeColor( const QColor &co std::unique_ptr< QgsSymbol > newSymbol( originalSymbol->clone() ); newSymbol->setColor( color ); node->setSymbol( newSymbol.release() ); - if ( QgsVectorLayer *layer = qobject_cast( QgsProject::instance()->mapLayer( layerId ) ) ) + if ( QgsVectorLayer *layer = QgsProject::instance()->mapLayer( layerId ) ) { layer->emitStyleChanged(); } diff --git a/src/app/qgslabelpropertydialog.cpp b/src/app/qgslabelpropertydialog.cpp index 1b94e26485d..abe1a7c64c0 100644 --- a/src/app/qgslabelpropertydialog.cpp +++ b/src/app/qgslabelpropertydialog.cpp @@ -93,7 +93,7 @@ void QgsLabelPropertyDialog::buttonBox_clicked( QAbstractButton *button ) void QgsLabelPropertyDialog::init( const QString &layerId, const QString &providerId, int featureId, const QString &labelText ) { //get feature attributes - QgsVectorLayer *vlayer = dynamic_cast( QgsProject::instance()->mapLayer( layerId ) ); + QgsVectorLayer *vlayer = QgsProject::instance()->mapLayer( layerId ); if ( !vlayer ) { return; diff --git a/src/app/qgsmaptoollabel.cpp b/src/app/qgsmaptoollabel.cpp index 2bdb268e374..a09b10c87f7 100644 --- a/src/app/qgsmaptoollabel.cpp +++ b/src/app/qgsmaptoollabel.cpp @@ -690,7 +690,7 @@ bool QgsMapToolLabel::diagramCanShowHide( QgsVectorLayer *vlayer, int &showCol ) QgsMapToolLabel::LabelDetails::LabelDetails( const QgsLabelPosition &p ) : pos( p ) { - layer = qobject_cast( QgsProject::instance()->mapLayer( pos.layerID ) ); + layer = QgsProject::instance()->mapLayer( pos.layerID ); if ( layer && layer->labelsEnabled() && !p.isDiagram ) { settings = layer->labeling()->settings( pos.providerID ); diff --git a/src/core/fieldformatter/qgsvaluerelationfieldformatter.cpp b/src/core/fieldformatter/qgsvaluerelationfieldformatter.cpp index 861cbb4de06..db79a5a155d 100644 --- a/src/core/fieldformatter/qgsvaluerelationfieldformatter.cpp +++ b/src/core/fieldformatter/qgsvaluerelationfieldformatter.cpp @@ -112,7 +112,7 @@ QgsValueRelationFieldFormatter::ValueRelationCache QgsValueRelationFieldFormatte { ValueRelationCache cache; - QgsVectorLayer *layer = qobject_cast( QgsProject::instance()->mapLayer( config.value( QStringLiteral( "Layer" ) ).toString() ) ); + QgsVectorLayer *layer = QgsProject::instance()->mapLayer( config.value( QStringLiteral( "Layer" ) ).toString() ); if ( !layer ) return cache; diff --git a/src/core/qgsmimedatautils.cpp b/src/core/qgsmimedatautils.cpp index 8cf81988175..c693abbd310 100644 --- a/src/core/qgsmimedatautils.cpp +++ b/src/core/qgsmimedatautils.cpp @@ -85,7 +85,7 @@ QgsVectorLayer *QgsMimeDataUtils::Uri::vectorLayer( bool &owner, QString &error return nullptr; } QString layerId = url.queryItemValue( QStringLiteral( "layerid" ) ); - QgsVectorLayer *vectorLayer = qobject_cast< QgsVectorLayer *>( QgsProject::instance()->mapLayer( layerId ) ); + QgsVectorLayer *vectorLayer = QgsProject::instance()->mapLayer( layerId ); if ( !vectorLayer ) { error = QObject::tr( "Cannot get memory layer." ); diff --git a/src/core/qgsproject.h b/src/core/qgsproject.h index 36d3f0554ba..bc25b3c5563 100644 --- a/src/core/qgsproject.h +++ b/src/core/qgsproject.h @@ -702,6 +702,30 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera */ QgsMapLayer *mapLayer( const QString &layerId ) const; +#ifndef SIP_RUN + + /** + * Retrieve a pointer to a registered layer by \p layerId converted + * to type T. This is a convenience template. + * A nullptr will be returned if the layer is not found or + * if it cannot be cast to type T. + * + * \code{cpp} + * QgsVectorLayer *layer = project->mapLayer( layerId ); + * \endcode + * + * \see mapLayer() + * \see mapLayers() + * + * \since QGIS 3.6 + */ + template + T mapLayer( const QString &layerId ) const + { + return qobject_cast( mapLayer( layerId ) ); + } +#endif + /** * Retrieve a list of matching registered layers by layer name. * \param layerName name of layers to match diff --git a/src/gui/editorwidgets/qgsvaluerelationconfigdlg.cpp b/src/gui/editorwidgets/qgsvaluerelationconfigdlg.cpp index 61b77a5e76d..2f553a8bf10 100644 --- a/src/gui/editorwidgets/qgsvaluerelationconfigdlg.cpp +++ b/src/gui/editorwidgets/qgsvaluerelationconfigdlg.cpp @@ -68,7 +68,7 @@ QVariantMap QgsValueRelationConfigDlg::config() void QgsValueRelationConfigDlg::setConfig( const QVariantMap &config ) { - QgsVectorLayer *lyr = qobject_cast( QgsProject::instance()->mapLayer( config.value( QStringLiteral( "Layer" ) ).toString() ) ); + QgsVectorLayer *lyr = QgsProject::instance()->mapLayer( config.value( QStringLiteral( "Layer" ) ).toString() ); mLayerName->setLayer( lyr ); mKeyColumn->setField( config.value( QStringLiteral( "Key" ) ).toString() ); mValueColumn->setField( config.value( QStringLiteral( "Value" ) ).toString() ); diff --git a/src/plugins/geometry_checker/qgsgeometrycheckersetuptab.cpp b/src/plugins/geometry_checker/qgsgeometrycheckersetuptab.cpp index d0e78b4464e..d64518d3169 100644 --- a/src/plugins/geometry_checker/qgsgeometrycheckersetuptab.cpp +++ b/src/plugins/geometry_checker/qgsgeometrycheckersetuptab.cpp @@ -162,7 +162,7 @@ QList QgsGeometryCheckerSetupTab::getSelectedLayers() if ( item->checkState() == Qt::Checked ) { QString layerId = item->data( LayerIdRole ).toString(); - QgsVectorLayer *layer = qobject_cast( QgsProject::instance()->mapLayer( layerId ) ); + QgsVectorLayer *layer = QgsProject::instance()->mapLayer( layerId ); if ( layer ) { layers.append( layer ); @@ -253,8 +253,8 @@ void QgsGeometryCheckerSetupTab::runChecks() } } } - QgsVectorLayer *lineLayerCheckLayer = ui.comboLineLayerIntersection->isEnabled() ? dynamic_cast( QgsProject::instance()->mapLayer( ui.comboLineLayerIntersection->currentData().toString() ) ) : nullptr; - QgsVectorLayer *followBoundaryCheckLayer = ui.comboBoxFollowBoundaries->isEnabled() ? dynamic_cast( QgsProject::instance()->mapLayer( ui.comboBoxFollowBoundaries->currentData().toString() ) ) : nullptr; + QgsVectorLayer *lineLayerCheckLayer = ui.comboLineLayerIntersection->isEnabled() ? QgsProject::instance()->mapLayer( ui.comboLineLayerIntersection->currentData().toString() ) : nullptr; + QgsVectorLayer *followBoundaryCheckLayer = ui.comboBoxFollowBoundaries->isEnabled() ? QgsProject::instance()->mapLayer( ui.comboBoxFollowBoundaries->currentData().toString() ) : nullptr; if ( layers.contains( lineLayerCheckLayer ) || layers.contains( followBoundaryCheckLayer ) ) { QMessageBox::critical( this, tr( "Check Geometries" ), tr( "The selected input layers cannot contain a layer also selected for a topology check." ) ); @@ -421,7 +421,7 @@ void QgsGeometryCheckerSetupTab::runChecks() // LineLayerIntersection check is enabled, make sure there is also a feature pool for that layer if ( ui.checkLineLayerIntersection->isChecked() && !featurePools.keys().contains( ui.comboLineLayerIntersection->currentData().toString() ) ) { - QgsVectorLayer *layer = dynamic_cast( QgsProject::instance()->mapLayer( ui.comboLineLayerIntersection->currentData().toString() ) ); + QgsVectorLayer *layer = QgsProject::instance()->mapLayer( ui.comboLineLayerIntersection->currentData().toString() ); Q_ASSERT( layer ); featurePools.insert( layer->id(), new QgsVectorDataProviderFeaturePool( layer, selectedOnly ) ); } diff --git a/src/plugins/geometry_checker/qgsgeometrycheckfactory.cpp b/src/plugins/geometry_checker/qgsgeometrycheckfactory.cpp index dc753885021..a3b8c3d98fb 100644 --- a/src/plugins/geometry_checker/qgsgeometrycheckfactory.cpp +++ b/src/plugins/geometry_checker/qgsgeometrycheckfactory.cpp @@ -271,7 +271,7 @@ template<> QgsGeometryCheck *QgsGeometryCheckFactoryTisChecked() ); if ( ui.checkBoxFollowBoundaries->isEnabled() && ui.checkBoxFollowBoundaries->isChecked() ) { - QgsVectorLayer *checkLayer = qobject_cast( QgsProject::instance()->mapLayer( ui.comboBoxFollowBoundaries->currentData().toString() ) ); + QgsVectorLayer *checkLayer = QgsProject::instance()->mapLayer( ui.comboBoxFollowBoundaries->currentData().toString() ); return new QgsGeometryFollowBoundariesCheck( context, QVariantMap(), checkLayer ); } else diff --git a/src/plugins/globe/qgsglobefeatureidentify.cpp b/src/plugins/globe/qgsglobefeatureidentify.cpp index 78e58142304..73806918c8b 100644 --- a/src/plugins/globe/qgsglobefeatureidentify.cpp +++ b/src/plugins/globe/qgsglobefeatureidentify.cpp @@ -57,7 +57,7 @@ void QgsGlobeFeatureIdentifyCallback::onHit( osgEarth::ObjectID id ) std::string layerId; if ( feature->getUserValue( "qgisLayerId", layerId ) ) { - QgsVectorLayer *lyr = qobject_cast( QgsProject::instance()->mapLayer( QString::fromStdString( layerId ) ) ); + QgsVectorLayer *lyr = QgsProject::instance()->mapLayer( QString::fromStdString( layerId ) ); #endif if ( lyr ) { diff --git a/src/plugins/topology/checkDock.cpp b/src/plugins/topology/checkDock.cpp index 7ee19ce5f04..4e5753ab0d0 100644 --- a/src/plugins/topology/checkDock.cpp +++ b/src/plugins/topology/checkDock.cpp @@ -151,7 +151,7 @@ void checkDock::deleteErrors() void checkDock::parseErrorListByLayer( const QString &layerId ) { - QgsVectorLayer *layer = qobject_cast( QgsProject::instance()->mapLayer( layerId ) ); + QgsVectorLayer *layer = QgsProject::instance()->mapLayer( layerId ); QList::Iterator it = mErrorList.begin(); while ( it != mErrorList.end() ) diff --git a/tests/src/core/testqgstranslateproject.cpp b/tests/src/core/testqgstranslateproject.cpp index bae84442f1e..6eca8988718 100644 --- a/tests/src/core/testqgstranslateproject.cpp +++ b/tests/src/core/testqgstranslateproject.cpp @@ -167,8 +167,8 @@ void TestQgsTranslateProject::translateProject() QgsProject::instance()->read( projectFileName ); //with the qm file containing translation from en to de, the project should be in german and renamed with postfix .de - QgsVectorLayer *points_layer = qobject_cast( QgsProject::instance()->mapLayer( "points_240d6bd6_9203_470a_994a_aae13cd9fa04" ) ); - QgsVectorLayer *lines_layer = qobject_cast( QgsProject::instance()->mapLayer( "lines_a677672a_bf5d_410d_98c9_d326a5719a1b" ) ); + QgsVectorLayer *points_layer = QgsProject::instance()->mapLayer( "points_240d6bd6_9203_470a_994a_aae13cd9fa04" ); + QgsVectorLayer *lines_layer = QgsProject::instance()->mapLayer( "lines_a677672a_bf5d_410d_98c9_d326a5719a1b" ); //LAYER NAMES //lines -> Linien