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(
const QgsProcessingParameterMultipleLayers *parameter,
const QVariantList &selectedOptions,
const QList<QgsProcessingModelChildParameterSource> &modelSources, QgsProcessingModelAlgorithm *model, QWidget *parent )
325 : QgsProcessingMultipleSelectionPanelWidget( QVariantList(), selectedOptions, parent )
326 , mParameter( parameter )
328 QPushButton *addFileButton =
new QPushButton( tr(
"Add File(s)…" ) );
329 connect( addFileButton, &QPushButton::clicked,
this, &QgsProcessingMultipleInputPanelWidget::addFiles );
330 buttonBox()->addButton( addFileButton, QDialogButtonBox::ActionRole );
332 QPushButton *addDirButton =
new QPushButton( tr(
"Add Directory…" ) );
333 connect( addDirButton, &QPushButton::clicked,
this, &QgsProcessingMultipleInputPanelWidget::addDirectory );
334 buttonBox()->addButton( addDirButton, QDialogButtonBox::ActionRole );
335 setAcceptDrops(
true );
338 for (
const QgsProcessingModelChildParameterSource &source : modelSources )
340 addOption( QVariant::fromValue( source ), source.friendlyIdentifier( model ),
false,
true );
344void QgsProcessingMultipleInputPanelWidget::setProject(
QgsProject *project )
347 populateFromProject( project );
350void QgsProcessingMultipleInputPanelWidget::addFiles()
353 QString path = settings.
value( u
"/Processing/LastInputPath"_s, QDir::homePath() ).toString();
357 filter = generator->createFileFilter();
359 filter = QObject::tr(
"All files (*.*)" );
361 const QStringList filenames = QFileDialog::getOpenFileNames(
this, tr(
"Select File(s)" ), path, filter );
362 if ( filenames.empty() )
365 settings.
setValue( u
"/Processing/LastInputPath"_s, QFileInfo( filenames.at( 0 ) ).path() );
367 for (
const QString &file : filenames )
369 addOption( file, file,
true );
372 emit selectionChanged();
375void QgsProcessingMultipleInputPanelWidget::addDirectory()
378 const QString path = settings.
value( u
"/Processing/LastInputPath"_s, QDir::homePath() ).toString();
380 const QString dir = QFileDialog::getExistingDirectory(
this, tr(
"Select Directory" ), path );
384 settings.
setValue( u
"/Processing/LastInputPath"_s, dir );
386 QStringList nameFilters;
390 for (
const QString &extension : extensions )
392 nameFilters << u
"*.%1"_s.arg( extension );
393 nameFilters << u
"*.%1"_s.arg( extension.toUpper() );
394 nameFilters << u
"*.%1"_s.arg( extension.toLower() );
398 QDirIterator it( dir, nameFilters, QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot, QDirIterator::Subdirectories );
399 while ( it.hasNext() )
401 const QString fullPath = it.next();
402 if ( fullPath.endsWith(
".dbf"_L1, Qt::CaseInsensitive ) )
404 if ( QFileInfo::exists( u
"%1.shp"_s.arg( fullPath.chopped( 4 ) ) ) || QFileInfo::exists( u
"%1.SHP"_s.arg( fullPath.chopped( 4 ) ) ) )
410 else if ( fullPath.endsWith(
".aux.xml"_L1, Qt::CaseInsensitive ) || fullPath.endsWith(
".shp.xml"_L1, Qt::CaseInsensitive ) )
415 addOption( fullPath, fullPath,
true );
417 emit selectionChanged();
420QList<int> QgsProcessingMultipleInputPanelWidget::existingMapLayerFromMimeData(
const QMimeData *data,
QgsMimeDataUtils::UriList &handledUrls )
const
428 bool matched =
false;
431 for (
int i = 0; i < mModel->rowCount(); ++i )
434 const QString userRole = mModel->item( i )->data( Qt::UserRole ).toString();
435 if ( userRole == layer->id() || userRole == layer->source() )
445 handledUrls.append( u );
454 QStringList skipUrlData;
455 skipUrlData.reserve( skipUrls.size() );
458 skipUrlData.append( u.data() );
466 if ( skipUrlData.contains( u.data() ) )
479 bool acceptable =
false;
523 if ( !uriList.isEmpty() )
527 QStringList rawPaths;
528 if ( data->hasUrls() )
530 const QList<QUrl> urls = data->urls();
531 rawPaths.reserve( urls.count() );
532 for (
const QUrl &url : urls )
534 const QString local = url.toLocalFile();
535 if ( !rawPaths.contains( local ) )
536 rawPaths.append( local );
539 if ( !data->text().isEmpty() && !rawPaths.contains( data->text() ) )
540 rawPaths.append( data->text() );
542 for (
const QString &path : std::as_const( rawPaths ) )
544 QFileInfo file( path );
555void QgsProcessingMultipleInputPanelWidget::dragEnterEvent( QDragEnterEvent *event )
557 if ( !( event->possibleActions() & Qt::CopyAction ) )
562 const QList<int> indexes = existingMapLayerFromMimeData( event->mimeData(), handledUris );
563 if ( !indexes.isEmpty() )
566 event->setDropAction( Qt::CopyAction );
572 const QStringList uris = compatibleUrisFromMimeData( mParameter, event->mimeData(), handledUris );
573 if ( !uris.isEmpty() )
576 event->setDropAction( Qt::CopyAction );
581void QgsProcessingMultipleInputPanelWidget::dropEvent( QDropEvent *event )
583 if ( !( event->possibleActions() & Qt::CopyAction ) )
587 const QList<int> indexes = existingMapLayerFromMimeData( event->mimeData(), handledUris );
588 if ( !indexes.isEmpty() )
591 setFocus( Qt::MouseFocusReason );
592 event->setDropAction( Qt::CopyAction );
595 for (
const int i : indexes )
597 mModel->item( i )->setCheckState( Qt::Checked );
599 emit selectionChanged();
603 const QStringList uris = compatibleUrisFromMimeData( mParameter, event->mimeData(), handledUris );
604 if ( !uris.isEmpty() )
606 for (
const QString &uri : uris )
608 addOption( uri, uri,
true );
610 emit selectionChanged();
614void QgsProcessingMultipleInputPanelWidget::populateFromProject(
QgsProject *project )
617 for (
int i = 0; i < mModel->rowCount(); ++i )
619 const QStandardItem *item = mModel->item( i );
620 if ( item->data( Qt::UserRole ) == layerId )
622 bool isChecked = ( item->checkState() == Qt::Checked );
623 mModel->removeRow( i );
626 emit selectionChanged();
635 const QString authid = layer->crs().authid();
637 if ( settings.value( u
"Processing/Configuration/SHOW_CRS_DEF"_s,
true ).toBool() && !authid.isEmpty() )
638 title = u
"%1 [%2]"_s.arg( layer->name(), authid );
640 title = layer->name();
644 QString
id = layer->id();
648 for (
int i = 0; i < mModel->rowCount(); ++i )
651 if ( mModel->item( i )->data( Qt::UserRole ) == layer->id() )
657 else if ( mModel->item( i )->data( Qt::UserRole ) == layer->source() )
659 id = layer->source();
665 addOption(
id, title,
false,
true, icon );
668 switch ( mParameter->layerType() )
815QgsProcessingMultipleInputDialog::QgsProcessingMultipleInputDialog(
const QgsProcessingParameterMultipleLayers *parameter,
const QVariantList &selectedOptions,
const QList<QgsProcessingModelChildParameterSource> &modelSources, QgsProcessingModelAlgorithm *model, QWidget *parent, Qt::WindowFlags flags )
816 : QDialog( parent, flags )
818 setWindowTitle( tr(
"Multiple Selection" ) );
819 QVBoxLayout *vLayout =
new QVBoxLayout();
820 mWidget =
new QgsProcessingMultipleInputPanelWidget( parameter, selectedOptions, modelSources, model,
nullptr );
821 vLayout->addWidget( mWidget );
822 mWidget->buttonBox()->addButton( QDialogButtonBox::Cancel );
823 connect( mWidget->buttonBox(), &QDialogButtonBox::accepted,
this, &QDialog::accept );
824 connect( mWidget->buttonBox(), &QDialogButtonBox::rejected,
this, &QDialog::reject );
825 setLayout( vLayout );
826 setAcceptDrops(
true );
829QVariantList QgsProcessingMultipleInputDialog::selectedOptions()
const
831 return mWidget->selectedOptions();
834void QgsProcessingMultipleInputDialog::setProject(
QgsProject *project )
836 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,...