Skip to content

Commit

Permalink
Merge pull request #8820 from m-kuhn/map_layer_template
Browse files Browse the repository at this point in the history
QgsProject::mapLayer<Qgs[Type]Layer *>( layerId ); template
  • Loading branch information
m-kuhn committed Jan 10, 2019
2 parents 43df43f + e32c739 commit c263750
Show file tree
Hide file tree
Showing 14 changed files with 44 additions and 19 deletions.
1 change: 1 addition & 0 deletions python/core/auto_generated/qgsproject.sip.in
Expand Up @@ -720,6 +720,7 @@ Retrieve a pointer to a registered layer by layer ID.
.. seealso:: :py:func:`mapLayers`
%End


QList<QgsMapLayer *> mapLayersByName( const QString &layerName ) const;
%Docstring
Retrieve a list of matching registered layers by layer name.
Expand Down
4 changes: 2 additions & 2 deletions src/app/locator/qgsinbuiltlocatorfilters.cpp
Expand Up @@ -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<QgsVectorLayer *>( layerId );
if ( !layer )
return;

Expand Down Expand Up @@ -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<QgsVectorLayer *>( layerId );
if ( !layer )
return;

Expand Down
6 changes: 3 additions & 3 deletions src/app/qgsapplayertreeviewmenuprovider.cpp
Expand Up @@ -682,7 +682,7 @@ void QgsAppLayerTreeViewMenuProvider::editVectorSymbol()
return;

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

Expand Down Expand Up @@ -712,7 +712,7 @@ void QgsAppLayerTreeViewMenuProvider::setVectorSymbolColor( const QColor &color
return;

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

Expand Down Expand Up @@ -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<QgsVectorLayer *>( QgsProject::instance()->mapLayer( layerId ) ) )
if ( QgsVectorLayer *layer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( layerId ) )
{
layer->emitStyleChanged();
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgslabelpropertydialog.cpp
Expand Up @@ -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<QgsVectorLayer *>( QgsProject::instance()->mapLayer( layerId ) );
QgsVectorLayer *vlayer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( layerId );
if ( !vlayer )
{
return;
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsmaptoollabel.cpp
Expand Up @@ -690,7 +690,7 @@ bool QgsMapToolLabel::diagramCanShowHide( QgsVectorLayer *vlayer, int &showCol )
QgsMapToolLabel::LabelDetails::LabelDetails( const QgsLabelPosition &p )
: pos( p )
{
layer = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( pos.layerID ) );
layer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( pos.layerID );
if ( layer && layer->labelsEnabled() && !p.isDiagram )
{
settings = layer->labeling()->settings( pos.providerID );
Expand Down
2 changes: 1 addition & 1 deletion src/core/fieldformatter/qgsvaluerelationfieldformatter.cpp
Expand Up @@ -112,7 +112,7 @@ QgsValueRelationFieldFormatter::ValueRelationCache QgsValueRelationFieldFormatte
{
ValueRelationCache cache;

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

if ( !layer )
return cache;
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsmimedatautils.cpp
Expand Up @@ -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<QgsVectorLayer *>( layerId );
if ( !vectorLayer )
{
error = QObject::tr( "Cannot get memory layer." );
Expand Down
24 changes: 24 additions & 0 deletions src/core/qgsproject.h
Expand Up @@ -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<QgsVectorLayer*>( layerId );
* \endcode
*
* \see mapLayer()
* \see mapLayers()
*
* \since QGIS 3.6
*/
template <class T>
T mapLayer( const QString &layerId ) const
{
return qobject_cast<T>( mapLayer( layerId ) );
}
#endif

/**
* Retrieve a list of matching registered layers by layer name.
* \param layerName name of layers to match
Expand Down
2 changes: 1 addition & 1 deletion src/gui/editorwidgets/qgsvaluerelationconfigdlg.cpp
Expand Up @@ -68,7 +68,7 @@ QVariantMap QgsValueRelationConfigDlg::config()

void QgsValueRelationConfigDlg::setConfig( const QVariantMap &config )
{
QgsVectorLayer *lyr = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( config.value( QStringLiteral( "Layer" ) ).toString() ) );
QgsVectorLayer *lyr = QgsProject::instance()->mapLayer<QgsVectorLayer *>( config.value( QStringLiteral( "Layer" ) ).toString() );
mLayerName->setLayer( lyr );
mKeyColumn->setField( config.value( QStringLiteral( "Key" ) ).toString() );
mValueColumn->setField( config.value( QStringLiteral( "Value" ) ).toString() );
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/geometry_checker/qgsgeometrycheckersetuptab.cpp
Expand Up @@ -162,7 +162,7 @@ QList<QgsVectorLayer *> QgsGeometryCheckerSetupTab::getSelectedLayers()
if ( item->checkState() == Qt::Checked )
{
QString layerId = item->data( LayerIdRole ).toString();
QgsVectorLayer *layer = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( layerId ) );
QgsVectorLayer *layer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( layerId );
if ( layer )
{
layers.append( layer );
Expand Down Expand Up @@ -253,8 +253,8 @@ void QgsGeometryCheckerSetupTab::runChecks()
}
}
}
QgsVectorLayer *lineLayerCheckLayer = ui.comboLineLayerIntersection->isEnabled() ? dynamic_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( ui.comboLineLayerIntersection->currentData().toString() ) ) : nullptr;
QgsVectorLayer *followBoundaryCheckLayer = ui.comboBoxFollowBoundaries->isEnabled() ? dynamic_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( ui.comboBoxFollowBoundaries->currentData().toString() ) ) : nullptr;
QgsVectorLayer *lineLayerCheckLayer = ui.comboLineLayerIntersection->isEnabled() ? QgsProject::instance()->mapLayer<QgsVectorLayer *>( ui.comboLineLayerIntersection->currentData().toString() ) : nullptr;
QgsVectorLayer *followBoundaryCheckLayer = ui.comboBoxFollowBoundaries->isEnabled() ? QgsProject::instance()->mapLayer<QgsVectorLayer *>( 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." ) );
Expand Down Expand Up @@ -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<QgsVectorLayer *>( QgsProject::instance()->mapLayer( ui.comboLineLayerIntersection->currentData().toString() ) );
QgsVectorLayer *layer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( ui.comboLineLayerIntersection->currentData().toString() );
Q_ASSERT( layer );
featurePools.insert( layer->id(), new QgsVectorDataProviderFeaturePool( layer, selectedOnly ) );
}
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/geometry_checker/qgsgeometrycheckfactory.cpp
Expand Up @@ -271,7 +271,7 @@ template<> QgsGeometryCheck *QgsGeometryCheckFactoryT<QgsGeometryFollowBoundarie
QgsSettings().setValue( sSettingsGroup + "checkFollowBoundaries", ui.checkBoxFollowBoundaries->isChecked() );
if ( ui.checkBoxFollowBoundaries->isEnabled() && ui.checkBoxFollowBoundaries->isChecked() )
{
QgsVectorLayer *checkLayer = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( ui.comboBoxFollowBoundaries->currentData().toString() ) );
QgsVectorLayer *checkLayer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( ui.comboBoxFollowBoundaries->currentData().toString() );
return new QgsGeometryFollowBoundariesCheck( context, QVariantMap(), checkLayer );
}
else
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/globe/qgsglobefeatureidentify.cpp
Expand Up @@ -57,7 +57,7 @@ void QgsGlobeFeatureIdentifyCallback::onHit( osgEarth::ObjectID id )
std::string layerId;
if ( feature->getUserValue( "qgisLayerId", layerId ) )
{
QgsVectorLayer *lyr = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( QString::fromStdString( layerId ) ) );
QgsVectorLayer *lyr = QgsProject::instance()->mapLayer<QgsVectorLayer *>( QString::fromStdString( layerId ) );
#endif
if ( lyr )
{
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/topology/checkDock.cpp
Expand Up @@ -151,7 +151,7 @@ void checkDock::deleteErrors()

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

while ( it != mErrorList.end() )
Expand Down
4 changes: 2 additions & 2 deletions tests/src/core/testqgstranslateproject.cpp
Expand Up @@ -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<QgsVectorLayer *>( QgsProject::instance()->mapLayer( "points_240d6bd6_9203_470a_994a_aae13cd9fa04" ) );
QgsVectorLayer *lines_layer = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( "lines_a677672a_bf5d_410d_98c9_d326a5719a1b" ) );
QgsVectorLayer *points_layer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( "points_240d6bd6_9203_470a_994a_aae13cd9fa04" );
QgsVectorLayer *lines_layer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( "lines_a677672a_bf5d_410d_98c9_d326a5719a1b" );

//LAYER NAMES
//lines -> Linien
Expand Down

0 comments on commit c263750

Please sign in to comment.