QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
qgsprocessingmatrixmodelerwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprocessingmatrixmodelerwidget.cpp
3 ------------------------------------
4 Date : March 2020
5 Copyright : (C) 2020 Alexander Bruy
6 Email : alexander dot bruy at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17#include "moc_qgsprocessingmatrixmodelerwidget.cpp"
18#include "qgsgui.h"
19#include <QInputDialog>
20#include <QMessageBox>
21#include <QLineEdit>
22#include <QToolButton>
23
25
26QgsProcessingMatrixModelerWidget::QgsProcessingMatrixModelerWidget( QWidget *parent )
27 : QWidget( parent )
28{
29 setupUi( this );
30
31 mModel = new QStandardItemModel( this );
32 mModel->appendColumn( QList<QStandardItem *>() << new QStandardItem( '0' ) );
33 mTableView->setModel( mModel );
34
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 );
41}
42
43void QgsProcessingMatrixModelerWidget::addColumn()
44{
45 QList<QStandardItem *> items;
46 for ( int i = 0; i < mModel->rowCount(); ++i )
47 items << new QStandardItem( '0' );
48
49 mModel->appendColumn( items );
50}
51
52void QgsProcessingMatrixModelerWidget::removeColumns()
53{
54 QModelIndexList selected = mTableView->selectionModel()->selectedColumns();
55 std::sort( selected.begin(), selected.end(), []( const QModelIndex &a, const QModelIndex &b ) { return b < a; } );
56
57 mTableView->setUpdatesEnabled( false );
58 for ( QModelIndex i : std::as_const( selected ) )
59 mModel->removeColumns( i.column(), 1 );
60
61 mTableView->setUpdatesEnabled( true );
62}
63
64void QgsProcessingMatrixModelerWidget::addRow()
65{
66 QList<QStandardItem *> items;
67 for ( int i = 0; i < mModel->columnCount(); ++i )
68 items << new QStandardItem( '0' );
69
70 mModel->appendRow( items );
71}
72
73void QgsProcessingMatrixModelerWidget::removeRows()
74{
75 QModelIndexList selected = mTableView->selectionModel()->selectedRows();
76 std::sort( selected.begin(), selected.end(), []( const QModelIndex &a, const QModelIndex &b ) { return b < a; } );
77
78 mTableView->setUpdatesEnabled( false );
79 for ( QModelIndex i : std::as_const( selected ) )
80 mModel->removeRows( i.row(), 1 );
81
82 mTableView->setUpdatesEnabled( true );
83}
84
85void QgsProcessingMatrixModelerWidget::clearTable()
86{
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 )
88 mModel->clear();
89}
90
91void QgsProcessingMatrixModelerWidget::changeHeader( int index )
92{
93 bool ok;
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 );
97}
98
99QStringList QgsProcessingMatrixModelerWidget::headers() const
100{
101 QStringList headers;
102 for ( int i = 0; i < mModel->columnCount(); ++i )
103 {
104 headers << mModel->headerData( i, Qt::Horizontal ).toString();
105 }
106 return headers;
107}
108
109QVariant QgsProcessingMatrixModelerWidget::value() const
110{
111 QVariantList defaults;
112 const int cols = mModel->columnCount();
113 const int rows = mModel->rowCount();
114
115 for ( int row = 0; row < rows; ++row )
116 {
117 for ( int col = 0; col < cols; ++col )
118 {
119 defaults << mModel->item( row, col )->text();
120 }
121 }
122
123 QVariant val( defaults );
124 return val;
125}
126
127void QgsProcessingMatrixModelerWidget::setValue( const QStringList &headers, const QVariant &defaultValue )
128{
129 QVariantList contents = defaultValue.toList();
130
131 const int cols = headers.count();
132 const int rows = contents.count() / cols;
133
134 mModel->setRowCount( rows );
135 mModel->setColumnCount( cols );
136 mModel->setHorizontalHeaderLabels( headers );
137
138 for ( int row = 0; row < rows; ++row )
139 {
140 for ( int col = 0; col < cols; ++col )
141 {
142 QStandardItem *item = new QStandardItem( contents.at( row * cols + col ).toString() );
143 mModel->setItem( row, col, item );
144 }
145 }
146 mTableView->setModel( mModel );
147}
148
149bool QgsProcessingMatrixModelerWidget::fixedRows() const
150{
151 return mFixedRows->isChecked();
152}
153
154void QgsProcessingMatrixModelerWidget::setFixedRows( bool fixedRows )
155{
156 mFixedRows->setChecked( fixedRows );
157}
158