Skip to content

Commit

Permalink
Pass proper expression context on to numeric format widget
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Sep 3, 2024
1 parent 61c0a44 commit 972b7cf
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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`.
Expand Down
8 changes: 8 additions & 0 deletions src/gui/numericformats/qgsnumericformatselectorwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
{
Expand Down
10 changes: 10 additions & 0 deletions src/gui/numericformats/qgsnumericformatselectorwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

class QgsNumericFormat;
class QgsBasicNumericFormat;
class QgsExpressionContextGenerator;


/**
Expand Down Expand Up @@ -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:

/**
Expand All @@ -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
15 changes: 14 additions & 1 deletion src/gui/numericformats/qgsnumericformatwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down Expand Up @@ -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 ) );
Expand All @@ -653,3 +665,4 @@ QgsNumericFormat *QgsExpressionBasedNumericFormatWidget::format()
{
return mFormat->clone();
}

18 changes: 16 additions & 2 deletions src/gui/numericformats/qgsnumericformatwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -59,13 +59,27 @@ 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:

/**
* Emitted whenever the configuration of the numeric format is changed.
*/
void changed();

private:

QgsExpressionContextGenerator *mExpressionContextGenerator = nullptr;

};


Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions src/gui/symbology/qgssymbollayerwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down

0 comments on commit 972b7cf

Please sign in to comment.