QGIS API Documentation 3.99.0-Master (c22de0620c0)
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
18#include "qgsgui.h"
19
20#include <QInputDialog>
21#include <QLineEdit>
22#include <QMessageBox>
23#include <QToolButton>
24
25#include "moc_qgsprocessingmatrixmodelerwidget.cpp"
26
28
29QgsProcessingMatrixModelerWidget::QgsProcessingMatrixModelerWidget( QWidget *parent )
30 : QWidget( parent )
31{
32 setupUi( this );
33
34 mModel = new QStandardItemModel( this );
35 mModel->appendColumn( QList<QStandardItem *>() << new QStandardItem( '0' ) );
36 mTableView->setModel( mModel );
37
38 connect( mButtonAddColumn, &QToolButton::clicked, this, &QgsProcessingMatrixModelerWidget::addColumn );
39 connect( mButtonRemoveColumn, &QToolButton::clicked, this, &QgsProcessingMatrixModelerWidget::removeColumns );
40 connect( mButtonAddRow, &QToolButton::clicked, this, &QgsProcessingMatrixModelerWidget::addRow );
41 connect( mButtonRemoveRow, &QToolButton::clicked, this, &QgsProcessingMatrixModelerWidget::removeRows );
42 connect( mButtonClear, &QToolButton::clicked, this, &QgsProcessingMatrixModelerWidget::clearTable );
43 connect( mTableView->horizontalHeader(), &QHeaderView::sectionDoubleClicked, this, &QgsProcessingMatrixModelerWidget::changeHeader );
44}
45
46void QgsProcessingMatrixModelerWidget::addColumn()
47{
48 QList<QStandardItem *> items;
49 for ( int i = 0; i < mModel->rowCount(); ++i )
50 items << new QStandardItem( '0' );
51
52 mModel->appendColumn( items );
53
54 emit changed();
55}
56
57void QgsProcessingMatrixModelerWidget::removeColumns()
58{
59 QModelIndexList selected = mTableView->selectionModel()->selectedColumns();
60 std::sort( selected.begin(), selected.end(), []( const QModelIndex &a, const QModelIndex &b ) { return b < a; } );
61
62 mTableView->setUpdatesEnabled( false );
63 for ( QModelIndex i : std::as_const( selected ) )
64 mModel->removeColumns( i.column(), 1 );
65
66 mTableView->setUpdatesEnabled( true );
67
68 emit changed();
69}
70
71void QgsProcessingMatrixModelerWidget::addRow()
72{
73 QList<QStandardItem *> items;
74 for ( int i = 0; i < mModel->columnCount(); ++i )
75 items << new QStandardItem( '0' );
76
77 mModel->appendRow( items );
78
79 emit changed();
80}
81
82void QgsProcessingMatrixModelerWidget::removeRows()
83{
84 QModelIndexList selected = mTableView->selectionModel()->selectedRows();
85 std::sort( selected.begin(), selected.end(), []( const QModelIndex &a, const QModelIndex &b ) { return b < a; } );
86
87 mTableView->setUpdatesEnabled( false );
88 for ( QModelIndex i : std::as_const( selected ) )
89 mModel->removeRows( i.row(), 1 );
90
91 mTableView->setUpdatesEnabled( true );
92
93 emit changed();
94}
95
96void QgsProcessingMatrixModelerWidget::clearTable()
97{
98 if ( QMessageBox::question( nullptr, tr( "Clear table" ), tr( "Are you sure you want to clear table?" ), QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) == QMessageBox::Yes )
99 {
100 mModel->clear();
101 emit changed();
102 }
103}
104
105void QgsProcessingMatrixModelerWidget::changeHeader( int index )
106{
107 bool ok;
108 QString text = QInputDialog::getText( nullptr, tr( "Enter column name" ), tr( "Column name" ), QLineEdit::Normal, QString(), &ok );
109 if ( ok && !text.isEmpty() )
110 {
111 mModel->setHeaderData( index, Qt::Horizontal, text );
112 emit changed();
113 }
114}
115
116QStringList QgsProcessingMatrixModelerWidget::headers() const
117{
118 QStringList headers;
119 for ( int i = 0; i < mModel->columnCount(); ++i )
120 {
121 headers << mModel->headerData( i, Qt::Horizontal ).toString();
122 }
123 return headers;
124}
125
126QVariant QgsProcessingMatrixModelerWidget::value() const
127{
128 QVariantList defaults;
129 const int cols = mModel->columnCount();
130 const int rows = mModel->rowCount();
131
132 for ( int row = 0; row < rows; ++row )
133 {
134 for ( int col = 0; col < cols; ++col )
135 {
136 defaults << mModel->item( row, col )->text();
137 }
138 }
139
140 QVariant val( defaults );
141 return val;
142}
143
144void QgsProcessingMatrixModelerWidget::setValue( const QStringList &headers, const QVariant &defaultValue )
145{
146 QVariantList contents = defaultValue.toList();
147
148 const int cols = headers.count();
149 const int rows = contents.count() / cols;
150
151 mModel->setRowCount( rows );
152 mModel->setColumnCount( cols );
153 mModel->setHorizontalHeaderLabels( headers );
154
155 for ( int row = 0; row < rows; ++row )
156 {
157 for ( int col = 0; col < cols; ++col )
158 {
159 QStandardItem *item = new QStandardItem( contents.at( row * cols + col ).toString() );
160 mModel->setItem( row, col, item );
161 }
162 }
163 mTableView->setModel( mModel );
164 emit changed();
165}
166
167bool QgsProcessingMatrixModelerWidget::fixedRows() const
168{
169 return mFixedRows->isChecked();
170}
171
172void QgsProcessingMatrixModelerWidget::setFixedRows( bool fixedRows )
173{
174 mFixedRows->setChecked( fixedRows );
175 emit changed();
176}
177