Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
QgsMaplayerRegistry function to get only vector or raster layers
  • Loading branch information
m-kuhn committed Apr 5, 2016
1 parent c58a198 commit f683803
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/core/qgsmaplayerregistry.cpp
Expand Up @@ -191,7 +191,7 @@ void QgsMapLayerRegistry::reloadAllLayers()
}
}

const QMap<QString, QgsMapLayer*>& QgsMapLayerRegistry::mapLayers()
QMap<QString, QgsMapLayer*> QgsMapLayerRegistry::mapLayers()
{
return mMapLayers;
}
Expand Down
27 changes: 26 additions & 1 deletion src/core/qgsmaplayerregistry.h
Expand Up @@ -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
Expand Down

4 comments on commit f683803

@NathanW2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member Author

@m-kuhn m-kuhn commented on f683803 Apr 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member Author

@m-kuhn m-kuhn commented on f683803 Apr 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@NathanW2
Copy link
Member

@NathanW2 NathanW2 commented on f683803 Apr 5, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.