Pass proper expression context on to numeric format widget

This commit is contained in:
Nyall Dawson 2024-08-29 14:21:19 +10:00
parent 995a17a0a0
commit 1eb3d82c11
9 changed files with 91 additions and 7 deletions

View File

@ -40,6 +40,14 @@ Sets the format to show in the widget.
Returns a new format object representing the settings currently configured in the widget.
The caller takes ownership of the returned object.
%End
void registerExpressionContextGenerator( QgsExpressionContextGenerator *generator );
%Docstring
Register an expression context generator class that will be used to retrieve
an expression context for the widget when required.
.. versionadded:: 3.40
%End
signals:

View File

@ -8,7 +8,7 @@
class QgsNumericFormatWidget : QgsPanelWidget
class QgsNumericFormatWidget : QgsPanelWidget, QgsExpressionContextGenerator
{
%Docstring(signature="appended")
Base class for widgets which allow control over the properties of :py:class:`QgsNumericFormat` subclasses
@ -42,6 +42,17 @@ Ownership of the returned object is transferred to the caller
.. seealso:: :py:func:`setFormat`
%End
void registerExpressionContextGenerator( QgsExpressionContextGenerator *generator );
%Docstring
Register an expression context generator class that will be used to retrieve
an expression context for the widget when required.
.. versionadded:: 3.40
%End
virtual QgsExpressionContext createExpressionContext() const;
signals:
void changed();
@ -305,7 +316,7 @@ Constructor for QgsFractionNumericFormatWidget, initially showing the specified
class QgsExpressionBasedNumericFormatWidget : QgsNumericFormatWidget, QgsExpressionContextGenerator
class QgsExpressionBasedNumericFormatWidget : QgsNumericFormatWidget
{
%Docstring(signature="appended")
A widget which allow control over the properties of a :py:class:`QgsExpressionBasedNumericFormat`.

View File

@ -40,6 +40,14 @@ Sets the format to show in the widget.
Returns a new format object representing the settings currently configured in the widget.
The caller takes ownership of the returned object.
%End
void registerExpressionContextGenerator( QgsExpressionContextGenerator *generator );
%Docstring
Register an expression context generator class that will be used to retrieve
an expression context for the widget when required.
.. versionadded:: 3.40
%End
signals:

View File

@ -8,7 +8,7 @@
class QgsNumericFormatWidget : QgsPanelWidget
class QgsNumericFormatWidget : QgsPanelWidget, QgsExpressionContextGenerator
{
%Docstring(signature="appended")
Base class for widgets which allow control over the properties of :py:class:`QgsNumericFormat` subclasses
@ -42,6 +42,17 @@ Ownership of the returned object is transferred to the caller
.. seealso:: :py:func:`setFormat`
%End
void registerExpressionContextGenerator( QgsExpressionContextGenerator *generator );
%Docstring
Register an expression context generator class that will be used to retrieve
an expression context for the widget when required.
.. versionadded:: 3.40
%End
virtual QgsExpressionContext createExpressionContext() const;
signals:
void changed();
@ -305,7 +316,7 @@ Constructor for QgsFractionNumericFormatWidget, initially showing the specified
class QgsExpressionBasedNumericFormatWidget : QgsNumericFormatWidget, QgsExpressionContextGenerator
class QgsExpressionBasedNumericFormatWidget : QgsNumericFormatWidget
{
%Docstring(signature="appended")
A widget which allow control over the properties of a :py:class:`QgsExpressionBasedNumericFormat`.

View File

@ -75,6 +75,13 @@ QgsNumericFormat *QgsNumericFormatSelectorWidget::format() const
return mCurrentFormat->clone();
}
void QgsNumericFormatSelectorWidget::registerExpressionContextGenerator( QgsExpressionContextGenerator *generator )
{
mExpressionContextGenerator = generator;
if ( QgsNumericFormatWidget *w = qobject_cast< QgsNumericFormatWidget * >( stackedWidget->currentWidget() ) )
w->registerExpressionContextGenerator( mExpressionContextGenerator );
}
void QgsNumericFormatSelectorWidget::formatTypeChanged()
{
const QString newId = mCategoryCombo->currentData().toString();
@ -142,6 +149,7 @@ void QgsNumericFormatSelectorWidget::updateFormatWidget()
stackedWidget->setCurrentWidget( w );
// start receiving updates from widget
connect( w, &QgsNumericFormatWidget::changed, this, &QgsNumericFormatSelectorWidget::formatChanged );
w->registerExpressionContextGenerator( mExpressionContextGenerator );
}
else
{

View File

@ -23,6 +23,7 @@
class QgsNumericFormat;
class QgsBasicNumericFormat;
class QgsExpressionContextGenerator;
/**
@ -56,6 +57,13 @@ class GUI_EXPORT QgsNumericFormatSelectorWidget : public QgsPanelWidget, private
*/
QgsNumericFormat *format() const SIP_TRANSFERBACK;
/**
* Register an expression context generator class that will be used to retrieve
* an expression context for the widget when required.
* \since QGIS 3.40
*/
void registerExpressionContextGenerator( QgsExpressionContextGenerator *generator );
signals:
/**
@ -75,6 +83,8 @@ class GUI_EXPORT QgsNumericFormatSelectorWidget : public QgsPanelWidget, private
std::unique_ptr< QgsNumericFormat > mCurrentFormat;
std::unique_ptr< QgsBasicNumericFormat > mPreviewFormat;
QgsExpressionContextGenerator *mExpressionContextGenerator = nullptr;
};
#endif //QGSNUMERICFORMATSELECTORWIDGET_H

View File

@ -26,6 +26,18 @@
#include "qgis.h"
#include <QDialogButtonBox>
void QgsNumericFormatWidget::registerExpressionContextGenerator( QgsExpressionContextGenerator *generator )
{
mExpressionContextGenerator = generator;
}
QgsExpressionContext QgsNumericFormatWidget::createExpressionContext() const
{
if ( mExpressionContextGenerator )
return mExpressionContextGenerator->createExpressionContext();
return QgsExpressionContext();
}
//
// QgsBasicNumericFormatWidget
//
@ -629,7 +641,7 @@ QgsExpressionBasedNumericFormatWidget::QgsExpressionBasedNumericFormatWidget( co
QgsExpressionContext QgsExpressionBasedNumericFormatWidget::createExpressionContext() const
{
QgsExpressionContext context;
QgsExpressionContext context = QgsNumericFormatWidget::createExpressionContext();
QgsExpressionContextScope *scope = new QgsExpressionContextScope();
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "value" ), 1234.5678 ) );
@ -653,3 +665,4 @@ QgsNumericFormat *QgsExpressionBasedNumericFormatWidget::format()
{
return mFormat->clone();
}

View File

@ -31,7 +31,7 @@ class QgsExpressionBasedNumericFormat;
* \brief Base class for widgets which allow control over the properties of QgsNumericFormat subclasses
* \since QGIS 3.12
*/
class GUI_EXPORT QgsNumericFormatWidget : public QgsPanelWidget
class GUI_EXPORT QgsNumericFormatWidget : public QgsPanelWidget, public QgsExpressionContextGenerator
{
Q_OBJECT
@ -59,6 +59,16 @@ class GUI_EXPORT QgsNumericFormatWidget : public QgsPanelWidget
*/
virtual QgsNumericFormat *format() = 0 SIP_TRANSFERBACK;
/**
* Register an expression context generator class that will be used to retrieve
* an expression context for the widget when required.
*
* \since QGIS 3.40
*/
void registerExpressionContextGenerator( QgsExpressionContextGenerator *generator );
QgsExpressionContext createExpressionContext() const override;
signals:
/**
@ -66,6 +76,10 @@ class GUI_EXPORT QgsNumericFormatWidget : public QgsPanelWidget
*/
void changed();
private:
QgsExpressionContextGenerator *mExpressionContextGenerator = nullptr;
};
@ -367,7 +381,7 @@ class GUI_EXPORT QgsFractionNumericFormatWidget : public QgsNumericFormatWidget,
* \brief A widget which allow control over the properties of a QgsExpressionBasedNumericFormat.
* \since QGIS 3.40
*/
class GUI_EXPORT QgsExpressionBasedNumericFormatWidget : public QgsNumericFormatWidget, public QgsExpressionContextGenerator, private Ui::QgsExpressionBasedNumericFormatWidgetBase
class GUI_EXPORT QgsExpressionBasedNumericFormatWidget : public QgsNumericFormatWidget, private Ui::QgsExpressionBasedNumericFormatWidgetBase
{
Q_OBJECT

View File

@ -5684,6 +5684,7 @@ void QgsLinearReferencingSymbolLayerWidget::changeNumberFormat()
QgsNumericFormatSelectorWidget *widget = new QgsNumericFormatSelectorWidget( this );
widget->setPanelTitle( tr( "Number Format" ) );
widget->setFormat( mLayer->numericFormat() );
widget->registerExpressionContextGenerator( this );
connect( widget, &QgsNumericFormatSelectorWidget::changed, this, [ = ]
{
if ( !mBlockChangesSignal )