18 #include <QInputDialog>
19 #include <QMessageBox>
21 #include <QToolButton>
25 QgsProcessingMatrixModelerWidget::QgsProcessingMatrixModelerWidget( QWidget *parent )
30 mModel =
new QStandardItemModel(
this );
31 mModel->appendColumn( QList< QStandardItem * >() <<
new QStandardItem(
'0' ) );
32 mTableView->setModel( mModel );
34 connect( mButtonAddColumn, &QToolButton::clicked,
this, &QgsProcessingMatrixModelerWidget::addColumn );
35 connect( mButtonRemoveColumn, &QToolButton::clicked,
this, &QgsProcessingMatrixModelerWidget::removeColumns );
36 connect( mButtonAddRow, &QToolButton::clicked,
this, &QgsProcessingMatrixModelerWidget::addRow );
37 connect( mButtonRemoveRow, &QToolButton::clicked,
this, &QgsProcessingMatrixModelerWidget::removeRows );
38 connect( mButtonClear, &QToolButton::clicked,
this, &QgsProcessingMatrixModelerWidget::clearTable );
39 connect( mTableView->horizontalHeader(), &QHeaderView::sectionDoubleClicked,
this, &QgsProcessingMatrixModelerWidget::changeHeader );
42 void QgsProcessingMatrixModelerWidget::addColumn()
44 QList< QStandardItem * > items;
45 for (
int i = 0; i < mModel->rowCount(); ++i )
46 items <<
new QStandardItem(
'0' );
48 mModel->appendColumn( items );
51 void QgsProcessingMatrixModelerWidget::removeColumns()
53 QModelIndexList selected = mTableView->selectionModel()->selectedColumns();
54 std::sort( selected.begin(), selected.end(), [](
const QModelIndex & a,
const QModelIndex & b ) { return b < a; } );
56 mTableView->setUpdatesEnabled(
false );
57 for ( QModelIndex i : std::as_const( selected ) )
58 mModel->removeColumns( i.column(), 1 );
60 mTableView->setUpdatesEnabled(
true );
63 void QgsProcessingMatrixModelerWidget::addRow()
65 QList< QStandardItem * > items;
66 for (
int i = 0; i < mModel->columnCount(); ++i )
67 items <<
new QStandardItem(
'0' );
69 mModel->appendRow( items );
72 void QgsProcessingMatrixModelerWidget::removeRows()
74 QModelIndexList selected = mTableView->selectionModel()->selectedRows();
75 std::sort( selected.begin(), selected.end(), [](
const QModelIndex & a,
const QModelIndex & b ) { return b < a; } );
77 mTableView->setUpdatesEnabled(
false );
78 for ( QModelIndex i : std::as_const( selected ) )
79 mModel->removeRows( i.row(), 1 );
81 mTableView->setUpdatesEnabled(
true );
84 void QgsProcessingMatrixModelerWidget::clearTable()
86 if ( QMessageBox::question(
nullptr, tr(
"Clear table" ),
87 tr(
"Are you sure you want to clear table?" ),
88 QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) == QMessageBox::Yes )
92 void QgsProcessingMatrixModelerWidget::changeHeader(
int index )
95 QString text = QInputDialog::getText(
nullptr, tr(
"Enter column name" ),
96 tr(
"Column name" ), QLineEdit::Normal,
98 if ( ok && !text.isEmpty() )
99 mModel->setHeaderData( index, Qt::Horizontal, text );
102 QStringList QgsProcessingMatrixModelerWidget::headers()
const
105 for (
int i = 0; i < mModel->columnCount(); ++i )
107 headers << mModel->headerData( i, Qt::Horizontal ).toString();
112 QVariant QgsProcessingMatrixModelerWidget::value()
const
114 QVariantList defaults;
115 const int cols = mModel->columnCount();
116 const int rows = mModel->rowCount();
118 for (
int row = 0; row < rows; ++row )
120 for (
int col = 0; col < cols; ++col )
122 defaults << mModel->item( row, col )->text();
126 QVariant val( defaults );
130 void QgsProcessingMatrixModelerWidget::setValue(
const QStringList &headers,
const QVariant &defaultValue )
132 QVariantList contents = defaultValue.toList();
134 const int cols = headers.count();
135 const int rows = contents.count() / cols;
137 mModel->setRowCount( rows );
138 mModel->setColumnCount( cols );
139 mModel->setHorizontalHeaderLabels( headers );
141 for (
int row = 0; row < rows; ++row )
143 for (
int col = 0; col < cols; ++col )
145 QStandardItem *item =
new QStandardItem( contents.at( row * cols + col ).toString() );
146 mModel->setItem( row, col, item );
149 mTableView->setModel( mModel );
152 bool QgsProcessingMatrixModelerWidget::fixedRows()
const
154 return mFixedRows->isChecked();
157 void QgsProcessingMatrixModelerWidget::setFixedRows(
bool fixedRows )
159 mFixedRows->setChecked( fixedRows );