QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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#include "moc_qgsprocessingparameterdefinitionwidget.cpp"
21#include "qgsgui.h"
23#include "qgsapplication.h"
26#include "qgscolorbutton.h"
27#include <QVBoxLayout>
28#include <QLabel>
29#include <QLineEdit>
30#include <QCheckBox>
31#include <QDialog>
32#include <QDialogButtonBox>
33#include <QMessageBox>
34#include <QTabWidget>
35#include <QTextEdit>
36
46
51
56
61
63{
64 return QgsProcessingGuiUtils::createExpressionContext( mContextGenerator, mWidgetContext, nullptr, nullptr );
65}
66
67//
68// QgsProcessingParameterDefinitionWidget
69//
70
72 QgsProcessingContext &context,
73 const QgsProcessingParameterWidgetContext &widgetContext,
74 const QgsProcessingParameterDefinition *definition,
75 const QgsProcessingAlgorithm *algorithm, QWidget *parent )
76 : QWidget( parent )
77 , mType( type )
78{
79 mDefinitionWidget = QgsGui::processingGuiRegistry()->createParameterDefinitionWidget( type, context, widgetContext, definition, algorithm );
80
81 QVBoxLayout *vlayout = new QVBoxLayout();
82
83 QLabel *label = new QLabel( tr( "Description" ) );
84 vlayout->addWidget( label );
85 mDescriptionLineEdit = new QLineEdit();
86 vlayout->addWidget( mDescriptionLineEdit );
87
88 if ( definition )
89 {
90 mDescriptionLineEdit->setText( definition->description() );
91 }
92
93 if ( mDefinitionWidget )
94 vlayout->addWidget( mDefinitionWidget );
95
96 vlayout->addSpacing( 20 );
97 mRequiredCheckBox = new QCheckBox( tr( "Mandatory" ) );
98 if ( definition )
99 mRequiredCheckBox->setChecked( !( definition->flags() & Qgis::ProcessingParameterFlag::Optional ) );
100 else
101 mRequiredCheckBox->setChecked( true );
102 vlayout->addWidget( mRequiredCheckBox );
103
104 mAdvancedCheckBox = new QCheckBox( tr( "Advanced" ) );
105 if ( definition )
106 mAdvancedCheckBox->setChecked( definition->flags() & Qgis::ProcessingParameterFlag::Advanced );
107 else
108 mAdvancedCheckBox->setChecked( false );
109 vlayout->addWidget( mAdvancedCheckBox );
110
111 vlayout->addStretch();
112 setLayout( vlayout );
113}
114
116{
117 std::unique_ptr< QgsProcessingParameterDefinition > param;
119
120 if ( !mRequiredCheckBox->isChecked() )
122 if ( mAdvancedCheckBox->isChecked() )
124
125 if ( mDefinitionWidget )
126 {
127 // if a specific definition widget exists, get it to create the parameter (since it will know
128 // how to set all the additional properties of that parameter, which we don't)
129 param.reset( mDefinitionWidget->createParameter( name, mDescriptionLineEdit->text(), flags ) );
130 }
131 else if ( QgsApplication::processingRegistry()->parameterType( mType ) )
132 {
133 // otherwise, just create a default version of the parameter
134 param.reset( QgsApplication::processingRegistry()->parameterType( mType )->create( name ) );
135 if ( param )
136 {
137 param->setDescription( mDescriptionLineEdit->text() );
138 param->setFlags( flags );
139 }
140 }
141
142 return param.release();
143}
144
146{
147 if ( mDefinitionWidget )
148 {
149 mDefinitionWidget->registerProcessingContextGenerator( generator );
150 }
151}
152
153//
154// QgsProcessingParameterDefinitionDialog
155//
156
158 QgsProcessingContext &context,
159 const QgsProcessingParameterWidgetContext &widgetContext,
160 const QgsProcessingParameterDefinition *definition,
162 QWidget *parent )
163 : QDialog( parent )
164{
165 QVBoxLayout *vLayout = new QVBoxLayout();
166 mTabWidget = new QTabWidget();
167 vLayout->addWidget( mTabWidget );
168
169 QVBoxLayout *vLayout2 = new QVBoxLayout();
170 mWidget = new QgsProcessingParameterDefinitionWidget( type, context, widgetContext, definition, algorithm );
171 vLayout2->addWidget( mWidget );
172 QWidget *w = new QWidget();
173 w->setLayout( vLayout2 );
174 mTabWidget->addTab( w, tr( "Properties" ) );
175
176 QVBoxLayout *commentLayout = new QVBoxLayout();
177 mCommentEdit = new QTextEdit();
178 mCommentEdit->setAcceptRichText( false );
179 commentLayout->addWidget( mCommentEdit, 1 );
180
181 QHBoxLayout *hl = new QHBoxLayout();
182 hl->setContentsMargins( 0, 0, 0, 0 );
183 hl->addWidget( new QLabel( tr( "Color" ) ) );
184 mCommentColorButton = new QgsColorButton();
185 mCommentColorButton->setAllowOpacity( true );
186 mCommentColorButton->setWindowTitle( tr( "Comment Color" ) );
187 mCommentColorButton->setShowNull( true, tr( "Default" ) );
188 hl->addWidget( mCommentColorButton );
189 commentLayout->addLayout( hl );
190
191 QWidget *w2 = new QWidget();
192 w2->setLayout( commentLayout );
193 mTabWidget->addTab( w2, tr( "Comments" ) );
194
195 QDialogButtonBox *bbox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Ok );
196 connect( bbox, &QDialogButtonBox::accepted, this, &QgsProcessingParameterDefinitionDialog::accept );
197 connect( bbox, &QDialogButtonBox::rejected, this, &QgsProcessingParameterDefinitionDialog::reject );
198
199 vLayout->addWidget( bbox );
200 setLayout( vLayout );
201 setWindowTitle( definition ? tr( "%1 Parameter Definition" ).arg( definition->description() )
202 : QgsApplication::processingRegistry()->parameterType( type ) ? tr( "%1 Parameter Definition" ).arg( QgsApplication::processingRegistry()->parameterType( type )->name() ) :
203 tr( "Parameter Definition" ) );
204 setObjectName( QStringLiteral( "QgsProcessingParameterDefinitionDialog" ) );
206}
207
209{
210 return mWidget->createParameter( name );
211}
212
214{
215 mCommentEdit->setPlainText( comments );
216}
217
219{
220 return mCommentEdit->toPlainText();
221}
222
224{
225 if ( color.isValid() )
226 mCommentColorButton->setColor( color );
227 else
228 mCommentColorButton->setToNull();
229}
230
232{
233 return !mCommentColorButton->isNull() ? mCommentColorButton->color() : QColor();
234}
235
237{
238 mTabWidget->setCurrentIndex( 1 );
239 mCommentEdit->setFocus();
240 mCommentEdit->selectAll();
241}
242
250
252{
253 if ( mWidget->mDescriptionLineEdit->text().isEmpty() )
254 {
255 QMessageBox::warning( this, tr( "Unable to define parameter" ),
256 tr( "Invalid parameter name" ) );
257 return;
258 }
259 QDialog::accept();
260}
QFlags< ProcessingParameterFlag > ProcessingParameterFlags
Flags which dictate the behavior of Processing parameters.
Definition qgis.h:3499
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
@ Optional
Parameter is optional.
static QgsProcessingRegistry * processingRegistry()
Returns the application's processing registry, used for managing processing providers,...
A cross platform button subclass for selecting colors.
void setShowNull(bool showNull, const QString &nullString=QString())
Sets whether a set to null (clear) option is shown in the button's drop-down menu.
void setAllowOpacity(bool allowOpacity)
Sets whether opacity modification (transparency) is permitted for the color.
void setToNull()
Sets color to null.
bool isNull() const
Returns true if the current color is null.
void setColor(const QColor &color)
Sets the current color 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:154
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:209
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...
virtual QgsProcessingParameterDefinition * createParameter(const QString &name, const QString &description, Qgis::ProcessingParameterFlags flags) const =0
Returns a new instance of a parameter definition, using the current settings defined in the dialog.
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 allow 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.
void setDescription(const QString &description)
Sets 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,...
QgsProcessingParameterType * parameterType(const QString &id) const
Returns the parameter type registered for id.
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