Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[processing] Allow configuration of order of outputs created by a model
This adds a new "Reorder Model Outputs" action to the model designer menu (to accompany the existing "Reorder Model Inputs" action). Selecting this option allows model creators to set a specific order which the outputs from their model must use when loading the results into a project. This gives the model creator a means of ensuring that layers are logically ordered, eg placing a vector layer over a raster layer and a point layer over a polygon layer. Optionally, the model creator can also set a "Group name" for the outputs. If this is specified then all outputs from the model will be placed into a (newly created if necessary) layer tree group with that name. Sponsored by the QGIS Germany User Group
- Loading branch information
1 parent
f1c0923
commit f27195c
Showing
11 changed files
with
684 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
src/gui/processing/models/qgsmodeloutputreorderwidget.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/*************************************************************************** | ||
qgsmodeloutputreorderwidget.cpp | ||
------------------------------------ | ||
Date : April 2023 | ||
Copyright : (C) 2023 Nyall Dawson | ||
Email : nyall dot dawson at gmail dot com | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgsmodeloutputreorderwidget.h" | ||
#include "qgsgui.h" | ||
#include "qgsprocessingmodelalgorithm.h" | ||
#include <QDialogButtonBox> | ||
#include <QStandardItemModel> | ||
///@cond NOT_STABLE | ||
|
||
QgsModelOutputReorderWidget::QgsModelOutputReorderWidget( QWidget *parent ) | ||
: QWidget( parent ) | ||
{ | ||
setupUi( this ); | ||
|
||
mItemModel = new QStandardItemModel( 0, 1, this ); | ||
mOutputsList->setModel( mItemModel ); | ||
|
||
mOutputsList->setDropIndicatorShown( true ); | ||
mOutputsList->setDragDropOverwriteMode( false ); | ||
mOutputsList->setDragEnabled( true ); | ||
mOutputsList->setDragDropMode( QAbstractItemView::InternalMove ); | ||
|
||
connect( mButtonUp, &QPushButton::clicked, this, [ = ] | ||
{ | ||
int currentRow = mOutputsList->currentIndex().row(); | ||
if ( currentRow == 0 ) | ||
return; | ||
|
||
mItemModel->insertRow( currentRow - 1, mItemModel->takeRow( currentRow ) ); | ||
mOutputsList->setCurrentIndex( mItemModel->index( currentRow - 1, 0 ) ); | ||
} ); | ||
|
||
connect( mButtonDown, &QPushButton::clicked, this, [ = ] | ||
{ | ||
int currentRow = mOutputsList->currentIndex().row(); | ||
if ( currentRow == mItemModel->rowCount() - 1 ) | ||
return; | ||
|
||
mItemModel->insertRow( currentRow + 1, mItemModel->takeRow( currentRow ) ); | ||
mOutputsList->setCurrentIndex( mItemModel->index( currentRow + 1, 0 ) ); | ||
} ); | ||
|
||
} | ||
|
||
void QgsModelOutputReorderWidget::setModel( QgsProcessingModelAlgorithm *model ) | ||
{ | ||
mModel = model; | ||
mOutputs = mModel->orderedOutputs(); | ||
mItemModel->clear(); | ||
for ( const QgsProcessingModelOutput &output : std::as_const( mOutputs ) ) | ||
{ | ||
QStandardItem *item = new QStandardItem( output.name() ); | ||
item->setData( QStringLiteral( "%1:%2" ).arg( output.childId(), output.childOutputName() ), Qt::UserRole + 1 ); | ||
item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled ); | ||
// we show the outputs list reversed in the gui, because we want the "higher" outputs to be at the top of the list | ||
mItemModel->insertRow( 0, item ); | ||
} | ||
|
||
mPlaceInGroupCheck->setChecked( !model->outputGroup().isEmpty() ); | ||
mGroupNameEdit->setText( model->outputGroup() ); | ||
} | ||
|
||
QStringList QgsModelOutputReorderWidget::outputOrder() const | ||
{ | ||
QStringList order; | ||
order.reserve( mItemModel->rowCount( ) ); | ||
// we show the outputs list reversed in the gui, because we want the "higher" outputs to be at the top of the list | ||
for ( int row = mItemModel->rowCount() - 1; row >= 0; --row ) | ||
{ | ||
order << mItemModel->data( mItemModel->index( row, 0 ), Qt::UserRole + 1 ).toString(); | ||
} | ||
return order; | ||
} | ||
|
||
QString QgsModelOutputReorderWidget::outputGroup() const | ||
{ | ||
return mPlaceInGroupCheck->isChecked() ? mGroupNameEdit->text() : QString(); | ||
} | ||
|
||
|
||
QgsModelOutputReorderDialog::QgsModelOutputReorderDialog( QWidget *parent ) | ||
: QDialog( parent ) | ||
{ | ||
setWindowTitle( tr( "Reorder Model Outputs" ) ); | ||
mWidget = new QgsModelOutputReorderWidget(); | ||
QVBoxLayout *vl = new QVBoxLayout(); | ||
vl->addWidget( mWidget, 1 ); | ||
QDialogButtonBox *buttonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel ); | ||
connect( buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept ); | ||
connect( buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject ); | ||
vl->addWidget( buttonBox ); | ||
setLayout( vl ); | ||
} | ||
|
||
void QgsModelOutputReorderDialog::setModel( QgsProcessingModelAlgorithm *model ) | ||
{ | ||
mWidget->setModel( model ); | ||
} | ||
|
||
QStringList QgsModelOutputReorderDialog::outputOrder() const | ||
{ | ||
return mWidget->outputOrder(); | ||
} | ||
|
||
QString QgsModelOutputReorderDialog::outputGroup() const | ||
{ | ||
return mWidget->outputGroup(); | ||
} | ||
|
||
///@endcond |
Oops, something went wrong.