QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
qgsprocessingenummodelerwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprocessingenummodelerwidget.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_qgsprocessingenummodelerwidget.cpp"
18#include "qgsgui.h"
19#include <QMessageBox>
20#include <QToolButton>
21
23
24QgsProcessingEnumModelerWidget::QgsProcessingEnumModelerWidget( QWidget *parent )
25 : QWidget( parent )
26{
27 setupUi( this );
28
29 mModel = new QStandardItemModel( this );
30 mItemList->setModel( mModel );
31 connect( mModel, &QStandardItemModel::itemChanged, this, &QgsProcessingEnumModelerWidget::onItemChanged );
32
33 connect( mButtonAdd, &QToolButton::clicked, this, &QgsProcessingEnumModelerWidget::addItem );
34 connect( mButtonRemove, &QToolButton::clicked, this, [=] { removeItems( false ); } );
35 connect( mButtonClear, &QToolButton::clicked, this, [=] { removeItems( true ); } );
36}
37
38void QgsProcessingEnumModelerWidget::addItem()
39{
40 QStandardItem *item = new QStandardItem( tr( "new item" ) );
41 item->setCheckable( true );
42 item->setDropEnabled( false );
43 item->setData( Qt::Unchecked );
44 mModel->appendRow( item );
45}
46
47void QgsProcessingEnumModelerWidget::removeItems( const bool removeAll )
48{
49 if ( removeAll )
50 {
51 if ( QMessageBox::question( nullptr, tr( "Delete items" ), tr( "Are you sure you want to delete all items" ), QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) == QMessageBox::Yes )
52 mModel->clear();
53 }
54 else
55 {
56 QModelIndexList selected = mItemList->selectionModel()->selectedIndexes();
57 QSet<int> rows;
58 rows.reserve( selected.count() );
59 for ( const QModelIndex &i : selected )
60 rows << i.row();
61
62 QList<int> rowsToDelete = qgis::setToList( rows );
63 std::sort( rowsToDelete.begin(), rowsToDelete.end(), std::greater<int>() );
64
65 mItemList->setUpdatesEnabled( false );
66 for ( int i : std::as_const( rowsToDelete ) )
67 mModel->removeRows( i, 1 );
68 mItemList->setUpdatesEnabled( true );
69 }
70}
71
72void QgsProcessingEnumModelerWidget::onItemChanged( QStandardItem *item )
73{
74 int checkedItemIndex = -1;
75 for ( int i = 0; i < mModel->rowCount(); i++ )
76 {
77 QStandardItem *itm = mModel->item( i );
78 if ( itm->checkState() == Qt::Checked && itm->data() == Qt::Checked )
79 {
80 checkedItemIndex = i;
81 break;
82 }
83 }
84
85 mModel->blockSignals( true );
86 if ( checkedItemIndex < 0 )
87 {
88 item->setData( item->checkState() );
89 }
90 else
91 {
92 if ( mAllowMultiple->isChecked() )
93 {
94 item->setData( item->checkState() );
95 }
96 else
97 {
98 mModel->item( checkedItemIndex )->setCheckState( Qt::Unchecked );
99 mModel->item( checkedItemIndex )->setData( Qt::Unchecked );
100 item->setData( item->checkState() );
101 }
102 }
103 mModel->blockSignals( false );
104}
105
106QStringList QgsProcessingEnumModelerWidget::options() const
107{
108 QStringList options;
109 options.reserve( mModel->rowCount() );
110 for ( int i = 0; i < mModel->rowCount(); ++i )
111 {
112 options << mModel->item( i )->text();
113 }
114 return options;
115}
116
117void QgsProcessingEnumModelerWidget::setOptions( const QStringList &options )
118{
119 for ( const QString &option : options )
120 {
121 QStandardItem *item = new QStandardItem( option );
122 item->setCheckable( true );
123 item->setDropEnabled( false );
124 item->setData( Qt::Unchecked );
125 mModel->appendRow( item );
126 }
127}
128
129QVariant QgsProcessingEnumModelerWidget::defaultOptions() const
130{
131 QVariantList defaults;
132 for ( int i = 0; i < mModel->rowCount(); ++i )
133 {
134 if ( mModel->item( i )->checkState() == Qt::Checked )
135 defaults << i;
136 }
137 QVariant val( defaults );
138 return val;
139}
140
141void QgsProcessingEnumModelerWidget::setDefaultOptions( const QVariant &defaultValue )
142{
143 if ( !defaultValue.isValid() )
144 return;
145
146 QVariant val = defaultValue;
147 QList<int> values;
148 if ( val.userType() == QMetaType::Type::QVariantList || val.userType() == QMetaType::Type::QStringList )
149 {
150 for ( const QVariant &var : val.toList() )
151 values << var.toInt();
152 }
153 else if ( val.userType() == QMetaType::Type::QString )
154 {
155 QStringList split = val.toString().split( ',' );
156 for ( const QString &var : split )
157 values << var.toInt();
158 }
159 else if ( val.userType() == QMetaType::Type::Int )
160 {
161 values << val.toInt();
162 }
163
164 QStandardItem *item;
165 for ( const int &i : values )
166 {
167 item = mModel->item( i );
168 if ( item )
169 {
170 item->setCheckState( Qt::Checked );
171 item->setData( Qt::Checked );
172 }
173 }
174}
175
176bool QgsProcessingEnumModelerWidget::allowMultiple() const
177{
178 return mAllowMultiple->isChecked();
179}
180
181void QgsProcessingEnumModelerWidget::setAllowMultiple( bool allowMultiple )
182{
183 mAllowMultiple->setChecked( allowMultiple );
184}
185