QGIS API Documentation 3.99.0-Master (a8f284845db)
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
48
53
58
63
65{
66 return QgsProcessingWidgetWrapperUtils::createExpressionContext( mContextGenerator, mWidgetContext, nullptr, nullptr );
67}
68
69//
70// QgsProcessingParameterDefinitionWidget
71//
72
74 : QWidget( parent )
75 , mType( type )
76{
77 mDefinitionWidget = QgsGui::processingGuiRegistry()->createParameterDefinitionWidget( type, context, widgetContext, definition, algorithm );
78
79 QVBoxLayout *vlayout = new QVBoxLayout();
80
81 QLabel *label = new QLabel( tr( "Description" ) );
82 vlayout->addWidget( label );
83 mDescriptionLineEdit = new QLineEdit();
84 vlayout->addWidget( mDescriptionLineEdit );
85
86 if ( definition )
87 {
88 mDescriptionLineEdit->setText( definition->description() );
89 }
90 connect( mDescriptionLineEdit, &QLineEdit::textEdited, this, &QgsProcessingParameterDefinitionWidget::changed );
91
92 if ( mDefinitionWidget )
93 {
95 vlayout->addWidget( mDefinitionWidget );
96 }
97
98 vlayout->addSpacing( 20 );
99 mRequiredCheckBox = new QCheckBox( tr( "Mandatory" ) );
100 if ( definition )
101 mRequiredCheckBox->setChecked( !( definition->flags() & Qgis::ProcessingParameterFlag::Optional ) );
102 else
103 mRequiredCheckBox->setChecked( true );
104 connect( mRequiredCheckBox, &QCheckBox::toggled, this, &QgsProcessingParameterDefinitionWidget::changed );
105
106 vlayout->addWidget( mRequiredCheckBox );
107
108 mAdvancedCheckBox = new QCheckBox( tr( "Advanced" ) );
109 if ( definition )
110 mAdvancedCheckBox->setChecked( definition->flags() & Qgis::ProcessingParameterFlag::Advanced );
111 else
112 mAdvancedCheckBox->setChecked( false );
113 connect( mAdvancedCheckBox, &QCheckBox::toggled, this, &QgsProcessingParameterDefinitionWidget::changed );
114 vlayout->addWidget( mAdvancedCheckBox );
115
116 vlayout->addStretch();
117 setLayout( vlayout );
118}
119
121{
122 std::unique_ptr<QgsProcessingParameterDefinition> param;
124
125 if ( !mRequiredCheckBox->isChecked() )
127 if ( mAdvancedCheckBox->isChecked() )
129
130 if ( mDefinitionWidget )
131 {
132 // if a specific definition widget exists, get it to create the parameter (since it will know
133 // how to set all the additional properties of that parameter, which we don't)
134 param.reset( mDefinitionWidget->createParameter( name, mDescriptionLineEdit->text(), flags ) );
135 }
136 else if ( QgsApplication::processingRegistry()->parameterType( mType ) )
137 {
138 // otherwise, just create a default version of the parameter
139 param.reset( QgsApplication::processingRegistry()->parameterType( mType )->create( name ) );
140 if ( param )
141 {
142 param->setDescription( mDescriptionLineEdit->text() );
143 param->setFlags( flags );
144 }
145 }
146
147 return param.release();
148}
149
151{
152 if ( mDefinitionWidget )
153 {
154 mDefinitionWidget->registerProcessingContextGenerator( generator );
155 }
156}
157
158
159//
160// QgsProcessingParameterDefinitionPanelWidget
161//
162
165{
166 QVBoxLayout *vLayout = new QVBoxLayout();
167 vLayout->setContentsMargins( 0, 0, 0, 0 );
168 mTabWidget = new QTabWidget();
169 vLayout->addWidget( mTabWidget );
170
171 QVBoxLayout *vLayout2 = new QVBoxLayout();
172 mWidget = new QgsProcessingParameterDefinitionWidget( type, context, widgetContext, definition, algorithm );
173
175
176 vLayout2->addWidget( mWidget );
177 QWidget *w = new QWidget();
178 w->setLayout( vLayout2 );
179 mTabWidget->addTab( w, tr( "Properties" ) );
180
181 QVBoxLayout *commentLayout = new QVBoxLayout();
182 mCommentEdit = new QTextEdit();
183 mCommentEdit->setAcceptRichText( false );
184 commentLayout->addWidget( mCommentEdit, 1 );
185
186 connect( mCommentEdit, &QTextEdit::textChanged, this, &QgsProcessingParameterDefinitionPanelWidget::widgetChanged );
187
188 QHBoxLayout *hl = new QHBoxLayout();
189 hl->setContentsMargins( 0, 0, 0, 0 );
190 hl->addWidget( new QLabel( tr( "Color" ) ) );
191 mCommentColorButton = new QgsColorButton();
192 mCommentColorButton->setAllowOpacity( true );
193 mCommentColorButton->setWindowTitle( tr( "Comment Color" ) );
194 mCommentColorButton->setShowNull( true, tr( "Default" ) );
195 hl->addWidget( mCommentColorButton );
196 commentLayout->addLayout( hl );
197
199
200 QWidget *w2 = new QWidget();
201 w2->setLayout( commentLayout );
202 mTabWidget->addTab( w2, tr( "Comments" ) );
203
204 setLayout( vLayout );
205 setPanelTitle( definition ? tr( "%1 Parameter Definition" ).arg( definition->description() ) : QgsApplication::processingRegistry()->parameterType( type ) ? tr( "%1 Parameter Definition" ).arg( QgsApplication::processingRegistry()->parameterType( type )->name() )
206 : tr( "Parameter Definition" ) );
207}
208
210{
211 return mWidget->createParameter( name );
212}
213
215{
216 mCommentEdit->setPlainText( comments );
217}
218
220{
221 return mCommentEdit->toPlainText();
222}
223
225{
226 if ( color.isValid() )
227 mCommentColorButton->setColor( color );
228 else
229 mCommentColorButton->setToNull();
230}
231
233{
234 return !mCommentColorButton->isNull() ? mCommentColorButton->color() : QColor();
235}
236
238{
239 mTabWidget->setCurrentIndex( 1 );
240 mCommentEdit->setFocus();
241 mCommentEdit->selectAll();
242}
243
245{
246 if ( mWidget )
247 {
248 mWidget->registerProcessingContextGenerator( generator );
249 }
250}
251
252#if 0
253void QgsProcessingParameterDefinitionPanelWidget::accept()
254{
255 if ( mWidget->mDescriptionLineEdit->text().isEmpty() )
256 {
257 QMessageBox::warning( this, tr( "Unable to define parameter" ), tr( "Invalid parameter name" ) );
258 return;
259 }
260 QDialog::accept();
261}
262#endif
263
264//
265// QgsProcessingParameterDefinitionDialog
266//
267
269 : QDialog( parent )
270{
271 QVBoxLayout *vLayout = new QVBoxLayout();
272 mWidget = new QgsProcessingParameterDefinitionPanelWidget( type, context, widgetContext, definition, algorithm );
273 vLayout->addWidget( mWidget );
274
275 connect( mWidget, &QgsPanelWidget::panelAccepted, this, &QgsProcessingParameterDefinitionDialog::reject );
276
277 QDialogButtonBox *bbox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Ok );
278 connect( bbox, &QDialogButtonBox::accepted, this, &QgsProcessingParameterDefinitionDialog::accept );
279 connect( bbox, &QDialogButtonBox::rejected, this, &QgsProcessingParameterDefinitionDialog::reject );
280
281 vLayout->addWidget( bbox );
282 setLayout( vLayout );
283 setWindowTitle( definition ? tr( "%1 Parameter Definition" ).arg( definition->description() ) : QgsApplication::processingRegistry()->parameterType( type ) ? tr( "%1 Parameter Definition" ).arg( QgsApplication::processingRegistry()->parameterType( type )->name() )
284 : tr( "Parameter Definition" ) );
285 setObjectName( u"QgsProcessingParameterDefinitionDialog"_s );
287}
288
290{
291 return mWidget->createParameter( name );
292}
293
295{
296 mWidget->setComments( comments );
297}
298
300{
301 return mWidget->comments();
302}
303
305{
306 mWidget->setCommentColor( color );
307}
308
310{
311 return mWidget->commentColor();
312}
313
315{
316 mWidget->switchToCommentTab();
317}
318
320{
321 mWidget->registerProcessingContextGenerator( generator );
322}
323
325{
326 if ( mWidget->mWidget->mDescriptionLineEdit->text().isEmpty() )
327 {
328 QMessageBox::warning( this, tr( "Unable to define parameter" ), tr( "Invalid parameter name" ) );
329 return;
330 }
331 QDialog::accept();
332}
QFlags< ProcessingParameterFlag > ProcessingParameterFlags
Flags which dictate the behavior of Processing parameters.
Definition qgis.h:3863
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition qgis.h:3849
@ Optional
Parameter is optional.
Definition qgis.h:3851
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