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" ), tr(
"Are you sure you want to delete all items" ), QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) == QMessageBox::Yes )
56 QModelIndexList selected = mItemList->selectionModel()->selectedIndexes();
58 rows.reserve( selected.count() );
59 for (
const QModelIndex &i : selected )
62 QList<int> rowsToDelete = qgis::setToList( rows );
63 std::sort( rowsToDelete.begin(), rowsToDelete.end(), std::greater<int>() );
65 mItemList->setUpdatesEnabled(
false );
66 for (
int i : std::as_const( rowsToDelete ) )
67 mModel->removeRows( i, 1 );
68 mItemList->setUpdatesEnabled(
true );
72void QgsProcessingEnumModelerWidget::onItemChanged( QStandardItem *item )
74 int checkedItemIndex = -1;
75 for (
int i = 0; i < mModel->rowCount(); i++ )
77 QStandardItem *itm = mModel->item( i );
78 if ( itm->checkState() == Qt::Checked && itm->data() == Qt::Checked )
85 mModel->blockSignals(
true );
86 if ( checkedItemIndex < 0 )
88 item->setData( item->checkState() );
92 if ( mAllowMultiple->isChecked() )
94 item->setData( item->checkState() );
98 mModel->item( checkedItemIndex )->setCheckState( Qt::Unchecked );
99 mModel->item( checkedItemIndex )->setData( Qt::Unchecked );
100 item->setData( item->checkState() );
103 mModel->blockSignals(
false );
106QStringList QgsProcessingEnumModelerWidget::options()
const
109 options.reserve( mModel->rowCount() );
110 for (
int i = 0; i < mModel->rowCount(); ++i )
112 options << mModel->item( i )->text();
117void QgsProcessingEnumModelerWidget::setOptions(
const QStringList &options )
119 for (
const QString &option : options )
121 QStandardItem *item =
new QStandardItem( option );
122 item->setCheckable(
true );
123 item->setDropEnabled(
false );
124 item->setData( Qt::Unchecked );
125 mModel->appendRow( item );
129QVariant QgsProcessingEnumModelerWidget::defaultOptions()
const
131 QVariantList defaults;
132 for (
int i = 0; i < mModel->rowCount(); ++i )
134 if ( mModel->item( i )->checkState() == Qt::Checked )
137 QVariant val( defaults );
141void QgsProcessingEnumModelerWidget::setDefaultOptions(
const QVariant &defaultValue )
143 if ( !defaultValue.isValid() )
146 QVariant val = defaultValue;
148 if ( val.userType() == QMetaType::Type::QVariantList || val.userType() == QMetaType::Type::QStringList )
150 for (
const QVariant &var : val.toList() )
151 values << var.toInt();
153 else if ( val.userType() == QMetaType::Type::QString )
155 QStringList split = val.toString().split(
',' );
156 for (
const QString &var : split )
157 values << var.toInt();
159 else if ( val.userType() == QMetaType::Type::Int )
161 values << val.toInt();
165 for (
const int &i : values )
167 item = mModel->item( i );
170 item->setCheckState( Qt::Checked );
171 item->setData( Qt::Checked );
176bool QgsProcessingEnumModelerWidget::allowMultiple()
const
178 return mAllowMultiple->isChecked();
181void QgsProcessingEnumModelerWidget::setAllowMultiple(
bool allowMultiple )
183 mAllowMultiple->setChecked( allowMultiple );