18 #include <QStandardItemModel> 
   19 #include <QStandardItem> 
   20 #include <QPushButton> 
   22 #include <QToolButton> 
   26 QgsProcessingMatrixParameterPanelWidget::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 );
 
   46   connect( mButtonBox->button( QDialogButtonBox::Ok ), &QPushButton::clicked, 
this, [ = ]
 
   51   connect( mButtonBox->button( QDialogButtonBox::Cancel ), &QPushButton::clicked, 
this, [ = ]
 
   56   connect( mButtonAdd, &QPushButton::clicked, 
this, &QgsProcessingMatrixParameterPanelWidget::addRow );
 
   57   connect( mButtonRemove, &QPushButton::clicked, 
this, &QgsProcessingMatrixParameterPanelWidget::deleteRow );
 
   58   connect( mButtonRemoveAll, &QPushButton::clicked, 
this, &QgsProcessingMatrixParameterPanelWidget::deleteAllRows );
 
   62     mButtonAdd->setEnabled( 
false );
 
   63     mButtonRemove->setEnabled( 
false );
 
   64     mButtonRemoveAll->setEnabled( 
false );
 
   67   populateTable( initialTable );
 
   70 QVariantList QgsProcessingMatrixParameterPanelWidget::table()
 const 
   72   const int cols = mModel->columnCount();
 
   73   const int rows = mModel->rowCount();
 
   76   res.reserve( cols * rows );
 
   77   for ( 
int row = 0; row < rows; ++row )
 
   79     for ( 
int col = 0; col < cols; ++col )
 
   81       res << mModel->item( row, col )->text();
 
   87 void QgsProcessingMatrixParameterPanelWidget::addRow()
 
   89   QList< QStandardItem * > items;
 
   90   for ( 
int i = 0; i < mTblView->model()->columnCount(); ++i )
 
   92     items << 
new QStandardItem( 
'0' );
 
   94   mModel->appendRow( items );
 
   97 void QgsProcessingMatrixParameterPanelWidget::deleteRow()
 
   99   QModelIndexList selected = mTblView->selectionModel()->selectedRows();
 
  101   rows.reserve( selected.count() );
 
  102   for ( 
const QModelIndex &i : selected )
 
  105   QList< int > rowsToDelete = qgis::setToList( rows );
 
  106   std::sort( rowsToDelete.begin(), rowsToDelete.end(), std::greater<int>() );
 
  107   mTblView->setUpdatesEnabled( 
false );
 
  108   for ( 
int i : std::as_const( rowsToDelete ) )
 
  109     mModel->removeRows( i, 1 );
 
  111   mTblView->setUpdatesEnabled( 
true );
 
  114 void QgsProcessingMatrixParameterPanelWidget::deleteAllRows()
 
  118     mModel->setHorizontalHeaderLabels( mParam->headers() );
 
  121 void QgsProcessingMatrixParameterPanelWidget::populateTable( 
const QVariantList &contents )
 
  126   const int cols = mParam->headers().count();
 
  127   const int rows = contents.length() / cols;
 
  128   mModel = 
new QStandardItemModel( rows, cols, 
this );
 
  129   mModel->setHorizontalHeaderLabels( mParam->headers() );
 
  131   for ( 
int row = 0; row < rows; ++row )
 
  133     for ( 
int col = 0; col < cols; ++col )
 
  135       QStandardItem *item = 
new QStandardItem( contents.at( row * cols + col ).toString() );
 
  136       mModel->setItem( row, col, item );
 
  139   mTblView->setModel( mModel );
 
  150   QHBoxLayout *hl = 
new QHBoxLayout();
 
  151   hl->setContentsMargins( 0, 0, 0, 0 );
 
  153   mLineEdit = 
new QLineEdit();
 
  154   mLineEdit->setEnabled( 
false );
 
  155   hl->addWidget( mLineEdit, 1 );
 
  157   mToolButton = 
new QToolButton();
 
  158   mToolButton->setText( QString( QChar( 0x2026 ) ) );
 
  159   hl->addWidget( mToolButton );
 
  165     for ( 
int row = 0; row < mParam->numberRows(); ++row )
 
  167       for ( 
int col = 0; col < mParam->headers().count(); ++col )
 
  169         mTable.append( 
'0' );
 
  172     mLineEdit->setText( tr( 
"Fixed table (%1x%2)" ).arg( mParam->numberRows() ).arg( mParam->headers().count() ) );
 
  175   connect( mToolButton, &QToolButton::clicked, 
this, &QgsProcessingMatrixParameterPanel::showDialog );
 
  178 void QgsProcessingMatrixParameterPanel::setValue( 
const QVariantList &value )
 
  185 void QgsProcessingMatrixParameterPanel::showDialog()
 
  189     QgsProcessingMatrixParameterPanelWidget *widget = 
new QgsProcessingMatrixParameterPanelWidget( 
this, mParam, mTable );
 
  191     widget->setPanelTitle( mParam->description() );
 
  193     panel->openPanel( widget );
 
  197       setValue( widget->table() );
 
  202 void QgsProcessingMatrixParameterPanel::updateSummaryText()
 
  205     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.