QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 <QVBoxLayout>
26 #include <QLabel>
27 #include <QLineEdit>
28 #include <QCheckBox>
29 #include <QDialog>
30 #include <QDialogButtonBox>
31 #include <QMessageBox>
32 
36  const QgsProcessingAlgorithm *, QWidget *parent )
37  : QWidget( parent )
38 {
39 
40 }
41 
42 //
43 // QgsProcessingParameterDefinitionWidget
44 //
45 
47  QgsProcessingContext &context,
48  const QgsProcessingParameterWidgetContext &widgetContext,
49  const QgsProcessingParameterDefinition *definition,
50  const QgsProcessingAlgorithm *algorithm, QWidget *parent )
51  : QWidget( parent )
52  , mType( type )
53 {
54  mDefinitionWidget = QgsGui::instance()->processingGuiRegistry()->createParameterDefinitionWidget( type, context, widgetContext, definition, algorithm );
55 
56  QVBoxLayout *vlayout = new QVBoxLayout();
57 
58  QLabel *label = new QLabel( tr( "Description" ) );
59  vlayout->addWidget( label );
60  mDescriptionLineEdit = new QLineEdit();
61  vlayout->addWidget( mDescriptionLineEdit );
62 
63  if ( definition )
64  {
65  mDescriptionLineEdit->setText( definition->description() );
66  }
67 
68  if ( mDefinitionWidget )
69  vlayout->addWidget( mDefinitionWidget );
70 
71  vlayout->addSpacing( 20 );
72  mRequiredCheckBox = new QCheckBox( tr( "Mandatory" ) );
73  if ( definition )
74  mRequiredCheckBox->setChecked( !( definition->flags() & QgsProcessingParameterDefinition::FlagOptional ) );
75  else
76  mRequiredCheckBox->setChecked( true );
77  vlayout->addWidget( mRequiredCheckBox );
78 
79  mAdvancedCheckBox = new QCheckBox( tr( "Advanced" ) );
80  if ( definition )
81  mAdvancedCheckBox->setChecked( definition->flags() & QgsProcessingParameterDefinition::FlagAdvanced );
82  else
83  mAdvancedCheckBox->setChecked( false );
84  vlayout->addWidget( mAdvancedCheckBox );
85 
86  vlayout->addStretch();
87  setLayout( vlayout );
88 }
89 
91 {
92  std::unique_ptr< QgsProcessingParameterDefinition > param;
93  QgsProcessingParameterDefinition::Flags flags = nullptr;
94 
95  if ( !mRequiredCheckBox->isChecked() )
97  if ( mAdvancedCheckBox->isChecked() )
99 
100  if ( mDefinitionWidget )
101  {
102  // if a specific definition widget exists, get it to create the parameter (since it will know
103  // how to set all the additional properties of that parameter, which we don't)
104  param.reset( mDefinitionWidget->createParameter( name, mDescriptionLineEdit->text(), flags ) );
105  }
106  else if ( QgsApplication::processingRegistry()->parameterType( mType ) )
107  {
108  // otherwise, just create a default version of the parameter
109  param.reset( QgsApplication::processingRegistry()->parameterType( mType )->create( name ) );
110  if ( param )
111  {
112  param->setDescription( mDescriptionLineEdit->text() );
113  param->setFlags( flags );
114  }
115  }
116 
117  return param.release();
118 }
119 
120 //
121 // QgsProcessingParameterDefinitionDialog
122 //
123 
125  QgsProcessingContext &context,
126  const QgsProcessingParameterWidgetContext &widgetContext,
127  const QgsProcessingParameterDefinition *definition,
129  QWidget *parent )
130  : QDialog( parent )
131 {
132  QVBoxLayout *vLayout = new QVBoxLayout();
133  mWidget = new QgsProcessingParameterDefinitionWidget( type, context, widgetContext, definition, algorithm );
134  vLayout->addWidget( mWidget );
135  QDialogButtonBox *bbox = new QDialogButtonBox( QDialogButtonBox::Cancel | QDialogButtonBox::Ok );
136  connect( bbox, &QDialogButtonBox::accepted, this, &QgsProcessingParameterDefinitionDialog::accept );
137  connect( bbox, &QDialogButtonBox::rejected, this, &QgsProcessingParameterDefinitionDialog::reject );
138  vLayout->addWidget( bbox );
139  setLayout( vLayout );
140  setWindowTitle( definition ? tr( "%1 Parameter Definition" ).arg( definition->description() )
141  : QgsApplication::processingRegistry()->parameterType( type ) ? tr( "%1 Parameter Definition" ).arg( QgsApplication::processingRegistry()->parameterType( type )->name() ) :
142  tr( "Parameter Definition" ) );
143  setObjectName( QStringLiteral( "QgsProcessingParameterDefinitionDialog" ) );
145 }
146 
148 {
149  return mWidget->createParameter( name );
150 }
151 
153 {
154  if ( mWidget->mDescriptionLineEdit->text().isEmpty() )
155  {
156  QMessageBox::warning( this, tr( "Unable to define parameter" ),
157  tr( "Invalid parameter name" ) );
158  return;
159  }
160  QDialog::accept();
161 }
static QgsProcessingGuiRegistry * processingGuiRegistry()
Returns the global processing gui registry, used for registering the GUI behavior of processing algor...
Definition: qgsgui.cpp:102
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...
Parameter is an advanced parameter which should be hidden from users by default.
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...
QgsProcessingParameterType * parameterType(const QString &id) const
Returns the parameter type registered for id.
static QgsGui * instance()
Returns a pointer to the singleton instance.
Definition: qgsgui.cpp:61
Abstract base class for processing algorithms.
QgsProcessingParameterDefinition * createParameter(const QString &name=QString()) const
Returns a new instance of a parameter definition, using the current settings defined in the dialog...
Flags flags() const
Returns any flags associated with the parameter.
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...
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
A widget which allow users to specify the properties of a Processing parameter.
Contains settings which reflect the context in which a Processing parameter widget is shown...
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...
Base class for the definition of processing parameters.
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:127
Contains information about the context in which a processing algorithm is executed.
QString description() const
Returns the description for the parameter.
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...
static QgsProcessingRegistry * processingRegistry()
Returns the application&#39;s processing registry, used for managing processing providers, algorithms, and various parameters and outputs.
QgsProcessingParameterDefinition * createParameter(const QString &name=QString()) const
Returns a new instance of a parameter definition, using the current settings defined in the dialog...