QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 {
42 
43 }
44 
45 //
46 // QgsProcessingParameterDefinitionWidget
47 //
48 
50  QgsProcessingContext &context,
51  const QgsProcessingParameterWidgetContext &widgetContext,
52  const QgsProcessingParameterDefinition *definition,
53  const QgsProcessingAlgorithm *algorithm, QWidget *parent )
54  : QWidget( parent )
55  , mType( type )
56 {
57  mDefinitionWidget = QgsGui::instance()->processingGuiRegistry()->createParameterDefinitionWidget( type, context, widgetContext, definition, algorithm );
58 
59  QVBoxLayout *vlayout = new QVBoxLayout();
60 
61  QLabel *label = new QLabel( tr( "Description" ) );
62  vlayout->addWidget( label );
63  mDescriptionLineEdit = new QLineEdit();
64  vlayout->addWidget( mDescriptionLineEdit );
65 
66  if ( definition )
67  {
68  mDescriptionLineEdit->setText( definition->description() );
69  }
70 
71  if ( mDefinitionWidget )
72  vlayout->addWidget( mDefinitionWidget );
73 
74  vlayout->addSpacing( 20 );
75  mRequiredCheckBox = new QCheckBox( tr( "Mandatory" ) );
76  if ( definition )
77  mRequiredCheckBox->setChecked( !( definition->flags() & QgsProcessingParameterDefinition::FlagOptional ) );
78  else
79  mRequiredCheckBox->setChecked( true );
80  vlayout->addWidget( mRequiredCheckBox );
81 
82  mAdvancedCheckBox = new QCheckBox( tr( "Advanced" ) );
83  if ( definition )
84  mAdvancedCheckBox->setChecked( definition->flags() & QgsProcessingParameterDefinition::FlagAdvanced );
85  else
86  mAdvancedCheckBox->setChecked( false );
87  vlayout->addWidget( mAdvancedCheckBox );
88 
89  vlayout->addStretch();
90  setLayout( vlayout );
91 }
92 
94 {
95  std::unique_ptr< QgsProcessingParameterDefinition > param;
96  QgsProcessingParameterDefinition::Flags flags = QgsProcessingParameterDefinition::Flags();
97 
98  if ( !mRequiredCheckBox->isChecked() )
100  if ( mAdvancedCheckBox->isChecked() )
102 
103  if ( mDefinitionWidget )
104  {
105  // if a specific definition widget exists, get it to create the parameter (since it will know
106  // how to set all the additional properties of that parameter, which we don't)
107  param.reset( mDefinitionWidget->createParameter( name, mDescriptionLineEdit->text(), flags ) );
108  }
109  else if ( QgsApplication::processingRegistry()->parameterType( mType ) )
110  {
111  // otherwise, just create a default version of the parameter
112  param.reset( QgsApplication::processingRegistry()->parameterType( mType )->create( name ) );
113  if ( param )
114  {
115  param->setDescription( mDescriptionLineEdit->text() );
116  param->setFlags( flags );
117  }
118  }
119 
120  return param.release();
121 }
122 
123 //
124 // QgsProcessingParameterDefinitionDialog
125 //
126 
128  QgsProcessingContext &context,
129  const QgsProcessingParameterWidgetContext &widgetContext,
130  const QgsProcessingParameterDefinition *definition,
132  QWidget *parent )
133  : QDialog( parent )
134 {
135  QVBoxLayout *vLayout = new QVBoxLayout();
136  mTabWidget = new QTabWidget();
137  vLayout->addWidget( mTabWidget );
138 
139  QVBoxLayout *vLayout2 = new QVBoxLayout();
140  mWidget = new QgsProcessingParameterDefinitionWidget( type, context, widgetContext, definition, algorithm );
141  vLayout2->addWidget( mWidget );
142  QWidget *w = new QWidget();
143  w->setLayout( vLayout2 );
144  mTabWidget->addTab( w, tr( "Properties" ) );
145 
146  QVBoxLayout *commentLayout = new QVBoxLayout();
147  mCommentEdit = new QTextEdit();
148  mCommentEdit->setAcceptRichText( false );
149  commentLayout->addWidget( mCommentEdit, 1 );
150 
151  QHBoxLayout *hl = new QHBoxLayout();
152  hl->setContentsMargins( 0, 0, 0, 0 );
153  hl->addWidget( new QLabel( tr( "Color" ) ) );
154  mCommentColorButton = new QgsColorButton();
155  mCommentColorButton->setAllowOpacity( true );
156  mCommentColorButton->setWindowTitle( tr( "Comment Color" ) );
157  mCommentColorButton->setShowNull( true, tr( "Default" ) );
158  hl->addWidget( mCommentColorButton );
159  commentLayout->addLayout( hl );
160 
161  QWidget *w2 = new QWidget();
162  w2->setLayout( commentLayout );
163  mTabWidget->addTab( w2, tr( "Comments" ) );
164 
165  QDialogButtonBox *bbox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Ok );
166  connect( bbox, &QDialogButtonBox::accepted, this, &QgsProcessingParameterDefinitionDialog::accept );
167  connect( bbox, &QDialogButtonBox::rejected, this, &QgsProcessingParameterDefinitionDialog::reject );
168 
169  vLayout->addWidget( bbox );
170  setLayout( vLayout );
171  setWindowTitle( definition ? tr( "%1 Parameter Definition" ).arg( definition->description() )
172  : QgsApplication::processingRegistry()->parameterType( type ) ? tr( "%1 Parameter Definition" ).arg( QgsApplication::processingRegistry()->parameterType( type )->name() ) :
173  tr( "Parameter Definition" ) );
174  setObjectName( QStringLiteral( "QgsProcessingParameterDefinitionDialog" ) );
176 }
177 
179 {
180  return mWidget->createParameter( name );
181 }
182 
184 {
185  mCommentEdit->setPlainText( comments );
186 }
187 
189 {
190  return mCommentEdit->toPlainText();
191 }
192 
194 {
195  if ( color.isValid() )
196  mCommentColorButton->setColor( color );
197  else
198  mCommentColorButton->setToNull();
199 }
200 
202 {
203  return !mCommentColorButton->isNull() ? mCommentColorButton->color() : QColor();
204 }
205 
207 {
208  mTabWidget->setCurrentIndex( 1 );
209  mCommentEdit->setFocus();
210  mCommentEdit->selectAll();
211 }
212 
214 {
215  if ( mWidget->mDescriptionLineEdit->text().isEmpty() )
216  {
217  QMessageBox::warning( this, tr( "Unable to define parameter" ),
218  tr( "Invalid parameter name" ) );
219  return;
220  }
221  QDialog::accept();
222 }
qgscolorbutton.h
QgsProcessingParameterWidgetContext
Contains settings which reflect the context in which a Processing parameter widget is shown,...
Definition: qgsprocessingwidgetwrapper.h:100
QgsApplication::processingRegistry
static QgsProcessingRegistry * processingRegistry()
Returns the application's processing registry, used for managing processing providers,...
Definition: qgsapplication.cpp:2253
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:178
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
QgsColorButton::isNull
bool isNull() const
Returns true if the current color is null.
Definition: qgscolorbutton.cpp:839
QgsProcessingParameterDefinition::description
QString description() const
Returns the description for the parameter.
Definition: qgsprocessingparameters.h:474
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
QgsColorButton::setColor
void setColor(const QColor &color)
Sets the current color for the button.
Definition: qgscolorbutton.cpp:646
qgsgui.h
QgsProcessingParameterDefinitionWidget
A widget which allow users to specify the properties of a Processing parameter.
Definition: qgsprocessingparameterdefinitionwidget.h:86
QgsProcessingParameterDefinition::flags
Flags flags() const
Returns any flags associated with the parameter.
Definition: qgsprocessingparameters.h:522
QgsProcessingParameterDefinition::FlagAdvanced
@ FlagAdvanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition: qgsprocessingparameters.h:423
QgsProcessingParameterDefinition
Base class for the definition of processing parameters.
Definition: qgsprocessingparameters.h:331
QgsProcessingParameterDefinitionDialog::accept
void accept() override
Definition: qgsprocessingparameterdefinitionwidget.cpp:213
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:139
QgsColorButton::setAllowOpacity
void setAllowOpacity(bool allowOpacity)
Sets whether opacity modification (transparency) is permitted for the color.
Definition: qgscolorbutton.cpp:793
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:44
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:188
QgsGui::processingGuiRegistry
static QgsProcessingGuiRegistry * processingGuiRegistry()
Returns the global processing gui registry, used for registering the GUI behavior of processing algor...
Definition: qgsgui.cpp:104
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:220
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:828
QgsGui::instance
static QgsGui * instance()
Returns a pointer to the singleton instance.
Definition: qgsgui.cpp:63
QgsProcessingAlgorithm
Abstract base class for processing algorithms.
Definition: qgsprocessingalgorithm.h:52
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:163
QgsColorButton
A cross platform button subclass for selecting colors.
Definition: qgscolorbutton.h:36
QgsProcessingParameterDefinitionDialog::setCommentColor
void setCommentColor(const QColor &color)
Sets the color for the comments for the parameter.
Definition: qgsprocessingparameterdefinitionwidget.cpp:193
QgsProcessingParameterDefinitionDialog::setComments
void setComments(const QString &comments)
Sets the comments for the parameter.
Definition: qgsprocessingparameterdefinitionwidget.cpp:183
QgsProcessingParameterDefinitionDialog::commentColor
QColor commentColor() const
Returns the color for the comments for the parameter.
Definition: qgsprocessingparameterdefinitionwidget.cpp:201
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:93
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:49
QgsColorButton::setToNull
void setToNull()
Sets color to null.
Definition: qgscolorbutton.cpp:160
qgsprocessingguiregistry.h
QgsProcessingParameterDefinition::FlagOptional
@ FlagOptional
Parameter is optional.
Definition: qgsprocessingparameters.h:425
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:127
qgsprocessingregistry.h
QgsProcessingParameterDefinitionDialog::switchToCommentTab
void switchToCommentTab()
Switches the dialog to the comments tab.
Definition: qgsprocessingparameterdefinitionwidget.cpp:206