QGIS API Documentation 3.99.0-Master (09f76ad7019)
Loading...
Searching...
No Matches
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
18#include "qgsgui.h"
20
21#include <QDialogButtonBox>
22#include <QStandardItemModel>
23#include <QString>
24
25#include "moc_qgsmodeloutputreorderwidget.cpp"
26
27using namespace Qt::StringLiterals;
28
30
31QgsModelOutputReorderWidget::QgsModelOutputReorderWidget( QWidget *parent )
32 : QWidget( parent )
33{
34 setupUi( this );
35
36 mItemModel = new QStandardItemModel( 0, 1, this );
37 mOutputsList->setModel( mItemModel );
38
39 mOutputsList->setDropIndicatorShown( true );
40 mOutputsList->setDragDropOverwriteMode( false );
41 mOutputsList->setDragEnabled( true );
42 mOutputsList->setDragDropMode( QAbstractItemView::InternalMove );
43
44 connect( mButtonUp, &QPushButton::clicked, this, [this] {
45 int currentRow = mOutputsList->currentIndex().row();
46 if ( currentRow == 0 )
47 return;
48
49 mItemModel->insertRow( currentRow - 1, mItemModel->takeRow( currentRow ) );
50 mOutputsList->setCurrentIndex( mItemModel->index( currentRow - 1, 0 ) );
51 } );
52
53 connect( mButtonDown, &QPushButton::clicked, this, [this] {
54 int currentRow = mOutputsList->currentIndex().row();
55 if ( currentRow == mItemModel->rowCount() - 1 )
56 return;
57
58 mItemModel->insertRow( currentRow + 1, mItemModel->takeRow( currentRow ) );
59 mOutputsList->setCurrentIndex( mItemModel->index( currentRow + 1, 0 ) );
60 } );
61}
62
63void QgsModelOutputReorderWidget::setModel( QgsProcessingModelAlgorithm *model )
64{
65 mModel = model;
66 mOutputs = mModel->orderedOutputs();
67 mItemModel->clear();
68 for ( const QgsProcessingModelOutput &output : std::as_const( mOutputs ) )
69 {
70 QStandardItem *item = new QStandardItem( output.name() );
71 item->setData( u"%1:%2"_s.arg( output.childId(), output.childOutputName() ), Qt::UserRole + 1 );
72 item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled );
73 // we show the outputs list reversed in the gui, because we want the "higher" outputs to be at the top of the list
74 mItemModel->insertRow( 0, item );
75 }
76
77 mPlaceInGroupCheck->setChecked( !model->outputGroup().isEmpty() );
78 mGroupNameEdit->setText( model->outputGroup() );
79}
80
81QStringList QgsModelOutputReorderWidget::outputOrder() const
82{
83 QStringList order;
84 order.reserve( mItemModel->rowCount() );
85 // we show the outputs list reversed in the gui, because we want the "higher" outputs to be at the top of the list
86 for ( int row = mItemModel->rowCount() - 1; row >= 0; --row )
87 {
88 order << mItemModel->data( mItemModel->index( row, 0 ), Qt::UserRole + 1 ).toString();
89 }
90 return order;
91}
92
93QString QgsModelOutputReorderWidget::outputGroup() const
94{
95 return mPlaceInGroupCheck->isChecked() ? mGroupNameEdit->text() : QString();
96}
97
98
99QgsModelOutputReorderDialog::QgsModelOutputReorderDialog( QWidget *parent )
100 : QDialog( parent )
101{
102 setWindowTitle( tr( "Reorder Output Layers" ) );
103 mWidget = new QgsModelOutputReorderWidget();
104 QVBoxLayout *vl = new QVBoxLayout();
105 vl->addWidget( mWidget, 1 );
106 QDialogButtonBox *buttonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel );
107 connect( buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
108 connect( buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
109 vl->addWidget( buttonBox );
110 setLayout( vl );
111}
112
113void QgsModelOutputReorderDialog::setModel( QgsProcessingModelAlgorithm *model )
114{
115 mWidget->setModel( model );
116}
117
118QStringList QgsModelOutputReorderDialog::outputOrder() const
119{
120 return mWidget->outputOrder();
121}
122
123QString QgsModelOutputReorderDialog::outputGroup() const
124{
125 return mWidget->outputGroup();
126}
127