QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 "qgsgui.h"
18 #include <QStandardItemModel>
19 #include <QStandardItem>
20 #include <QPushButton>
21 #include <QLineEdit>
22 #include <QToolButton>
23 
25 
26 QgsProcessingMatrixParameterDialog::QgsProcessingMatrixParameterDialog( QWidget *parent, Qt::WindowFlags flags, const QgsProcessingParameterMatrix *param, const QVariantList &initialTable )
27  : QDialog( parent, flags )
28  , mParam( param )
29 {
30  setupUi( this );
31 
33 
34  mTblView->setSelectionBehavior( QAbstractItemView::SelectRows );
35  mTblView->setSelectionMode( QAbstractItemView::ExtendedSelection );
36 
37  mButtonAdd = new QPushButton( tr( "Add Row" ) );
38  mButtonBox->addButton( mButtonAdd, QDialogButtonBox::ActionRole );
39 
40  mButtonRemove = new QPushButton( tr( "Remove Row(s)" ) );
41  mButtonBox->addButton( mButtonRemove, QDialogButtonBox::ActionRole );
42 
43  mButtonRemoveAll = new QPushButton( tr( "Remove All" ) );
44  mButtonBox->addButton( mButtonRemoveAll, QDialogButtonBox::ActionRole );
45 
46  connect( mButtonAdd, &QPushButton::clicked, this, &QgsProcessingMatrixParameterDialog::addRow );
47  connect( mButtonRemove, &QPushButton::clicked, this, &QgsProcessingMatrixParameterDialog::deleteRow );
48  connect( mButtonRemoveAll, &QPushButton::clicked, this, &QgsProcessingMatrixParameterDialog::deleteAllRows );
49 
50  if ( param && param->hasFixedNumberRows() )
51  {
52  mButtonAdd->setEnabled( false );
53  mButtonRemove->setEnabled( false );
54  mButtonRemoveAll->setEnabled( false );
55  }
56 
57  populateTable( initialTable );
58 }
59 
60 QVariantList QgsProcessingMatrixParameterDialog::table() const
61 {
62  const int cols = mModel->columnCount();
63  const int rows = mModel->rowCount();
64  // Table MUST BE 1-dimensional to match core QgsProcessingParameterMatrix expectations
65  QVariantList res;
66  res.reserve( cols * rows );
67  for ( int row = 0; row < rows; ++row )
68  {
69  for ( int col = 0; col < cols; ++col )
70  {
71  res << mModel->item( row, col )->text();
72  }
73  }
74  return res;
75 }
76 
77 void QgsProcessingMatrixParameterDialog::addRow()
78 {
79  QList< QStandardItem * > items;
80  for ( int i = 0; i < mTblView->model()->columnCount(); ++i )
81  {
82  items << new QStandardItem( '0' );
83  }
84  mModel->appendRow( items );
85 }
86 
87 void QgsProcessingMatrixParameterDialog::deleteRow()
88 {
89  QModelIndexList selected = mTblView->selectionModel()->selectedRows();
90  QSet< int > rows;
91  rows.reserve( selected.count() );
92  for ( const QModelIndex &i : selected )
93  rows << i.row();
94 
95  QList< int > rowsToDelete = rows.toList();
96  std::sort( rowsToDelete.begin(), rowsToDelete.end(), std::greater<int>() );
97  mTblView->setUpdatesEnabled( false );
98  for ( int i : qgis::as_const( rowsToDelete ) )
99  mModel->removeRows( i, 1 );
100 
101  mTblView->setUpdatesEnabled( true );
102 }
103 
104 void QgsProcessingMatrixParameterDialog::deleteAllRows()
105 {
106  mModel->clear();
107  if ( mParam )
108  mModel->setHorizontalHeaderLabels( mParam->headers() );
109 }
110 
111 void QgsProcessingMatrixParameterDialog::populateTable( const QVariantList &contents )
112 {
113  if ( !mParam )
114  return;
115 
116  const int cols = mParam->headers().count();
117  const int rows = contents.length() / cols;
118  mModel = new QStandardItemModel( rows, cols, this );
119  mModel->setHorizontalHeaderLabels( mParam->headers() );
120 
121  for ( int row = 0; row < rows; ++row )
122  {
123  for ( int col = 0; col < cols; ++col )
124  {
125  QStandardItem *item = new QStandardItem( contents.at( row * cols + col ).toString() );
126  mModel->setItem( row, col, item );
127  }
128  }
129  mTblView->setModel( mModel );
130 }
131 
132 //
133 // QgsProcessingMatrixParameterPanel
134 //
135 
136 QgsProcessingMatrixParameterPanel::QgsProcessingMatrixParameterPanel( QWidget *parent, const QgsProcessingParameterMatrix *param )
137  : QWidget( parent )
138  , mParam( param )
139 {
140  QHBoxLayout *hl = new QHBoxLayout();
141  hl->setMargin( 0 );
142  hl->setContentsMargins( 0, 0, 0, 0 );
143 
144  mLineEdit = new QLineEdit();
145  mLineEdit->setEnabled( false );
146  hl->addWidget( mLineEdit, 1 );
147 
148  mToolButton = new QToolButton();
149  mToolButton->setText( tr( "…" ) );
150  hl->addWidget( mToolButton );
151 
152  setLayout( hl );
153 
154  if ( mParam )
155  {
156  for ( int row = 0; row < mParam->numberRows(); ++row )
157  {
158  for ( int col = 0; col < mParam->headers().count(); ++col )
159  {
160  mTable.append( '0' );
161  }
162  }
163  mLineEdit->setText( tr( "Fixed table (%1x%2)" ).arg( mParam->numberRows() ).arg( mParam->headers().count() ) );
164  }
165 
166  connect( mToolButton, &QToolButton::clicked, this, &QgsProcessingMatrixParameterPanel::showDialog );
167 }
168 
169 void QgsProcessingMatrixParameterPanel::setValue( const QVariantList &value )
170 {
171  mTable = value;
172  updateSummaryText();
173  emit changed();
174 }
175 
176 void QgsProcessingMatrixParameterPanel::showDialog()
177 {
178  QgsProcessingMatrixParameterDialog dlg( this, nullptr, mParam, mTable );
179  if ( dlg.exec() )
180  {
181  setValue( dlg.table() );
182  }
183 }
184 
185 void QgsProcessingMatrixParameterPanel::updateSummaryText()
186 {
187  if ( mParam )
188  mLineEdit->setText( tr( "Fixed table (%1x%2)" ).arg( mTable.count() / mParam->headers().count() ).arg( mParam->headers().count() ) );
189 }
190 
191 
bool hasFixedNumberRows() const
Returns whether the table has a fixed number of rows.
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:104
A table (matrix) parameter for processing algorithms.