From 972b7cf62128b59ee97e8099103b831ed277db35 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Thu, 29 Aug 2024 14:21:19 +1000 Subject: [PATCH] Pass proper expression context on to numeric format widget --- .../qgsnumericformatselectorwidget.sip.in | 8 ++++++++ .../qgsnumericformatwidget.sip.in | 15 +++++++++++++-- .../qgsnumericformatselectorwidget.sip.in | 8 ++++++++ .../qgsnumericformatwidget.sip.in | 15 +++++++++++++-- .../qgsnumericformatselectorwidget.cpp | 8 ++++++++ .../qgsnumericformatselectorwidget.h | 10 ++++++++++ .../numericformats/qgsnumericformatwidget.cpp | 15 ++++++++++++++- .../numericformats/qgsnumericformatwidget.h | 18 ++++++++++++++++-- src/gui/symbology/qgssymbollayerwidget.cpp | 1 + 9 files changed, 91 insertions(+), 7 deletions(-) diff --git a/python/PyQt6/gui/auto_generated/numericformats/qgsnumericformatselectorwidget.sip.in b/python/PyQt6/gui/auto_generated/numericformats/qgsnumericformatselectorwidget.sip.in index 678bbf90496de..d7dad870c07ca 100644 --- a/python/PyQt6/gui/auto_generated/numericformats/qgsnumericformatselectorwidget.sip.in +++ b/python/PyQt6/gui/auto_generated/numericformats/qgsnumericformatselectorwidget.sip.in @@ -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: diff --git a/python/PyQt6/gui/auto_generated/numericformats/qgsnumericformatwidget.sip.in b/python/PyQt6/gui/auto_generated/numericformats/qgsnumericformatwidget.sip.in index 13e3fedfae7e4..50005baee7952 100644 --- a/python/PyQt6/gui/auto_generated/numericformats/qgsnumericformatwidget.sip.in +++ b/python/PyQt6/gui/auto_generated/numericformats/qgsnumericformatwidget.sip.in @@ -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`. diff --git a/python/gui/auto_generated/numericformats/qgsnumericformatselectorwidget.sip.in b/python/gui/auto_generated/numericformats/qgsnumericformatselectorwidget.sip.in index 678bbf90496de..d7dad870c07ca 100644 --- a/python/gui/auto_generated/numericformats/qgsnumericformatselectorwidget.sip.in +++ b/python/gui/auto_generated/numericformats/qgsnumericformatselectorwidget.sip.in @@ -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: diff --git a/python/gui/auto_generated/numericformats/qgsnumericformatwidget.sip.in b/python/gui/auto_generated/numericformats/qgsnumericformatwidget.sip.in index 13e3fedfae7e4..50005baee7952 100644 --- a/python/gui/auto_generated/numericformats/qgsnumericformatwidget.sip.in +++ b/python/gui/auto_generated/numericformats/qgsnumericformatwidget.sip.in @@ -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`. diff --git a/src/gui/numericformats/qgsnumericformatselectorwidget.cpp b/src/gui/numericformats/qgsnumericformatselectorwidget.cpp index 214f212981a68..3331dd2434580 100644 --- a/src/gui/numericformats/qgsnumericformatselectorwidget.cpp +++ b/src/gui/numericformats/qgsnumericformatselectorwidget.cpp @@ -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 { diff --git a/src/gui/numericformats/qgsnumericformatselectorwidget.h b/src/gui/numericformats/qgsnumericformatselectorwidget.h index b5585dc6c2fe3..7bdddef0d7478 100644 --- a/src/gui/numericformats/qgsnumericformatselectorwidget.h +++ b/src/gui/numericformats/qgsnumericformatselectorwidget.h @@ -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 diff --git a/src/gui/numericformats/qgsnumericformatwidget.cpp b/src/gui/numericformats/qgsnumericformatwidget.cpp index 6ae4b1ec4b675..aef12dca079e5 100644 --- a/src/gui/numericformats/qgsnumericformatwidget.cpp +++ b/src/gui/numericformats/qgsnumericformatwidget.cpp @@ -26,6 +26,18 @@ #include "qgis.h" #include +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(); } + diff --git a/src/gui/numericformats/qgsnumericformatwidget.h b/src/gui/numericformats/qgsnumericformatwidget.h index aa4c8f25cfda3..69461fd69a7e8 100644 --- a/src/gui/numericformats/qgsnumericformatwidget.h +++ b/src/gui/numericformats/qgsnumericformatwidget.h @@ -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 diff --git a/src/gui/symbology/qgssymbollayerwidget.cpp b/src/gui/symbology/qgssymbollayerwidget.cpp index 21c8b061b6a23..652d5f28e09ac 100644 --- a/src/gui/symbology/qgssymbollayerwidget.cpp +++ b/src/gui/symbology/qgssymbollayerwidget.cpp @@ -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 )