Add more context to the legend filter expressions in layouts

Fix #53229
This commit is contained in:
Alessandro Pasotti 2023-05-31 09:56:25 +02:00
parent 5d8e1dc79b
commit e3196484e1
4 changed files with 33 additions and 1 deletions

View File

@ -39,6 +39,14 @@ Returns the current text used as filter expression
%Docstring
Sets the current text used as filter expression.
This will update the menu
%End
void registerExpressionContextGenerator( QgsExpressionContextGenerator *generator );
%Docstring
Register an expression context generator class that will be used to retrieve
an expression context for the button when required.
.. versionadded:: 3.32
%End
QgsVectorLayer *vectorLayer() const;

View File

@ -203,6 +203,7 @@ QgsLayoutLegendWidget::QgsLayoutLegendWidget( QgsLayoutItemLegend *legend, QgsMa
mGroupFontButton->registerExpressionContextGenerator( this );
mLayerFontButton->registerExpressionContextGenerator( this );
mItemFontButton->registerExpressionContextGenerator( this );
mExpressionFilterButton->registerExpressionContextGenerator( this );
mTitleFontButton->setLayer( coverageLayer() );
mGroupFontButton->setLayer( coverageLayer() );

View File

@ -20,6 +20,7 @@
#include "qgsapplication.h"
#include "qgsexpressionbuilderdialog.h"
#include "qgsexpressioncontextutils.h"
QgsLegendFilterButton::QgsLegendFilterButton( QWidget *parent )
: QToolButton( parent )
@ -58,7 +59,14 @@ void QgsLegendFilterButton::onToggle( bool checked )
void QgsLegendFilterButton::onSetLegendFilterExpression()
{
QgsExpressionBuilderDialog dlg( mLayer, mExpression );
QgsExpressionContext context;
if ( mExpressionContextGenerator )
context = mExpressionContextGenerator->createExpressionContext();
else
{
context.appendScopes( QgsExpressionContextUtils::globalProjectLayerScopes( mLayer ) );
}
QgsExpressionBuilderDialog dlg( mLayer, mExpression, nullptr, QStringLiteral( "generic" ), context );
if ( dlg.exec() )
{
setExpressionText( dlg.expressionText() );
@ -79,6 +87,11 @@ void QgsLegendFilterButton::onSetLegendFilterExpression()
}
}
void QgsLegendFilterButton::registerExpressionContextGenerator( QgsExpressionContextGenerator *generator )
{
mExpressionContextGenerator = generator;
}
void QgsLegendFilterButton::onClearFilterExpression()
{
mClearExpressionAction->setEnabled( false );

View File

@ -17,8 +17,10 @@
#include <QToolButton>
#include "qgis_gui.h"
#include "qgsexpressioncontext.h"
class QgsVectorLayer;
class QgsExpressionContextGenerator;
/**
* \ingroup gui
@ -52,6 +54,13 @@ class GUI_EXPORT QgsLegendFilterButton: public QToolButton
*/
void setExpressionText( const QString &expression );
/**
* Register an expression context generator class that will be used to retrieve
* an expression context for the button when required.
* \since QGIS 3.32
*/
void registerExpressionContextGenerator( QgsExpressionContextGenerator *generator );
/**
* Returns the current associated vectorLayer
* May be NULLPTR
@ -76,6 +85,7 @@ class GUI_EXPORT QgsLegendFilterButton: public QToolButton
QAction *mSetExpressionAction = nullptr;
QAction *mClearExpressionAction = nullptr;
QString mExpression;
QgsExpressionContextGenerator *mExpressionContextGenerator = nullptr;
void updateMenu();