18 #include <QStandardItemModel> 19 #include <QStandardItem> 20 #include <QPushButton> 22 #include <QToolButton> 26 QgsProcessingMultipleSelectionDialog::QgsProcessingMultipleSelectionDialog(
const QVariantList &availableOptions,
27 const QVariantList &selectedOptions,
28 QWidget *parent, Qt::WindowFlags flags )
29 : QDialog( parent, flags )
30 , mValueFormatter( []( const QVariant & v )->QString {
return v.toString(); } )
36 mSelectionList->setSelectionBehavior( QAbstractItemView::SelectRows );
37 mSelectionList->setSelectionMode( QAbstractItemView::ExtendedSelection );
38 mSelectionList->setDragDropMode( QAbstractItemView::InternalMove );
40 mButtonSelectAll =
new QPushButton( tr(
"Select All" ) );
41 mButtonBox->addButton( mButtonSelectAll, QDialogButtonBox::ActionRole );
43 mButtonClearSelection =
new QPushButton( tr(
"Clear Selection" ) );
44 mButtonBox->addButton( mButtonClearSelection, QDialogButtonBox::ActionRole );
46 mButtonToggleSelection =
new QPushButton( tr(
"Toggle Selection" ) );
47 mButtonBox->addButton( mButtonToggleSelection, QDialogButtonBox::ActionRole );
49 connect( mButtonSelectAll, &QPushButton::clicked,
this, [ = ] { selectAll(
true ); } );
50 connect( mButtonClearSelection, &QPushButton::clicked,
this, [ = ] { selectAll(
false ); } );
51 connect( mButtonToggleSelection, &QPushButton::clicked,
this, &QgsProcessingMultipleSelectionDialog::toggleSelection );
53 populateList( availableOptions, selectedOptions );
56 void QgsProcessingMultipleSelectionDialog::setValueFormatter(
const std::function<QString(
const QVariant & )> &formatter )
58 mValueFormatter = formatter;
60 for (
int i = 0; i < mModel->rowCount(); ++i )
62 mModel->item( i )->setText( mValueFormatter( mModel->item( i )->data( Qt::UserRole ) ) );
66 QVariantList QgsProcessingMultipleSelectionDialog::selectedOptions()
const 69 options.reserve( mModel->rowCount() );
70 for (
int i = 0; i < mModel->rowCount(); ++i )
72 if ( mModel->item( i )->checkState() == Qt::Checked )
73 options << mModel->item( i )->data( Qt::UserRole );
78 void QgsProcessingMultipleSelectionDialog::selectAll(
const bool checked )
80 const QList<QStandardItem *> items = currentItems();
81 for ( QStandardItem *item : items )
83 item->setCheckState( checked ? Qt::Checked : Qt::Unchecked );
87 void QgsProcessingMultipleSelectionDialog::toggleSelection()
89 const QList<QStandardItem *> items = currentItems();
90 for ( QStandardItem *item : items )
92 item->setCheckState( item->checkState() == Qt::Unchecked ? Qt::Checked : Qt::Unchecked );
96 QList<QStandardItem *> QgsProcessingMultipleSelectionDialog::currentItems()
98 QList<QStandardItem *> items;
99 const QModelIndexList selection = mSelectionList->selectionModel()->selectedIndexes();
100 if ( selection.size() > 1 )
102 items.reserve( selection.size() );
103 for (
const QModelIndex &index : selection )
105 items << mModel->itemFromIndex( index );
110 items.reserve( mModel->rowCount() );
111 for (
int i = 0; i < mModel->rowCount(); ++i )
113 items << mModel->item( i );
119 void QgsProcessingMultipleSelectionDialog::populateList(
const QVariantList &availableOptions,
const QVariantList &selectedOptions )
121 mModel =
new QStandardItemModel(
this );
123 QVariantList remainingOptions = availableOptions;
126 for (
const QVariant &option : selectedOptions )
131 std::unique_ptr< QStandardItem > item = qgis::make_unique< QStandardItem >( mValueFormatter( option ) );
132 item->setData( option, Qt::UserRole );
133 item->setCheckState( Qt::Checked );
134 item->setCheckable(
true );
135 item->setDropEnabled(
false );
136 mModel->appendRow( item.release() );
137 remainingOptions.removeAll( option );
140 for (
const QVariant &option : qgis::as_const( remainingOptions ) )
142 std::unique_ptr< QStandardItem > item = qgis::make_unique< QStandardItem >( mValueFormatter( option ) );
143 item->setData( option, Qt::UserRole );
144 item->setCheckState( Qt::Unchecked );
145 item->setCheckable(
true );
146 item->setDropEnabled(
false );
147 mModel->appendRow( item.release() );
150 mSelectionList->setModel( mModel );
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...