23QgsProcessingEnumModelerWidget::QgsProcessingEnumModelerWidget( QWidget *parent )
28 mModel =
new QStandardItemModel(
this );
29 mItemList->setModel( mModel );
30 connect( mModel, &QStandardItemModel::itemChanged,
this, &QgsProcessingEnumModelerWidget::onItemChanged );
32 connect( mButtonAdd, &QToolButton::clicked,
this, &QgsProcessingEnumModelerWidget::addItem );
33 connect( mButtonRemove, &QToolButton::clicked,
this, [ = ] { removeItems(
false ); } );
34 connect( mButtonClear, &QToolButton::clicked,
this, [ = ] { removeItems(
true ); } );
37void QgsProcessingEnumModelerWidget::addItem()
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 );
46void QgsProcessingEnumModelerWidget::removeItems(
const bool removeAll )
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 )
57 QModelIndexList selected = mItemList->selectionModel()->selectedIndexes();
59 rows.reserve( selected.count() );
60 for (
const QModelIndex &i : selected )
63 QList< int > rowsToDelete = qgis::setToList( rows );
64 std::sort( rowsToDelete.begin(), rowsToDelete.end(), std::greater<int>() );
66 mItemList->setUpdatesEnabled(
false );
67 for (
int i : std::as_const( rowsToDelete ) )
68 mModel->removeRows( i, 1 );
69 mItemList->setUpdatesEnabled(
true );
73void QgsProcessingEnumModelerWidget::onItemChanged( QStandardItem *item )
75 int checkedItemIndex = -1;
76 for (
int i = 0; i < mModel->rowCount(); i++ )
78 QStandardItem *itm = mModel->item( i );
79 if ( itm->checkState() == Qt::Checked && itm->data() == Qt::Checked )
86 mModel->blockSignals(
true );
87 if ( checkedItemIndex < 0 )
89 item->setData( item->checkState() );
93 if ( mAllowMultiple->isChecked() )
95 item->setData( item->checkState() );
99 mModel->item( checkedItemIndex )->setCheckState( Qt::Unchecked );
100 mModel->item( checkedItemIndex )->setData( Qt::Unchecked );
101 item->setData( item->checkState() );
104 mModel->blockSignals(
false );
107QStringList QgsProcessingEnumModelerWidget::options()
const
110 options.reserve( mModel->rowCount() );
111 for (
int i = 0; i < mModel->rowCount(); ++i )
113 options << mModel->item( i )->text();
118void QgsProcessingEnumModelerWidget::setOptions(
const QStringList &options )
120 for (
const QString &option : options )
122 QStandardItem *item =
new QStandardItem( option );
123 item->setCheckable(
true );
124 item->setDropEnabled(
false );
125 item->setData( Qt::Unchecked );
126 mModel->appendRow( item );
130QVariant QgsProcessingEnumModelerWidget::defaultOptions()
const
132 QVariantList defaults;
133 for (
int i = 0; i < mModel->rowCount(); ++i )
135 if ( mModel->item( i )->checkState() == Qt::Checked )
138 QVariant val( defaults );
142void QgsProcessingEnumModelerWidget::setDefaultOptions(
const QVariant &defaultValue )
144 if ( !defaultValue.isValid() )
147 QVariant val = defaultValue;
149 if ( val.type() == QVariant::List || val.type() == QVariant::StringList )
151 for (
const QVariant &var : val.toList() )
152 values << var.toInt();
154 else if ( val.type() == QVariant::String )
156 QStringList split = val.toString().split(
',' );
157 for (
const QString &var : split )
158 values << var.toInt();
160 else if ( val.type() == QVariant::Int )
162 values << val.toInt();
166 for (
const int &i : values )
168 item = mModel->item( i );
171 item->setCheckState( Qt::Checked );
172 item->setData( Qt::Checked );
177bool QgsProcessingEnumModelerWidget::allowMultiple()
const
179 return mAllowMultiple->isChecked();
182void QgsProcessingEnumModelerWidget::setAllowMultiple(
bool allowMultiple )
184 mAllowMultiple->setChecked( allowMultiple );