Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use QgsMapLayerUtils::combinedExtent
  • Loading branch information
nyalldawson committed Nov 25, 2021
1 parent 780db4e commit 937dd80
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 44 deletions.
2 changes: 1 addition & 1 deletion python/core/auto_generated/qgsgrouplayer.sip.in
Expand Up @@ -87,7 +87,7 @@ the parent :py:class:`QgsProject` wherever appropriate.
.. seealso:: :py:func:`childLayers`
%End

QList< QgsMapLayer * > childLayers();
QList< QgsMapLayer * > childLayers() const;
%Docstring
Returns the child layers contained by the group.

Expand Down
45 changes: 3 additions & 42 deletions src/core/qgsgrouplayer.cpp
Expand Up @@ -27,6 +27,7 @@
#include "qgsmessagelog.h"
#include "qgspainteffectregistry.h"
#include "qgsapplication.h"
#include "qgsmaplayerutils.h"

QgsGroupLayer::QgsGroupLayer( const QString &name, const LayerOptions &options )
: QgsMapLayer( QgsMapLayerType::GroupLayer, name )
Expand Down Expand Up @@ -66,47 +67,7 @@ QgsMapLayerRenderer *QgsGroupLayer::createMapRenderer( QgsRenderContext &context

QgsRectangle QgsGroupLayer::extent() const
{
QgsRectangle rect;
rect.setMinimal();

const QList< QgsMapLayer * > currentLayers = _qgis_listRefToRaw( mChildren );
for ( const QgsMapLayer *layer : currentLayers )
{
if ( !layer->isSpatial() )
continue;

QgsRectangle layerExtent = layer->extent();
if ( const QgsVectorLayer *vLayer = qobject_cast<const QgsVectorLayer *>( layer ) )
{
if ( layerExtent.isEmpty() )
{
const_cast< QgsVectorLayer * >( vLayer )->updateExtents();
layerExtent = vLayer->extent();
}
}

if ( layerExtent.isNull() )
continue;

//transform extent
try
{
QgsCoordinateTransform ct = QgsCoordinateTransform( layer->crs(), crs(), mTransformContext );
if ( ct.isValid() )
{
ct.setBallparkTransformsAreAppropriate( true );
layerExtent = ct.transformBoundingBox( layerExtent );
}
}
catch ( QgsCsException &cse )
{
QgsMessageLog::logMessage( QObject::tr( "Transform error caught: %1" ).arg( cse.what() ), QObject::tr( "CRS" ) );
}

rect.combineExtentWith( layerExtent );
}

return rect;
return QgsMapLayerUtils::combinedExtent( childLayers(), crs(), mTransformContext );
}

void QgsGroupLayer::setTransformContext( const QgsCoordinateTransformContext &context )
Expand Down Expand Up @@ -325,7 +286,7 @@ void QgsGroupLayer::setChildLayers( const QList< QgsMapLayer * > &layers )
triggerRepaint();
}

QList< QgsMapLayer * > QgsGroupLayer::childLayers()
QList< QgsMapLayer * > QgsGroupLayer::childLayers() const
{
return _qgis_listRefToRaw( mChildren );
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsgrouplayer.h
Expand Up @@ -109,7 +109,7 @@ class CORE_EXPORT QgsGroupLayer : public QgsMapLayer
*
* \see setChildLayers()
*/
QList< QgsMapLayer * > childLayers();
QList< QgsMapLayer * > childLayers() const;

/**
* Returns the current paint effect for the group layer.
Expand Down

0 comments on commit 937dd80

Please sign in to comment.