17#include "moc_qgsprocessingenummodelerwidget.cpp"
24QgsProcessingEnumModelerWidget::QgsProcessingEnumModelerWidget( QWidget *parent )
29 mModel =
new QStandardItemModel(
this );
30 mItemList->setModel( mModel );
31 connect( mModel, &QStandardItemModel::itemChanged,
this, &QgsProcessingEnumModelerWidget::onItemChanged );
33 connect( mButtonAdd, &QToolButton::clicked,
this, &QgsProcessingEnumModelerWidget::addItem );
34 connect( mButtonRemove, &QToolButton::clicked,
this, [ = ] { removeItems(
false ); } );
35 connect( mButtonClear, &QToolButton::clicked,
this, [ = ] { removeItems(
true ); } );
38void QgsProcessingEnumModelerWidget::addItem()
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 );
47void QgsProcessingEnumModelerWidget::removeItems(
const bool removeAll )
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 )
58 QModelIndexList selected = mItemList->selectionModel()->selectedIndexes();
60 rows.reserve( selected.count() );
61 for (
const QModelIndex &i : selected )
64 QList< int > rowsToDelete = qgis::setToList( rows );
65 std::sort( rowsToDelete.begin(), rowsToDelete.end(), std::greater<int>() );
67 mItemList->setUpdatesEnabled(
false );
68 for (
int i : std::as_const( rowsToDelete ) )
69 mModel->removeRows( i, 1 );
70 mItemList->setUpdatesEnabled(
true );
74void QgsProcessingEnumModelerWidget::onItemChanged( QStandardItem *item )
76 int checkedItemIndex = -1;
77 for (
int i = 0; i < mModel->rowCount(); i++ )
79 QStandardItem *itm = mModel->item( i );
80 if ( itm->checkState() == Qt::Checked && itm->data() == Qt::Checked )
87 mModel->blockSignals(
true );
88 if ( checkedItemIndex < 0 )
90 item->setData( item->checkState() );
94 if ( mAllowMultiple->isChecked() )
96 item->setData( item->checkState() );
100 mModel->item( checkedItemIndex )->setCheckState( Qt::Unchecked );
101 mModel->item( checkedItemIndex )->setData( Qt::Unchecked );
102 item->setData( item->checkState() );
105 mModel->blockSignals(
false );
108QStringList QgsProcessingEnumModelerWidget::options()
const
111 options.reserve( mModel->rowCount() );
112 for (
int i = 0; i < mModel->rowCount(); ++i )
114 options << mModel->item( i )->text();
119void QgsProcessingEnumModelerWidget::setOptions(
const QStringList &options )
121 for (
const QString &option : options )
123 QStandardItem *item =
new QStandardItem( option );
124 item->setCheckable(
true );
125 item->setDropEnabled(
false );
126 item->setData( Qt::Unchecked );
127 mModel->appendRow( item );
131QVariant QgsProcessingEnumModelerWidget::defaultOptions()
const
133 QVariantList defaults;
134 for (
int i = 0; i < mModel->rowCount(); ++i )
136 if ( mModel->item( i )->checkState() == Qt::Checked )
139 QVariant val( defaults );
143void QgsProcessingEnumModelerWidget::setDefaultOptions(
const QVariant &defaultValue )
145 if ( !defaultValue.isValid() )
148 QVariant val = defaultValue;
150 if ( val.userType() == QMetaType::Type::QVariantList || val.userType() == QMetaType::Type::QStringList )
152 for (
const QVariant &var : val.toList() )
153 values << var.toInt();
155 else if ( val.userType() == QMetaType::Type::QString )
157 QStringList split = val.toString().split(
',' );
158 for (
const QString &var : split )
159 values << var.toInt();
161 else if ( val.userType() == QMetaType::Type::Int )
163 values << val.toInt();
167 for (
const int &i : values )
169 item = mModel->item( i );
172 item->setCheckState( Qt::Checked );
173 item->setData( Qt::Checked );
178bool QgsProcessingEnumModelerWidget::allowMultiple()
const
180 return mAllowMultiple->isChecked();
183void QgsProcessingEnumModelerWidget::setAllowMultiple(
bool allowMultiple )
185 mAllowMultiple->setChecked( allowMultiple );