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