Skip to content

Commit

Permalink
Allow setting the context for widget wrappers
Browse files Browse the repository at this point in the history
The context contains settings which reflect the context ini
which a Processing parameter widget is shown, e.g., the
parent model algorithm, a linked map canvas, and other relevant
information which allows the widget to fine-tune its behavior.
  • Loading branch information
nyalldawson committed Sep 21, 2018
1 parent 33eb295 commit 55e22b9
Show file tree
Hide file tree
Showing 3 changed files with 239 additions and 0 deletions.
Expand Up @@ -36,6 +36,79 @@ return a pointer to a context which they have already created and own.
virtual ~QgsProcessingContextGenerator();
};

class QgsProcessingParameterWidgetContext
{
%Docstring
Contains settings which reflect the context in which a Processing parameter widget is shown, e.g., the
parent model algorithm, a linked map canvas, and other relevant information which allows the widget
to fine-tune its behavior.

.. versionadded:: 3.4
%End

%TypeHeaderCode
#include "qgsprocessingwidgetwrapper.h"
%End
public:

QgsProcessingParameterWidgetContext();
%Docstring
Constructor for QgsProcessingParameterWidgetContext.
%End

void setMapCanvas( QgsMapCanvas *canvas );
%Docstring
Sets the map ``canvas`` associated with the widget. This allows the widget to retrieve the current
map scale and other properties from the canvas.

.. seealso:: :py:func:`mapCanvas`
%End

QgsMapCanvas *mapCanvas() const;
%Docstring
Returns the map canvas associated with the widget.

.. seealso:: :py:func:`setMapCanvas`
%End

QgsProcessingModelAlgorithm *model() const;
%Docstring
Returns the model which the parameter widget is associated with.

.. seealso:: :py:func:`setModel`

.. seealso:: :py:func:`modelChildAlgorithmId`
%End

void setModel( QgsProcessingModelAlgorithm *model );
%Docstring
Sets the ``model`` which the parameter widget is associated with.

.. seealso:: :py:func:`model`

.. seealso:: :py:func:`setModelChildAlgorithmId`
%End

QString modelChildAlgorithmId() const;
%Docstring
Returns the child algorithm ID within the model which the parameter widget is associated with.

.. seealso:: :py:func:`setModelChildAlgorithmId`

.. seealso:: :py:func:`model`
%End

void setModelChildAlgorithmId( const QString &id );
%Docstring
Sets the child algorithm ``id`` within the model which the parameter widget is associated with.

.. seealso:: :py:func:`modelChildAlgorithmId`

.. seealso:: :py:func:`setModel`
%End

};

class QgsAbstractProcessingParameterWidgetWrapper : QObject
{
%Docstring
Expand Down Expand Up @@ -71,6 +144,26 @@ Constructor for QgsAbstractProcessingParameterWidgetWrapper, for the specified
QgsProcessingGui::WidgetType type() const;
%Docstring
Returns the dialog type for which widgets and labels will be created by this wrapper.
%End

virtual void setWidgetContext( const QgsProcessingParameterWidgetContext &context );
%Docstring
Sets the ``context`` in which the Processing parameter widget is shown, e.g., the
parent model algorithm, a linked map canvas, and other relevant information which allows the widget
to fine-tune its behavior.

Subclasses should take care to call the base class method when reimplementing this method.

.. seealso:: :py:func:`widgetContext`
%End

const QgsProcessingParameterWidgetContext &widgetContext() const;
%Docstring
Returns the context in which the Processing parameter widget is shown, e.g., the
parent model algorithm, a linked map canvas, and other relevant information which allows the widget
to fine-tune its behavior.

.. seealso:: :py:func:`setWidgetContext`
%End

QWidget *createWrappedWidget( QgsProcessingContext &context ) /Factory/;
Expand Down
50 changes: 50 additions & 0 deletions src/gui/processing/qgsprocessingwidgetwrapper.cpp
Expand Up @@ -23,6 +23,45 @@
#include <QLabel>
#include <QHBoxLayout>

//
// QgsProcessingParameterWidgetContext
//

void QgsProcessingParameterWidgetContext::setMapCanvas( QgsMapCanvas *canvas )
{
mMapCanvas = canvas;
}

QgsMapCanvas *QgsProcessingParameterWidgetContext::mapCanvas() const
{
return mMapCanvas;
}

QString QgsProcessingParameterWidgetContext::modelChildAlgorithmId() const
{
return mModelChildAlgorithmId;
}

void QgsProcessingParameterWidgetContext::setModelChildAlgorithmId( const QString &modelChildAlgorithmId )
{
mModelChildAlgorithmId = modelChildAlgorithmId;
}

QgsProcessingModelAlgorithm *QgsProcessingParameterWidgetContext::model() const
{
return mModel;
}

void QgsProcessingParameterWidgetContext::setModel( QgsProcessingModelAlgorithm *model )
{
mModel = model;
}


//
// QgsAbstractProcessingParameterWidgetWrapper
//

QgsAbstractProcessingParameterWidgetWrapper::QgsAbstractProcessingParameterWidgetWrapper( const QgsProcessingParameterDefinition *parameter, QgsProcessingGui::WidgetType type, QObject *parent )
: QObject( parent )
, mType( type )
Expand All @@ -35,6 +74,16 @@ QgsProcessingGui::WidgetType QgsAbstractProcessingParameterWidgetWrapper::type()
return mType;
}

void QgsAbstractProcessingParameterWidgetWrapper::setWidgetContext( const QgsProcessingParameterWidgetContext &context )
{
mWidgetContext = context;
}

const QgsProcessingParameterWidgetContext &QgsAbstractProcessingParameterWidgetWrapper::widgetContext() const
{
return mWidgetContext;
}

QWidget *QgsAbstractProcessingParameterWidgetWrapper::createWrappedWidget( QgsProcessingContext &context )
{
if ( mWidget )
Expand Down Expand Up @@ -222,3 +271,4 @@ QString QgsProcessingParameterWidgetFactoryInterface::modelerExpressionFormatStr
{
return QString();
}

96 changes: 96 additions & 0 deletions src/gui/processing/qgsprocessingwidgetwrapper.h
Expand Up @@ -34,6 +34,8 @@ class QgsProcessingModelAlgorithm;
class QLabel;
class QgsPropertyOverrideButton;
class QgsVectorLayer;
class QgsProcessingModelAlgorithm;
class QgsMapCanvas;

/**
* \class QgsProcessingContextGenerator
Expand All @@ -59,6 +61,79 @@ class GUI_EXPORT QgsProcessingContextGenerator
virtual ~QgsProcessingContextGenerator() = default;
};

/**
* \ingroup gui
* \class QgsProcessingParameterWidgetContext
* Contains settings which reflect the context in which a Processing parameter widget is shown, e.g., the
* parent model algorithm, a linked map canvas, and other relevant information which allows the widget
* to fine-tune its behavior.
*
* \since QGIS 3.4
*/
class GUI_EXPORT QgsProcessingParameterWidgetContext
{
public:

/**
* Constructor for QgsProcessingParameterWidgetContext.
*/
QgsProcessingParameterWidgetContext() = default;

/**
* Sets the map \a canvas associated with the widget. This allows the widget to retrieve the current
* map scale and other properties from the canvas.
* \see mapCanvas()
*/
void setMapCanvas( QgsMapCanvas *canvas );

/**
* Returns the map canvas associated with the widget.
* \see setMapCanvas()
*/
QgsMapCanvas *mapCanvas() const;

/**
* Returns the model which the parameter widget is associated with.
*
* \see setModel()
* \see modelChildAlgorithmId()
*/
QgsProcessingModelAlgorithm *model() const;

/**
* Sets the \a model which the parameter widget is associated with.
*
* \see model()
* \see setModelChildAlgorithmId()
*/
void setModel( QgsProcessingModelAlgorithm *model );

/**
* Returns the child algorithm ID within the model which the parameter widget is associated with.
*
* \see setModelChildAlgorithmId()
* \see model()
*/
QString modelChildAlgorithmId() const;

/**
* Sets the child algorithm \a id within the model which the parameter widget is associated with.
*
* \see modelChildAlgorithmId()
* \see setModel()
*/
void setModelChildAlgorithmId( const QString &id );

private:

QgsProcessingModelAlgorithm *mModel = nullptr;

QString mModelChildAlgorithmId;

QgsMapCanvas *mMapCanvas = nullptr;

};

/**
* \class QgsAbstractProcessingParameterWidgetWrapper
*
Expand Down Expand Up @@ -96,6 +171,26 @@ class GUI_EXPORT QgsAbstractProcessingParameterWidgetWrapper : public QObject
*/
QgsProcessingGui::WidgetType type() const;

/**
* Sets the \a context in which the Processing parameter widget is shown, e.g., the
* parent model algorithm, a linked map canvas, and other relevant information which allows the widget
* to fine-tune its behavior.
*
* Subclasses should take care to call the base class method when reimplementing this method.
*
* \see widgetContext()
*/
virtual void setWidgetContext( const QgsProcessingParameterWidgetContext &context );

/**
* Returns the context in which the Processing parameter widget is shown, e.g., the
* parent model algorithm, a linked map canvas, and other relevant information which allows the widget
* to fine-tune its behavior.
*
* \see setWidgetContext()
*/
const QgsProcessingParameterWidgetContext &widgetContext() const;

/**
* Creates and return a new wrapped widget which allows customization of the parameter's value.
*
Expand Down Expand Up @@ -226,6 +321,7 @@ class GUI_EXPORT QgsAbstractProcessingParameterWidgetWrapper : public QObject
protected:

QgsProcessingContextGenerator *mProcessingContextGenerator = nullptr;
QgsProcessingParameterWidgetContext mWidgetContext;

private slots:

Expand Down

0 comments on commit 55e22b9

Please sign in to comment.