Cleaner code

This commit is contained in:
Nyall Dawson 2023-02-03 10:04:19 +10:00
parent 8e20bf245c
commit beb91a5a26
4 changed files with 41 additions and 10 deletions

View File

@ -108,6 +108,17 @@ Sets the current paint ``effect`` for the renderer.
Ownership is transferred to the renderer.
.. seealso:: :py:func:`paintEffect`
%End
void prepareLayersForRemovalFromGroup();
%Docstring
Prepares all child layers in the group prior to removal from the group.
This method should be called before removing a group layer from a project, to ensure
that the existing child layers are in a state which is compatible with non-group
layer children.
.. versionadded:: 3.30
%End
};

View File

@ -490,15 +490,7 @@ void QgsLayerTreeGroup::setGroupLayer( QgsGroupLayer *layer )
{
if ( !layer )
{
// clearing the group layer -- ensure all child layers have consistent settings
const QList< QgsMapLayer * > children = groupLayer->childLayers();
for ( QgsMapLayer *child : children )
{
if ( QgsPainting::isClippingMode( QgsPainting::getBlendModeEnum( child->blendMode() ) ) )
{
child->setBlendMode( QPainter::CompositionMode_SourceOver );
}
}
groupLayer->prepareLayersForRemovalFromGroup();
}
}
mGroupLayer.setLayer( layer );

View File

@ -289,9 +289,15 @@ void QgsGroupLayer::setChildLayers( const QList< QgsMapLayer * > &layers )
}
for ( QgsMapLayer *layer : currentLayers )
{
if ( !layers.contains( layer ) )
if ( layer && !layers.contains( layer ) )
{
// layer removed from group
disconnect( layer, &QgsMapLayer::repaintRequested, this, &QgsMapLayer::triggerRepaint );
if ( QgsPainting::isClippingMode( QgsPainting::getBlendModeEnum( layer->blendMode() ) ) )
{
layer->setBlendMode( QPainter::CompositionMode_SourceOver );
}
}
}
mChildren = _qgis_listRawToRef( layers );
@ -331,6 +337,17 @@ void QgsGroupLayer::setPaintEffect( QgsPaintEffect *effect )
mPaintEffect.reset( effect );
}
void QgsGroupLayer::prepareLayersForRemovalFromGroup()
{
for ( const QgsMapLayerRef &child : std::as_const( mChildren ) )
{
if ( child.get() && QgsPainting::isClippingMode( QgsPainting::getBlendModeEnum( child->blendMode() ) ) )
{
child->setBlendMode( QPainter::CompositionMode_SourceOver );
}
}
}
//
// QgsGroupLayerDataProvider
//

View File

@ -126,6 +126,17 @@ class CORE_EXPORT QgsGroupLayer : public QgsMapLayer
*/
void setPaintEffect( QgsPaintEffect *effect SIP_TRANSFER );
/**
* Prepares all child layers in the group prior to removal from the group.
*
* This method should be called before removing a group layer from a project, to ensure
* that the existing child layers are in a state which is compatible with non-group
* layer children.
*
* \since QGIS 3.30
*/
void prepareLayersForRemovalFromGroup();
private:
QgsGroupLayerDataProvider *mDataProvider = nullptr;