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