20#include "moc_qgsprocessingconfigurationwidgets.cpp"
27#include <QTableWidget>
36QgsFilterAlgorithmConfigurationWidget::QgsFilterAlgorithmConfigurationWidget( QWidget *parent )
39 setContentsMargins( 0, 0, 0, 0 );
41 mOutputExpressionWidget =
new QTableWidget();
42 mOutputExpressionWidget->setColumnCount( 3 );
43 mOutputExpressionWidget->setHorizontalHeaderItem( 0,
new QTableWidgetItem( tr(
"Output Name" ) ) );
44 mOutputExpressionWidget->setHorizontalHeaderItem( 1,
new QTableWidgetItem( tr(
"Filter Expression" ) ) );
45 mOutputExpressionWidget->setHorizontalHeaderItem( 2,
new QTableWidgetItem( tr(
"Final Output" ) ) );
46 mOutputExpressionWidget->horizontalHeader()->setSectionResizeMode( 1, QHeaderView::Stretch );
47 QGridLayout *layout =
new QGridLayout();
50 layout->addWidget(
new QLabel( tr(
"Outputs and filters" ) ), 0, 0, 1, 2 );
51 layout->addWidget( mOutputExpressionWidget, 1, 0, 4, 1 );
52 QToolButton *addOutputButton =
new QToolButton();
54 addOutputButton->setText( tr(
"Add Output" ) );
56 QToolButton *removeOutputButton =
new QToolButton();
58 removeOutputButton->setToolTip( tr(
"Remove Selected Outputs" ) );
60 layout->addWidget( addOutputButton, 2, 1, 1, 1 );
61 layout->addWidget( removeOutputButton, 3, 1, 1, 1 );
63 connect( addOutputButton, &QToolButton::clicked,
this, &QgsFilterAlgorithmConfigurationWidget::addOutput );
64 connect( removeOutputButton, &QToolButton::clicked,
this, &QgsFilterAlgorithmConfigurationWidget::removeSelectedOutputs );
66 connect( mOutputExpressionWidget->selectionModel(), &QItemSelectionModel::selectionChanged,
this, [removeOutputButton,
this]
68 removeOutputButton->setEnabled( !mOutputExpressionWidget->selectionModel()->selectedIndexes().isEmpty() );
72QVariantMap QgsFilterAlgorithmConfigurationWidget::configuration()
const
76 for (
int i = 0; i < mOutputExpressionWidget->rowCount(); ++i )
79 output.insert( QStringLiteral(
"name" ), mOutputExpressionWidget->item( i, 0 )->text() );
80 output.insert( QStringLiteral(
"expression" ), qobject_cast<QgsExpressionLineEdit *>( mOutputExpressionWidget->cellWidget( i, 1 ) )->expression() );
81 output.insert( QStringLiteral(
"isModelOutput" ), qobject_cast<QCheckBox *>( mOutputExpressionWidget->cellWidget( i, 2 ) )->isChecked() );
82 outputs.append( output );
86 map.insert(
"outputs", outputs );
92void QgsFilterAlgorithmConfigurationWidget::setConfiguration(
const QVariantMap &configuration )
94 mOutputExpressionWidget->setRowCount( 0 );
96 const QVariantList outputs = configuration.value(
"outputs" ).toList();
98 for (
const QVariant &outputvar : outputs )
100 const QVariantMap output = outputvar.toMap();
101 mOutputExpressionWidget->insertRow( currentRow );
102 mOutputExpressionWidget->setItem( currentRow, 0,
new QTableWidgetItem( output.value(
"name" ).toString() ) );
105 expressionBuilder->
setExpression( output.value(
"expression" ).toString() );
106 mOutputExpressionWidget->setCellWidget( currentRow, 1, expressionBuilder );
107 QCheckBox *isModelOutput =
new QCheckBox();
108 isModelOutput->setChecked( output.value(
"isModelOutput" ).toBool() );
109 mOutputExpressionWidget->setCellWidget( currentRow, 2, isModelOutput );
114 if ( outputs.isEmpty() )
118void QgsFilterAlgorithmConfigurationWidget::removeSelectedOutputs()
120 const QItemSelection selection( mOutputExpressionWidget->selectionModel()->selection() );
123 const QModelIndexList indexes = selection.indexes();
124 for (
const QModelIndex &index : indexes )
126 rows.append( index.row() );
129 std::sort( rows.begin(), rows.end() );
132 for (
int i = rows.count() - 1; i >= 0; i -= 1 )
134 const int current = rows[i];
135 if ( current != prev )
137 mOutputExpressionWidget->removeRow( current );
143void QgsFilterAlgorithmConfigurationWidget::addOutput()
145 const int rowIndex = mOutputExpressionWidget->rowCount();
146 mOutputExpressionWidget->setRowCount( rowIndex + 1 );
149 mOutputExpressionWidget->setItem( rowIndex, 0,
new QTableWidgetItem( QString() ) );
150 mOutputExpressionWidget->setCellWidget( rowIndex, 1, expressionBuilder );
151 mOutputExpressionWidget->setCellWidget( rowIndex, 2,
new QCheckBox() );
157 return new QgsFilterAlgorithmConfigurationWidget();
172QgsConditionalBranchAlgorithmConfigurationWidget::QgsConditionalBranchAlgorithmConfigurationWidget( QWidget *parent )
175 setContentsMargins( 0, 0, 0, 0 );
177 mConditionExpressionWidget =
new QTableWidget();
178 mConditionExpressionWidget->setColumnCount( 2 );
179 mConditionExpressionWidget->setHorizontalHeaderItem( 0,
new QTableWidgetItem( tr(
"Branch Name" ) ) );
180 mConditionExpressionWidget->setHorizontalHeaderItem( 1,
new QTableWidgetItem( tr(
"Condition" ) ) );
181 mConditionExpressionWidget->horizontalHeader()->setSectionResizeMode( 1, QHeaderView::Stretch );
182 QGridLayout *layout =
new QGridLayout();
185 layout->addWidget(
new QLabel( tr(
"Conditions" ) ), 0, 0, 1, 2 );
186 layout->addWidget( mConditionExpressionWidget, 1, 0, 4, 1 );
187 QToolButton *addConditionButton =
new QToolButton();
189 addConditionButton->setText( tr(
"Add Condition" ) );
191 QToolButton *removeConditionButton =
new QToolButton();
193 removeConditionButton->setToolTip( tr(
"Remove Selected Conditions" ) );
195 layout->addWidget( addConditionButton, 2, 1, 1, 1 );
196 layout->addWidget( removeConditionButton, 3, 1, 1, 1 );
198 connect( addConditionButton, &QToolButton::clicked,
this, &QgsConditionalBranchAlgorithmConfigurationWidget::addCondition );
199 connect( removeConditionButton, &QToolButton::clicked,
this, &QgsConditionalBranchAlgorithmConfigurationWidget::removeSelectedConditions );
201 connect( mConditionExpressionWidget->selectionModel(), &QItemSelectionModel::selectionChanged,
this, [removeConditionButton,
this]
203 removeConditionButton->setEnabled( !mConditionExpressionWidget->selectionModel()->selectedIndexes().isEmpty() );
207QVariantMap QgsConditionalBranchAlgorithmConfigurationWidget::configuration()
const
209 QVariantList outputs;
211 for (
int i = 0; i < mConditionExpressionWidget->rowCount(); ++i )
214 output.insert( QStringLiteral(
"name" ), mConditionExpressionWidget->item( i, 0 )->text() );
215 output.insert( QStringLiteral(
"expression" ), qobject_cast<QgsExpressionLineEdit *>( mConditionExpressionWidget->cellWidget( i, 1 ) )->expression() );
216 outputs.append( output );
220 map.insert(
"conditions", outputs );
226void QgsConditionalBranchAlgorithmConfigurationWidget::setConfiguration(
const QVariantMap &configuration )
228 mConditionExpressionWidget->setRowCount( 0 );
230 const QVariantList conditions = configuration.value(
"conditions" ).toList();
232 for (
const QVariant &conditionvar : conditions )
234 const QVariantMap output = conditionvar.toMap();
235 mConditionExpressionWidget->insertRow( currentRow );
236 mConditionExpressionWidget->setItem( currentRow, 0,
new QTableWidgetItem( output.value(
"name" ).toString() ) );
239 expressionBuilder->
setExpression( output.value(
"expression" ).toString() );
240 mConditionExpressionWidget->setCellWidget( currentRow, 1, expressionBuilder );
244 if ( conditions .isEmpty() )
248void QgsConditionalBranchAlgorithmConfigurationWidget::removeSelectedConditions()
250 const QItemSelection selection( mConditionExpressionWidget->selectionModel()->selection() );
253 const QModelIndexList indexes = selection.indexes();
254 for (
const QModelIndex &index : indexes )
256 rows.append( index.row() );
259 std::sort( rows.begin(), rows.end() );
262 for (
int i = rows.count() - 1; i >= 0; i -= 1 )
264 const int current = rows[i];
265 if ( current != prev )
267 mConditionExpressionWidget->removeRow( current );
273void QgsConditionalBranchAlgorithmConfigurationWidget::addCondition()
275 const int rowIndex = mConditionExpressionWidget->rowCount();
276 mConditionExpressionWidget->setRowCount( rowIndex + 1 );
279 mConditionExpressionWidget->setItem( rowIndex, 0,
new QTableWidgetItem( QString() ) );
280 mConditionExpressionWidget->setCellWidget( rowIndex, 1, expressionBuilder );
283QgsConditionalBranchAlgorithmConfigurationWidget *QgsConditionalBranchAlgorithmConfigurationWidgetFactory::create(
const QgsProcessingAlgorithm *
algorithm )
const
286 return new QgsConditionalBranchAlgorithmConfigurationWidget();
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
The QgsExpressionLineEdit widget includes a line edit for entering expressions together with a button...
void registerExpressionContextGenerator(const QgsExpressionContextGenerator *generator)
Register an expression context generator class that will be used to retrieve an expression context fo...
void setExpression(const QString &expression)
Sets the current expression to show in the widget.
Abstract base class for processing algorithms.
virtual QString name() const =0
Returns the algorithm name, used for identifying the 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