QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
55void QgsProcessingMatrixModelerWidget::removeColumns()
56{
57 QModelIndexList selected = mTableView->selectionModel()->selectedColumns();
58 std::sort( selected.begin(), selected.end(), []( const QModelIndex &a, const QModelIndex &b ) { return b < a; } );
59
60 mTableView->setUpdatesEnabled( false );
61 for ( QModelIndex i : std::as_const( selected ) )
62 mModel->removeColumns( i.column(), 1 );
63
64 mTableView->setUpdatesEnabled( true );
65}
66
67void QgsProcessingMatrixModelerWidget::addRow()
68{
69 QList<QStandardItem *> items;
70 for ( int i = 0; i < mModel->columnCount(); ++i )
71 items << new QStandardItem( '0' );
72
73 mModel->appendRow( items );
74}
75
76void QgsProcessingMatrixModelerWidget::removeRows()
77{
78 QModelIndexList selected = mTableView->selectionModel()->selectedRows();
79 std::sort( selected.begin(), selected.end(), []( const QModelIndex &a, const QModelIndex &b ) { return b < a; } );
80
81 mTableView->setUpdatesEnabled( false );
82 for ( QModelIndex i : std::as_const( selected ) )
83 mModel->removeRows( i.row(), 1 );
84
85 mTableView->setUpdatesEnabled( true );
86}
87
88void QgsProcessingMatrixModelerWidget::clearTable()
89{
90 if ( QMessageBox::question( nullptr, tr( "Clear table" ), tr( "Are you sure you want to clear table?" ), QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) == QMessageBox::Yes )
91 mModel->clear();
92}
93
94void QgsProcessingMatrixModelerWidget::changeHeader( int index )
95{
96 bool ok;
97 QString text = QInputDialog::getText( nullptr, tr( "Enter column name" ), tr( "Column name" ), QLineEdit::Normal, QString(), &ok );
98 if ( ok && !text.isEmpty() )
99 mModel->setHeaderData( index, Qt::Horizontal, text );
100}
101
102QStringList QgsProcessingMatrixModelerWidget::headers() const
103{
104 QStringList headers;
105 for ( int i = 0; i < mModel->columnCount(); ++i )
106 {
107 headers << mModel->headerData( i, Qt::Horizontal ).toString();
108 }
109 return headers;
110}
111
112QVariant QgsProcessingMatrixModelerWidget::value() const
113{
114 QVariantList defaults;
115 const int cols = mModel->columnCount();
116 const int rows = mModel->rowCount();
117
118 for ( int row = 0; row < rows; ++row )
119 {
120 for ( int col = 0; col < cols; ++col )
121 {
122 defaults << mModel->item( row, col )->text();
123 }
124 }
125
126 QVariant val( defaults );
127 return val;
128}
129
130void QgsProcessingMatrixModelerWidget::setValue( const QStringList &headers, const QVariant &defaultValue )
131{
132 QVariantList contents = defaultValue.toList();
133
134 const int cols = headers.count();
135 const int rows = contents.count() / cols;
136
137 mModel->setRowCount( rows );
138 mModel->setColumnCount( cols );
139 mModel->setHorizontalHeaderLabels( headers );
140
141 for ( int row = 0; row < rows; ++row )
142 {
143 for ( int col = 0; col < cols; ++col )
144 {
145 QStandardItem *item = new QStandardItem( contents.at( row * cols + col ).toString() );
146 mModel->setItem( row, col, item );
147 }
148 }
149 mTableView->setModel( mModel );
150}
151
152bool QgsProcessingMatrixModelerWidget::fixedRows() const
153{
154 return mFixedRows->isChecked();
155}
156
157void QgsProcessingMatrixModelerWidget::setFixedRows( bool fixedRows )
158{
159 mFixedRows->setChecked( fixedRows );
160}
161