Skip to content

Commit

Permalink
add template method to return specific type of layers in map canvas (#…
Browse files Browse the repository at this point in the history
…58878)

* add template method to return specific type of layers in map canvas

* Apply suggestions from code review

Co-authored-by: Matthias Kuhn <matthias@opengis.ch>

---------

Co-authored-by: Matthias Kuhn <matthias@opengis.ch>
  • Loading branch information
3nids and m-kuhn authored Sep 26, 2024
1 parent da1f55b commit 590232d
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions python/PyQt6/core/auto_generated/qgsmapsettings.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ all their child layers.
.. seealso:: :py:func:`layerIds`
%End



void setLayers( const QList<QgsMapLayer *> &layers );
%Docstring
Sets the list of ``layers`` to render in the map.
Expand Down
1 change: 1 addition & 0 deletions python/PyQt6/gui/auto_generated/qgsmapcanvas.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ all their child layers.
.. seealso:: :py:func:`setLayers`
%End


void freeze( bool frozen = true );
%Docstring
Freeze/thaw the map canvas. This is used to prevent the canvas from
Expand Down
2 changes: 2 additions & 0 deletions python/core/auto_generated/qgsmapsettings.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ all their child layers.
.. seealso:: :py:func:`layerIds`
%End



void setLayers( const QList<QgsMapLayer *> &layers );
%Docstring
Sets the list of ``layers`` to render in the map.
Expand Down
1 change: 1 addition & 0 deletions python/gui/auto_generated/qgsmapcanvas.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ all their child layers.
.. seealso:: :py:func:`setLayers`
%End


void freeze( bool frozen = true );
%Docstring
Freeze/thaw the map canvas. This is used to prevent the canvas from
Expand Down
18 changes: 18 additions & 0 deletions src/core/qgsmapsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,23 @@ QList<QgsMapLayer *> QgsMapSettings::layers( bool expandGroupLayers ) const
return result;
}

template<typename T>
QVector<T> QgsMapSettings::layers() const
{
const QList<QgsMapLayer *> actualLayers = _qgis_listQPointerToRaw( mLayers );

QVector<T> layers;
for ( QgsMapLayer *layer : actualLayers )
{
T tLayer = qobject_cast<T>( layer );
if ( tLayer )
{
layers << tLayer;
}
}
return layers;
}

void QgsMapSettings::setLayers( const QList<QgsMapLayer *> &layers )
{
// filter list, removing null layers and non-spatial layers
Expand Down Expand Up @@ -898,3 +915,4 @@ void QgsMapSettings::setElevationShadingRenderer( const QgsElevationShadingRende
{
mShadingRenderer = elevationShadingRenderer;
}

12 changes: 12 additions & 0 deletions src/core/qgsmapsettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,18 @@ class CORE_EXPORT QgsMapSettings : public QgsTemporalRangeObject
*/
QList<QgsMapLayer *> layers( bool expandGroupLayers = false ) const;


#ifndef SIP_RUN
/**
* Returns a list of registered map layers with a specified layer type.
*
* \note not available in Python bindings
* \since QGIS 3.40
*/
template <typename T>
QVector<T> layers() const;
#endif

/**
* Sets the list of \a layers to render in the map.
*
Expand Down
11 changes: 11 additions & 0 deletions src/gui/qgsmapcanvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,17 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView, public QgsExpressionContex
*/
QList<QgsMapLayer *> layers( bool expandGroupLayers = false ) const;

#ifndef SIP_RUN
/**
* Returns a list of registered map layers with a specified layer type.
*
* \note not available in Python bindings
* \since QGIS 3.40
*/
template <typename T>
QVector<T> layers() const {return mapSettings().layers<T>();}
#endif

/**
* Freeze/thaw the map canvas. This is used to prevent the canvas from
* responding to events while layers are being added/removed etc.
Expand Down

0 comments on commit 590232d

Please sign in to comment.