Skip to content

Commit 55e22b9

Browse files
committedSep 21, 2018
Allow setting the context for widget wrappers
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.
1 parent 33eb295 commit 55e22b9

File tree

3 files changed

+239
-0
lines changed

3 files changed

+239
-0
lines changed
 

‎python/gui/auto_generated/processing/qgsprocessingwidgetwrapper.sip.in

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,79 @@ return a pointer to a context which they have already created and own.
3636
virtual ~QgsProcessingContextGenerator();
3737
};
3838

39+
class QgsProcessingParameterWidgetContext
40+
{
41+
%Docstring
42+
Contains settings which reflect the context in which a Processing parameter widget is shown, e.g., the
43+
parent model algorithm, a linked map canvas, and other relevant information which allows the widget
44+
to fine-tune its behavior.
45+
46+
.. versionadded:: 3.4
47+
%End
48+
49+
%TypeHeaderCode
50+
#include "qgsprocessingwidgetwrapper.h"
51+
%End
52+
public:
53+
54+
QgsProcessingParameterWidgetContext();
55+
%Docstring
56+
Constructor for QgsProcessingParameterWidgetContext.
57+
%End
58+
59+
void setMapCanvas( QgsMapCanvas *canvas );
60+
%Docstring
61+
Sets the map ``canvas`` associated with the widget. This allows the widget to retrieve the current
62+
map scale and other properties from the canvas.
63+
64+
.. seealso:: :py:func:`mapCanvas`
65+
%End
66+
67+
QgsMapCanvas *mapCanvas() const;
68+
%Docstring
69+
Returns the map canvas associated with the widget.
70+
71+
.. seealso:: :py:func:`setMapCanvas`
72+
%End
73+
74+
QgsProcessingModelAlgorithm *model() const;
75+
%Docstring
76+
Returns the model which the parameter widget is associated with.
77+
78+
.. seealso:: :py:func:`setModel`
79+
80+
.. seealso:: :py:func:`modelChildAlgorithmId`
81+
%End
82+
83+
void setModel( QgsProcessingModelAlgorithm *model );
84+
%Docstring
85+
Sets the ``model`` which the parameter widget is associated with.
86+
87+
.. seealso:: :py:func:`model`
88+
89+
.. seealso:: :py:func:`setModelChildAlgorithmId`
90+
%End
91+
92+
QString modelChildAlgorithmId() const;
93+
%Docstring
94+
Returns the child algorithm ID within the model which the parameter widget is associated with.
95+
96+
.. seealso:: :py:func:`setModelChildAlgorithmId`
97+
98+
.. seealso:: :py:func:`model`
99+
%End
100+
101+
void setModelChildAlgorithmId( const QString &id );
102+
%Docstring
103+
Sets the child algorithm ``id`` within the model which the parameter widget is associated with.
104+
105+
.. seealso:: :py:func:`modelChildAlgorithmId`
106+
107+
.. seealso:: :py:func:`setModel`
108+
%End
109+
110+
};
111+
39112
class QgsAbstractProcessingParameterWidgetWrapper : QObject
40113
{
41114
%Docstring
@@ -71,6 +144,26 @@ Constructor for QgsAbstractProcessingParameterWidgetWrapper, for the specified
71144
QgsProcessingGui::WidgetType type() const;
72145
%Docstring
73146
Returns the dialog type for which widgets and labels will be created by this wrapper.
147+
%End
148+
149+
virtual void setWidgetContext( const QgsProcessingParameterWidgetContext &context );
150+
%Docstring
151+
Sets the ``context`` in which the Processing parameter widget is shown, e.g., the
152+
parent model algorithm, a linked map canvas, and other relevant information which allows the widget
153+
to fine-tune its behavior.
154+
155+
Subclasses should take care to call the base class method when reimplementing this method.
156+
157+
.. seealso:: :py:func:`widgetContext`
158+
%End
159+
160+
const QgsProcessingParameterWidgetContext &widgetContext() const;
161+
%Docstring
162+
Returns the context in which the Processing parameter widget is shown, e.g., the
163+
parent model algorithm, a linked map canvas, and other relevant information which allows the widget
164+
to fine-tune its behavior.
165+
166+
.. seealso:: :py:func:`setWidgetContext`
74167
%End
75168

76169
QWidget *createWrappedWidget( QgsProcessingContext &context ) /Factory/;

‎src/gui/processing/qgsprocessingwidgetwrapper.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,45 @@
2323
#include <QLabel>
2424
#include <QHBoxLayout>
2525

26+
//
27+
// QgsProcessingParameterWidgetContext
28+
//
29+
30+
void QgsProcessingParameterWidgetContext::setMapCanvas( QgsMapCanvas *canvas )
31+
{
32+
mMapCanvas = canvas;
33+
}
34+
35+
QgsMapCanvas *QgsProcessingParameterWidgetContext::mapCanvas() const
36+
{
37+
return mMapCanvas;
38+
}
39+
40+
QString QgsProcessingParameterWidgetContext::modelChildAlgorithmId() const
41+
{
42+
return mModelChildAlgorithmId;
43+
}
44+
45+
void QgsProcessingParameterWidgetContext::setModelChildAlgorithmId( const QString &modelChildAlgorithmId )
46+
{
47+
mModelChildAlgorithmId = modelChildAlgorithmId;
48+
}
49+
50+
QgsProcessingModelAlgorithm *QgsProcessingParameterWidgetContext::model() const
51+
{
52+
return mModel;
53+
}
54+
55+
void QgsProcessingParameterWidgetContext::setModel( QgsProcessingModelAlgorithm *model )
56+
{
57+
mModel = model;
58+
}
59+
60+
61+
//
62+
// QgsAbstractProcessingParameterWidgetWrapper
63+
//
64+
2665
QgsAbstractProcessingParameterWidgetWrapper::QgsAbstractProcessingParameterWidgetWrapper( const QgsProcessingParameterDefinition *parameter, QgsProcessingGui::WidgetType type, QObject *parent )
2766
: QObject( parent )
2867
, mType( type )
@@ -35,6 +74,16 @@ QgsProcessingGui::WidgetType QgsAbstractProcessingParameterWidgetWrapper::type()
3574
return mType;
3675
}
3776

77+
void QgsAbstractProcessingParameterWidgetWrapper::setWidgetContext( const QgsProcessingParameterWidgetContext &context )
78+
{
79+
mWidgetContext = context;
80+
}
81+
82+
const QgsProcessingParameterWidgetContext &QgsAbstractProcessingParameterWidgetWrapper::widgetContext() const
83+
{
84+
return mWidgetContext;
85+
}
86+
3887
QWidget *QgsAbstractProcessingParameterWidgetWrapper::createWrappedWidget( QgsProcessingContext &context )
3988
{
4089
if ( mWidget )
@@ -222,3 +271,4 @@ QString QgsProcessingParameterWidgetFactoryInterface::modelerExpressionFormatStr
222271
{
223272
return QString();
224273
}
274+

‎src/gui/processing/qgsprocessingwidgetwrapper.h

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class QgsProcessingModelAlgorithm;
3434
class QLabel;
3535
class QgsPropertyOverrideButton;
3636
class QgsVectorLayer;
37+
class QgsProcessingModelAlgorithm;
38+
class QgsMapCanvas;
3739

3840
/**
3941
* \class QgsProcessingContextGenerator
@@ -59,6 +61,79 @@ class GUI_EXPORT QgsProcessingContextGenerator
5961
virtual ~QgsProcessingContextGenerator() = default;
6062
};
6163

64+
/**
65+
* \ingroup gui
66+
* \class QgsProcessingParameterWidgetContext
67+
* Contains settings which reflect the context in which a Processing parameter widget is shown, e.g., the
68+
* parent model algorithm, a linked map canvas, and other relevant information which allows the widget
69+
* to fine-tune its behavior.
70+
*
71+
* \since QGIS 3.4
72+
*/
73+
class GUI_EXPORT QgsProcessingParameterWidgetContext
74+
{
75+
public:
76+
77+
/**
78+
* Constructor for QgsProcessingParameterWidgetContext.
79+
*/
80+
QgsProcessingParameterWidgetContext() = default;
81+
82+
/**
83+
* Sets the map \a canvas associated with the widget. This allows the widget to retrieve the current
84+
* map scale and other properties from the canvas.
85+
* \see mapCanvas()
86+
*/
87+
void setMapCanvas( QgsMapCanvas *canvas );
88+
89+
/**
90+
* Returns the map canvas associated with the widget.
91+
* \see setMapCanvas()
92+
*/
93+
QgsMapCanvas *mapCanvas() const;
94+
95+
/**
96+
* Returns the model which the parameter widget is associated with.
97+
*
98+
* \see setModel()
99+
* \see modelChildAlgorithmId()
100+
*/
101+
QgsProcessingModelAlgorithm *model() const;
102+
103+
/**
104+
* Sets the \a model which the parameter widget is associated with.
105+
*
106+
* \see model()
107+
* \see setModelChildAlgorithmId()
108+
*/
109+
void setModel( QgsProcessingModelAlgorithm *model );
110+
111+
/**
112+
* Returns the child algorithm ID within the model which the parameter widget is associated with.
113+
*
114+
* \see setModelChildAlgorithmId()
115+
* \see model()
116+
*/
117+
QString modelChildAlgorithmId() const;
118+
119+
/**
120+
* Sets the child algorithm \a id within the model which the parameter widget is associated with.
121+
*
122+
* \see modelChildAlgorithmId()
123+
* \see setModel()
124+
*/
125+
void setModelChildAlgorithmId( const QString &id );
126+
127+
private:
128+
129+
QgsProcessingModelAlgorithm *mModel = nullptr;
130+
131+
QString mModelChildAlgorithmId;
132+
133+
QgsMapCanvas *mMapCanvas = nullptr;
134+
135+
};
136+
62137
/**
63138
* \class QgsAbstractProcessingParameterWidgetWrapper
64139
*
@@ -96,6 +171,26 @@ class GUI_EXPORT QgsAbstractProcessingParameterWidgetWrapper : public QObject
96171
*/
97172
QgsProcessingGui::WidgetType type() const;
98173

174+
/**
175+
* Sets the \a context in which the Processing parameter widget is shown, e.g., the
176+
* parent model algorithm, a linked map canvas, and other relevant information which allows the widget
177+
* to fine-tune its behavior.
178+
*
179+
* Subclasses should take care to call the base class method when reimplementing this method.
180+
*
181+
* \see widgetContext()
182+
*/
183+
virtual void setWidgetContext( const QgsProcessingParameterWidgetContext &context );
184+
185+
/**
186+
* Returns the context in which the Processing parameter widget is shown, e.g., the
187+
* parent model algorithm, a linked map canvas, and other relevant information which allows the widget
188+
* to fine-tune its behavior.
189+
*
190+
* \see setWidgetContext()
191+
*/
192+
const QgsProcessingParameterWidgetContext &widgetContext() const;
193+
99194
/**
100195
* Creates and return a new wrapped widget which allows customization of the parameter's value.
101196
*
@@ -226,6 +321,7 @@ class GUI_EXPORT QgsAbstractProcessingParameterWidgetWrapper : public QObject
226321
protected:
227322

228323
QgsProcessingContextGenerator *mProcessingContextGenerator = nullptr;
324+
QgsProcessingParameterWidgetContext mWidgetContext;
229325

230326
private slots:
231327

0 commit comments

Comments
 (0)
Please sign in to comment.