26 #include <QTableWidget>
27 #include <QGridLayout>
28 #include <QToolButton>
31 #include <QHeaderView>
35 QgsFilterAlgorithmConfigurationWidget::QgsFilterAlgorithmConfigurationWidget( QWidget *parent )
38 setContentsMargins( 0, 0, 0, 0 );
40 mOutputExpressionWidget =
new QTableWidget();
41 mOutputExpressionWidget->setColumnCount( 3 );
42 mOutputExpressionWidget->setHorizontalHeaderItem( 0,
new QTableWidgetItem( tr(
"Output Name" ) ) );
43 mOutputExpressionWidget->setHorizontalHeaderItem( 1,
new QTableWidgetItem( tr(
"Filter Expression" ) ) );
44 mOutputExpressionWidget->setHorizontalHeaderItem( 2,
new QTableWidgetItem( tr(
"Final Output" ) ) );
45 mOutputExpressionWidget->horizontalHeader()->setSectionResizeMode( 1, QHeaderView::Stretch );
46 QGridLayout *layout =
new QGridLayout();
49 layout->addWidget(
new QLabel( tr(
"Outputs and filters" ) ), 0, 0, 1, 2 );
50 layout->addWidget( mOutputExpressionWidget, 1, 0, 4, 1 );
51 QToolButton *addOutputButton =
new QToolButton();
53 addOutputButton->setText( tr(
"Add Output" ) );
55 QToolButton *removeOutputButton =
new QToolButton();
57 removeOutputButton->setToolTip( tr(
"Remove Selected Outputs" ) );
59 layout->addWidget( addOutputButton, 2, 1, 1, 1 );
60 layout->addWidget( removeOutputButton, 3, 1, 1, 1 );
62 connect( addOutputButton, &QToolButton::clicked,
this, &QgsFilterAlgorithmConfigurationWidget::addOutput );
63 connect( removeOutputButton, &QToolButton::clicked,
this, &QgsFilterAlgorithmConfigurationWidget::removeSelectedOutputs );
65 connect( mOutputExpressionWidget->selectionModel(), &QItemSelectionModel::selectionChanged,
this, [removeOutputButton,
this]
67 removeOutputButton->setEnabled( !mOutputExpressionWidget->selectionModel()->selectedIndexes().isEmpty() );
71 QVariantMap QgsFilterAlgorithmConfigurationWidget::configuration()
const
75 for (
int i = 0; i < mOutputExpressionWidget->rowCount(); ++i )
78 output.insert( QStringLiteral(
"name" ), mOutputExpressionWidget->item( i, 0 )->text() );
79 output.insert( QStringLiteral(
"expression" ), qobject_cast<QgsExpressionLineEdit *>( mOutputExpressionWidget->cellWidget( i, 1 ) )->expression() );
80 output.insert( QStringLiteral(
"isModelOutput" ), qobject_cast<QCheckBox *>( mOutputExpressionWidget->cellWidget( i, 2 ) )->isChecked() );
81 outputs.append( output );
85 map.insert(
"outputs", outputs );
91 void QgsFilterAlgorithmConfigurationWidget::setConfiguration(
const QVariantMap &configuration )
93 mOutputExpressionWidget->setRowCount( 0 );
95 const QVariantList outputs = configuration.value(
"outputs" ).toList();
97 for (
const QVariant &outputvar : outputs )
99 const QVariantMap output = outputvar.toMap();
100 mOutputExpressionWidget->insertRow( currentRow );
101 mOutputExpressionWidget->setItem( currentRow, 0,
new QTableWidgetItem( output.value(
"name" ).toString() ) );
104 expressionBuilder->
setExpression( output.value(
"expression" ).toString() );
105 mOutputExpressionWidget->setCellWidget( currentRow, 1, expressionBuilder );
106 QCheckBox *isModelOutput =
new QCheckBox();
107 isModelOutput->setChecked( output.value(
"isModelOutput" ).toBool() );
108 mOutputExpressionWidget->setCellWidget( currentRow, 2, isModelOutput );
113 if ( outputs.isEmpty() )
117 void QgsFilterAlgorithmConfigurationWidget::removeSelectedOutputs()
119 QItemSelection selection( mOutputExpressionWidget->selectionModel()->selection() );
122 const QModelIndexList indexes = selection.indexes();
123 for (
const QModelIndex &index : indexes )
125 rows.append( index.row() );
128 std::sort( rows.begin(), rows.end() );
131 for (
int i = rows.count() - 1; i >= 0; i -= 1 )
133 int current = rows[i];
134 if ( current != prev )
136 mOutputExpressionWidget->removeRow( current );
142 void QgsFilterAlgorithmConfigurationWidget::addOutput()
144 int rowIndex = mOutputExpressionWidget->rowCount();
145 mOutputExpressionWidget->setRowCount( rowIndex + 1 );
148 mOutputExpressionWidget->setItem( rowIndex, 0,
new QTableWidgetItem( QString() ) );
149 mOutputExpressionWidget->setCellWidget( rowIndex, 1, expressionBuilder );
150 mOutputExpressionWidget->setCellWidget( rowIndex, 2,
new QCheckBox() );
156 return new QgsFilterAlgorithmConfigurationWidget();
171 QgsConditionalBranchAlgorithmConfigurationWidget::QgsConditionalBranchAlgorithmConfigurationWidget( QWidget *parent )
174 setContentsMargins( 0, 0, 0, 0 );
176 mConditionExpressionWidget =
new QTableWidget();
177 mConditionExpressionWidget->setColumnCount( 2 );
178 mConditionExpressionWidget->setHorizontalHeaderItem( 0,
new QTableWidgetItem( tr(
"Branch Name" ) ) );
179 mConditionExpressionWidget->setHorizontalHeaderItem( 1,
new QTableWidgetItem( tr(
"Condition" ) ) );
180 mConditionExpressionWidget->horizontalHeader()->setSectionResizeMode( 1, QHeaderView::Stretch );
181 QGridLayout *layout =
new QGridLayout();
184 layout->addWidget(
new QLabel( tr(
"Conditions" ) ), 0, 0, 1, 2 );
185 layout->addWidget( mConditionExpressionWidget, 1, 0, 4, 1 );
186 QToolButton *addConditionButton =
new QToolButton();
188 addConditionButton->setText( tr(
"Add Condition" ) );
190 QToolButton *removeConditionButton =
new QToolButton();
192 removeConditionButton->setToolTip( tr(
"Remove Selected Conditions" ) );
194 layout->addWidget( addConditionButton, 2, 1, 1, 1 );
195 layout->addWidget( removeConditionButton, 3, 1, 1, 1 );
197 connect( addConditionButton, &QToolButton::clicked,
this, &QgsConditionalBranchAlgorithmConfigurationWidget::addCondition );
198 connect( removeConditionButton, &QToolButton::clicked,
this, &QgsConditionalBranchAlgorithmConfigurationWidget::removeSelectedConditions );
200 connect( mConditionExpressionWidget->selectionModel(), &QItemSelectionModel::selectionChanged,
this, [removeConditionButton,
this]
202 removeConditionButton->setEnabled( !mConditionExpressionWidget->selectionModel()->selectedIndexes().isEmpty() );
206 QVariantMap QgsConditionalBranchAlgorithmConfigurationWidget::configuration()
const
208 QVariantList outputs;
210 for (
int i = 0; i < mConditionExpressionWidget->rowCount(); ++i )
213 output.insert( QStringLiteral(
"name" ), mConditionExpressionWidget->item( i, 0 )->text() );
214 output.insert( QStringLiteral(
"expression" ), qobject_cast<QgsExpressionLineEdit *>( mConditionExpressionWidget->cellWidget( i, 1 ) )->expression() );
215 outputs.append( output );
219 map.insert(
"conditions", outputs );
225 void QgsConditionalBranchAlgorithmConfigurationWidget::setConfiguration(
const QVariantMap &configuration )
227 mConditionExpressionWidget->setRowCount( 0 );
229 const QVariantList conditions = configuration.value(
"conditions" ).toList();
231 for (
const QVariant &conditionvar : conditions )
233 const QVariantMap output = conditionvar.toMap();
234 mConditionExpressionWidget->insertRow( currentRow );
235 mConditionExpressionWidget->setItem( currentRow, 0,
new QTableWidgetItem( output.value(
"name" ).toString() ) );
238 expressionBuilder->
setExpression( output.value(
"expression" ).toString() );
239 mConditionExpressionWidget->setCellWidget( currentRow, 1, expressionBuilder );
243 if ( conditions .isEmpty() )
247 void QgsConditionalBranchAlgorithmConfigurationWidget::removeSelectedConditions()
249 QItemSelection selection( mConditionExpressionWidget->selectionModel()->selection() );
252 const QModelIndexList indexes = selection.indexes();
253 for (
const QModelIndex &index : indexes )
255 rows.append( index.row() );
258 std::sort( rows.begin(), rows.end() );
261 for (
int i = rows.count() - 1; i >= 0; i -= 1 )
263 int current = rows[i];
264 if ( current != prev )
266 mConditionExpressionWidget->removeRow( current );
272 void QgsConditionalBranchAlgorithmConfigurationWidget::addCondition()
274 int rowIndex = mConditionExpressionWidget->rowCount();
275 mConditionExpressionWidget->setRowCount( rowIndex + 1 );
278 mConditionExpressionWidget->setItem( rowIndex, 0,
new QTableWidgetItem( QString() ) );
279 mConditionExpressionWidget->setCellWidget( rowIndex, 1, expressionBuilder );
282 QgsConditionalBranchAlgorithmConfigurationWidget *QgsConditionalBranchAlgorithmConfigurationWidgetFactory::create(
const QgsProcessingAlgorithm *
algorithm )
const
285 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