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