QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
qgsprocessingmatrixparameterdialog.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprocessingmatrixparameterdialog.cpp
3 ------------------------------------
4 Date : February 2019
5 Copyright : (C) 2019 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_qgsprocessingmatrixparameterdialog.cpp"
18#include "qgsgui.h"
19#include <QStandardItemModel>
20#include <QStandardItem>
21#include <QPushButton>
22#include <QLineEdit>
23#include <QToolButton>
24
26
27QgsProcessingMatrixParameterPanelWidget::QgsProcessingMatrixParameterPanelWidget( QWidget *parent, const QgsProcessingParameterMatrix *param, const QVariantList &initialTable )
28 : QgsPanelWidget( parent )
29 , mParam( param )
30{
31 setupUi( this );
32
34
35 mTblView->setSelectionBehavior( QAbstractItemView::SelectRows );
36 mTblView->setSelectionMode( QAbstractItemView::ExtendedSelection );
37
38 mButtonAdd = new QPushButton( tr( "Add Row" ) );
39 mButtonBox->addButton( mButtonAdd, QDialogButtonBox::ActionRole );
40
41 mButtonRemove = new QPushButton( tr( "Remove Row(s)" ) );
42 mButtonBox->addButton( mButtonRemove, QDialogButtonBox::ActionRole );
43
44 mButtonRemoveAll = new QPushButton( tr( "Remove All" ) );
45 mButtonBox->addButton( mButtonRemoveAll, QDialogButtonBox::ActionRole );
46
47 connect( mButtonBox->button( QDialogButtonBox::Ok ), &QPushButton::clicked, this, &QgsPanelWidget::acceptPanel );
48 connect( mButtonBox->button( QDialogButtonBox::Cancel ), &QPushButton::clicked, this, [ = ]
49 {
50 mWasCanceled = true;
51 acceptPanel();
52 } );
53 connect( this, &QgsPanelWidget::panelAccepted, this, [ = ]()
54 {
55 // save any current cell edits
56 mTblView->setCurrentIndex( QModelIndex() );
57
58 if ( !mWasCanceled )
59 emit widgetChanged();
60 } );
61
62 connect( mButtonAdd, &QPushButton::clicked, this, &QgsProcessingMatrixParameterPanelWidget::addRow );
63 connect( mButtonRemove, &QPushButton::clicked, this, &QgsProcessingMatrixParameterPanelWidget::deleteRow );
64 connect( mButtonRemoveAll, &QPushButton::clicked, this, &QgsProcessingMatrixParameterPanelWidget::deleteAllRows );
65
66 if ( param && param->hasFixedNumberRows() )
67 {
68 mButtonAdd->setEnabled( false );
69 mButtonRemove->setEnabled( false );
70 mButtonRemoveAll->setEnabled( false );
71 }
72
73 populateTable( initialTable );
74}
75
76QVariantList QgsProcessingMatrixParameterPanelWidget::table() const
77{
78 const int cols = mModel->columnCount();
79 const int rows = mModel->rowCount();
80 // Table MUST BE 1-dimensional to match core QgsProcessingParameterMatrix expectations
81 QVariantList res;
82 res.reserve( cols * rows );
83 for ( int row = 0; row < rows; ++row )
84 {
85 for ( int col = 0; col < cols; ++col )
86 {
87 res << mModel->item( row, col )->text();
88 }
89 }
90 return res;
91}
92
93void QgsProcessingMatrixParameterPanelWidget::addRow()
94{
95 QList< QStandardItem * > items;
96 for ( int i = 0; i < mTblView->model()->columnCount(); ++i )
97 {
98 items << new QStandardItem( '0' );
99 }
100 mModel->appendRow( items );
101}
102
103void QgsProcessingMatrixParameterPanelWidget::deleteRow()
104{
105 QModelIndexList selected = mTblView->selectionModel()->selectedRows();
106 QSet< int > rows;
107 rows.reserve( selected.count() );
108 for ( const QModelIndex &i : selected )
109 rows << i.row();
110
111 QList< int > rowsToDelete = qgis::setToList( rows );
112 std::sort( rowsToDelete.begin(), rowsToDelete.end(), std::greater<int>() );
113 mTblView->setUpdatesEnabled( false );
114 for ( int i : std::as_const( rowsToDelete ) )
115 mModel->removeRows( i, 1 );
116
117 mTblView->setUpdatesEnabled( true );
118}
119
120void QgsProcessingMatrixParameterPanelWidget::deleteAllRows()
121{
122 mModel->clear();
123 if ( mParam )
124 mModel->setHorizontalHeaderLabels( mParam->headers() );
125}
126
127void QgsProcessingMatrixParameterPanelWidget::populateTable( const QVariantList &contents )
128{
129 if ( !mParam )
130 return;
131
132 const int cols = mParam->headers().count();
133 const int rows = contents.length() / cols;
134 mModel = new QStandardItemModel( rows, cols, this );
135 mModel->setHorizontalHeaderLabels( mParam->headers() );
136
137 for ( int row = 0; row < rows; ++row )
138 {
139 for ( int col = 0; col < cols; ++col )
140 {
141 QStandardItem *item = new QStandardItem( contents.at( row * cols + col ).toString() );
142 mModel->setItem( row, col, item );
143 }
144 }
145 mTblView->setModel( mModel );
146}
147
148//
149// QgsProcessingMatrixParameterPanel
150//
151
152QgsProcessingMatrixParameterPanel::QgsProcessingMatrixParameterPanel( QWidget *parent, const QgsProcessingParameterMatrix *param )
153 : QWidget( parent )
154 , mParam( param )
155{
156 QHBoxLayout *hl = new QHBoxLayout();
157 hl->setContentsMargins( 0, 0, 0, 0 );
158
159 mLineEdit = new QLineEdit();
160 mLineEdit->setEnabled( false );
161 hl->addWidget( mLineEdit, 1 );
162
163 mToolButton = new QToolButton();
164 mToolButton->setText( QString( QChar( 0x2026 ) ) );
165 hl->addWidget( mToolButton );
166
167 setLayout( hl );
168
169 if ( mParam )
170 {
171 for ( int row = 0; row < mParam->numberRows(); ++row )
172 {
173 for ( int col = 0; col < mParam->headers().count(); ++col )
174 {
175 mTable.append( '0' );
176 }
177 }
178 mLineEdit->setText( tr( "Fixed table (%1x%2)" ).arg( mParam->numberRows() ).arg( mParam->headers().count() ) );
179 }
180
181 connect( mToolButton, &QToolButton::clicked, this, &QgsProcessingMatrixParameterPanel::showDialog );
182}
183
184void QgsProcessingMatrixParameterPanel::setValue( const QVariantList &value )
185{
186 mTable = value;
187 updateSummaryText();
188 emit changed();
189}
190
191void QgsProcessingMatrixParameterPanel::showDialog()
192{
193 if ( QgsPanelWidget *panel = QgsPanelWidget::findParentPanel( this ) )
194 {
195 QgsProcessingMatrixParameterPanelWidget *widget = new QgsProcessingMatrixParameterPanelWidget( this, mParam, mTable );
196
197 widget->setPanelTitle( mParam->description() );
198
199 panel->openPanel( widget );
200
201 connect( widget, &QgsPanelWidget::widgetChanged, this, [ = ]
202 {
203 setValue( widget->table() );
204 } );
205 }
206}
207
208void QgsProcessingMatrixParameterPanel::updateSummaryText()
209{
210 if ( mParam )
211 mLineEdit->setText( tr( "Fixed table (%1x%2)" ).arg( mTable.count() / mParam->headers().count() ).arg( mParam->headers().count() ) );
212}
213
214
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Definition qgsgui.cpp:209
Base class for any widget that can be shown as a inline panel.
void panelAccepted(QgsPanelWidget *panel)
Emitted when the panel is accepted by the user.
void widgetChanged()
Emitted when the widget state changes.
void acceptPanel()
Accept the panel.
static QgsPanelWidget * findParentPanel(QWidget *widget)
Traces through the parents of a widget to find if it is contained within a QgsPanelWidget widget.
A table (matrix) parameter for processing algorithms.
bool hasFixedNumberRows() const
Returns whether the table has a fixed number of rows.