22#include <QStandardItem>
23#include <QStandardItemModel>
26#include "moc_qgsprocessingmatrixparameterdialog.cpp"
30QgsProcessingMatrixParameterPanelWidget::QgsProcessingMatrixParameterPanelWidget( QWidget *parent,
const QgsProcessingParameterMatrix *param,
const QVariantList &initialTable )
38 mTblView->setSelectionBehavior( QAbstractItemView::SelectRows );
39 mTblView->setSelectionMode( QAbstractItemView::ExtendedSelection );
41 mButtonAdd =
new QPushButton( tr(
"Add Row" ) );
42 mButtonBox->addButton( mButtonAdd, QDialogButtonBox::ActionRole );
44 mButtonRemove =
new QPushButton( tr(
"Remove Row(s)" ) );
45 mButtonBox->addButton( mButtonRemove, QDialogButtonBox::ActionRole );
47 mButtonRemoveAll =
new QPushButton( tr(
"Remove All" ) );
48 mButtonBox->addButton( mButtonRemoveAll, QDialogButtonBox::ActionRole );
51 connect( mButtonBox->button( QDialogButtonBox::Cancel ), &QPushButton::clicked,
this, [
this] {
57 mTblView->setCurrentIndex( QModelIndex() );
63 connect( mButtonAdd, &QPushButton::clicked,
this, &QgsProcessingMatrixParameterPanelWidget::addRow );
64 connect( mButtonRemove, &QPushButton::clicked,
this, &QgsProcessingMatrixParameterPanelWidget::deleteRow );
65 connect( mButtonRemoveAll, &QPushButton::clicked,
this, &QgsProcessingMatrixParameterPanelWidget::deleteAllRows );
69 mButtonAdd->setEnabled(
false );
70 mButtonRemove->setEnabled(
false );
71 mButtonRemoveAll->setEnabled(
false );
74 populateTable( initialTable );
77QVariantList QgsProcessingMatrixParameterPanelWidget::table()
const
79 const int cols = mModel->columnCount();
80 const int rows = mModel->rowCount();
83 res.reserve( cols * rows );
84 for (
int row = 0; row < rows; ++row )
86 for (
int col = 0; col < cols; ++col )
88 res << mModel->item( row, col )->text();
94void QgsProcessingMatrixParameterPanelWidget::addRow()
96 QList<QStandardItem *> items;
97 for (
int i = 0; i < mTblView->model()->columnCount(); ++i )
99 items <<
new QStandardItem(
'0' );
101 mModel->appendRow( items );
104void QgsProcessingMatrixParameterPanelWidget::deleteRow()
106 QModelIndexList selected = mTblView->selectionModel()->selectedRows();
108 rows.reserve( selected.count() );
109 for (
const QModelIndex &i : selected )
112 QList<int> rowsToDelete = qgis::setToList( rows );
113 std::sort( rowsToDelete.begin(), rowsToDelete.end(), std::greater<int>() );
114 mTblView->setUpdatesEnabled(
false );
115 for (
int i : std::as_const( rowsToDelete ) )
116 mModel->removeRows( i, 1 );
118 mTblView->setUpdatesEnabled(
true );
121void QgsProcessingMatrixParameterPanelWidget::deleteAllRows()
125 mModel->setHorizontalHeaderLabels( mParam->headers() );
128void QgsProcessingMatrixParameterPanelWidget::populateTable(
const QVariantList &contents )
133 const int cols = mParam->headers().count();
134 const int rows = contents.length() / cols;
135 mModel =
new QStandardItemModel( rows, cols,
this );
136 mModel->setHorizontalHeaderLabels( mParam->headers() );
138 for (
int row = 0; row < rows; ++row )
140 for (
int col = 0; col < cols; ++col )
142 QStandardItem *item =
new QStandardItem( contents.at( row * cols + col ).toString() );
143 mModel->setItem( row, col, item );
146 mTblView->setModel( mModel );
157 QHBoxLayout *hl =
new QHBoxLayout();
158 hl->setContentsMargins( 0, 0, 0, 0 );
160 mLineEdit =
new QLineEdit();
161 mLineEdit->setEnabled(
false );
162 hl->addWidget( mLineEdit, 1 );
164 mToolButton =
new QToolButton();
165 mToolButton->setText( QString( QChar( 0x2026 ) ) );
166 hl->addWidget( mToolButton );
172 for (
int row = 0; row < mParam->numberRows(); ++row )
174 for (
int col = 0; col < mParam->headers().count(); ++col )
176 mTable.append(
'0' );
179 mLineEdit->setText( tr(
"Fixed table (%1x%2)" ).arg( mParam->numberRows() ).arg( mParam->headers().count() ) );
182 connect( mToolButton, &QToolButton::clicked,
this, &QgsProcessingMatrixParameterPanel::showDialog );
185QVariantList QgsProcessingMatrixParameterPanel::value()
const
189 return mPanelWidget->table();
194void QgsProcessingMatrixParameterPanel::setValue(
const QVariantList &value )
201void QgsProcessingMatrixParameterPanel::showDialog()
205 mPanelWidget =
new QgsProcessingMatrixParameterPanelWidget(
this, mParam, mTable );
207 mPanelWidget->setPanelTitle( mParam->description() );
209 panel->openPanel( mPanelWidget );
212 setValue( mPanelWidget->table() );
217void QgsProcessingMatrixParameterPanel::updateSummaryText()
220 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.