18 #include <QStandardItemModel> 19 #include <QStandardItem> 20 #include <QPushButton> 22 #include <QToolButton> 26 QgsProcessingMatrixParameterDialog::QgsProcessingMatrixParameterDialog( QWidget *parent, Qt::WindowFlags flags,
const QgsProcessingParameterMatrix *param,
const QVariantList &initialTable )
27 : QDialog( parent, flags )
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( mButtonAdd, &QPushButton::clicked,
this, &QgsProcessingMatrixParameterDialog::addRow );
47 connect( mButtonRemove, &QPushButton::clicked,
this, &QgsProcessingMatrixParameterDialog::deleteRow );
48 connect( mButtonRemoveAll, &QPushButton::clicked,
this, &QgsProcessingMatrixParameterDialog::deleteAllRows );
52 mButtonAdd->setEnabled(
false );
53 mButtonRemove->setEnabled(
false );
54 mButtonRemoveAll->setEnabled(
false );
57 populateTable( initialTable );
60 QVariantList QgsProcessingMatrixParameterDialog::table()
const 62 const int cols = mModel->columnCount();
63 const int rows = mModel->rowCount();
66 res.reserve( cols * rows );
67 for (
int row = 0; row < rows; ++row )
69 for (
int col = 0; col < cols; ++col )
71 res << mModel->item( row, col )->text();
77 void QgsProcessingMatrixParameterDialog::addRow()
79 QList< QStandardItem * > items;
80 for (
int i = 0; i < mTblView->model()->columnCount(); ++i )
82 items <<
new QStandardItem(
'0' );
84 mModel->appendRow( items );
87 void QgsProcessingMatrixParameterDialog::deleteRow()
89 QModelIndexList selected = mTblView->selectionModel()->selectedRows();
91 rows.reserve( selected.count() );
92 for (
const QModelIndex &i : selected )
95 QList< int > rowsToDelete = rows.toList();
96 std::sort( rowsToDelete.begin(), rowsToDelete.end(), std::greater<int>() );
97 mTblView->setUpdatesEnabled(
false );
98 for (
int i : qgis::as_const( rowsToDelete ) )
99 mModel->removeRows( i, 1 );
101 mTblView->setUpdatesEnabled(
true );
104 void QgsProcessingMatrixParameterDialog::deleteAllRows()
108 mModel->setHorizontalHeaderLabels( mParam->headers() );
111 void QgsProcessingMatrixParameterDialog::populateTable(
const QVariantList &contents )
116 const int cols = mParam->headers().count();
117 const int rows = contents.length() / cols;
118 mModel =
new QStandardItemModel( rows, cols,
this );
119 mModel->setHorizontalHeaderLabels( mParam->headers() );
121 for (
int row = 0; row < rows; ++row )
123 for (
int col = 0; col < cols; ++col )
125 QStandardItem *item =
new QStandardItem( contents.at( row * cols + col ).toString() );
126 mModel->setItem( row, col, item );
129 mTblView->setModel( mModel );
140 QHBoxLayout *hl =
new QHBoxLayout();
142 hl->setContentsMargins( 0, 0, 0, 0 );
144 mLineEdit =
new QLineEdit();
145 mLineEdit->setEnabled(
false );
146 hl->addWidget( mLineEdit, 1 );
148 mToolButton =
new QToolButton();
149 mToolButton->setText( tr(
"…" ) );
150 hl->addWidget( mToolButton );
156 for (
int row = 0; row < mParam->numberRows(); ++row )
158 for (
int col = 0; col < mParam->headers().count(); ++col )
160 mTable.append(
'0' );
163 mLineEdit->setText( tr(
"Fixed table (%1x%2)" ).arg( mParam->numberRows() ).arg( mParam->headers().count() ) );
166 connect( mToolButton, &QToolButton::clicked,
this, &QgsProcessingMatrixParameterPanel::showDialog );
169 void QgsProcessingMatrixParameterPanel::setValue(
const QVariantList &value )
176 void QgsProcessingMatrixParameterPanel::showDialog()
178 QgsProcessingMatrixParameterDialog dlg(
this,
nullptr, mParam, mTable );
181 setValue( dlg.table() );
185 void QgsProcessingMatrixParameterPanel::updateSummaryText()
188 mLineEdit->setText( tr(
"Fixed table (%1x%2)" ).arg( mTable.count() / mParam->headers().count() ).arg( mParam->headers().count() ) );
bool hasFixedNumberRows() const
Returns whether the table has a fixed number of rows.
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.