QGIS API Documentation 3.41.0-Master (3440c17df1d)
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" ),
52 tr( "Are you sure you want to delete all items" ),
53 QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) == QMessageBox::Yes )
54 mModel->clear();
55 }
56 else
57 {
58 QModelIndexList selected = mItemList->selectionModel()->selectedIndexes();
59 QSet< int > rows;
60 rows.reserve( selected.count() );
61 for ( const QModelIndex &i : selected )
62 rows << i.row();
63
64 QList< int > rowsToDelete = qgis::setToList( rows );
65 std::sort( rowsToDelete.begin(), rowsToDelete.end(), std::greater<int>() );
66
67 mItemList->setUpdatesEnabled( false );
68 for ( int i : std::as_const( rowsToDelete ) )
69 mModel->removeRows( i, 1 );
70 mItemList->setUpdatesEnabled( true );
71 }
72}
73
74void QgsProcessingEnumModelerWidget::onItemChanged( QStandardItem *item )
75{
76 int checkedItemIndex = -1;
77 for ( int i = 0; i < mModel->rowCount(); i++ )
78 {
79 QStandardItem *itm = mModel->item( i );
80 if ( itm->checkState() == Qt::Checked && itm->data() == Qt::Checked )
81 {
82 checkedItemIndex = i;
83 break;
84 }
85 }
86
87 mModel->blockSignals( true );
88 if ( checkedItemIndex < 0 )
89 {
90 item->setData( item->checkState() );
91 }
92 else
93 {
94 if ( mAllowMultiple->isChecked() )
95 {
96 item->setData( item->checkState() );
97 }
98 else
99 {
100 mModel->item( checkedItemIndex )->setCheckState( Qt::Unchecked );
101 mModel->item( checkedItemIndex )->setData( Qt::Unchecked );
102 item->setData( item->checkState() );
103 }
104 }
105 mModel->blockSignals( false );
106}
107
108QStringList QgsProcessingEnumModelerWidget::options() const
109{
110 QStringList options;
111 options.reserve( mModel->rowCount() );
112 for ( int i = 0; i < mModel->rowCount(); ++i )
113 {
114 options << mModel->item( i )->text();
115 }
116 return options;
117}
118
119void QgsProcessingEnumModelerWidget::setOptions( const QStringList &options )
120{
121 for ( const QString &option : options )
122 {
123 QStandardItem *item = new QStandardItem( option );
124 item->setCheckable( true );
125 item->setDropEnabled( false );
126 item->setData( Qt::Unchecked );
127 mModel->appendRow( item );
128 }
129}
130
131QVariant QgsProcessingEnumModelerWidget::defaultOptions() const
132{
133 QVariantList defaults;
134 for ( int i = 0; i < mModel->rowCount(); ++i )
135 {
136 if ( mModel->item( i )->checkState() == Qt::Checked )
137 defaults << i;
138 }
139 QVariant val( defaults );
140 return val;
141}
142
143void QgsProcessingEnumModelerWidget::setDefaultOptions( const QVariant &defaultValue )
144{
145 if ( !defaultValue.isValid() )
146 return;
147
148 QVariant val = defaultValue;
149 QList< int > values;
150 if ( val.userType() == QMetaType::Type::QVariantList || val.userType() == QMetaType::Type::QStringList )
151 {
152 for ( const QVariant &var : val.toList() )
153 values << var.toInt();
154 }
155 else if ( val.userType() == QMetaType::Type::QString )
156 {
157 QStringList split = val.toString().split( ',' );
158 for ( const QString &var : split )
159 values << var.toInt();
160 }
161 else if ( val.userType() == QMetaType::Type::Int )
162 {
163 values << val.toInt();
164 }
165
166 QStandardItem *item;
167 for ( const int &i : values )
168 {
169 item = mModel->item( i );
170 if ( item )
171 {
172 item->setCheckState( Qt::Checked );
173 item->setData( Qt::Checked );
174 }
175 }
176}
177
178bool QgsProcessingEnumModelerWidget::allowMultiple() const
179{
180 return mAllowMultiple->isChecked();
181}
182
183void QgsProcessingEnumModelerWidget::setAllowMultiple( bool allowMultiple )
184{
185 mAllowMultiple->setChecked( allowMultiple );
186}
187