18#include <QStandardItemModel> 
   19#include <QStandardItem> 
   26QgsProcessingMatrixParameterPanelWidget::QgsProcessingMatrixParameterPanelWidget( QWidget *parent, 
const QgsProcessingParameterMatrix *param, 
const QVariantList &initialTable )
 
   34  mTblView->setSelectionBehavior( QAbstractItemView::SelectRows );
 
   35  mTblView->setSelectionMode( QAbstractItemView::ExtendedSelection );
 
   37  mButtonAdd = 
new QPushButton( tr( 
"Add Row" ) );
 
   38  mButtonBox->addButton( mButtonAdd, QDialogButtonBox::ActionRole );
 
   40  mButtonRemove = 
new QPushButton( tr( 
"Remove Row(s)" ) );
 
   41  mButtonBox->addButton( mButtonRemove, QDialogButtonBox::ActionRole );
 
   43  mButtonRemoveAll = 
new QPushButton( tr( 
"Remove All" ) );
 
   44  mButtonBox->addButton( mButtonRemoveAll, QDialogButtonBox::ActionRole );
 
   47  connect( mButtonBox->button( QDialogButtonBox::Cancel ), &QPushButton::clicked, 
this, [ = ]
 
   55    mTblView->setCurrentIndex( QModelIndex() );
 
   61  connect( mButtonAdd, &QPushButton::clicked, 
this, &QgsProcessingMatrixParameterPanelWidget::addRow );
 
   62  connect( mButtonRemove, &QPushButton::clicked, 
this, &QgsProcessingMatrixParameterPanelWidget::deleteRow );
 
   63  connect( mButtonRemoveAll, &QPushButton::clicked, 
this, &QgsProcessingMatrixParameterPanelWidget::deleteAllRows );
 
   67    mButtonAdd->setEnabled( 
false );
 
   68    mButtonRemove->setEnabled( 
false );
 
   69    mButtonRemoveAll->setEnabled( 
false );
 
   72  populateTable( initialTable );
 
   75QVariantList QgsProcessingMatrixParameterPanelWidget::table()
 const 
   77  const int cols = mModel->columnCount();
 
   78  const int rows = mModel->rowCount();
 
   81  res.reserve( cols * rows );
 
   82  for ( 
int row = 0; row < rows; ++row )
 
   84    for ( 
int col = 0; col < cols; ++col )
 
   86      res << mModel->item( row, col )->text();
 
   92void QgsProcessingMatrixParameterPanelWidget::addRow()
 
   94  QList< QStandardItem * > items;
 
   95  for ( 
int i = 0; i < mTblView->model()->columnCount(); ++i )
 
   97    items << 
new QStandardItem( 
'0' );
 
   99  mModel->appendRow( items );
 
  102void QgsProcessingMatrixParameterPanelWidget::deleteRow()
 
  104  QModelIndexList selected = mTblView->selectionModel()->selectedRows();
 
  106  rows.reserve( selected.count() );
 
  107  for ( 
const QModelIndex &i : selected )
 
  110  QList< int > rowsToDelete = qgis::setToList( rows );
 
  111  std::sort( rowsToDelete.begin(), rowsToDelete.end(), std::greater<int>() );
 
  112  mTblView->setUpdatesEnabled( 
false );
 
  113  for ( 
int i : std::as_const( rowsToDelete ) )
 
  114    mModel->removeRows( i, 1 );
 
  116  mTblView->setUpdatesEnabled( 
true );
 
  119void QgsProcessingMatrixParameterPanelWidget::deleteAllRows()
 
  123    mModel->setHorizontalHeaderLabels( mParam->headers() );
 
  126void QgsProcessingMatrixParameterPanelWidget::populateTable( 
const QVariantList &contents )
 
  131  const int cols = mParam->headers().count();
 
  132  const int rows = contents.length() / cols;
 
  133  mModel = 
new QStandardItemModel( rows, cols, 
this );
 
  134  mModel->setHorizontalHeaderLabels( mParam->headers() );
 
  136  for ( 
int row = 0; row < rows; ++row )
 
  138    for ( 
int col = 0; col < cols; ++col )
 
  140      QStandardItem *item = 
new QStandardItem( contents.at( row * cols + col ).toString() );
 
  141      mModel->setItem( row, col, item );
 
  144  mTblView->setModel( mModel );
 
  155  QHBoxLayout *hl = 
new QHBoxLayout();
 
  156  hl->setContentsMargins( 0, 0, 0, 0 );
 
  158  mLineEdit = 
new QLineEdit();
 
  159  mLineEdit->setEnabled( 
false );
 
  160  hl->addWidget( mLineEdit, 1 );
 
  162  mToolButton = 
new QToolButton();
 
  163  mToolButton->setText( QString( QChar( 0x2026 ) ) );
 
  164  hl->addWidget( mToolButton );
 
  170    for ( 
int row = 0; row < mParam->numberRows(); ++row )
 
  172      for ( 
int col = 0; col < mParam->headers().count(); ++col )
 
  174        mTable.append( 
'0' );
 
  177    mLineEdit->setText( tr( 
"Fixed table (%1x%2)" ).arg( mParam->numberRows() ).arg( mParam->headers().count() ) );
 
  180  connect( mToolButton, &QToolButton::clicked, 
this, &QgsProcessingMatrixParameterPanel::showDialog );
 
  183void QgsProcessingMatrixParameterPanel::setValue( 
const QVariantList &value )
 
  190void QgsProcessingMatrixParameterPanel::showDialog()
 
  194    QgsProcessingMatrixParameterPanelWidget *widget = 
new QgsProcessingMatrixParameterPanelWidget( 
this, mParam, mTable );
 
  196    widget->setPanelTitle( mParam->description() );
 
  198    panel->openPanel( widget );
 
  202      setValue( widget->table() );
 
  207void QgsProcessingMatrixParameterPanel::updateSummaryText()
 
  210    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.