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 %Docstring
Sets the current text used as filter expression. Sets the current text used as filter expression.
This will update the menu 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 %End
QgsVectorLayer *vectorLayer() const; QgsVectorLayer *vectorLayer() const;

View File

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

View File

@ -20,6 +20,7 @@
#include "qgsapplication.h" #include "qgsapplication.h"
#include "qgsexpressionbuilderdialog.h" #include "qgsexpressionbuilderdialog.h"
#include "qgsexpressioncontextutils.h"
QgsLegendFilterButton::QgsLegendFilterButton( QWidget *parent ) QgsLegendFilterButton::QgsLegendFilterButton( QWidget *parent )
: QToolButton( parent ) : QToolButton( parent )
@ -58,7 +59,14 @@ void QgsLegendFilterButton::onToggle( bool checked )
void QgsLegendFilterButton::onSetLegendFilterExpression() 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() ) if ( dlg.exec() )
{ {
setExpressionText( dlg.expressionText() ); setExpressionText( dlg.expressionText() );
@ -79,6 +87,11 @@ void QgsLegendFilterButton::onSetLegendFilterExpression()
} }
} }
void QgsLegendFilterButton::registerExpressionContextGenerator( QgsExpressionContextGenerator *generator )
{
mExpressionContextGenerator = generator;
}
void QgsLegendFilterButton::onClearFilterExpression() void QgsLegendFilterButton::onClearFilterExpression()
{ {
mClearExpressionAction->setEnabled( false ); mClearExpressionAction->setEnabled( false );

View File

@ -17,8 +17,10 @@
#include <QToolButton> #include <QToolButton>
#include "qgis_gui.h" #include "qgis_gui.h"
#include "qgsexpressioncontext.h"
class QgsVectorLayer; class QgsVectorLayer;
class QgsExpressionContextGenerator;
/** /**
* \ingroup gui * \ingroup gui
@ -52,6 +54,13 @@ class GUI_EXPORT QgsLegendFilterButton: public QToolButton
*/ */
void setExpressionText( const QString &expression ); 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 * Returns the current associated vectorLayer
* May be NULLPTR * May be NULLPTR
@ -76,6 +85,7 @@ class GUI_EXPORT QgsLegendFilterButton: public QToolButton
QAction *mSetExpressionAction = nullptr; QAction *mSetExpressionAction = nullptr;
QAction *mClearExpressionAction = nullptr; QAction *mClearExpressionAction = nullptr;
QString mExpression; QString mExpression;
QgsExpressionContextGenerator *mExpressionContextGenerator = nullptr;
void updateMenu(); void updateMenu();