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" ),
88 tr(
"Are you sure you want to clear table?" ),
89 QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) == QMessageBox::Yes )
93void QgsProcessingMatrixModelerWidget::changeHeader(
int index )
96 QString text = QInputDialog::getText(
nullptr, tr(
"Enter column name" ),
97 tr(
"Column name" ), QLineEdit::Normal,
99 if ( ok && !text.isEmpty() )
100 mModel->setHeaderData( index, Qt::Horizontal, text );
103QStringList QgsProcessingMatrixModelerWidget::headers()
const
106 for (
int i = 0; i < mModel->columnCount(); ++i )
108 headers << mModel->headerData( i, Qt::Horizontal ).toString();
113QVariant QgsProcessingMatrixModelerWidget::value()
const
115 QVariantList defaults;
116 const int cols = mModel->columnCount();
117 const int rows = mModel->rowCount();
119 for (
int row = 0; row < rows; ++row )
121 for (
int col = 0; col < cols; ++col )
123 defaults << mModel->item( row, col )->text();
127 QVariant val( defaults );
131void QgsProcessingMatrixModelerWidget::setValue(
const QStringList &headers,
const QVariant &defaultValue )
133 QVariantList contents = defaultValue.toList();
135 const int cols = headers.count();
136 const int rows = contents.count() / cols;
138 mModel->setRowCount( rows );
139 mModel->setColumnCount( cols );
140 mModel->setHorizontalHeaderLabels( headers );
142 for (
int row = 0; row < rows; ++row )
144 for (
int col = 0; col < cols; ++col )
146 QStandardItem *item =
new QStandardItem( contents.at( row * cols + col ).toString() );
147 mModel->setItem( row, col, item );
150 mTableView->setModel( mModel );
153bool QgsProcessingMatrixModelerWidget::fixedRows()
const
155 return mFixedRows->isChecked();
158void QgsProcessingMatrixModelerWidget::setFixedRows(
bool fixedRows )
160 mFixedRows->setChecked( fixedRows );