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