36#include <QDirIterator>
37#include <QDragEnterEvent>
41#include <QStandardItem>
42#include <QStandardItemModel>
46#include "moc_qgsprocessingmultipleselectiondialog.cpp"
48using namespace Qt::StringLiterals;
52QgsProcessingMultipleSelectionPanelWidget::QgsProcessingMultipleSelectionPanelWidget(
const QVariantList &availableOptions,
const QVariantList &selectedOptions, QWidget *parent )
54 , mValueFormatter( []( const QVariant &v ) -> QString {
55 if ( v.userType() == qMetaTypeId<QgsProcessingModelChildParameterSource>() )
56 return v.value<QgsProcessingModelChildParameterSource>().staticValue().toString();
65 mSelectionList->setSelectionBehavior( QAbstractItemView::SelectRows );
66 mSelectionList->setSelectionMode( QAbstractItemView::ExtendedSelection );
67 mSelectionList->setDragDropMode( QAbstractItemView::InternalMove );
70 mModel =
new QStandardItemModel(
this );
71 mSelectionList->setModel( mModel );
73 mButtonSelectAll =
new QPushButton( tr(
"Select All" ) );
74 mButtonBox->addButton( mButtonSelectAll, QDialogButtonBox::ActionRole );
76 mButtonClearSelection =
new QPushButton( tr(
"Clear Selection" ) );
77 mButtonBox->addButton( mButtonClearSelection, QDialogButtonBox::ActionRole );
79 mButtonToggleSelection =
new QPushButton( tr(
"Toggle Selection" ) );
80 mButtonBox->addButton( mButtonToggleSelection, QDialogButtonBox::ActionRole );
82 connect( mButtonSelectAll, &QPushButton::clicked,
this, [
this] { selectAll(
true ); } );
83 connect( mButtonClearSelection, &QPushButton::clicked,
this, [
this] { selectAll(
false ); } );
84 connect( mButtonToggleSelection, &QPushButton::clicked,
this, &QgsProcessingMultipleSelectionPanelWidget::toggleSelection );
86 connect( mButtonBox, &QDialogButtonBox::accepted,
this, &QgsProcessingMultipleSelectionPanelWidget::acceptClicked );
87 populateList( availableOptions, selectedOptions );
89 connect( mModel, &QStandardItemModel::itemChanged,
this, &QgsProcessingMultipleSelectionPanelWidget::selectionChanged );
93 connect( mModel, &QStandardItemModel::rowsRemoved,
this, &QgsProcessingMultipleSelectionPanelWidget::selectionChanged );
96void QgsProcessingMultipleSelectionPanelWidget::setValueFormatter(
const std::function<QString(
const QVariant & )> &formatter )
98 mValueFormatter = formatter;
100 for (
int i = 0; i < mModel->rowCount(); ++i )
102 mModel->item( i )->setText( mValueFormatter( mModel->item( i )->data( Qt::UserRole ) ) );
106QVariantList QgsProcessingMultipleSelectionPanelWidget::selectedOptions()
const
108 QVariantList options;
109 options.reserve( mModel->rowCount() );
110 bool hasModelSources =
false;
111 for (
int i = 0; i < mModel->rowCount(); ++i )
113 QStandardItem *item = mModel->item( i );
119 if ( item->checkState() == Qt::Checked )
121 const QVariant option = item->data( Qt::UserRole );
123 if ( option.userType() == qMetaTypeId<QgsProcessingModelChildParameterSource>() )
124 hasModelSources =
true;
130 if ( hasModelSources )
133 QVariantList originalOptions = options;
135 for (
const QVariant &option : originalOptions )
137 if ( option.userType() == qMetaTypeId<QgsProcessingModelChildParameterSource>() )
140 options << QVariant::fromValue( QgsProcessingModelChildParameterSource::fromStaticValue( option ) );
148void QgsProcessingMultipleSelectionPanelWidget::selectAll(
const bool checked )
150 const QList<QStandardItem *> items = currentItems();
151 for ( QStandardItem *item : items )
153 item->setCheckState( checked ? Qt::Checked : Qt::Unchecked );
157void QgsProcessingMultipleSelectionPanelWidget::toggleSelection()
159 const QList<QStandardItem *> items = currentItems();
160 for ( QStandardItem *item : items )
162 item->setCheckState( item->checkState() == Qt::Unchecked ? Qt::Checked : Qt::Unchecked );
166QList<QStandardItem *> QgsProcessingMultipleSelectionPanelWidget::currentItems()
168 QList<QStandardItem *> items;
169 const QModelIndexList selection = mSelectionList->selectionModel()->selectedIndexes();
170 if ( selection.size() > 1 )
172 items.reserve( selection.size() );
173 for (
const QModelIndex &index : selection )
175 items << mModel->itemFromIndex( index );
180 items.reserve( mModel->rowCount() );
181 for (
int i = 0; i < mModel->rowCount(); ++i )
183 items << mModel->item( i );
189void QgsProcessingMultipleSelectionPanelWidget::populateList(
const QVariantList &availableOptions,
const QVariantList &selectedOptions )
191 QVariantList remainingOptions = availableOptions;
194 for (
const QVariant &option : selectedOptions )
200 addOption( option, mValueFormatter( option ),
true );
201 remainingOptions.removeAll( option );
204 for (
const QVariant &option : std::as_const( remainingOptions ) )
206 addOption( option, mValueFormatter( option ),
false );
210QList<int> QgsProcessingMultipleSelectionPanelWidget::existingMapLayerFromMimeData(
const QMimeData *data )
const
219 for (
int i = 0; i < mModel->rowCount(); ++i )
222 QString userRole = mModel->item( i )->data( Qt::UserRole ).toString();
223 if ( userRole == layer->id() || userRole == layer->source() )
233void QgsProcessingMultipleSelectionPanelWidget::dragEnterEvent( QDragEnterEvent *event )
235 if ( !( event->possibleActions() & Qt::CopyAction ) )
238 const QList<int> indexes = existingMapLayerFromMimeData( event->mimeData() );
239 if ( !indexes.isEmpty() )
242 event->setDropAction( Qt::CopyAction );
247void QgsProcessingMultipleSelectionPanelWidget::dropEvent( QDropEvent *event )
249 if ( !( event->possibleActions() & Qt::CopyAction ) )
252 const QList<int> indexes = existingMapLayerFromMimeData( event->mimeData() );
253 if ( !indexes.isEmpty() )
256 setFocus( Qt::MouseFocusReason );
257 event->setDropAction( Qt::CopyAction );
260 for (
const int i : indexes )
262 mModel->item( i )->setCheckState( Qt::Checked );
264 emit selectionChanged();
268void QgsProcessingMultipleSelectionPanelWidget::addOption(
const QVariant &value,
const QString &title,
bool selected,
bool updateExistingTitle, QIcon icon )
271 for (
int i = 0; i < mModel->rowCount(); ++i )
273 if ( mModel->item( i )->data( Qt::UserRole ) == value || ( mModel->item( i )->data( Qt::UserRole ).userType() == qMetaTypeId<QgsProcessingModelChildParameterSource>() && value.userType() == qMetaTypeId<QgsProcessingModelChildParameterSource>() && mModel->item( i )->data( Qt::UserRole ).value<QgsProcessingModelChildParameterSource>() == value.value<QgsProcessingModelChildParameterSource>() ) )
275 if ( updateExistingTitle )
276 mModel->item( i )->setText( title );
281 auto item = std::make_unique<QStandardItem>( title );
282 item->setData( value, Qt::UserRole );
283 item->setCheckState( selected ? Qt::Checked : Qt::Unchecked );
284 item->setCheckable(
true );
285 item->setDropEnabled(
false );
286 if ( !icon.isNull() )
287 item->setData( icon, Qt::DecorationRole );
288 mModel->appendRow( item.release() );
296QgsProcessingMultipleSelectionDialog::QgsProcessingMultipleSelectionDialog(
const QVariantList &availableOptions,
const QVariantList &selectedOptions, QWidget *parent, Qt::WindowFlags flags )
297 : QDialog( parent, flags )
299 setWindowTitle( tr(
"Multiple Selection" ) );
300 QVBoxLayout *vLayout =
new QVBoxLayout();
301 mWidget =
new QgsProcessingMultipleSelectionPanelWidget( availableOptions, selectedOptions );
302 vLayout->addWidget( mWidget );
303 mWidget->buttonBox()->addButton( QDialogButtonBox::Cancel );
304 connect( mWidget->buttonBox(), &QDialogButtonBox::accepted,
this, &QDialog::accept );
305 connect( mWidget->buttonBox(), &QDialogButtonBox::rejected,
this, &QDialog::reject );
306 setLayout( vLayout );
309void QgsProcessingMultipleSelectionDialog::setValueFormatter(
const std::function<QString(
const QVariant & )> &formatter )
311 mWidget->setValueFormatter( formatter );
314QVariantList QgsProcessingMultipleSelectionDialog::selectedOptions()
const
316 return mWidget->selectedOptions();
324QgsProcessingMultipleInputPanelWidget::QgsProcessingMultipleInputPanelWidget(
326 const QVariantList &selectedOptions,
327 const QList<QgsProcessingModelChildParameterSource> &modelSources,
328 QgsProcessingModelAlgorithm *model,
331 : QgsProcessingMultipleSelectionPanelWidget( QVariantList(), selectedOptions, parent )
332 , mParameter( parameter )
334 QPushButton *addFileButton =
new QPushButton( tr(
"Add File(s)…" ) );
335 connect( addFileButton, &QPushButton::clicked,
this, &QgsProcessingMultipleInputPanelWidget::addFiles );
336 buttonBox()->addButton( addFileButton, QDialogButtonBox::ActionRole );
338 QPushButton *addDirButton =
new QPushButton( tr(
"Add Directory…" ) );
339 connect( addDirButton, &QPushButton::clicked,
this, &QgsProcessingMultipleInputPanelWidget::addDirectory );
340 buttonBox()->addButton( addDirButton, QDialogButtonBox::ActionRole );
341 setAcceptDrops(
true );
344 for (
const QgsProcessingModelChildParameterSource &source : modelSources )
346 addOption( QVariant::fromValue( source ), source.friendlyIdentifier( model ),
false,
true );
350void QgsProcessingMultipleInputPanelWidget::setProject(
QgsProject *project )
353 populateFromProject( project );
356void QgsProcessingMultipleInputPanelWidget::addFiles()
359 QString path = settings.
value( u
"/Processing/LastInputPath"_s, QDir::homePath() ).toString();
363 filter = generator->createFileFilter();
365 filter = QObject::tr(
"All files (*.*)" );
367 const QStringList filenames = QFileDialog::getOpenFileNames(
this, tr(
"Select File(s)" ), path, filter );
368 if ( filenames.empty() )
371 settings.
setValue( u
"/Processing/LastInputPath"_s, QFileInfo( filenames.at( 0 ) ).path() );
373 for (
const QString &file : filenames )
375 addOption( file, file,
true );
378 emit selectionChanged();
381void QgsProcessingMultipleInputPanelWidget::addDirectory()
384 const QString path = settings.
value( u
"/Processing/LastInputPath"_s, QDir::homePath() ).toString();
386 const QString dir = QFileDialog::getExistingDirectory(
this, tr(
"Select Directory" ), path );
390 settings.
setValue( u
"/Processing/LastInputPath"_s, dir );
392 QStringList nameFilters;
396 for (
const QString &extension : extensions )
398 nameFilters << u
"*.%1"_s.arg( extension );
399 nameFilters << u
"*.%1"_s.arg( extension.toUpper() );
400 nameFilters << u
"*.%1"_s.arg( extension.toLower() );
404 QDirIterator it( dir, nameFilters, QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot, QDirIterator::Subdirectories );
405 while ( it.hasNext() )
407 const QString fullPath = it.next();
408 if ( fullPath.endsWith(
".dbf"_L1, Qt::CaseInsensitive ) )
410 if ( QFileInfo::exists( u
"%1.shp"_s.arg( fullPath.chopped( 4 ) ) ) || QFileInfo::exists( u
"%1.SHP"_s.arg( fullPath.chopped( 4 ) ) ) )
416 else if ( fullPath.endsWith(
".aux.xml"_L1, Qt::CaseInsensitive ) || fullPath.endsWith(
".shp.xml"_L1, Qt::CaseInsensitive ) )
421 addOption( fullPath, fullPath,
true );
423 emit selectionChanged();
426QList<int> QgsProcessingMultipleInputPanelWidget::existingMapLayerFromMimeData(
const QMimeData *data,
QgsMimeDataUtils::UriList &handledUrls )
const
434 bool matched =
false;
437 for (
int i = 0; i < mModel->rowCount(); ++i )
440 const QString userRole = mModel->item( i )->data( Qt::UserRole ).toString();
441 if ( userRole == layer->id() || userRole == layer->source() )
451 handledUrls.append( u );
460 QStringList skipUrlData;
461 skipUrlData.reserve( skipUrls.size() );
464 skipUrlData.append( u.data() );
472 if ( skipUrlData.contains( u.data() ) )
485 bool acceptable =
false;
526 && u.providerKey ==
"gdal"_L1 )
530 && u.providerKey ==
"mdal"_L1 )
540 if ( !uriList.isEmpty() )
544 QStringList rawPaths;
545 if ( data->hasUrls() )
547 const QList<QUrl> urls = data->urls();
548 rawPaths.reserve( urls.count() );
549 for (
const QUrl &url : urls )
551 const QString local = url.toLocalFile();
552 if ( !rawPaths.contains( local ) )
553 rawPaths.append( local );
556 if ( !data->text().isEmpty() && !rawPaths.contains( data->text() ) )
557 rawPaths.append( data->text() );
559 for (
const QString &path : std::as_const( rawPaths ) )
561 QFileInfo file( path );
572void QgsProcessingMultipleInputPanelWidget::dragEnterEvent( QDragEnterEvent *event )
574 if ( !( event->possibleActions() & Qt::CopyAction ) )
579 const QList<int> indexes = existingMapLayerFromMimeData( event->mimeData(), handledUris );
580 if ( !indexes.isEmpty() )
583 event->setDropAction( Qt::CopyAction );
589 const QStringList uris = compatibleUrisFromMimeData( mParameter, event->mimeData(), handledUris );
590 if ( !uris.isEmpty() )
593 event->setDropAction( Qt::CopyAction );
598void QgsProcessingMultipleInputPanelWidget::dropEvent( QDropEvent *event )
600 if ( !( event->possibleActions() & Qt::CopyAction ) )
604 const QList<int> indexes = existingMapLayerFromMimeData( event->mimeData(), handledUris );
605 if ( !indexes.isEmpty() )
608 setFocus( Qt::MouseFocusReason );
609 event->setDropAction( Qt::CopyAction );
612 for (
const int i : indexes )
614 mModel->item( i )->setCheckState( Qt::Checked );
616 emit selectionChanged();
620 const QStringList uris = compatibleUrisFromMimeData( mParameter, event->mimeData(), handledUris );
621 if ( !uris.isEmpty() )
623 for (
const QString &uri : uris )
625 addOption( uri, uri,
true );
627 emit selectionChanged();
631void QgsProcessingMultipleInputPanelWidget::populateFromProject(
QgsProject *project )
634 for (
int i = 0; i < mModel->rowCount(); ++i )
636 const QStandardItem *item = mModel->item( i );
637 if ( item->data( Qt::UserRole ) == layerId )
639 bool isChecked = ( item->checkState() == Qt::Checked );
640 mModel->removeRow( i );
643 emit selectionChanged();
652 const QString authid = layer->crs().authid();
654 if ( settings.value( u
"Processing/Configuration/SHOW_CRS_DEF"_s,
true ).toBool() && !authid.isEmpty() )
655 title = u
"%1 [%2]"_s.arg( layer->name(), authid );
657 title = layer->name();
661 QString
id = layer->id();
665 for (
int i = 0; i < mModel->rowCount(); ++i )
668 if ( mModel->item( i )->data( Qt::UserRole ) == layer->id() )
674 else if ( mModel->item( i )->data( Qt::UserRole ) == layer->source() )
676 id = layer->source();
682 addOption(
id, title,
false,
true, icon );
685 switch ( mParameter->layerType() )
832QgsProcessingMultipleInputDialog::QgsProcessingMultipleInputDialog(
834 const QVariantList &selectedOptions,
835 const QList<QgsProcessingModelChildParameterSource> &modelSources,
836 QgsProcessingModelAlgorithm *model,
838 Qt::WindowFlags flags
840 : QDialog( parent, flags )
842 setWindowTitle( tr(
"Multiple Selection" ) );
843 QVBoxLayout *vLayout =
new QVBoxLayout();
844 mWidget =
new QgsProcessingMultipleInputPanelWidget( parameter, selectedOptions, modelSources, model,
nullptr );
845 vLayout->addWidget( mWidget );
846 mWidget->buttonBox()->addButton( QDialogButtonBox::Cancel );
847 connect( mWidget->buttonBox(), &QDialogButtonBox::accepted,
this, &QDialog::accept );
848 connect( mWidget->buttonBox(), &QDialogButtonBox::rejected,
this, &QDialog::reject );
849 setLayout( vLayout );
850 setAcceptDrops(
true );
853QVariantList QgsProcessingMultipleInputDialog::selectedOptions()
const
855 return mWidget->selectedOptions();
858void QgsProcessingMultipleInputDialog::setProject(
QgsProject *project )
860 mWidget->setProject( project );
@ File
Files (i.e. non map layer sources, such as text files).
@ TiledScene
Tiled scene layers.
@ Annotation
Annotation layers.
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
@ VectorTile
Vector tile layers.
@ MapLayer
Any map layer type (raster, vector, mesh, point cloud, annotation or plugin layer).
@ VectorAnyGeometry
Any vector layer with geometry.
@ VectorPoint
Vector point layers.
@ VectorPolygon
Vector polygon layers.
@ VectorLine
Vector line layers.
@ PointCloud
Point cloud layers.
@ VectorTile
Vector tile layer. Added in QGIS 3.14.
@ Mesh
Mesh layer. Added in QGIS 3.2.
@ PointCloud
Point cloud layer. Added in QGIS 3.18.
Represents a map layer containing a set of georeferenced annotations, e.g.
Abstract interface for classes which generate a file filter string.
static QStringList extensionsFromFilter(const QString &filter)
Returns a list of the extensions contained within a file filter string.
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...
static QIcon iconForLayer(const QgsMapLayer *layer)
Returns the icon corresponding to a specified map layer.
static QString typeToString(Qgis::LayerType type)
Converts a map layer type to a string value.
Base class for all map layer types.
Represents a mesh layer supporting display of data on structured or unstructured meshes.
QList< QgsMimeDataUtils::Uri > UriList
static UriList decodeUriList(const QMimeData *data)
Base class for plugin layers.
Represents a map layer supporting display of point clouds.
A parameter for processing algorithms which accepts multiple map layers.
Qgis::ProcessingSourceType layerType() const
Returns the layer type for layers acceptable by the parameter.
static QList< QgsTiledSceneLayer * > compatibleTiledSceneLayers(QgsProject *project, bool sort=true)
Returns a list of tiled scene layers from a project which are compatible with the processing framewor...
static QList< QgsAnnotationLayer * > compatibleAnnotationLayers(QgsProject *project, bool sort=true)
Returns a list of annotation layers from a project which are compatible with the processing framework...
static QString encodeProviderKeyAndUri(const QString &providerKey, const QString &uri)
Encodes a provider key and layer uri to a single string, for use with decodeProviderKeyAndUri().
static QList< QgsRasterLayer * > compatibleRasterLayers(QgsProject *project, bool sort=true)
Returns a list of raster layers from a project which are compatible with the processing framework.
static QList< QgsPluginLayer * > compatiblePluginLayers(QgsProject *project, bool sort=true)
Returns a list of plugin layers from a project which are compatible with the processing framework.
static QList< QgsVectorLayer * > compatibleVectorLayers(QgsProject *project, const QList< int > &sourceTypes=QList< int >(), bool sort=true)
Returns a list of vector layers from a project which are compatible with the processing framework.
static QList< QgsVectorTileLayer * > compatibleVectorTileLayers(QgsProject *project, bool sort=true)
Returns a list of vector tile layers from a project which are compatible with the processing framewor...
static QList< QgsPointCloudLayer * > compatiblePointCloudLayers(QgsProject *project, bool sort=true)
Returns a list of point cloud layers from a project which are compatible with the processing framewor...
static QList< QgsMeshLayer * > compatibleMeshLayers(QgsProject *project, bool sort=true)
Returns a list of mesh layers from a project which are compatible with the processing framework.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
QgsAnnotationLayer * mainAnnotationLayer()
Returns the main annotation layer associated with the project.
void layerRemoved(const QString &layerId)
Emitted after a layer was removed from the registry.
Represents a raster layer.
Stores settings for use within QGIS.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
Represents a map layer supporting display of tiled scene objects.
Represents a vector layer which manages a vector based dataset.
Implements a map layer that is dedicated to rendering of vector tiles.
static Qgis::GeometryType geometryType(Qgis::WkbType type)
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...