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 : qgis::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();
152 hl->setContentsMargins( 0, 0, 0, 0 );
154 mLineEdit =
new QLineEdit();
155 mLineEdit->setEnabled(
false );
156 hl->addWidget( mLineEdit, 1 );
158 mToolButton =
new QToolButton();
159 mToolButton->setText( QString( QChar( 0x2026 ) ) );
160 hl->addWidget( mToolButton );
166 for (
int row = 0; row < mParam->numberRows(); ++row )
168 for (
int col = 0; col < mParam->headers().count(); ++col )
170 mTable.append(
'0' );
173 mLineEdit->setText( tr(
"Fixed table (%1x%2)" ).arg( mParam->numberRows() ).arg( mParam->headers().count() ) );
176 connect( mToolButton, &QToolButton::clicked,
this, &QgsProcessingMatrixParameterPanel::showDialog );
179 void QgsProcessingMatrixParameterPanel::setValue(
const QVariantList &value )
186 void QgsProcessingMatrixParameterPanel::showDialog()
190 QgsProcessingMatrixParameterPanelWidget *widget =
new QgsProcessingMatrixParameterPanelWidget(
this, mParam, mTable );
192 widget->setPanelTitle( mParam->description() );
194 panel->openPanel( widget );
198 setValue( widget->table() );
203 void QgsProcessingMatrixParameterPanel::updateSummaryText()
206 mLineEdit->setText( tr(
"Fixed table (%1x%2)" ).arg( mTable.count() / mParam->headers().count() ).arg( mParam->headers().count() ) );