23 #include "processing/models/qgsprocessingmodelchildparametersource.h"
24 #include <QStandardItemModel>
25 #include <QStandardItem>
26 #include <QPushButton>
28 #include <QToolButton>
29 #include <QFileDialog>
33 QgsProcessingMultipleSelectionPanelWidget::QgsProcessingMultipleSelectionPanelWidget(
const QVariantList &availableOptions,
34 const QVariantList &selectedOptions,
37 , mValueFormatter( []( const QVariant & v )->QString
39 if ( v.canConvert< QgsProcessingModelChildParameterSource >() )
41 return v.value< QgsProcessingModelChildParameterSource >().staticValue().toString();
54 mSelectionList->setSelectionBehavior( QAbstractItemView::SelectRows );
55 mSelectionList->setSelectionMode( QAbstractItemView::ExtendedSelection );
56 mSelectionList->setDragDropMode( QAbstractItemView::InternalMove );
58 mButtonSelectAll =
new QPushButton( tr(
"Select All" ) );
59 mButtonBox->addButton( mButtonSelectAll, QDialogButtonBox::ActionRole );
61 mButtonClearSelection =
new QPushButton( tr(
"Clear Selection" ) );
62 mButtonBox->addButton( mButtonClearSelection, QDialogButtonBox::ActionRole );
64 mButtonToggleSelection =
new QPushButton( tr(
"Toggle Selection" ) );
65 mButtonBox->addButton( mButtonToggleSelection, QDialogButtonBox::ActionRole );
67 connect( mButtonSelectAll, &QPushButton::clicked,
this, [ = ] { selectAll(
true ); } );
68 connect( mButtonClearSelection, &QPushButton::clicked,
this, [ = ] { selectAll(
false ); } );
69 connect( mButtonToggleSelection, &QPushButton::clicked,
this, &QgsProcessingMultipleSelectionPanelWidget::toggleSelection );
71 connect( mButtonBox, &QDialogButtonBox::accepted,
this, &QgsProcessingMultipleSelectionPanelWidget::acceptClicked );
72 populateList( availableOptions, selectedOptions );
74 connect( mModel, &QStandardItemModel::itemChanged,
this, &QgsProcessingMultipleSelectionPanelWidget::selectionChanged );
77 void QgsProcessingMultipleSelectionPanelWidget::setValueFormatter(
const std::function<QString(
const QVariant & )> &
formatter )
81 for (
int i = 0; i < mModel->rowCount(); ++i )
83 mModel->item( i )->setText( mValueFormatter( mModel->item( i )->data( Qt::UserRole ) ) );
87 QVariantList QgsProcessingMultipleSelectionPanelWidget::selectedOptions()
const
90 options.reserve( mModel->rowCount() );
91 for (
int i = 0; i < mModel->rowCount(); ++i )
93 if ( mModel->item( i )->checkState() == Qt::Checked )
94 options << mModel->item( i )->data( Qt::UserRole );
100 void QgsProcessingMultipleSelectionPanelWidget::selectAll(
const bool checked )
102 const QList<QStandardItem *> items = currentItems();
103 for ( QStandardItem *item : items )
105 item->setCheckState( checked ? Qt::Checked : Qt::Unchecked );
109 void QgsProcessingMultipleSelectionPanelWidget::toggleSelection()
111 const QList<QStandardItem *> items = currentItems();
112 for ( QStandardItem *item : items )
114 item->setCheckState( item->checkState() == Qt::Unchecked ? Qt::Checked : Qt::Unchecked );
118 QList<QStandardItem *> QgsProcessingMultipleSelectionPanelWidget::currentItems()
120 QList<QStandardItem *> items;
121 const QModelIndexList selection = mSelectionList->selectionModel()->selectedIndexes();
122 if ( selection.size() > 1 )
124 items.reserve( selection.size() );
125 for (
const QModelIndex &index : selection )
127 items << mModel->itemFromIndex( index );
132 items.reserve( mModel->rowCount() );
133 for (
int i = 0; i < mModel->rowCount(); ++i )
135 items << mModel->item( i );
141 void QgsProcessingMultipleSelectionPanelWidget::populateList(
const QVariantList &availableOptions,
const QVariantList &selectedOptions )
143 mModel =
new QStandardItemModel(
this );
145 QVariantList remainingOptions = availableOptions;
148 for (
const QVariant &option : selectedOptions )
154 addOption( option, mValueFormatter( option ),
true );
155 remainingOptions.removeAll( option );
158 for (
const QVariant &option : qgis::as_const( remainingOptions ) )
160 addOption( option, mValueFormatter( option ),
false );
163 mSelectionList->setModel( mModel );
167 void QgsProcessingMultipleSelectionPanelWidget::addOption(
const QVariant &value,
const QString &title,
bool selected,
bool updateExistingTitle )
170 for (
int i = 0; i < mModel->rowCount(); ++i )
172 if ( mModel->item( i )->data( Qt::UserRole ) == value ||
173 ( mModel->item( i )->data( Qt::UserRole ).canConvert< QgsProcessingModelChildParameterSource >() &&
174 value.canConvert< QgsProcessingModelChildParameterSource >() &&
175 mModel->item( i )->data( Qt::UserRole ).value< QgsProcessingModelChildParameterSource >() ==
176 value.value< QgsProcessingModelChildParameterSource >() )
179 if ( updateExistingTitle )
180 mModel->item( i )->setText( title );
185 std::unique_ptr< QStandardItem > item = qgis::make_unique< QStandardItem >( title );
186 item->setData( value, Qt::UserRole );
187 item->setCheckState( selected ? Qt::Checked : Qt::Unchecked );
188 item->setCheckable(
true );
189 item->setDropEnabled(
false );
190 mModel->appendRow( item.release() );
199 QgsProcessingMultipleSelectionDialog::QgsProcessingMultipleSelectionDialog(
const QVariantList &availableOptions,
const QVariantList &selectedOptions, QWidget *parent, Qt::WindowFlags flags )
200 : QDialog( parent, flags )
202 setWindowTitle( tr(
"Multiple Selection" ) );
203 QVBoxLayout *vLayout =
new QVBoxLayout();
204 mWidget =
new QgsProcessingMultipleSelectionPanelWidget( availableOptions, selectedOptions );
205 vLayout->addWidget( mWidget );
206 mWidget->buttonBox()->addButton( QDialogButtonBox::Cancel );
207 connect( mWidget->buttonBox(), &QDialogButtonBox::accepted,
this, &QDialog::accept );
208 connect( mWidget->buttonBox(), &QDialogButtonBox::rejected,
this, &QDialog::reject );
209 setLayout( vLayout );
212 void QgsProcessingMultipleSelectionDialog::setValueFormatter(
const std::function<QString(
const QVariant & )> &
formatter )
217 QVariantList QgsProcessingMultipleSelectionDialog::selectedOptions()
const
219 return mWidget->selectedOptions();
228 const QList<QgsProcessingModelChildParameterSource> &modelSources,
229 QgsProcessingModelAlgorithm *model, QWidget *parent )
230 : QgsProcessingMultipleSelectionPanelWidget( QVariantList(), selectedOptions, parent )
231 , mParameter( parameter )
233 QPushButton *addFileButton =
new QPushButton( tr(
"Add File(s)…" ) );
234 connect( addFileButton, &QPushButton::clicked,
this, &QgsProcessingMultipleInputPanelWidget::addFiles );
235 buttonBox()->addButton( addFileButton, QDialogButtonBox::ActionRole );
237 QPushButton *addDirButton =
new QPushButton( tr(
"Add Directory…" ) );
238 connect( addDirButton, &QPushButton::clicked,
this, &QgsProcessingMultipleInputPanelWidget::addDirectory );
239 buttonBox()->addButton( addDirButton, QDialogButtonBox::ActionRole );
241 for (
const QgsProcessingModelChildParameterSource &source : modelSources )
243 addOption( QVariant::fromValue( source ), source.friendlyIdentifier( model ),
false,
true );
247 void QgsProcessingMultipleInputPanelWidget::setProject(
QgsProject *project )
250 populateFromProject( project );
253 void QgsProcessingMultipleInputPanelWidget::addFiles()
256 QString path = settings.
value( QStringLiteral(
"/Processing/LastInputPath" ), QDir::homePath() ).toString();
260 filter = generator->createFileFilter();
262 filter = QObject::tr(
"All files (*.*)" );
264 const QStringList filenames = QFileDialog::getOpenFileNames(
this, tr(
"Select File(s)" ), path, filter );
265 if ( filenames.empty() )
268 settings.
setValue( QStringLiteral(
"/Processing/LastInputPath" ), QFileInfo( filenames.at( 0 ) ).path() );
270 for (
const QString &file : filenames )
272 addOption( file, file,
true );
275 emit selectionChanged();
278 void QgsProcessingMultipleInputPanelWidget::addDirectory()
281 QString path = settings.
value( QStringLiteral(
"/Processing/LastInputPath" ), QDir::homePath() ).toString();
283 const QString dir = QFileDialog::getExistingDirectory(
this, tr(
"Select Directory" ), path );
287 settings.
setValue( QStringLiteral(
"/Processing/LastInputPath" ), dir );
289 QStringList nameFilters;
293 for (
const QString &extension : extensions )
295 nameFilters << QStringLiteral(
"*.%1" ).arg( extension );
296 nameFilters << QStringLiteral(
"*.%1" ).arg( extension.toUpper() );
297 nameFilters << QStringLiteral(
"*.%1" ).arg( extension.toLower() );
301 QDirIterator it( path, nameFilters, QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot, QDirIterator::Subdirectories );
303 while ( it.hasNext() )
305 const QString fullPath = it.next();
306 addOption( fullPath, fullPath,
true );
308 emit selectionChanged();
311 void QgsProcessingMultipleInputPanelWidget::populateFromProject(
QgsProject *project )
316 const QString authid = layer->crs().authid();
318 if ( settings.
value( QStringLiteral(
"Processing/Configuration/SHOW_CRS_DEF" ),
true ).toBool() && !authid.isEmpty() )
319 title = QStringLiteral(
"%1 [%2]" ).arg( layer->name(), authid );
321 title = layer->name();
324 QString
id = layer->id();
325 for (
int i = 0; i < mModel->rowCount(); ++i )
328 if ( mModel->item( i )->data( Qt::UserRole ) == layer->id() )
333 else if ( mModel->item( i )->data( Qt::UserRole ) == layer->source() )
335 id = layer->source();
340 addOption(
id, title,
false,
true );
343 switch ( mParameter->layerType() )
421 const QList< QgsProcessingModelChildParameterSource > &modelSources, QgsProcessingModelAlgorithm *model, QWidget *parent, Qt::WindowFlags flags )
422 : QDialog( parent, flags )
424 setWindowTitle( tr(
"Multiple Selection" ) );
425 QVBoxLayout *vLayout =
new QVBoxLayout();
426 mWidget =
new QgsProcessingMultipleInputPanelWidget( parameter, selectedOptions, modelSources, model );
427 vLayout->addWidget( mWidget );
428 mWidget->buttonBox()->addButton( QDialogButtonBox::Cancel );
429 connect( mWidget->buttonBox(), &QDialogButtonBox::accepted,
this, &QDialog::accept );
430 connect( mWidget->buttonBox(), &QDialogButtonBox::rejected,
this, &QDialog::reject );
431 setLayout( vLayout );
434 QVariantList QgsProcessingMultipleInputDialog::selectedOptions()
const
436 return mWidget->selectedOptions();
439 void QgsProcessingMultipleInputDialog::setProject(
QgsProject *project )
441 mWidget->setProject( project );