Skip to content
Permalink
Browse files

QgsMaplayerRegistry function to get only vector or raster layers

  • Loading branch information
m-kuhn committed Apr 5, 2016
1 parent c58a198 commit f6838032fab54235533db5d43e036d8860590796
Showing with 27 additions and 2 deletions.
  1. +1 −1 src/core/qgsmaplayerregistry.cpp
  2. +26 −1 src/core/qgsmaplayerregistry.h
@@ -191,7 +191,7 @@ void QgsMapLayerRegistry::reloadAllLayers()
}
}

const QMap<QString, QgsMapLayer*>& QgsMapLayerRegistry::mapLayers()
QMap<QString, QgsMapLayer*> QgsMapLayerRegistry::mapLayers()
{
return mMapLayers;
}
@@ -50,7 +50,32 @@ class CORE_EXPORT QgsMapLayerRegistry : public QObject
QList<QgsMapLayer *> mapLayersByName( const QString& layerName );

//! Retrieve the mapLayers collection (mainly intended for use by projection)
const QMap<QString, QgsMapLayer*> & mapLayers();
QMap<QString, QgsMapLayer*> mapLayers();

/**
* Get map layers of a certain type.
*
* Example:
*
* QVector<QgsVectorLayer*> vectorLayers = QgsMapLayerRegistry::instance()->layers<QgsVectorLayer*>();
*
* @note not available in Python bindings
* @note added in QGIS 2.16
*/
template <typename T>
QVector<T> layers() const
{
QVector<T> layers;
Q_FOREACH ( QgsMapLayer* layer, mMapLayers.values() )
{
T tLayer = qobject_cast<T>( layer );
if ( tLayer )
{
layers << tLayer;
}
}
return layers;
}

/**
* @brief

4 comments on commit f683803

@NathanW2

This comment has been minimized.

Copy link
Member

@NathanW2 NathanW2 replied Apr 5, 2016

Wouldn't something like:

layers(QgsMapLayer::Type type)

be easier:

Python version of logic:

for layer in maplayers:
    if layer.type == type:
      # keep it
@m-kuhn

This comment has been minimized.

Copy link
Member Author

@m-kuhn m-kuhn replied Apr 5, 2016

For C++ the current solution is "easier".

For python the problem is less urgent and its syntax allows for more flexibility without much overhead:

[layer for layer in QgsMapLayerRegistry.instance().mapLayers().values() if layer.type() == QgsMapLayer.RasterLayer]

But anyway, I don't mind a shorthand in either QgsMapLayerRegistry.sip or the pyqgis_wrappers

@m-kuhn

This comment has been minimized.

Copy link
Member Author

@m-kuhn m-kuhn replied Apr 5, 2016

I.e. no need for casting in C++ with the template approach.

@NathanW2

This comment has been minimized.

Copy link
Member

@NathanW2 NathanW2 replied Apr 5, 2016

Please sign in to comment.
You can’t perform that action at this time.