Simplify context passing

This commit is contained in:
Alessandro Pasotti 2023-06-06 10:08:17 +02:00
parent 7e9147c712
commit e7ebb8bc1b
6 changed files with 13 additions and 70 deletions

View File

@ -55,20 +55,6 @@ Returns the map of layer IDs to legend filter expression.
Sets the map of layer IDs to legend filter expression.
.. seealso:: :py:func:`layerFilterExpressions`
%End
void setFilterExpressionsContext( const QgsExpressionContext &context );
%Docstring
Sets the filter expression context.
.. seealso:: :py:func:`filterExpressionsContext`
%End
QgsExpressionContext filterExpressionsContext() const;
%Docstring
Returns the filter expression context.
.. seealso:: :py:func:`setFilterExpressionsContext`
%End
void setLayerFilterExpressionsFromLayerTree( QgsLayerTree *tree );

View File

@ -37,7 +37,6 @@ QgsLayerTreeFilterSettings::QgsLayerTreeFilterSettings( const QgsLayerTreeFilter
, mFlags( other.mFlags )
, mLayers( other.mLayers )
, mLayerExtents( other.mLayerExtents )
, mFilterExpressionsContext( other.mFilterExpressionsContext )
{
}
@ -50,7 +49,6 @@ QgsLayerTreeFilterSettings &QgsLayerTreeFilterSettings::operator=( const QgsLaye
mFlags = other.mFlags;
mLayers = other.mLayers;
mLayerExtents = other.mLayerExtents;
mFilterExpressionsContext = other.mFilterExpressionsContext;
return *this;
}
@ -69,16 +67,6 @@ void QgsLayerTreeFilterSettings::setLayerFilterExpressions( const QMap<QString,
mLayerFilterExpressions = expressions;
}
void QgsLayerTreeFilterSettings::setFilterExpressionsContext( const QgsExpressionContext &context )
{
mFilterExpressionsContext = context;
}
QgsExpressionContext QgsLayerTreeFilterSettings::filterExpressionsContext() const
{
return mFilterExpressionsContext;
}
void QgsLayerTreeFilterSettings::setLayerFilterExpressionsFromLayerTree( QgsLayerTree *tree )
{
QMap<QString, QString> legendFilterExpressions;

View File

@ -74,18 +74,6 @@ class CORE_EXPORT QgsLayerTreeFilterSettings
*/
void setLayerFilterExpressions( const QMap<QString, QString> &expressions );
/**
* Sets the filter expression context.
* \see filterExpressionsContext()
*/
void setFilterExpressionsContext( const QgsExpressionContext &context );
/**
* Returns the filter expression context.
* \see setFilterExpressionsContext()
*/
QgsExpressionContext filterExpressionsContext() const;
/**
* Sets layer filter expressions using a layer \a tree.
*/
@ -185,8 +173,6 @@ class CORE_EXPORT QgsLayerTreeFilterSettings
// geometry must be in layer CRS
QMap<QString, QVector< QgsGeometry > > mLayerExtents;
QgsExpressionContext mFilterExpressionsContext;
};
#endif // QGSLAYERTREEFILTERSETTINGS_H

View File

@ -1120,7 +1120,7 @@ void QgsLayoutItemLegend::doUpdateFilterByMap()
linkedFilterMaps.insert( mMap );
}
QgsMapSettings ms;
QgsMapSettings mapSettings;
QgsGeometry filterGeometry;
if ( mMap )
{
@ -1128,7 +1128,7 @@ void QgsLayoutItemLegend::doUpdateFilterByMap()
const QgsRectangle requestRectangle = mMap->requestedExtent();
QSizeF size( requestRectangle.width(), requestRectangle.height() );
size *= mLayout->convertFromLayoutUnits( mMap->mapUnitsToLayoutUnits(), Qgis::LayoutUnit::Millimeters ).length() * dpi / 25.4;
ms = mMap->mapSettings( requestRectangle, size, dpi, true );
mapSettings = mMap->mapSettings( requestRectangle, size, dpi, true );
filterGeometry = QgsGeometry::fromQPolygonF( mMap->visibleExtentPolygon() );
}
@ -1138,14 +1138,16 @@ void QgsLayoutItemLegend::doUpdateFilterByMap()
const QgsRectangle requestRectangle = ( *linkedFilterMaps.constBegin() )->requestedExtent();
QSizeF size( requestRectangle.width(), requestRectangle.height() );
size *= mLayout->convertFromLayoutUnits( ( *linkedFilterMaps.constBegin() )->mapUnitsToLayoutUnits(), Qgis::LayoutUnit::Millimeters ).length() * dpi / 25.4;
ms = ( *linkedFilterMaps.constBegin() )->mapSettings( requestRectangle, size, dpi, true );
mapSettings = ( *linkedFilterMaps.constBegin() )->mapSettings( requestRectangle, size, dpi, true );
filterGeometry = QgsGeometry::fromQPolygonF( ( *linkedFilterMaps.constBegin() )->visibleExtentPolygon() );
}
const QgsGeometry atlasGeometry = mInAtlas ? mLayout->reportContext().currentGeometry( ms.destinationCrs() ) : QgsGeometry();
mapSettings.setExpressionContext( createExpressionContext() );
QgsLayerTreeFilterSettings filterSettings( ms );
const QgsGeometry atlasGeometry = mInAtlas ? mLayout->reportContext().currentGeometry( mapSettings.destinationCrs() ) : QgsGeometry();
QgsLayerTreeFilterSettings filterSettings( mapSettings );
if ( !linkedFilterMaps.empty() )
{
@ -1157,7 +1159,7 @@ void QgsLayoutItemLegend::doUpdateFilterByMap()
QgsGeometry mapExtent = QgsGeometry::fromQPolygonF( map->visibleExtentPolygon() );
//transform back to destination CRS
const QgsCoordinateTransform mapTransform( map->crs(), ms.destinationCrs(), mLayout->project() );
const QgsCoordinateTransform mapTransform( map->crs(), mapSettings.destinationCrs(), mLayout->project() );
try
{
mapExtent.transform( mapTransform );
@ -1175,7 +1177,7 @@ void QgsLayoutItemLegend::doUpdateFilterByMap()
mapExtent = mapExtent.intersection( atlasGeometry );
}
filterSettings.addVisibleExtentForLayer( layer, QgsReferencedGeometry( mapExtent, ms.destinationCrs() ) );
filterSettings.addVisibleExtentForLayer( layer, QgsReferencedGeometry( mapExtent, mapSettings.destinationCrs() ) );
}
}
}
@ -1183,9 +1185,9 @@ void QgsLayoutItemLegend::doUpdateFilterByMap()
if ( mInAtlas )
{
if ( !filterGeometry.isEmpty() )
filterGeometry = mLayout->reportContext().currentGeometry( ms.destinationCrs() );
filterGeometry = mLayout->reportContext().currentGeometry( mapSettings.destinationCrs() );
else
filterGeometry = filterGeometry.intersection( mLayout->reportContext().currentGeometry( ms.destinationCrs() ) );
filterGeometry = filterGeometry.intersection( mLayout->reportContext().currentGeometry( mapSettings.destinationCrs() ) );
}
filterSettings.setLayerFilterExpressionsFromLayerTree( mLegendModel->rootGroup() );
@ -1198,11 +1200,6 @@ void QgsLayoutItemLegend::doUpdateFilterByMap()
filterSettings.setFlags( Qgis::LayerTreeFilterFlag::SkipVisibilityCheck );
}
if ( mInAtlas )
{
filterSettings.setFilterExpressionsContext( createExpressionContext() );
}
mLegendModel->setFilterSettings( &filterSettings );
}
else

View File

@ -80,14 +80,6 @@ void QgsMapHitTest::run()
context.setExtent( extent.boundingBox() );
}
// If not explicitly set, the expression context from the filter settings can be a default constructed one,
// with no scopes, when it is set it is meant to override the context from the map settings (e.g. when
// set from layout items).
if ( mSettings.filterExpressionsContext().scopeCount() > 0 )
{
context.setExpressionContext( mSettings.filterExpressionsContext() );
}
context.expressionContext() << QgsExpressionContextUtils::layerScope( vl );
SymbolSet &usedSymbols = mHitTest[vl->id()];
SymbolSet &usedSymbolsRuleKey = mHitTestRuleKey[vl->id()];
@ -386,13 +378,6 @@ bool QgsMapHitTestTask::run()
QgsRenderContext context = QgsRenderContext::fromMapSettings( mapSettings );
// If not explicitly set, the expression context from the filter settings can be a default constructed one,
// with no scopes, when it is set it is meant to override the context from the map settings (e.g. when
// set from layout items).
if ( mSettings.filterExpressionsContext().scopeCount() > 0 )
{
context.setExpressionContext( mSettings.filterExpressionsContext() );
}
context.setPainter( &painter ); // we are not going to draw anything, but we still need a working painter
std::size_t layerIdx = 0;

View File

@ -1350,6 +1350,8 @@ void TestQgsLegendRenderer::testFilterByExpressionWithContext()
scope->setVariable( QStringLiteral( "test_var" ), QStringLiteral( "test_value" ) );
context.appendScope( scope.release() );
mapSettings.setExpressionContext( context );
// Point layer
QgsLayerTreeLayer *layer = legendModel.rootGroup()->findLayer( mVL3->id() );
QVERIFY( layer );
@ -1357,7 +1359,6 @@ void TestQgsLegendRenderer::testFilterByExpressionWithContext()
QgsLayerTreeFilterSettings filterSettings( mapSettings );
filterSettings.setLayerFilterExpressionsFromLayerTree( root.get() );
filterSettings.setFilterExpressionsContext( context );
legendModel.setFilterSettings( &filterSettings );
QgsLegendSettings settings;