QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 "qgsgui.h"
22 #include "qgsapplication.h"
23 #include "qgsprocessingregistry.h"
25 #include "qgscolorbutton.h"
26 #include <QVBoxLayout>
27 #include <QLabel>
28 #include <QLineEdit>
29 #include <QCheckBox>
30 #include <QDialog>
31 #include <QDialogButtonBox>
32 #include <QMessageBox>
33 #include <QTabWidget>
34 #include <QTextEdit>
35 
39  const QgsProcessingAlgorithm *, QWidget *parent )
40  : QWidget( parent )
41  , mWidgetContext( context )
42 {
43 
44 }
45 
47 {
48  mWidgetContext = context;
49 }
50 
52 {
53  return mWidgetContext;
54 }
55 
57 {
58  mContextGenerator = generator;
59 }
60 
62 {
63  return QgsProcessingGuiUtils::createExpressionContext( mContextGenerator, mWidgetContext, nullptr, nullptr );
64 }
65 
66 //
67 // QgsProcessingParameterDefinitionWidget
68 //
69 
71  QgsProcessingContext &context,
72  const QgsProcessingParameterWidgetContext &widgetContext,
73  const QgsProcessingParameterDefinition *definition,
74  const QgsProcessingAlgorithm *algorithm, QWidget *parent )
75  : QWidget( parent )
76  , mType( type )
77 {
78  mDefinitionWidget = QgsGui::processingGuiRegistry()->createParameterDefinitionWidget( type, context, widgetContext, definition, algorithm );
79 
80  QVBoxLayout *vlayout = new QVBoxLayout();
81 
82  QLabel *label = new QLabel( tr( "Description" ) );
83  vlayout->addWidget( label );
84  mDescriptionLineEdit = new QLineEdit();
85  vlayout->addWidget( mDescriptionLineEdit );
86 
87  if ( definition )
88  {
89  mDescriptionLineEdit->setText( definition->description() );
90  }
91 
92  if ( mDefinitionWidget )
93  vlayout->addWidget( mDefinitionWidget );
94 
95  vlayout->addSpacing( 20 );
96  mRequiredCheckBox = new QCheckBox( tr( "Mandatory" ) );
97  if ( definition )
98  mRequiredCheckBox->setChecked( !( definition->flags() & QgsProcessingParameterDefinition::FlagOptional ) );
99  else
100  mRequiredCheckBox->setChecked( true );
101  vlayout->addWidget( mRequiredCheckBox );
102 
103  mAdvancedCheckBox = new QCheckBox( tr( "Advanced" ) );
104  if ( definition )
105  mAdvancedCheckBox->setChecked( definition->flags() & QgsProcessingParameterDefinition::FlagAdvanced );
106  else
107  mAdvancedCheckBox->setChecked( false );
108  vlayout->addWidget( mAdvancedCheckBox );
109 
110  vlayout->addStretch();
111  setLayout( vlayout );
112 }
113 
115 {
116  std::unique_ptr< QgsProcessingParameterDefinition > param;
117  QgsProcessingParameterDefinition::Flags flags = QgsProcessingParameterDefinition::Flags();
118 
119  if ( !mRequiredCheckBox->isChecked() )
121  if ( mAdvancedCheckBox->isChecked() )
123 
124  if ( mDefinitionWidget )
125  {
126  // if a specific definition widget exists, get it to create the parameter (since it will know
127  // how to set all the additional properties of that parameter, which we don't)
128  param.reset( mDefinitionWidget->createParameter( name, mDescriptionLineEdit->text(), flags ) );
129  }
130  else if ( QgsApplication::processingRegistry()->parameterType( mType ) )
131  {
132  // otherwise, just create a default version of the parameter
133  param.reset( QgsApplication::processingRegistry()->parameterType( mType )->create( name ) );
134  if ( param )
135  {
136  param->setDescription( mDescriptionLineEdit->text() );
137  param->setFlags( flags );
138  }
139  }
140 
141  return param.release();
142 }
143 
145 {
146  if ( mDefinitionWidget )
147  {
148  mDefinitionWidget->registerProcessingContextGenerator( generator );
149  }
150 }
151 
152 //
153 // QgsProcessingParameterDefinitionDialog
154 //
155 
157  QgsProcessingContext &context,
158  const QgsProcessingParameterWidgetContext &widgetContext,
159  const QgsProcessingParameterDefinition *definition,
161  QWidget *parent )
162  : QDialog( parent )
163 {
164  QVBoxLayout *vLayout = new QVBoxLayout();
165  mTabWidget = new QTabWidget();
166  vLayout->addWidget( mTabWidget );
167 
168  QVBoxLayout *vLayout2 = new QVBoxLayout();
169  mWidget = new QgsProcessingParameterDefinitionWidget( type, context, widgetContext, definition, algorithm );
170  vLayout2->addWidget( mWidget );
171  QWidget *w = new QWidget();
172  w->setLayout( vLayout2 );
173  mTabWidget->addTab( w, tr( "Properties" ) );
174 
175  QVBoxLayout *commentLayout = new QVBoxLayout();
176  mCommentEdit = new QTextEdit();
177  mCommentEdit->setAcceptRichText( false );
178  commentLayout->addWidget( mCommentEdit, 1 );
179 
180  QHBoxLayout *hl = new QHBoxLayout();
181  hl->setContentsMargins( 0, 0, 0, 0 );
182  hl->addWidget( new QLabel( tr( "Color" ) ) );
183  mCommentColorButton = new QgsColorButton();
184  mCommentColorButton->setAllowOpacity( true );
185  mCommentColorButton->setWindowTitle( tr( "Comment Color" ) );
186  mCommentColorButton->setShowNull( true, tr( "Default" ) );
187  hl->addWidget( mCommentColorButton );
188  commentLayout->addLayout( hl );
189 
190  QWidget *w2 = new QWidget();
191  w2->setLayout( commentLayout );
192  mTabWidget->addTab( w2, tr( "Comments" ) );
193 
194  QDialogButtonBox *bbox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Ok );
195  connect( bbox, &QDialogButtonBox::accepted, this, &QgsProcessingParameterDefinitionDialog::accept );
196  connect( bbox, &QDialogButtonBox::rejected, this, &QgsProcessingParameterDefinitionDialog::reject );
197 
198  vLayout->addWidget( bbox );
199  setLayout( vLayout );
200  setWindowTitle( definition ? tr( "%1 Parameter Definition" ).arg( definition->description() )
201  : QgsApplication::processingRegistry()->parameterType( type ) ? tr( "%1 Parameter Definition" ).arg( QgsApplication::processingRegistry()->parameterType( type )->name() ) :
202  tr( "Parameter Definition" ) );
203  setObjectName( QStringLiteral( "QgsProcessingParameterDefinitionDialog" ) );
205 }
206 
208 {
209  return mWidget->createParameter( name );
210 }
211 
213 {
214  mCommentEdit->setPlainText( comments );
215 }
216 
218 {
219  return mCommentEdit->toPlainText();
220 }
221 
223 {
224  if ( color.isValid() )
225  mCommentColorButton->setColor( color );
226  else
227  mCommentColorButton->setToNull();
228 }
229 
231 {
232  return !mCommentColorButton->isNull() ? mCommentColorButton->color() : QColor();
233 }
234 
236 {
237  mTabWidget->setCurrentIndex( 1 );
238  mCommentEdit->setFocus();
239  mCommentEdit->selectAll();
240 }
241 
243 {
244  if ( mWidget )
245  {
246  mWidget->registerProcessingContextGenerator( generator );
247  }
248 }
249 
251 {
252  if ( mWidget->mDescriptionLineEdit->text().isEmpty() )
253  {
254  QMessageBox::warning( this, tr( "Unable to define parameter" ),
255  tr( "Invalid parameter name" ) );
256  return;
257  }
258  QDialog::accept();
259 }
qgscolorbutton.h
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:406
QgsProcessingParameterWidgetContext
Contains settings which reflect the context in which a Processing parameter widget is shown,...
Definition: qgsprocessingwidgetwrapper.h:115
QgsApplication::processingRegistry
static QgsProcessingRegistry * processingRegistry()
Returns the application's processing registry, used for managing processing providers,...
Definition: qgsapplication.cpp:2455
qgsprocessingparametertype.h
QgsProcessingParameterDefinitionDialog::createParameter
QgsProcessingParameterDefinition * createParameter(const QString &name=QString()) const
Returns a new instance of a parameter definition, using the current settings defined in the dialog.
Definition: qgsprocessingparameterdefinitionwidget.cpp:207
qgsprocessingparameterdefinitionwidget.h
QgsProcessingAbstractParameterDefinitionWidget::QgsProcessingAbstractParameterDefinitionWidget
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.
Definition: qgsprocessingparameterdefinitionwidget.cpp:36
QgsProcessingAbstractParameterDefinitionWidget::widgetContext
const QgsProcessingParameterWidgetContext & widgetContext() const
Returns the context in which the Processing definition widget is shown, e.g., the parent model algori...
Definition: qgsprocessingparameterdefinitionwidget.cpp:51
QgsColorButton::isNull
bool isNull() const
Returns true if the current color is null.
Definition: qgscolorbutton.cpp:851
QgsProcessingParameterDefinition::description
QString description() const
Returns the description for the parameter.
Definition: qgsprocessingparameters.h:502
algorithm
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
QgsProcessingAbstractParameterDefinitionWidget::createExpressionContext
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
Definition: qgsprocessingparameterdefinitionwidget.cpp:61
QgsColorButton::setColor
void setColor(const QColor &color)
Sets the current color for the button.
Definition: qgscolorbutton.cpp:658
qgsgui.h
QgsProcessingParameterDefinitionWidget
A widget which allow users to specify the properties of a Processing parameter.
Definition: qgsprocessingparameterdefinitionwidget.h:125
QgsProcessingParameterDefinition::flags
Flags flags() const
Returns any flags associated with the parameter.
Definition: qgsprocessingparameters.h:594
QgsProcessingParameterDefinition::FlagAdvanced
@ FlagAdvanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition: qgsprocessingparameters.h:451
QgsProcessingParameterDefinition
Base class for the definition of processing parameters.
Definition: qgsprocessingparameters.h:334
QgsProcessingParameterDefinitionWidget::registerProcessingContextGenerator
void registerProcessingContextGenerator(QgsProcessingContextGenerator *generator)
Registers a Processing context generator class that will be used to retrieve a Processing context for...
Definition: qgsprocessingparameterdefinitionwidget.cpp:144
QgsProcessingParameterDefinitionDialog::accept
void accept() override
Definition: qgsprocessingparameterdefinitionwidget.cpp:250
qgsapplication.h
QgsGui::enableAutoGeometryRestore
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:180
QgsColorButton::setAllowOpacity
void setAllowOpacity(bool allowOpacity)
Sets whether opacity modification (transparency) is permitted for the color.
Definition: qgscolorbutton.cpp:805
QgsProcessingAbstractParameterDefinitionWidget::registerProcessingContextGenerator
void registerProcessingContextGenerator(QgsProcessingContextGenerator *generator)
Registers a Processing context generator class that will be used to retrieve a Processing context for...
Definition: qgsprocessingparameterdefinitionwidget.cpp:56
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
QgsProcessingAbstractParameterDefinitionWidget::createParameter
virtual QgsProcessingParameterDefinition * createParameter(const QString &name, const QString &description, QgsProcessingParameterDefinition::Flags flags) const =0
Returns a new instance of a parameter definition, using the current settings defined in the dialog.
QgsProcessingParameterDefinitionDialog::comments
QString comments() const
Returns the comments for the parameter.
Definition: qgsprocessingparameterdefinitionwidget.cpp:217
QgsGui::processingGuiRegistry
static QgsProcessingGuiRegistry * processingGuiRegistry()
Returns the global processing gui registry, used for registering the GUI behavior of processing algor...
Definition: qgsgui.cpp:140
QgsColorButton::color
QColor color
Definition: qgscolorbutton.h:50
QgsProcessingRegistry::parameterType
QgsProcessingParameterType * parameterType(const QString &id) const
Returns the parameter type registered for id.
Definition: qgsprocessingregistry.cpp:227
QgsProcessingAbstractParameterDefinitionWidget::setWidgetContext
virtual void setWidgetContext(const QgsProcessingParameterWidgetContext &context)
Sets the context in which the Processing definition widget is shown, e.g., the parent model algorithm...
Definition: qgsprocessingparameterdefinitionwidget.cpp:46
QgsColorButton::setShowNull
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.
Definition: qgscolorbutton.cpp:840
QgsProcessingAlgorithm
Abstract base class for processing algorithms.
Definition: qgsprocessingalgorithm.h:52
QgsProcessingParameterDefinitionDialog::registerProcessingContextGenerator
void registerProcessingContextGenerator(QgsProcessingContextGenerator *generator)
Registers a Processing context generator class that will be used to retrieve a Processing context for...
Definition: qgsprocessingparameterdefinitionwidget.cpp:242
QgsProcessingGuiRegistry::createParameterDefinitionWidget
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...
Definition: qgsprocessingguiregistry.cpp:172
QgsColorButton
A cross platform button subclass for selecting colors. Will open a color chooser dialog when clicked....
Definition: qgscolorbutton.h:35
QgsProcessingParameterDefinitionDialog::setCommentColor
void setCommentColor(const QColor &color)
Sets the color for the comments for the parameter.
Definition: qgsprocessingparameterdefinitionwidget.cpp:222
QgsProcessingParameterDefinitionDialog::setComments
void setComments(const QString &comments)
Sets the comments for the parameter.
Definition: qgsprocessingparameterdefinitionwidget.cpp:212
QgsProcessingParameterDefinitionDialog::commentColor
QColor commentColor() const
Returns the color for the comments for the parameter.
Definition: qgsprocessingparameterdefinitionwidget.cpp:230
QgsProcessingParameterDefinitionWidget::createParameter
QgsProcessingParameterDefinition * createParameter(const QString &name=QString()) const
Returns a new instance of a parameter definition, using the current settings defined in the dialog.
Definition: qgsprocessingparameterdefinitionwidget.cpp:114
QgsProcessingParameterDefinitionWidget::QgsProcessingParameterDefinitionWidget
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.
Definition: qgsprocessingparameterdefinitionwidget.cpp:70
QgsColorButton::setToNull
void setToNull()
Sets color to null.
Definition: qgscolorbutton.cpp:157
qgsprocessingguiregistry.h
QgsProcessingParameterDefinition::FlagOptional
@ FlagOptional
Parameter is optional.
Definition: qgsprocessingparameters.h:453
QgsProcessingParameterDefinitionDialog::QgsProcessingParameterDefinitionDialog
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.
Definition: qgsprocessingparameterdefinitionwidget.cpp:156
qgsprocessingregistry.h
QgsProcessingContextGenerator
An interface for objects which can create Processing contexts.
Definition: qgsprocessingwidgetwrapper.h:52
QgsProcessingParameterDefinitionDialog::switchToCommentTab
void switchToCommentTab()
Switches the dialog to the comments tab.
Definition: qgsprocessingparameterdefinitionwidget.cpp:235