Skip to content

Commit c263750

Browse files
authoredJan 10, 2019
Merge pull request #8820 from m-kuhn/map_layer_template
QgsProject::mapLayer<Qgs[Type]Layer *>( layerId ); template
2 parents 43df43f + e32c739 commit c263750

14 files changed

+44
-19
lines changed
 

‎python/core/auto_generated/qgsproject.sip.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,7 @@ Retrieve a pointer to a registered layer by layer ID.
720720
.. seealso:: :py:func:`mapLayers`
721721
%End
722722

723+
723724
QList<QgsMapLayer *> mapLayersByName( const QString &layerName ) const;
724725
%Docstring
725726
Retrieve a list of matching registered layers by layer name.

‎src/app/locator/qgsinbuiltlocatorfilters.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ void QgsActiveLayerFeaturesLocatorFilter::triggerResult( const QgsLocatorResult
317317
QVariantList dataList = result.userData.toList();
318318
QgsFeatureId id = dataList.at( 0 ).toLongLong();
319319
QString layerId = dataList.at( 1 ).toString();
320-
QgsVectorLayer *layer = qobject_cast< QgsVectorLayer *>( QgsProject::instance()->mapLayer( layerId ) );
320+
QgsVectorLayer *layer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( layerId );
321321
if ( !layer )
322322
return;
323323

@@ -431,7 +431,7 @@ void QgsAllLayersFeaturesLocatorFilter::triggerResultFromAction( const QgsLocato
431431
QVariantList dataList = result.userData.toList();
432432
QgsFeatureId fid = dataList.at( 0 ).toLongLong();
433433
QString layerId = dataList.at( 1 ).toString();
434-
QgsVectorLayer *layer = qobject_cast< QgsVectorLayer *>( QgsProject::instance()->mapLayer( layerId ) );
434+
QgsVectorLayer *layer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( layerId );
435435
if ( !layer )
436436
return;
437437

‎src/app/qgsapplayertreeviewmenuprovider.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ void QgsAppLayerTreeViewMenuProvider::editVectorSymbol()
682682
return;
683683

684684
QString layerId = action->property( "layerId" ).toString();
685-
QgsVectorLayer *layer = dynamic_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( layerId ) );
685+
QgsVectorLayer *layer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( layerId );
686686
if ( !layer )
687687
return;
688688

@@ -712,7 +712,7 @@ void QgsAppLayerTreeViewMenuProvider::setVectorSymbolColor( const QColor &color
712712
return;
713713

714714
QString layerId = action->property( "layerId" ).toString();
715-
QgsVectorLayer *layer = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( layerId ) );
715+
QgsVectorLayer *layer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( layerId );
716716
if ( !layer )
717717
return;
718718

@@ -805,7 +805,7 @@ void QgsAppLayerTreeViewMenuProvider::setSymbolLegendNodeColor( const QColor &co
805805
std::unique_ptr< QgsSymbol > newSymbol( originalSymbol->clone() );
806806
newSymbol->setColor( color );
807807
node->setSymbol( newSymbol.release() );
808-
if ( QgsVectorLayer *layer = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( layerId ) ) )
808+
if ( QgsVectorLayer *layer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( layerId ) )
809809
{
810810
layer->emitStyleChanged();
811811
}

‎src/app/qgslabelpropertydialog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ void QgsLabelPropertyDialog::buttonBox_clicked( QAbstractButton *button )
9393
void QgsLabelPropertyDialog::init( const QString &layerId, const QString &providerId, int featureId, const QString &labelText )
9494
{
9595
//get feature attributes
96-
QgsVectorLayer *vlayer = dynamic_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( layerId ) );
96+
QgsVectorLayer *vlayer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( layerId );
9797
if ( !vlayer )
9898
{
9999
return;

‎src/app/qgsmaptoollabel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ bool QgsMapToolLabel::diagramCanShowHide( QgsVectorLayer *vlayer, int &showCol )
690690
QgsMapToolLabel::LabelDetails::LabelDetails( const QgsLabelPosition &p )
691691
: pos( p )
692692
{
693-
layer = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( pos.layerID ) );
693+
layer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( pos.layerID );
694694
if ( layer && layer->labelsEnabled() && !p.isDiagram )
695695
{
696696
settings = layer->labeling()->settings( pos.providerID );

‎src/core/fieldformatter/qgsvaluerelationfieldformatter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ QgsValueRelationFieldFormatter::ValueRelationCache QgsValueRelationFieldFormatte
112112
{
113113
ValueRelationCache cache;
114114

115-
QgsVectorLayer *layer = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( config.value( QStringLiteral( "Layer" ) ).toString() ) );
115+
QgsVectorLayer *layer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( config.value( QStringLiteral( "Layer" ) ).toString() );
116116

117117
if ( !layer )
118118
return cache;

‎src/core/qgsmimedatautils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ QgsVectorLayer *QgsMimeDataUtils::Uri::vectorLayer( bool &owner, QString &error
8585
return nullptr;
8686
}
8787
QString layerId = url.queryItemValue( QStringLiteral( "layerid" ) );
88-
QgsVectorLayer *vectorLayer = qobject_cast< QgsVectorLayer *>( QgsProject::instance()->mapLayer( layerId ) );
88+
QgsVectorLayer *vectorLayer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( layerId );
8989
if ( !vectorLayer )
9090
{
9191
error = QObject::tr( "Cannot get memory layer." );

‎src/core/qgsproject.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,30 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
702702
*/
703703
QgsMapLayer *mapLayer( const QString &layerId ) const;
704704

705+
#ifndef SIP_RUN
706+
707+
/**
708+
* Retrieve a pointer to a registered layer by \p layerId converted
709+
* to type T. This is a convenience template.
710+
* A nullptr will be returned if the layer is not found or
711+
* if it cannot be cast to type T.
712+
*
713+
* \code{cpp}
714+
* QgsVectorLayer *layer = project->mapLayer<QgsVectorLayer*>( layerId );
715+
* \endcode
716+
*
717+
* \see mapLayer()
718+
* \see mapLayers()
719+
*
720+
* \since QGIS 3.6
721+
*/
722+
template <class T>
723+
T mapLayer( const QString &layerId ) const
724+
{
725+
return qobject_cast<T>( mapLayer( layerId ) );
726+
}
727+
#endif
728+
705729
/**
706730
* Retrieve a list of matching registered layers by layer name.
707731
* \param layerName name of layers to match

‎src/gui/editorwidgets/qgsvaluerelationconfigdlg.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ QVariantMap QgsValueRelationConfigDlg::config()
6868

6969
void QgsValueRelationConfigDlg::setConfig( const QVariantMap &config )
7070
{
71-
QgsVectorLayer *lyr = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( config.value( QStringLiteral( "Layer" ) ).toString() ) );
71+
QgsVectorLayer *lyr = QgsProject::instance()->mapLayer<QgsVectorLayer *>( config.value( QStringLiteral( "Layer" ) ).toString() );
7272
mLayerName->setLayer( lyr );
7373
mKeyColumn->setField( config.value( QStringLiteral( "Key" ) ).toString() );
7474
mValueColumn->setField( config.value( QStringLiteral( "Value" ) ).toString() );

‎src/plugins/geometry_checker/qgsgeometrycheckersetuptab.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ QList<QgsVectorLayer *> QgsGeometryCheckerSetupTab::getSelectedLayers()
162162
if ( item->checkState() == Qt::Checked )
163163
{
164164
QString layerId = item->data( LayerIdRole ).toString();
165-
QgsVectorLayer *layer = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( layerId ) );
165+
QgsVectorLayer *layer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( layerId );
166166
if ( layer )
167167
{
168168
layers.append( layer );
@@ -253,8 +253,8 @@ void QgsGeometryCheckerSetupTab::runChecks()
253253
}
254254
}
255255
}
256-
QgsVectorLayer *lineLayerCheckLayer = ui.comboLineLayerIntersection->isEnabled() ? dynamic_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( ui.comboLineLayerIntersection->currentData().toString() ) ) : nullptr;
257-
QgsVectorLayer *followBoundaryCheckLayer = ui.comboBoxFollowBoundaries->isEnabled() ? dynamic_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( ui.comboBoxFollowBoundaries->currentData().toString() ) ) : nullptr;
256+
QgsVectorLayer *lineLayerCheckLayer = ui.comboLineLayerIntersection->isEnabled() ? QgsProject::instance()->mapLayer<QgsVectorLayer *>( ui.comboLineLayerIntersection->currentData().toString() ) : nullptr;
257+
QgsVectorLayer *followBoundaryCheckLayer = ui.comboBoxFollowBoundaries->isEnabled() ? QgsProject::instance()->mapLayer<QgsVectorLayer *>( ui.comboBoxFollowBoundaries->currentData().toString() ) : nullptr;
258258
if ( layers.contains( lineLayerCheckLayer ) || layers.contains( followBoundaryCheckLayer ) )
259259
{
260260
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()
421421
// LineLayerIntersection check is enabled, make sure there is also a feature pool for that layer
422422
if ( ui.checkLineLayerIntersection->isChecked() && !featurePools.keys().contains( ui.comboLineLayerIntersection->currentData().toString() ) )
423423
{
424-
QgsVectorLayer *layer = dynamic_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( ui.comboLineLayerIntersection->currentData().toString() ) );
424+
QgsVectorLayer *layer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( ui.comboLineLayerIntersection->currentData().toString() );
425425
Q_ASSERT( layer );
426426
featurePools.insert( layer->id(), new QgsVectorDataProviderFeaturePool( layer, selectedOnly ) );
427427
}

‎src/plugins/geometry_checker/qgsgeometrycheckfactory.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ template<> QgsGeometryCheck *QgsGeometryCheckFactoryT<QgsGeometryFollowBoundarie
271271
QgsSettings().setValue( sSettingsGroup + "checkFollowBoundaries", ui.checkBoxFollowBoundaries->isChecked() );
272272
if ( ui.checkBoxFollowBoundaries->isEnabled() && ui.checkBoxFollowBoundaries->isChecked() )
273273
{
274-
QgsVectorLayer *checkLayer = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( ui.comboBoxFollowBoundaries->currentData().toString() ) );
274+
QgsVectorLayer *checkLayer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( ui.comboBoxFollowBoundaries->currentData().toString() );
275275
return new QgsGeometryFollowBoundariesCheck( context, QVariantMap(), checkLayer );
276276
}
277277
else

‎src/plugins/globe/qgsglobefeatureidentify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ void QgsGlobeFeatureIdentifyCallback::onHit( osgEarth::ObjectID id )
5757
std::string layerId;
5858
if ( feature->getUserValue( "qgisLayerId", layerId ) )
5959
{
60-
QgsVectorLayer *lyr = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( QString::fromStdString( layerId ) ) );
60+
QgsVectorLayer *lyr = QgsProject::instance()->mapLayer<QgsVectorLayer *>( QString::fromStdString( layerId ) );
6161
#endif
6262
if ( lyr )
6363
{

‎src/plugins/topology/checkDock.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ void checkDock::deleteErrors()
151151

152152
void checkDock::parseErrorListByLayer( const QString &layerId )
153153
{
154-
QgsVectorLayer *layer = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( layerId ) );
154+
QgsVectorLayer *layer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( layerId );
155155
QList<TopolError *>::Iterator it = mErrorList.begin();
156156

157157
while ( it != mErrorList.end() )

‎tests/src/core/testqgstranslateproject.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ void TestQgsTranslateProject::translateProject()
167167
QgsProject::instance()->read( projectFileName );
168168

169169
//with the qm file containing translation from en to de, the project should be in german and renamed with postfix .de
170-
QgsVectorLayer *points_layer = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( "points_240d6bd6_9203_470a_994a_aae13cd9fa04" ) );
171-
QgsVectorLayer *lines_layer = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( "lines_a677672a_bf5d_410d_98c9_d326a5719a1b" ) );
170+
QgsVectorLayer *points_layer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( "points_240d6bd6_9203_470a_994a_aae13cd9fa04" );
171+
QgsVectorLayer *lines_layer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( "lines_a677672a_bf5d_410d_98c9_d326a5719a1b" );
172172

173173
//LAYER NAMES
174174
//lines -> Linien

0 commit comments

Comments
 (0)
Please sign in to comment.