QGIS API Documentation 3.99.0-Master (d270888f95f)
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
91 if ( mDefinitionWidget )
92 vlayout->addWidget( mDefinitionWidget );
93
94 vlayout->addSpacing( 20 );
95 mRequiredCheckBox = new QCheckBox( tr( "Mandatory" ) );
96 if ( definition )
97 mRequiredCheckBox->setChecked( !( definition->flags() & Qgis::ProcessingParameterFlag::Optional ) );
98 else
99 mRequiredCheckBox->setChecked( true );
100 vlayout->addWidget( mRequiredCheckBox );
101
102 mAdvancedCheckBox = new QCheckBox( tr( "Advanced" ) );
103 if ( definition )
104 mAdvancedCheckBox->setChecked( definition->flags() & Qgis::ProcessingParameterFlag::Advanced );
105 else
106 mAdvancedCheckBox->setChecked( false );
107 vlayout->addWidget( mAdvancedCheckBox );
108
109 vlayout->addStretch();
110 setLayout( vlayout );
111}
112
114{
115 std::unique_ptr<QgsProcessingParameterDefinition> param;
117
118 if ( !mRequiredCheckBox->isChecked() )
120 if ( mAdvancedCheckBox->isChecked() )
122
123 if ( mDefinitionWidget )
124 {
125 // if a specific definition widget exists, get it to create the parameter (since it will know
126 // how to set all the additional properties of that parameter, which we don't)
127 param.reset( mDefinitionWidget->createParameter( name, mDescriptionLineEdit->text(), flags ) );
128 }
129 else if ( QgsApplication::processingRegistry()->parameterType( mType ) )
130 {
131 // otherwise, just create a default version of the parameter
132 param.reset( QgsApplication::processingRegistry()->parameterType( mType )->create( name ) );
133 if ( param )
134 {
135 param->setDescription( mDescriptionLineEdit->text() );
136 param->setFlags( flags );
137 }
138 }
139
140 return param.release();
141}
142
144{
145 if ( mDefinitionWidget )
146 {
147 mDefinitionWidget->registerProcessingContextGenerator( generator );
148 }
149}
150
151//
152// QgsProcessingParameterDefinitionDialog
153//
154
156 : QDialog( parent )
157{
158 QVBoxLayout *vLayout = new QVBoxLayout();
159 mTabWidget = new QTabWidget();
160 vLayout->addWidget( mTabWidget );
161
162 QVBoxLayout *vLayout2 = new QVBoxLayout();
163 mWidget = new QgsProcessingParameterDefinitionWidget( type, context, widgetContext, definition, algorithm );
164 vLayout2->addWidget( mWidget );
165 QWidget *w = new QWidget();
166 w->setLayout( vLayout2 );
167 mTabWidget->addTab( w, tr( "Properties" ) );
168
169 QVBoxLayout *commentLayout = new QVBoxLayout();
170 mCommentEdit = new QTextEdit();
171 mCommentEdit->setAcceptRichText( false );
172 commentLayout->addWidget( mCommentEdit, 1 );
173
174 QHBoxLayout *hl = new QHBoxLayout();
175 hl->setContentsMargins( 0, 0, 0, 0 );
176 hl->addWidget( new QLabel( tr( "Color" ) ) );
177 mCommentColorButton = new QgsColorButton();
178 mCommentColorButton->setAllowOpacity( true );
179 mCommentColorButton->setWindowTitle( tr( "Comment Color" ) );
180 mCommentColorButton->setShowNull( true, tr( "Default" ) );
181 hl->addWidget( mCommentColorButton );
182 commentLayout->addLayout( hl );
183
184 QWidget *w2 = new QWidget();
185 w2->setLayout( commentLayout );
186 mTabWidget->addTab( w2, tr( "Comments" ) );
187
188 QDialogButtonBox *bbox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Ok );
189 connect( bbox, &QDialogButtonBox::accepted, this, &QgsProcessingParameterDefinitionDialog::accept );
190 connect( bbox, &QDialogButtonBox::rejected, this, &QgsProcessingParameterDefinitionDialog::reject );
191
192 vLayout->addWidget( bbox );
193 setLayout( vLayout );
194 setWindowTitle( definition ? tr( "%1 Parameter Definition" ).arg( definition->description() ) : QgsApplication::processingRegistry()->parameterType( type ) ? tr( "%1 Parameter Definition" ).arg( QgsApplication::processingRegistry()->parameterType( type )->name() )
195 : tr( "Parameter Definition" ) );
196 setObjectName( u"QgsProcessingParameterDefinitionDialog"_s );
198}
199
201{
202 return mWidget->createParameter( name );
203}
204
206{
207 mCommentEdit->setPlainText( comments );
208}
209
211{
212 return mCommentEdit->toPlainText();
213}
214
216{
217 if ( color.isValid() )
218 mCommentColorButton->setColor( color );
219 else
220 mCommentColorButton->setToNull();
221}
222
224{
225 return !mCommentColorButton->isNull() ? mCommentColorButton->color() : QColor();
226}
227
229{
230 mTabWidget->setCurrentIndex( 1 );
231 mCommentEdit->setFocus();
232 mCommentEdit->selectAll();
233}
234
236{
237 if ( mWidget )
238 {
239 mWidget->registerProcessingContextGenerator( generator );
240 }
241}
242
244{
245 if ( mWidget->mDescriptionLineEdit->text().isEmpty() )
246 {
247 QMessageBox::warning( this, tr( "Unable to define parameter" ), tr( "Invalid parameter name" ) );
248 return;
249 }
250 QDialog::accept();
251}
QFlags< ProcessingParameterFlag > ProcessingParameterFlags
Flags which dictate the behavior of Processing parameters.
Definition qgis.h:3836
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition qgis.h:3822
@ Optional
Parameter is optional.
Definition qgis.h:3824
static QgsProcessingRegistry * processingRegistry()
Returns the application's processing registry, used for managing processing providers,...
A cross platform button subclass for selecting colors.
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
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...
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 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...
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