QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
qgsmodeloutputreorderwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmodeloutputreorderwidget.cpp
3 ------------------------------------
4 Date : April 2023
5 Copyright : (C) 2023 Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17#include "qgsgui.h"
19#include <QDialogButtonBox>
20#include <QStandardItemModel>
22
23QgsModelOutputReorderWidget::QgsModelOutputReorderWidget( QWidget *parent )
24 : QWidget( parent )
25{
26 setupUi( this );
27
28 mItemModel = new QStandardItemModel( 0, 1, this );
29 mOutputsList->setModel( mItemModel );
30
31 mOutputsList->setDropIndicatorShown( true );
32 mOutputsList->setDragDropOverwriteMode( false );
33 mOutputsList->setDragEnabled( true );
34 mOutputsList->setDragDropMode( QAbstractItemView::InternalMove );
35
36 connect( mButtonUp, &QPushButton::clicked, this, [ = ]
37 {
38 int currentRow = mOutputsList->currentIndex().row();
39 if ( currentRow == 0 )
40 return;
41
42 mItemModel->insertRow( currentRow - 1, mItemModel->takeRow( currentRow ) );
43 mOutputsList->setCurrentIndex( mItemModel->index( currentRow - 1, 0 ) );
44 } );
45
46 connect( mButtonDown, &QPushButton::clicked, this, [ = ]
47 {
48 int currentRow = mOutputsList->currentIndex().row();
49 if ( currentRow == mItemModel->rowCount() - 1 )
50 return;
51
52 mItemModel->insertRow( currentRow + 1, mItemModel->takeRow( currentRow ) );
53 mOutputsList->setCurrentIndex( mItemModel->index( currentRow + 1, 0 ) );
54 } );
55
56}
57
58void QgsModelOutputReorderWidget::setModel( QgsProcessingModelAlgorithm *model )
59{
60 mModel = model;
61 mOutputs = mModel->orderedOutputs();
62 mItemModel->clear();
63 for ( const QgsProcessingModelOutput &output : std::as_const( mOutputs ) )
64 {
65 QStandardItem *item = new QStandardItem( output.name() );
66 item->setData( QStringLiteral( "%1:%2" ).arg( output.childId(), output.childOutputName() ), Qt::UserRole + 1 );
67 item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled );
68 // we show the outputs list reversed in the gui, because we want the "higher" outputs to be at the top of the list
69 mItemModel->insertRow( 0, item );
70 }
71
72 mPlaceInGroupCheck->setChecked( !model->outputGroup().isEmpty() );
73 mGroupNameEdit->setText( model->outputGroup() );
74}
75
76QStringList QgsModelOutputReorderWidget::outputOrder() const
77{
78 QStringList order;
79 order.reserve( mItemModel->rowCount( ) );
80 // we show the outputs list reversed in the gui, because we want the "higher" outputs to be at the top of the list
81 for ( int row = mItemModel->rowCount() - 1; row >= 0; --row )
82 {
83 order << mItemModel->data( mItemModel->index( row, 0 ), Qt::UserRole + 1 ).toString();
84 }
85 return order;
86}
87
88QString QgsModelOutputReorderWidget::outputGroup() const
89{
90 return mPlaceInGroupCheck->isChecked() ? mGroupNameEdit->text() : QString();
91}
92
93
94QgsModelOutputReorderDialog::QgsModelOutputReorderDialog( QWidget *parent )
95 : QDialog( parent )
96{
97 setWindowTitle( tr( "Reorder Output Layers" ) );
98 mWidget = new QgsModelOutputReorderWidget();
99 QVBoxLayout *vl = new QVBoxLayout();
100 vl->addWidget( mWidget, 1 );
101 QDialogButtonBox *buttonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel );
102 connect( buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
103 connect( buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
104 vl->addWidget( buttonBox );
105 setLayout( vl );
106}
107
108void QgsModelOutputReorderDialog::setModel( QgsProcessingModelAlgorithm *model )
109{
110 mWidget->setModel( model );
111}
112
113QStringList QgsModelOutputReorderDialog::outputOrder() const
114{
115 return mWidget->outputOrder();
116}
117
118QString QgsModelOutputReorderDialog::outputGroup() const
119{
120 return mWidget->outputGroup();
121}
122