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