17#include "moc_qgsprocessingmatrixmodelerwidget.cpp"
19#include <QInputDialog>
26QgsProcessingMatrixModelerWidget::QgsProcessingMatrixModelerWidget( QWidget *parent )
31 mModel =
new QStandardItemModel(
this );
32 mModel->appendColumn( QList<QStandardItem *>() <<
new QStandardItem(
'0' ) );
33 mTableView->setModel( mModel );
35 connect( mButtonAddColumn, &QToolButton::clicked,
this, &QgsProcessingMatrixModelerWidget::addColumn );
36 connect( mButtonRemoveColumn, &QToolButton::clicked,
this, &QgsProcessingMatrixModelerWidget::removeColumns );
37 connect( mButtonAddRow, &QToolButton::clicked,
this, &QgsProcessingMatrixModelerWidget::addRow );
38 connect( mButtonRemoveRow, &QToolButton::clicked,
this, &QgsProcessingMatrixModelerWidget::removeRows );
39 connect( mButtonClear, &QToolButton::clicked,
this, &QgsProcessingMatrixModelerWidget::clearTable );
40 connect( mTableView->horizontalHeader(), &QHeaderView::sectionDoubleClicked,
this, &QgsProcessingMatrixModelerWidget::changeHeader );
43void QgsProcessingMatrixModelerWidget::addColumn()
45 QList<QStandardItem *> items;
46 for (
int i = 0; i < mModel->rowCount(); ++i )
47 items <<
new QStandardItem(
'0' );
49 mModel->appendColumn( items );
52void QgsProcessingMatrixModelerWidget::removeColumns()
54 QModelIndexList selected = mTableView->selectionModel()->selectedColumns();
55 std::sort( selected.begin(), selected.end(), [](
const QModelIndex &a,
const QModelIndex &b ) { return b < a; } );
57 mTableView->setUpdatesEnabled(
false );
58 for ( QModelIndex i : std::as_const( selected ) )
59 mModel->removeColumns( i.column(), 1 );
61 mTableView->setUpdatesEnabled(
true );
64void QgsProcessingMatrixModelerWidget::addRow()
66 QList<QStandardItem *> items;
67 for (
int i = 0; i < mModel->columnCount(); ++i )
68 items <<
new QStandardItem(
'0' );
70 mModel->appendRow( items );
73void QgsProcessingMatrixModelerWidget::removeRows()
75 QModelIndexList selected = mTableView->selectionModel()->selectedRows();
76 std::sort( selected.begin(), selected.end(), [](
const QModelIndex &a,
const QModelIndex &b ) { return b < a; } );
78 mTableView->setUpdatesEnabled(
false );
79 for ( QModelIndex i : std::as_const( selected ) )
80 mModel->removeRows( i.row(), 1 );
82 mTableView->setUpdatesEnabled(
true );
85void QgsProcessingMatrixModelerWidget::clearTable()
87 if ( QMessageBox::question(
nullptr, tr(
"Clear table" ), tr(
"Are you sure you want to clear table?" ), QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) == QMessageBox::Yes )
91void QgsProcessingMatrixModelerWidget::changeHeader(
int index )
94 QString text = QInputDialog::getText(
nullptr, tr(
"Enter column name" ), tr(
"Column name" ), QLineEdit::Normal, QString(), &ok );
95 if ( ok && !text.isEmpty() )
96 mModel->setHeaderData( index, Qt::Horizontal, text );
99QStringList QgsProcessingMatrixModelerWidget::headers()
const
102 for (
int i = 0; i < mModel->columnCount(); ++i )
104 headers << mModel->headerData( i, Qt::Horizontal ).toString();
109QVariant QgsProcessingMatrixModelerWidget::value()
const
111 QVariantList defaults;
112 const int cols = mModel->columnCount();
113 const int rows = mModel->rowCount();
115 for (
int row = 0; row < rows; ++row )
117 for (
int col = 0; col < cols; ++col )
119 defaults << mModel->item( row, col )->text();
123 QVariant val( defaults );
127void QgsProcessingMatrixModelerWidget::setValue(
const QStringList &headers,
const QVariant &defaultValue )
129 QVariantList contents = defaultValue.toList();
131 const int cols = headers.count();
132 const int rows = contents.count() / cols;
134 mModel->setRowCount( rows );
135 mModel->setColumnCount( cols );
136 mModel->setHorizontalHeaderLabels( 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 mTableView->setModel( mModel );
149bool QgsProcessingMatrixModelerWidget::fixedRows()
const
151 return mFixedRows->isChecked();
154void QgsProcessingMatrixModelerWidget::setFixedRows(
bool fixedRows )
156 mFixedRows->setChecked( fixedRows );