QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
qgsmodelinputreorderwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmodelinputreorderwidget.cpp
3 ------------------------------------
4 Date : April 2020
5 Copyright : (C) 2020 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_qgsmodelinputreorderwidget.cpp"
18#include "qgsgui.h"
20#include <QDialogButtonBox>
21#include <QStandardItemModel>
23
24QgsModelInputReorderWidget::QgsModelInputReorderWidget( QWidget *parent )
25 : QWidget( parent )
26{
27 setupUi( this );
28
29 mItemModel = new QStandardItemModel( 0, 1, this );
30 mInputsList->setModel( mItemModel );
31
32 mInputsList->setDropIndicatorShown( true );
33 mInputsList->setDragDropOverwriteMode( false );
34 mInputsList->setDragEnabled( true );
35 mInputsList->setDragDropMode( QAbstractItemView::InternalMove );
36
37 connect( mButtonUp, &QPushButton::clicked, this, [ = ]
38 {
39 int currentRow = mInputsList->currentIndex().row();
40 if ( currentRow == 0 )
41 return;
42
43 mItemModel->insertRow( currentRow - 1, mItemModel->takeRow( currentRow ) );
44 mInputsList->setCurrentIndex( mItemModel->index( currentRow - 1, 0 ) );
45 } );
46
47 connect( mButtonDown, &QPushButton::clicked, this, [ = ]
48 {
49 int currentRow = mInputsList->currentIndex().row();
50 if ( currentRow == mItemModel->rowCount() - 1 )
51 return;
52
53 mItemModel->insertRow( currentRow + 1, mItemModel->takeRow( currentRow ) );
54 mInputsList->setCurrentIndex( mItemModel->index( currentRow + 1, 0 ) );
55 } );
56
57}
58
59void QgsModelInputReorderWidget::setModel( QgsProcessingModelAlgorithm *model )
60{
61 mModel = model;
62 mParameters = mModel->orderedParameters();
63 mItemModel->clear();
64 for ( const QgsProcessingModelParameter &param : std::as_const( mParameters ) )
65 {
66 QStandardItem *item = new QStandardItem( mModel->parameterDefinition( param.parameterName() )->description() );
67 item->setData( param.parameterName() );
68 item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled );
69 mItemModel->appendRow( item );
70 }
71}
72
73QStringList QgsModelInputReorderWidget::inputOrder() const
74{
75 QStringList order;
76 order.reserve( mItemModel->rowCount( ) );
77 for ( int row = 0; row < mItemModel->rowCount(); ++row )
78 {
79 order << mItemModel->data( mItemModel->index( row, 0 ), Qt::UserRole + 1 ).toString();
80 }
81 return order;
82}
83
84
85QgsModelInputReorderDialog::QgsModelInputReorderDialog( QWidget *parent )
86 : QDialog( parent )
87{
88 setWindowTitle( tr( "Reorder Model Inputs" ) );
89 mWidget = new QgsModelInputReorderWidget();
90 QVBoxLayout *vl = new QVBoxLayout();
91 vl->addWidget( mWidget, 1 );
92 QDialogButtonBox *buttonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel );
93 connect( buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
94 connect( buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
95 vl->addWidget( buttonBox );
96 setLayout( vl );
97}
98
99void QgsModelInputReorderDialog::setModel( QgsProcessingModelAlgorithm *model )
100{
101 mWidget->setModel( model );
102}
103
104QStringList QgsModelInputReorderDialog::inputOrder() const
105{
106 return mWidget->inputOrder();
107}
108