QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
Loading...
Searching...
No Matches
qgsprocessingparameterdefinitionwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprocessingparameterdefinitionwidget.cpp
3 ------------------------------------------
4 begin : July 2019
5 copyright : (C) 2019 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18
20
21#include "qgsapplication.h"
22#include "qgscolorbutton.h"
23#include "qgsgui.h"
27
28#include <QCheckBox>
29#include <QDialog>
30#include <QDialogButtonBox>
31#include <QLabel>
32#include <QLineEdit>
33#include <QMessageBox>
34#include <QString>
35#include <QTabWidget>
36#include <QTextEdit>
37#include <QVBoxLayout>
38
39#include "moc_qgsprocessingparameterdefinitionwidget.cpp"
40
41using namespace Qt::StringLiterals;
42
49
54
59
64
66{
67 return QgsProcessingWidgetWrapperUtils::createExpressionContext( mContextGenerator, mWidgetContext, nullptr, nullptr );
68}
69
70//
71// QgsProcessingParameterDefinitionWidget
72//
73
75 const QString &type,
76 QgsProcessingContext &context,
77 const QgsProcessingParameterWidgetContext &widgetContext,
78 const QgsProcessingParameterDefinition *definition,
80 QWidget *parent
81)
82 : QWidget( parent )
83 , mType( type )
84{
85 mDefinitionWidget = QgsGui::processingGuiRegistry()->createParameterDefinitionWidget( type, context, widgetContext, definition, algorithm );
86
87 QVBoxLayout *vlayout = new QVBoxLayout();
88
89 QLabel *label = new QLabel( tr( "Description" ) );
90 vlayout->addWidget( label );
91 mDescriptionLineEdit = new QLineEdit();
92 vlayout->addWidget( mDescriptionLineEdit );
93
94 if ( definition )
95 {
96 mDescriptionLineEdit->setText( definition->description() );
97 }
98 connect( mDescriptionLineEdit, &QLineEdit::textEdited, this, &QgsProcessingParameterDefinitionWidget::changed );
99
100 if ( mDefinitionWidget )
101 {
103 vlayout->addWidget( mDefinitionWidget );
104 }
105
106 vlayout->addSpacing( 20 );
107 mRequiredCheckBox = new QCheckBox( tr( "Mandatory" ) );
108 if ( definition )
109 mRequiredCheckBox->setChecked( !( definition->flags() & Qgis::ProcessingParameterFlag::Optional ) );
110 else
111 mRequiredCheckBox->setChecked( true );
112 connect( mRequiredCheckBox, &QCheckBox::toggled, this, &QgsProcessingParameterDefinitionWidget::changed );
113
114 vlayout->addWidget( mRequiredCheckBox );
115
116 mAdvancedCheckBox = new QCheckBox( tr( "Advanced" ) );
117 if ( definition )
118 mAdvancedCheckBox->setChecked( definition->flags() & Qgis::ProcessingParameterFlag::Advanced );
119 else
120 mAdvancedCheckBox->setChecked( false );
121 connect( mAdvancedCheckBox, &QCheckBox::toggled, this, &QgsProcessingParameterDefinitionWidget::changed );
122 vlayout->addWidget( mAdvancedCheckBox );
123
124 vlayout->addStretch();
125 setLayout( vlayout );
126}
127
129{
130 std::unique_ptr<QgsProcessingParameterDefinition> param;
132
133 if ( !mRequiredCheckBox->isChecked() )
135 if ( mAdvancedCheckBox->isChecked() )
137
138 if ( mDefinitionWidget )
139 {
140 // if a specific definition widget exists, get it to create the parameter (since it will know
141 // how to set all the additional properties of that parameter, which we don't)
142 param.reset( mDefinitionWidget->createParameter( name, mDescriptionLineEdit->text(), flags ) );
143 }
144 else if ( QgsApplication::processingRegistry()->parameterType( mType ) )
145 {
146 // otherwise, just create a default version of the parameter
147 param.reset( QgsApplication::processingRegistry()->parameterType( mType )->create( name ) );
148 if ( param )
149 {
150 param->setDescription( mDescriptionLineEdit->text() );
151 param->setFlags( flags );
152 }
153 }
154
155 return param.release();
156}
157
159{
160 if ( mDefinitionWidget )
161 {
162 mDefinitionWidget->registerProcessingContextGenerator( generator );
163 }
164}
165
166
167//
168// QgsProcessingParameterDefinitionPanelWidget
169//
170
172 const QString &type,
173 QgsProcessingContext &context,
174 const QgsProcessingParameterWidgetContext &widgetContext,
175 const QgsProcessingParameterDefinition *definition,
177 QWidget *parent
178)
180{
181 QVBoxLayout *vLayout = new QVBoxLayout();
182 vLayout->setContentsMargins( 0, 0, 0, 0 );
183 mTabWidget = new QTabWidget();
184 vLayout->addWidget( mTabWidget );
185
186 QVBoxLayout *vLayout2 = new QVBoxLayout();
187 mWidget = new QgsProcessingParameterDefinitionWidget( type, context, widgetContext, definition, algorithm );
188
190
191 vLayout2->addWidget( mWidget );
192 QWidget *w = new QWidget();
193 w->setLayout( vLayout2 );
194 mTabWidget->addTab( w, tr( "Properties" ) );
195
196 QVBoxLayout *commentLayout = new QVBoxLayout();
197 mCommentEdit = new QTextEdit();
198 mCommentEdit->setAcceptRichText( false );
199 commentLayout->addWidget( mCommentEdit, 1 );
200
201 connect( mCommentEdit, &QTextEdit::textChanged, this, &QgsProcessingParameterDefinitionPanelWidget::widgetChanged );
202
203 QHBoxLayout *hl = new QHBoxLayout();
204 hl->setContentsMargins( 0, 0, 0, 0 );
205 hl->addWidget( new QLabel( tr( "Color" ) ) );
206 mCommentColorButton = new QgsColorButton();
207 mCommentColorButton->setAllowOpacity( true );
208 mCommentColorButton->setWindowTitle( tr( "Comment Color" ) );
209 mCommentColorButton->setShowNull( true, tr( "Default" ) );
210 hl->addWidget( mCommentColorButton );
211 commentLayout->addLayout( hl );
212
214
215 QWidget *w2 = new QWidget();
216 w2->setLayout( commentLayout );
217 mTabWidget->addTab( w2, tr( "Comments" ) );
218
219 setLayout( vLayout );
221 definition ? tr( "%1 Parameter Definition" ).arg( definition->description() )
222 : QgsApplication::processingRegistry()->parameterType( type ) ? tr( "%1 Parameter Definition" ).arg( QgsApplication::processingRegistry()->parameterType( type )->name() )
223 : tr( "Parameter Definition" )
224 );
225}
226
228{
229 return mWidget->createParameter( name );
230}
231
233{
234 mCommentEdit->setPlainText( comments );
235}
236
238{
239 return mCommentEdit->toPlainText();
240}
241
243{
244 if ( color.isValid() )
245 mCommentColorButton->setColor( color );
246 else
247 mCommentColorButton->setToNull();
248}
249
251{
252 return !mCommentColorButton->isNull() ? mCommentColorButton->color() : QColor();
253}
254
256{
257 mTabWidget->setCurrentIndex( 1 );
258 mCommentEdit->setFocus();
259 mCommentEdit->selectAll();
260}
261
263{
264 if ( mWidget )
265 {
266 mWidget->registerProcessingContextGenerator( generator );
267 }
268}
269
270#if 0
271void QgsProcessingParameterDefinitionPanelWidget::accept()
272{
273 if ( mWidget->mDescriptionLineEdit->text().isEmpty() )
274 {
275 QMessageBox::warning( this, tr( "Unable to define parameter" ), tr( "Invalid parameter name" ) );
276 return;
277 }
278 QDialog::accept();
279}
280#endif
281
282//
283// QgsProcessingParameterDefinitionDialog
284//
285
287 const QString &type,
288 QgsProcessingContext &context,
289 const QgsProcessingParameterWidgetContext &widgetContext,
290 const QgsProcessingParameterDefinition *definition,
292 QWidget *parent
293)
294 : QDialog( parent )
295{
296 QVBoxLayout *vLayout = new QVBoxLayout();
297 mWidget = new QgsProcessingParameterDefinitionPanelWidget( type, context, widgetContext, definition, algorithm );
298 vLayout->addWidget( mWidget );
299
300 connect( mWidget, &QgsPanelWidget::panelAccepted, this, &QgsProcessingParameterDefinitionDialog::reject );
301
302 QDialogButtonBox *bbox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Ok );
303 connect( bbox, &QDialogButtonBox::accepted, this, &QgsProcessingParameterDefinitionDialog::accept );
304 connect( bbox, &QDialogButtonBox::rejected, this, &QgsProcessingParameterDefinitionDialog::reject );
305
306 vLayout->addWidget( bbox );
307 setLayout( vLayout );
308 setWindowTitle(
309 definition ? tr( "%1 Parameter Definition" ).arg( definition->description() )
310 : QgsApplication::processingRegistry()->parameterType( type ) ? tr( "%1 Parameter Definition" ).arg( QgsApplication::processingRegistry()->parameterType( type )->name() )
311 : tr( "Parameter Definition" )
312 );
313 setObjectName( u"QgsProcessingParameterDefinitionDialog"_s );
315}
316
318{
319 return mWidget->createParameter( name );
320}
321
323{
324 mWidget->setComments( comments );
325}
326
328{
329 return mWidget->comments();
330}
331
333{
334 mWidget->setCommentColor( color );
335}
336
338{
339 return mWidget->commentColor();
340}
341
343{
344 mWidget->switchToCommentTab();
345}
346
348{
349 mWidget->registerProcessingContextGenerator( generator );
350}
351
353{
354 if ( mWidget->mWidget->mDescriptionLineEdit->text().isEmpty() )
355 {
356 QMessageBox::warning( this, tr( "Unable to define parameter" ), tr( "Invalid parameter name" ) );
357 return;
358 }
359 QDialog::accept();
360}
QFlags< ProcessingParameterFlag > ProcessingParameterFlags
Flags which dictate the behavior of Processing parameters.
Definition qgis.h:3894
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition qgis.h:3880
@ Optional
Parameter is optional.
Definition qgis.h:3882
static QgsProcessingRegistry * processingRegistry()
Returns the application's processing registry, used for managing processing providers,...
A cross platform button subclass for selecting colors.
void colorChanged(const QColor &color)
Emitted whenever a new color is set for the button.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
static QgsProcessingGuiRegistry * processingGuiRegistry()
Returns the global processing gui registry, used for registering the GUI behavior of processing algor...
Definition qgsgui.cpp:169
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Definition qgsgui.cpp:224
void panelAccepted(QgsPanelWidget *panel)
Emitted when the panel is accepted by the user.
void widgetChanged()
Emitted when the widget state changes.
void setPanelTitle(const QString &panelTitle)
Set the title of the panel when shown in the interface.
void changed()
Emitted whenever the definition of the parameter is changed in the widget.
QgsProcessingAbstractParameterDefinitionWidget(QgsProcessingContext &context, const QgsProcessingParameterWidgetContext &widgetContext, const QgsProcessingParameterDefinition *definition=nullptr, const QgsProcessingAlgorithm *algorithm=nullptr, QWidget *parent=nullptr)
Creates a new QgsProcessingAbstractParameterDefinitionWidget, with the specified parent widget.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
const QgsProcessingParameterWidgetContext & widgetContext() const
Returns the context in which the Processing definition widget is shown, e.g., the parent model algori...
virtual void setWidgetContext(const QgsProcessingParameterWidgetContext &context)
Sets the context in which the Processing definition widget is shown, e.g., the parent model algorithm...
void registerProcessingContextGenerator(QgsProcessingContextGenerator *generator)
Registers a Processing context generator class that will be used to retrieve a Processing context for...
Abstract base class for processing algorithms.
An interface for objects which can create Processing contexts.
Contains information about the context in which a processing algorithm is executed.
QgsProcessingAbstractParameterDefinitionWidget * createParameterDefinitionWidget(const QString &type, QgsProcessingContext &context, const QgsProcessingParameterWidgetContext &widgetContext, const QgsProcessingParameterDefinition *definition=nullptr, const QgsProcessingAlgorithm *algorithm=nullptr)
Creates a new parameter definition widget allowing for configuration of an instance of a specific par...
QgsProcessingModelConfigWidget(QWidget *parent=nullptr)
Constructor for QgsProcessingModelConfigWidget().
void registerProcessingContextGenerator(QgsProcessingContextGenerator *generator)
Registers a Processing context generator class that will be used to retrieve a Processing context for...
void setComments(const QString &comments)
Sets the comments for the parameter.
void switchToCommentTab()
Switches the dialog to the comments tab.
QgsProcessingParameterDefinitionDialog(const QString &type, QgsProcessingContext &context, const QgsProcessingParameterWidgetContext &widgetContext, const QgsProcessingParameterDefinition *definition=nullptr, const QgsProcessingAlgorithm *algorithm=nullptr, QWidget *parent=nullptr)
Constructor for QgsProcessingParameterDefinitionDialog, for a parameter of the specified type.
void setCommentColor(const QColor &color)
Sets the color for the comments for the parameter.
QgsProcessingParameterDefinition * createParameter(const QString &name=QString()) const
Returns a new instance of a parameter definition, using the current settings defined in the dialog.
QString comments() const
Returns the comments for the parameter.
QColor commentColor() const
Returns the color for the comments for the parameter.
A panel widget which allows users to specify the properties of a Processing parameter.
QColor commentColor() const
Returns the color for the comments for the parameter.
QgsProcessingParameterDefinition * createParameter(const QString &name=QString()) const
Returns a new instance of a parameter definition, using the current settings defined in the dialog.
void setComments(const QString &comments)
Sets the comments for the parameter.
void setCommentColor(const QColor &color)
Sets the color for the comments for the parameter.
void switchToCommentTab()
Switches the widget to the comments tab.
QString comments() const
Returns the comments for the parameter.
void registerProcessingContextGenerator(QgsProcessingContextGenerator *generator)
Registers a Processing context generator class that will be used to retrieve a Processing context for...
QgsProcessingParameterDefinitionPanelWidget(const QString &type, QgsProcessingContext &context, const QgsProcessingParameterWidgetContext &widgetContext, const QgsProcessingParameterDefinition *definition=nullptr, const QgsProcessingAlgorithm *algorithm=nullptr, QWidget *parent=nullptr)
Constructor for QgsProcessingParameterDefinitionPanelWidget, for a parameter of the specified type.
A widget which allows users to specify the properties of a Processing parameter.
void registerProcessingContextGenerator(QgsProcessingContextGenerator *generator)
Registers a Processing context generator class that will be used to retrieve a Processing context for...
void changed()
Emitted whenever the definition of the parameter is changed in the widget.
QgsProcessingParameterDefinitionWidget(const QString &type, QgsProcessingContext &context, const QgsProcessingParameterWidgetContext &widgetContext, const QgsProcessingParameterDefinition *definition=nullptr, const QgsProcessingAlgorithm *algorithm=nullptr, QWidget *parent=nullptr)
Constructor for QgsProcessingParameterDefinitionWidget, for a parameter of the specified type.
QgsProcessingParameterDefinition * createParameter(const QString &name=QString()) const
Returns a new instance of a parameter definition, using the current settings defined in the dialog.
Base class for the definition of processing parameters.
QString description() const
Returns the description for the parameter.
Qgis::ProcessingParameterFlags flags() const
Returns any flags associated with the parameter.
Contains settings which reflect the context in which a Processing parameter widget is shown.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into allowing algorithms to be written in pure substantial changes are required in order to port existing x Processing algorithms for QGIS x The most significant changes are outlined not GeoAlgorithm For algorithms which operate on features one by consider subclassing the QgsProcessingFeatureBasedAlgorithm class This class allows much of the boilerplate code for looping over features from a vector layer to be bypassed and instead requires implementation of a processFeature method Ensure that your algorithm(or algorithm 's parent class) implements the new pure virtual createInstance(self) call