17#include "moc_qgsprocessingmatrixparameterdialog.cpp" 
   19#include <QStandardItemModel> 
   20#include <QStandardItem> 
   27QgsProcessingMatrixParameterPanelWidget::QgsProcessingMatrixParameterPanelWidget( QWidget *parent, 
const QgsProcessingParameterMatrix *param, 
const QVariantList &initialTable )
 
   35  mTblView->setSelectionBehavior( QAbstractItemView::SelectRows );
 
   36  mTblView->setSelectionMode( QAbstractItemView::ExtendedSelection );
 
   38  mButtonAdd = 
new QPushButton( tr( 
"Add Row" ) );
 
   39  mButtonBox->addButton( mButtonAdd, QDialogButtonBox::ActionRole );
 
   41  mButtonRemove = 
new QPushButton( tr( 
"Remove Row(s)" ) );
 
   42  mButtonBox->addButton( mButtonRemove, QDialogButtonBox::ActionRole );
 
   44  mButtonRemoveAll = 
new QPushButton( tr( 
"Remove All" ) );
 
   45  mButtonBox->addButton( mButtonRemoveAll, QDialogButtonBox::ActionRole );
 
   48  connect( mButtonBox->button( QDialogButtonBox::Cancel ), &QPushButton::clicked, 
this, [
this] {
 
   54    mTblView->setCurrentIndex( QModelIndex() );
 
   60  connect( mButtonAdd, &QPushButton::clicked, 
this, &QgsProcessingMatrixParameterPanelWidget::addRow );
 
   61  connect( mButtonRemove, &QPushButton::clicked, 
this, &QgsProcessingMatrixParameterPanelWidget::deleteRow );
 
   62  connect( mButtonRemoveAll, &QPushButton::clicked, 
this, &QgsProcessingMatrixParameterPanelWidget::deleteAllRows );
 
   66    mButtonAdd->setEnabled( 
false );
 
   67    mButtonRemove->setEnabled( 
false );
 
   68    mButtonRemoveAll->setEnabled( 
false );
 
   71  populateTable( initialTable );
 
   74QVariantList QgsProcessingMatrixParameterPanelWidget::table()
 const 
   76  const int cols = mModel->columnCount();
 
   77  const int rows = mModel->rowCount();
 
   80  res.reserve( cols * rows );
 
   81  for ( 
int row = 0; row < rows; ++row )
 
   83    for ( 
int col = 0; col < cols; ++col )
 
   85      res << mModel->item( row, col )->text();
 
   91void QgsProcessingMatrixParameterPanelWidget::addRow()
 
   93  QList<QStandardItem *> items;
 
   94  for ( 
int i = 0; i < mTblView->model()->columnCount(); ++i )
 
   96    items << 
new QStandardItem( 
'0' );
 
   98  mModel->appendRow( items );
 
  101void QgsProcessingMatrixParameterPanelWidget::deleteRow()
 
  103  QModelIndexList selected = mTblView->selectionModel()->selectedRows();
 
  105  rows.reserve( selected.count() );
 
  106  for ( 
const QModelIndex &i : selected )
 
  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 );
 
  115  mTblView->setUpdatesEnabled( 
true );
 
  118void QgsProcessingMatrixParameterPanelWidget::deleteAllRows()
 
  122    mModel->setHorizontalHeaderLabels( mParam->headers() );
 
  125void QgsProcessingMatrixParameterPanelWidget::populateTable( 
const QVariantList &contents )
 
  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() );
 
  135  for ( 
int row = 0; row < rows; ++row )
 
  137    for ( 
int col = 0; col < cols; ++col )
 
  139      QStandardItem *item = 
new QStandardItem( contents.at( row * cols + col ).toString() );
 
  140      mModel->setItem( row, col, item );
 
  143  mTblView->setModel( mModel );
 
  154  QHBoxLayout *hl = 
new QHBoxLayout();
 
  155  hl->setContentsMargins( 0, 0, 0, 0 );
 
  157  mLineEdit = 
new QLineEdit();
 
  158  mLineEdit->setEnabled( 
false );
 
  159  hl->addWidget( mLineEdit, 1 );
 
  161  mToolButton = 
new QToolButton();
 
  162  mToolButton->setText( QString( QChar( 0x2026 ) ) );
 
  163  hl->addWidget( mToolButton );
 
  169    for ( 
int row = 0; row < mParam->numberRows(); ++row )
 
  171      for ( 
int col = 0; col < mParam->headers().count(); ++col )
 
  173        mTable.append( 
'0' );
 
  176    mLineEdit->setText( tr( 
"Fixed table (%1x%2)" ).arg( mParam->numberRows() ).arg( mParam->headers().count() ) );
 
  179  connect( mToolButton, &QToolButton::clicked, 
this, &QgsProcessingMatrixParameterPanel::showDialog );
 
  182QVariantList QgsProcessingMatrixParameterPanel::value()
 const 
  186    return mPanelWidget->table();
 
  191void QgsProcessingMatrixParameterPanel::setValue( 
const QVariantList &value )
 
  198void QgsProcessingMatrixParameterPanel::showDialog()
 
  202    mPanelWidget = 
new QgsProcessingMatrixParameterPanelWidget( 
this, mParam, mTable );
 
  204    mPanelWidget->setPanelTitle( mParam->description() );
 
  206    panel->openPanel( mPanelWidget );
 
  209      setValue( mPanelWidget->table() );
 
  214void QgsProcessingMatrixParameterPanel::updateSummaryText()
 
  217    mLineEdit->setText( tr( 
"Fixed table (%1x%2)" ).arg( mTable.count() / mParam->headers().count() ).arg( mParam->headers().count() ) );
 
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...
 
A table (matrix) parameter for processing algorithms.
 
bool hasFixedNumberRows() const
Returns whether the table has a fixed number of rows.