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, [ = ]
56 mTblView->setCurrentIndex( QModelIndex() );
62 connect( mButtonAdd, &QPushButton::clicked,
this, &QgsProcessingMatrixParameterPanelWidget::addRow );
63 connect( mButtonRemove, &QPushButton::clicked,
this, &QgsProcessingMatrixParameterPanelWidget::deleteRow );
64 connect( mButtonRemoveAll, &QPushButton::clicked,
this, &QgsProcessingMatrixParameterPanelWidget::deleteAllRows );
68 mButtonAdd->setEnabled(
false );
69 mButtonRemove->setEnabled(
false );
70 mButtonRemoveAll->setEnabled(
false );
73 populateTable( initialTable );
76QVariantList QgsProcessingMatrixParameterPanelWidget::table()
const
78 const int cols = mModel->columnCount();
79 const int rows = mModel->rowCount();
82 res.reserve( cols * rows );
83 for (
int row = 0; row < rows; ++row )
85 for (
int col = 0; col < cols; ++col )
87 res << mModel->item( row, col )->text();
93void QgsProcessingMatrixParameterPanelWidget::addRow()
95 QList< QStandardItem * > items;
96 for (
int i = 0; i < mTblView->model()->columnCount(); ++i )
98 items <<
new QStandardItem(
'0' );
100 mModel->appendRow( items );
103void QgsProcessingMatrixParameterPanelWidget::deleteRow()
105 QModelIndexList selected = mTblView->selectionModel()->selectedRows();
107 rows.reserve( selected.count() );
108 for (
const QModelIndex &i : selected )
111 QList< int > rowsToDelete = qgis::setToList( rows );
112 std::sort( rowsToDelete.begin(), rowsToDelete.end(), std::greater<int>() );
113 mTblView->setUpdatesEnabled(
false );
114 for (
int i : std::as_const( rowsToDelete ) )
115 mModel->removeRows( i, 1 );
117 mTblView->setUpdatesEnabled(
true );
120void QgsProcessingMatrixParameterPanelWidget::deleteAllRows()
124 mModel->setHorizontalHeaderLabels( mParam->headers() );
127void QgsProcessingMatrixParameterPanelWidget::populateTable(
const QVariantList &contents )
132 const int cols = mParam->headers().count();
133 const int rows = contents.length() / cols;
134 mModel =
new QStandardItemModel( rows, cols,
this );
135 mModel->setHorizontalHeaderLabels( mParam->headers() );
137 for (
int row = 0; row < rows; ++row )
139 for (
int col = 0; col < cols; ++col )
141 QStandardItem *item =
new QStandardItem( contents.at( row * cols + col ).toString() );
142 mModel->setItem( row, col, item );
145 mTblView->setModel( mModel );
156 QHBoxLayout *hl =
new QHBoxLayout();
157 hl->setContentsMargins( 0, 0, 0, 0 );
159 mLineEdit =
new QLineEdit();
160 mLineEdit->setEnabled(
false );
161 hl->addWidget( mLineEdit, 1 );
163 mToolButton =
new QToolButton();
164 mToolButton->setText( QString( QChar( 0x2026 ) ) );
165 hl->addWidget( mToolButton );
171 for (
int row = 0; row < mParam->numberRows(); ++row )
173 for (
int col = 0; col < mParam->headers().count(); ++col )
175 mTable.append(
'0' );
178 mLineEdit->setText( tr(
"Fixed table (%1x%2)" ).arg( mParam->numberRows() ).arg( mParam->headers().count() ) );
181 connect( mToolButton, &QToolButton::clicked,
this, &QgsProcessingMatrixParameterPanel::showDialog );
184void QgsProcessingMatrixParameterPanel::setValue(
const QVariantList &value )
191void QgsProcessingMatrixParameterPanel::showDialog()
195 QgsProcessingMatrixParameterPanelWidget *widget =
new QgsProcessingMatrixParameterPanelWidget(
this, mParam, mTable );
197 widget->setPanelTitle( mParam->description() );
199 panel->openPanel( widget );
203 setValue( widget->table() );
208void QgsProcessingMatrixParameterPanel::updateSummaryText()
211 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.