QGIS API Documentation 3.41.0-Master (af5edcb665c)
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 mWasCanceled = true;
50 acceptPanel();
51 } );
52 connect( this, &QgsPanelWidget::panelAccepted, this, [=]() {
53 // save any current cell edits
54 mTblView->setCurrentIndex( QModelIndex() );
55
56 if ( !mWasCanceled )
57 emit widgetChanged();
58 } );
59
60 connect( mButtonAdd, &QPushButton::clicked, this, &QgsProcessingMatrixParameterPanelWidget::addRow );
61 connect( mButtonRemove, &QPushButton::clicked, this, &QgsProcessingMatrixParameterPanelWidget::deleteRow );
62 connect( mButtonRemoveAll, &QPushButton::clicked, this, &QgsProcessingMatrixParameterPanelWidget::deleteAllRows );
63
64 if ( param && param->hasFixedNumberRows() )
65 {
66 mButtonAdd->setEnabled( false );
67 mButtonRemove->setEnabled( false );
68 mButtonRemoveAll->setEnabled( false );
69 }
70
71 populateTable( initialTable );
72}
73
74QVariantList QgsProcessingMatrixParameterPanelWidget::table() const
75{
76 const int cols = mModel->columnCount();
77 const int rows = mModel->rowCount();
78 // Table MUST BE 1-dimensional to match core QgsProcessingParameterMatrix expectations
79 QVariantList res;
80 res.reserve( cols * rows );
81 for ( int row = 0; row < rows; ++row )
82 {
83 for ( int col = 0; col < cols; ++col )
84 {
85 res << mModel->item( row, col )->text();
86 }
87 }
88 return res;
89}
90
91void QgsProcessingMatrixParameterPanelWidget::addRow()
92{
93 QList<QStandardItem *> items;
94 for ( int i = 0; i < mTblView->model()->columnCount(); ++i )
95 {
96 items << new QStandardItem( '0' );
97 }
98 mModel->appendRow( items );
99}
100
101void QgsProcessingMatrixParameterPanelWidget::deleteRow()
102{
103 QModelIndexList selected = mTblView->selectionModel()->selectedRows();
104 QSet<int> rows;
105 rows.reserve( selected.count() );
106 for ( const QModelIndex &i : selected )
107 rows << i.row();
108
109 QList<int> rowsToDelete = qgis::setToList( rows );
110 std::sort( rowsToDelete.begin(), rowsToDelete.end(), std::greater<int>() );
111 mTblView->setUpdatesEnabled( false );
112 for ( int i : std::as_const( rowsToDelete ) )
113 mModel->removeRows( i, 1 );
114
115 mTblView->setUpdatesEnabled( true );
116}
117
118void QgsProcessingMatrixParameterPanelWidget::deleteAllRows()
119{
120 mModel->clear();
121 if ( mParam )
122 mModel->setHorizontalHeaderLabels( mParam->headers() );
123}
124
125void QgsProcessingMatrixParameterPanelWidget::populateTable( const QVariantList &contents )
126{
127 if ( !mParam )
128 return;
129
130 const int cols = mParam->headers().count();
131 const int rows = contents.length() / cols;
132 mModel = new QStandardItemModel( rows, cols, this );
133 mModel->setHorizontalHeaderLabels( mParam->headers() );
134
135 for ( int row = 0; row < rows; ++row )
136 {
137 for ( int col = 0; col < cols; ++col )
138 {
139 QStandardItem *item = new QStandardItem( contents.at( row * cols + col ).toString() );
140 mModel->setItem( row, col, item );
141 }
142 }
143 mTblView->setModel( mModel );
144}
145
146//
147// QgsProcessingMatrixParameterPanel
148//
149
150QgsProcessingMatrixParameterPanel::QgsProcessingMatrixParameterPanel( QWidget *parent, const QgsProcessingParameterMatrix *param )
151 : QWidget( parent )
152 , mParam( param )
153{
154 QHBoxLayout *hl = new QHBoxLayout();
155 hl->setContentsMargins( 0, 0, 0, 0 );
156
157 mLineEdit = new QLineEdit();
158 mLineEdit->setEnabled( false );
159 hl->addWidget( mLineEdit, 1 );
160
161 mToolButton = new QToolButton();
162 mToolButton->setText( QString( QChar( 0x2026 ) ) );
163 hl->addWidget( mToolButton );
164
165 setLayout( hl );
166
167 if ( mParam )
168 {
169 for ( int row = 0; row < mParam->numberRows(); ++row )
170 {
171 for ( int col = 0; col < mParam->headers().count(); ++col )
172 {
173 mTable.append( '0' );
174 }
175 }
176 mLineEdit->setText( tr( "Fixed table (%1x%2)" ).arg( mParam->numberRows() ).arg( mParam->headers().count() ) );
177 }
178
179 connect( mToolButton, &QToolButton::clicked, this, &QgsProcessingMatrixParameterPanel::showDialog );
180}
181
182void QgsProcessingMatrixParameterPanel::setValue( const QVariantList &value )
183{
184 mTable = value;
185 updateSummaryText();
186 emit changed();
187}
188
189void QgsProcessingMatrixParameterPanel::showDialog()
190{
191 if ( QgsPanelWidget *panel = QgsPanelWidget::findParentPanel( this ) )
192 {
193 QgsProcessingMatrixParameterPanelWidget *widget = new QgsProcessingMatrixParameterPanelWidget( this, mParam, mTable );
194
195 widget->setPanelTitle( mParam->description() );
196
197 panel->openPanel( widget );
198
199 connect( widget, &QgsPanelWidget::widgetChanged, this, [=] {
200 setValue( widget->table() );
201 } );
202 }
203}
204
205void QgsProcessingMatrixParameterPanel::updateSummaryText()
206{
207 if ( mParam )
208 mLineEdit->setText( tr( "Fixed table (%1x%2)" ).arg( mTable.count() / mParam->headers().count() ).arg( mParam->headers().count() ) );
209}
210
211
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:210
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.