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