QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
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 "qgsgui.h"
18#include <QInputDialog>
19#include <QMessageBox>
20#include <QLineEdit>
21#include <QToolButton>
22
24
25QgsProcessingMatrixModelerWidget::QgsProcessingMatrixModelerWidget( QWidget *parent )
26 : QWidget( parent )
27{
28 setupUi( this );
29
30 mModel = new QStandardItemModel( this );
31 mModel->appendColumn( QList< QStandardItem * >() << new QStandardItem( '0' ) );
32 mTableView->setModel( mModel );
33
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 );
40}
41
42void QgsProcessingMatrixModelerWidget::addColumn()
43{
44 QList< QStandardItem * > items;
45 for ( int i = 0; i < mModel->rowCount(); ++i )
46 items << new QStandardItem( '0' );
47
48 mModel->appendColumn( items );
49}
50
51void QgsProcessingMatrixModelerWidget::removeColumns()
52{
53 QModelIndexList selected = mTableView->selectionModel()->selectedColumns();
54 std::sort( selected.begin(), selected.end(), []( const QModelIndex & a, const QModelIndex & b ) { return b < a; } );
55
56 mTableView->setUpdatesEnabled( false );
57 for ( QModelIndex i : std::as_const( selected ) )
58 mModel->removeColumns( i.column(), 1 );
59
60 mTableView->setUpdatesEnabled( true );
61}
62
63void QgsProcessingMatrixModelerWidget::addRow()
64{
65 QList< QStandardItem * > items;
66 for ( int i = 0; i < mModel->columnCount(); ++i )
67 items << new QStandardItem( '0' );
68
69 mModel->appendRow( items );
70}
71
72void QgsProcessingMatrixModelerWidget::removeRows()
73{
74 QModelIndexList selected = mTableView->selectionModel()->selectedRows();
75 std::sort( selected.begin(), selected.end(), []( const QModelIndex & a, const QModelIndex & b ) { return b < a; } );
76
77 mTableView->setUpdatesEnabled( false );
78 for ( QModelIndex i : std::as_const( selected ) )
79 mModel->removeRows( i.row(), 1 );
80
81 mTableView->setUpdatesEnabled( true );
82}
83
84void QgsProcessingMatrixModelerWidget::clearTable()
85{
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 )
89 mModel->clear();
90}
91
92void QgsProcessingMatrixModelerWidget::changeHeader( int index )
93{
94 bool ok;
95 QString text = QInputDialog::getText( nullptr, tr( "Enter column name" ),
96 tr( "Column name" ), QLineEdit::Normal,
97 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