QGIS API Documentation 3.99.0-Master (26c88405ac0)
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 <QTabWidget>
35#include <QTextEdit>
36#include <QVBoxLayout>
37
38#include "moc_qgsprocessingparameterdefinitionwidget.cpp"
39
45
50
55
60
62{
63 return QgsProcessingWidgetWrapperUtils::createExpressionContext( mContextGenerator, mWidgetContext, nullptr, nullptr );
64}
65
66//
67// QgsProcessingParameterDefinitionWidget
68//
69
71 : QWidget( parent )
72 , mType( type )
73{
74 mDefinitionWidget = QgsGui::processingGuiRegistry()->createParameterDefinitionWidget( type, context, widgetContext, definition, algorithm );
75
76 QVBoxLayout *vlayout = new QVBoxLayout();
77
78 QLabel *label = new QLabel( tr( "Description" ) );
79 vlayout->addWidget( label );
80 mDescriptionLineEdit = new QLineEdit();
81 vlayout->addWidget( mDescriptionLineEdit );
82
83 if ( definition )
84 {
85 mDescriptionLineEdit->setText( definition->description() );
86 }
87
88 if ( mDefinitionWidget )
89 vlayout->addWidget( mDefinitionWidget );
90
91 vlayout->addSpacing( 20 );
92 mRequiredCheckBox = new QCheckBox( tr( "Mandatory" ) );
93 if ( definition )
94 mRequiredCheckBox->setChecked( !( definition->flags() & Qgis::ProcessingParameterFlag::Optional ) );
95 else
96 mRequiredCheckBox->setChecked( true );
97 vlayout->addWidget( mRequiredCheckBox );
98
99 mAdvancedCheckBox = new QCheckBox( tr( "Advanced" ) );
100 if ( definition )
101 mAdvancedCheckBox->setChecked( definition->flags() & Qgis::ProcessingParameterFlag::Advanced );
102 else
103 mAdvancedCheckBox->setChecked( false );
104 vlayout->addWidget( mAdvancedCheckBox );
105
106 vlayout->addStretch();
107 setLayout( vlayout );
108}
109
111{
112 std::unique_ptr<QgsProcessingParameterDefinition> param;
114
115 if ( !mRequiredCheckBox->isChecked() )
117 if ( mAdvancedCheckBox->isChecked() )
119
120 if ( mDefinitionWidget )
121 {
122 // if a specific definition widget exists, get it to create the parameter (since it will know
123 // how to set all the additional properties of that parameter, which we don't)
124 param.reset( mDefinitionWidget->createParameter( name, mDescriptionLineEdit->text(), flags ) );
125 }
126 else if ( QgsApplication::processingRegistry()->parameterType( mType ) )
127 {
128 // otherwise, just create a default version of the parameter
129 param.reset( QgsApplication::processingRegistry()->parameterType( mType )->create( name ) );
130 if ( param )
131 {
132 param->setDescription( mDescriptionLineEdit->text() );
133 param->setFlags( flags );
134 }
135 }
136
137 return param.release();
138}
139
141{
142 if ( mDefinitionWidget )
143 {
144 mDefinitionWidget->registerProcessingContextGenerator( generator );
145 }
146}
147
148//
149// QgsProcessingParameterDefinitionDialog
150//
151
153 : QDialog( parent )
154{
155 QVBoxLayout *vLayout = new QVBoxLayout();
156 mTabWidget = new QTabWidget();
157 vLayout->addWidget( mTabWidget );
158
159 QVBoxLayout *vLayout2 = new QVBoxLayout();
160 mWidget = new QgsProcessingParameterDefinitionWidget( type, context, widgetContext, definition, algorithm );
161 vLayout2->addWidget( mWidget );
162 QWidget *w = new QWidget();
163 w->setLayout( vLayout2 );
164 mTabWidget->addTab( w, tr( "Properties" ) );
165
166 QVBoxLayout *commentLayout = new QVBoxLayout();
167 mCommentEdit = new QTextEdit();
168 mCommentEdit->setAcceptRichText( false );
169 commentLayout->addWidget( mCommentEdit, 1 );
170
171 QHBoxLayout *hl = new QHBoxLayout();
172 hl->setContentsMargins( 0, 0, 0, 0 );
173 hl->addWidget( new QLabel( tr( "Color" ) ) );
174 mCommentColorButton = new QgsColorButton();
175 mCommentColorButton->setAllowOpacity( true );
176 mCommentColorButton->setWindowTitle( tr( "Comment Color" ) );
177 mCommentColorButton->setShowNull( true, tr( "Default" ) );
178 hl->addWidget( mCommentColorButton );
179 commentLayout->addLayout( hl );
180
181 QWidget *w2 = new QWidget();
182 w2->setLayout( commentLayout );
183 mTabWidget->addTab( w2, tr( "Comments" ) );
184
185 QDialogButtonBox *bbox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Ok );
186 connect( bbox, &QDialogButtonBox::accepted, this, &QgsProcessingParameterDefinitionDialog::accept );
187 connect( bbox, &QDialogButtonBox::rejected, this, &QgsProcessingParameterDefinitionDialog::reject );
188
189 vLayout->addWidget( bbox );
190 setLayout( vLayout );
191 setWindowTitle( definition ? tr( "%1 Parameter Definition" ).arg( definition->description() ) : QgsApplication::processingRegistry()->parameterType( type ) ? tr( "%1 Parameter Definition" ).arg( QgsApplication::processingRegistry()->parameterType( type )->name() )
192 : tr( "Parameter Definition" ) );
193 setObjectName( QStringLiteral( "QgsProcessingParameterDefinitionDialog" ) );
195}
196
198{
199 return mWidget->createParameter( name );
200}
201
203{
204 mCommentEdit->setPlainText( comments );
205}
206
208{
209 return mCommentEdit->toPlainText();
210}
211
213{
214 if ( color.isValid() )
215 mCommentColorButton->setColor( color );
216 else
217 mCommentColorButton->setToNull();
218}
219
221{
222 return !mCommentColorButton->isNull() ? mCommentColorButton->color() : QColor();
223}
224
226{
227 mTabWidget->setCurrentIndex( 1 );
228 mCommentEdit->setFocus();
229 mCommentEdit->selectAll();
230}
231
233{
234 if ( mWidget )
235 {
236 mWidget->registerProcessingContextGenerator( generator );
237 }
238}
239
241{
242 if ( mWidget->mDescriptionLineEdit->text().isEmpty() )
243 {
244 QMessageBox::warning( this, tr( "Unable to define parameter" ), tr( "Invalid parameter name" ) );
245 return;
246 }
247 QDialog::accept();
248}
QFlags< ProcessingParameterFlag > ProcessingParameterFlags
Flags which dictate the behavior of Processing parameters.
Definition qgis.h:3777
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition qgis.h:3763
@ Optional
Parameter is optional.
Definition qgis.h:3765
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:166
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:221
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