QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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" ),
88 tr( "Are you sure you want to clear table?" ),
89 QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) == QMessageBox::Yes )
90 mModel->clear();
91}
92
93void QgsProcessingMatrixModelerWidget::changeHeader( int index )
94{
95 bool ok;
96 QString text = QInputDialog::getText( nullptr, tr( "Enter column name" ),
97 tr( "Column name" ), QLineEdit::Normal,
98 QString(), &ok );
99 if ( ok && !text.isEmpty() )
100 mModel->setHeaderData( index, Qt::Horizontal, text );
101}
102
103QStringList QgsProcessingMatrixModelerWidget::headers() const
104{
105 QStringList headers;
106 for ( int i = 0; i < mModel->columnCount(); ++i )
107 {
108 headers << mModel->headerData( i, Qt::Horizontal ).toString();
109 }
110 return headers;
111}
112
113QVariant QgsProcessingMatrixModelerWidget::value() const
114{
115 QVariantList defaults;
116 const int cols = mModel->columnCount();
117 const int rows = mModel->rowCount();
118
119 for ( int row = 0; row < rows; ++row )
120 {
121 for ( int col = 0; col < cols; ++col )
122 {
123 defaults << mModel->item( row, col )->text();
124 }
125 }
126
127 QVariant val( defaults );
128 return val;
129}
130
131void QgsProcessingMatrixModelerWidget::setValue( const QStringList &headers, const QVariant &defaultValue )
132{
133 QVariantList contents = defaultValue.toList();
134
135 const int cols = headers.count();
136 const int rows = contents.count() / cols;
137
138 mModel->setRowCount( rows );
139 mModel->setColumnCount( cols );
140 mModel->setHorizontalHeaderLabels( headers );
141
142 for ( int row = 0; row < rows; ++row )
143 {
144 for ( int col = 0; col < cols; ++col )
145 {
146 QStandardItem *item = new QStandardItem( contents.at( row * cols + col ).toString() );
147 mModel->setItem( row, col, item );
148 }
149 }
150 mTableView->setModel( mModel );
151}
152
153bool QgsProcessingMatrixModelerWidget::fixedRows() const
154{
155 return mFixedRows->isChecked();
156}
157
158void QgsProcessingMatrixModelerWidget::setFixedRows( bool fixedRows )
159{
160 mFixedRows->setChecked( fixedRows );
161}
162