34#include <QDirIterator>
35#include <QDragEnterEvent>
39#include <QStandardItem>
40#include <QStandardItemModel>
43#include "moc_qgsprocessingmultipleselectiondialog.cpp"
47QgsProcessingMultipleSelectionPanelWidget::QgsProcessingMultipleSelectionPanelWidget(
const QVariantList &availableOptions,
const QVariantList &selectedOptions, QWidget *parent )
49 , mValueFormatter( []( const QVariant &v ) -> QString {
50 if ( v.userType() == qMetaTypeId<QgsProcessingModelChildParameterSource>() )
51 return v.value<QgsProcessingModelChildParameterSource>().staticValue().toString();
60 mSelectionList->setSelectionBehavior( QAbstractItemView::SelectRows );
61 mSelectionList->setSelectionMode( QAbstractItemView::ExtendedSelection );
62 mSelectionList->setDragDropMode( QAbstractItemView::InternalMove );
64 mButtonSelectAll =
new QPushButton( tr(
"Select All" ) );
65 mButtonBox->addButton( mButtonSelectAll, QDialogButtonBox::ActionRole );
67 mButtonClearSelection =
new QPushButton( tr(
"Clear Selection" ) );
68 mButtonBox->addButton( mButtonClearSelection, QDialogButtonBox::ActionRole );
70 mButtonToggleSelection =
new QPushButton( tr(
"Toggle Selection" ) );
71 mButtonBox->addButton( mButtonToggleSelection, QDialogButtonBox::ActionRole );
73 connect( mButtonSelectAll, &QPushButton::clicked,
this, [
this] { selectAll(
true ); } );
74 connect( mButtonClearSelection, &QPushButton::clicked,
this, [
this] { selectAll(
false ); } );
75 connect( mButtonToggleSelection, &QPushButton::clicked,
this, &QgsProcessingMultipleSelectionPanelWidget::toggleSelection );
77 connect( mButtonBox, &QDialogButtonBox::accepted,
this, &QgsProcessingMultipleSelectionPanelWidget::acceptClicked );
78 populateList( availableOptions, selectedOptions );
80 connect( mModel, &QStandardItemModel::itemChanged,
this, &QgsProcessingMultipleSelectionPanelWidget::selectionChanged );
84 connect( mModel, &QStandardItemModel::rowsRemoved,
this, &QgsProcessingMultipleSelectionPanelWidget::selectionChanged );
87void QgsProcessingMultipleSelectionPanelWidget::setValueFormatter(
const std::function<QString(
const QVariant & )> &formatter )
89 mValueFormatter = formatter;
91 for (
int i = 0; i < mModel->rowCount(); ++i )
93 mModel->item( i )->setText( mValueFormatter( mModel->item( i )->data( Qt::UserRole ) ) );
97QVariantList QgsProcessingMultipleSelectionPanelWidget::selectedOptions()
const
100 options.reserve( mModel->rowCount() );
101 bool hasModelSources =
false;
102 for (
int i = 0; i < mModel->rowCount(); ++i )
104 QStandardItem *item = mModel->item( i );
110 if ( item->checkState() == Qt::Checked )
112 const QVariant option = item->data( Qt::UserRole );
114 if ( option.userType() == qMetaTypeId<QgsProcessingModelChildParameterSource>() )
115 hasModelSources =
true;
121 if ( hasModelSources )
124 QVariantList originalOptions = options;
126 for (
const QVariant &option : originalOptions )
128 if ( option.userType() == qMetaTypeId<QgsProcessingModelChildParameterSource>() )
131 options << QVariant::fromValue( QgsProcessingModelChildParameterSource::fromStaticValue( option ) );
139void QgsProcessingMultipleSelectionPanelWidget::selectAll(
const bool checked )
141 const QList<QStandardItem *> items = currentItems();
142 for ( QStandardItem *item : items )
144 item->setCheckState( checked ? Qt::Checked : Qt::Unchecked );
148void QgsProcessingMultipleSelectionPanelWidget::toggleSelection()
150 const QList<QStandardItem *> items = currentItems();
151 for ( QStandardItem *item : items )
153 item->setCheckState( item->checkState() == Qt::Unchecked ? Qt::Checked : Qt::Unchecked );
157QList<QStandardItem *> QgsProcessingMultipleSelectionPanelWidget::currentItems()
159 QList<QStandardItem *> items;
160 const QModelIndexList selection = mSelectionList->selectionModel()->selectedIndexes();
161 if ( selection.size() > 1 )
163 items.reserve( selection.size() );
164 for (
const QModelIndex &index : selection )
166 items << mModel->itemFromIndex( index );
171 items.reserve( mModel->rowCount() );
172 for (
int i = 0; i < mModel->rowCount(); ++i )
174 items << mModel->item( i );
180void QgsProcessingMultipleSelectionPanelWidget::populateList(
const QVariantList &availableOptions,
const QVariantList &selectedOptions )
182 mModel =
new QStandardItemModel(
this );
184 QVariantList remainingOptions = availableOptions;
187 for (
const QVariant &option : selectedOptions )
193 addOption( option, mValueFormatter( option ),
true );
194 remainingOptions.removeAll( option );
197 for (
const QVariant &option : std::as_const( remainingOptions ) )
199 addOption( option, mValueFormatter( option ),
false );
202 mSelectionList->setModel( mModel );
205QList<int> QgsProcessingMultipleSelectionPanelWidget::existingMapLayerFromMimeData(
const QMimeData *data )
const
214 for (
int i = 0; i < mModel->rowCount(); ++i )
217 QString userRole = mModel->item( i )->data( Qt::UserRole ).toString();
218 if ( userRole == layer->id() || userRole == layer->source() )
228void QgsProcessingMultipleSelectionPanelWidget::dragEnterEvent( QDragEnterEvent *event )
230 if ( !( event->possibleActions() & Qt::CopyAction ) )
233 const QList<int> indexes = existingMapLayerFromMimeData( event->mimeData() );
234 if ( !indexes.isEmpty() )
237 event->setDropAction( Qt::CopyAction );
242void QgsProcessingMultipleSelectionPanelWidget::dropEvent( QDropEvent *event )
244 if ( !( event->possibleActions() & Qt::CopyAction ) )
247 const QList<int> indexes = existingMapLayerFromMimeData( event->mimeData() );
248 if ( !indexes.isEmpty() )
251 setFocus( Qt::MouseFocusReason );
252 event->setDropAction( Qt::CopyAction );
255 for (
const int i : indexes )
257 mModel->item( i )->setCheckState( Qt::Checked );
259 emit selectionChanged();
263void QgsProcessingMultipleSelectionPanelWidget::addOption(
const QVariant &value,
const QString &title,
bool selected,
bool updateExistingTitle )
266 for (
int i = 0; i < mModel->rowCount(); ++i )
268 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>() ) )
270 if ( updateExistingTitle )
271 mModel->item( i )->setText( title );
276 auto item = std::make_unique<QStandardItem>( title );
277 item->setData( value, Qt::UserRole );
278 item->setCheckState( selected ? Qt::Checked : Qt::Unchecked );
279 item->setCheckable(
true );
280 item->setDropEnabled(
false );
281 mModel->appendRow( item.release() );
289QgsProcessingMultipleSelectionDialog::QgsProcessingMultipleSelectionDialog(
const QVariantList &availableOptions,
const QVariantList &selectedOptions, QWidget *parent, Qt::WindowFlags flags )
290 : QDialog( parent, flags )
292 setWindowTitle( tr(
"Multiple Selection" ) );
293 QVBoxLayout *vLayout =
new QVBoxLayout();
294 mWidget =
new QgsProcessingMultipleSelectionPanelWidget( availableOptions, selectedOptions );
295 vLayout->addWidget( mWidget );
296 mWidget->buttonBox()->addButton( QDialogButtonBox::Cancel );
297 connect( mWidget->buttonBox(), &QDialogButtonBox::accepted,
this, &QDialog::accept );
298 connect( mWidget->buttonBox(), &QDialogButtonBox::rejected,
this, &QDialog::reject );
299 setLayout( vLayout );
302void QgsProcessingMultipleSelectionDialog::setValueFormatter(
const std::function<QString(
const QVariant & )> &formatter )
304 mWidget->setValueFormatter( formatter );
307QVariantList QgsProcessingMultipleSelectionDialog::selectedOptions()
const
309 return mWidget->selectedOptions();
317QgsProcessingMultipleInputPanelWidget::QgsProcessingMultipleInputPanelWidget(
const QgsProcessingParameterMultipleLayers *parameter,
const QVariantList &selectedOptions,
const QList<QgsProcessingModelChildParameterSource> &modelSources, QgsProcessingModelAlgorithm *model, QWidget *parent )
318 : QgsProcessingMultipleSelectionPanelWidget( QVariantList(), selectedOptions, parent )
319 , mParameter( parameter )
321 QPushButton *addFileButton =
new QPushButton( tr(
"Add File(s)…" ) );
322 connect( addFileButton, &QPushButton::clicked,
this, &QgsProcessingMultipleInputPanelWidget::addFiles );
323 buttonBox()->addButton( addFileButton, QDialogButtonBox::ActionRole );
325 QPushButton *addDirButton =
new QPushButton( tr(
"Add Directory…" ) );
326 connect( addDirButton, &QPushButton::clicked,
this, &QgsProcessingMultipleInputPanelWidget::addDirectory );
327 buttonBox()->addButton( addDirButton, QDialogButtonBox::ActionRole );
328 setAcceptDrops(
true );
329 for (
const QgsProcessingModelChildParameterSource &source : modelSources )
331 addOption( QVariant::fromValue( source ), source.friendlyIdentifier( model ),
false,
true );
335void QgsProcessingMultipleInputPanelWidget::setProject(
QgsProject *project )
338 populateFromProject( project );
341void QgsProcessingMultipleInputPanelWidget::addFiles()
344 QString path = settings.
value( QStringLiteral(
"/Processing/LastInputPath" ), QDir::homePath() ).toString();
348 filter = generator->createFileFilter();
350 filter = QObject::tr(
"All files (*.*)" );
352 const QStringList filenames = QFileDialog::getOpenFileNames(
this, tr(
"Select File(s)" ), path, filter );
353 if ( filenames.empty() )
356 settings.
setValue( QStringLiteral(
"/Processing/LastInputPath" ), QFileInfo( filenames.at( 0 ) ).path() );
358 for (
const QString &file : filenames )
360 addOption( file, file,
true );
363 emit selectionChanged();
366void QgsProcessingMultipleInputPanelWidget::addDirectory()
369 const QString path = settings.
value( QStringLiteral(
"/Processing/LastInputPath" ), QDir::homePath() ).toString();
371 const QString dir = QFileDialog::getExistingDirectory(
this, tr(
"Select Directory" ), path );
375 settings.
setValue( QStringLiteral(
"/Processing/LastInputPath" ), dir );
377 QStringList nameFilters;
381 for (
const QString &extension : extensions )
383 nameFilters << QStringLiteral(
"*.%1" ).arg( extension );
384 nameFilters << QStringLiteral(
"*.%1" ).arg( extension.toUpper() );
385 nameFilters << QStringLiteral(
"*.%1" ).arg( extension.toLower() );
389 QDirIterator it( dir, nameFilters, QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot, QDirIterator::Subdirectories );
390 while ( it.hasNext() )
392 const QString fullPath = it.next();
393 if ( fullPath.endsWith( QLatin1String(
".dbf" ), Qt::CaseInsensitive ) )
395 if ( QFileInfo::exists( QStringLiteral(
"%1.shp" ).arg( fullPath.chopped( 4 ) ) ) || QFileInfo::exists( QStringLiteral(
"%1.SHP" ).arg( fullPath.chopped( 4 ) ) ) )
401 else if ( fullPath.endsWith( QLatin1String(
".aux.xml" ), Qt::CaseInsensitive ) || fullPath.endsWith( QLatin1String(
".shp.xml" ), Qt::CaseInsensitive ) )
406 addOption( fullPath, fullPath,
true );
408 emit selectionChanged();
411QList<int> QgsProcessingMultipleInputPanelWidget::existingMapLayerFromMimeData(
const QMimeData *data,
QgsMimeDataUtils::UriList &handledUrls )
const
419 bool matched =
false;
422 for (
int i = 0; i < mModel->rowCount(); ++i )
425 const QString userRole = mModel->item( i )->data( Qt::UserRole ).toString();
426 if ( userRole == layer->id() || userRole == layer->source() )
436 handledUrls.append( u );
445 QStringList skipUrlData;
446 skipUrlData.reserve( skipUrls.size() );
449 skipUrlData.append( u.data() );
457 if ( skipUrlData.contains( u.data() ) )
470 bool acceptable =
false;
514 if ( !uriList.isEmpty() )
518 QStringList rawPaths;
519 if ( data->hasUrls() )
521 const QList<QUrl> urls = data->urls();
522 rawPaths.reserve( urls.count() );
523 for (
const QUrl &url : urls )
525 const QString local = url.toLocalFile();
526 if ( !rawPaths.contains( local ) )
527 rawPaths.append( local );
530 if ( !data->text().isEmpty() && !rawPaths.contains( data->text() ) )
531 rawPaths.append( data->text() );
533 for (
const QString &path : std::as_const( rawPaths ) )
535 QFileInfo file( path );
546void QgsProcessingMultipleInputPanelWidget::dragEnterEvent( QDragEnterEvent *event )
548 if ( !( event->possibleActions() & Qt::CopyAction ) )
553 const QList<int> indexes = existingMapLayerFromMimeData( event->mimeData(), handledUris );
554 if ( !indexes.isEmpty() )
557 event->setDropAction( Qt::CopyAction );
563 const QStringList uris = compatibleUrisFromMimeData( mParameter, event->mimeData(), handledUris );
564 if ( !uris.isEmpty() )
567 event->setDropAction( Qt::CopyAction );
572void QgsProcessingMultipleInputPanelWidget::dropEvent( QDropEvent *event )
574 if ( !( event->possibleActions() & Qt::CopyAction ) )
578 const QList<int> indexes = existingMapLayerFromMimeData( event->mimeData(), handledUris );
579 if ( !indexes.isEmpty() )
582 setFocus( Qt::MouseFocusReason );
583 event->setDropAction( Qt::CopyAction );
586 for (
const int i : indexes )
588 mModel->item( i )->setCheckState( Qt::Checked );
590 emit selectionChanged();
594 const QStringList uris = compatibleUrisFromMimeData( mParameter, event->mimeData(), handledUris );
595 if ( !uris.isEmpty() )
597 for (
const QString &uri : uris )
599 addOption( uri, uri,
true );
601 emit selectionChanged();
605void QgsProcessingMultipleInputPanelWidget::populateFromProject(
QgsProject *project )
608 for (
int i = 0; i < mModel->rowCount(); ++i )
610 const QStandardItem *item = mModel->item( i );
611 if ( item->data( Qt::UserRole ) == layerId )
613 bool isChecked = ( item->checkState() == Qt::Checked );
614 mModel->removeRow( i );
617 emit selectionChanged();
626 const QString authid = layer->crs().authid();
628 if ( settings.value( QStringLiteral(
"Processing/Configuration/SHOW_CRS_DEF" ),
true ).toBool() && !authid.isEmpty() )
629 title = QStringLiteral(
"%1 [%2]" ).arg( layer->name(), authid );
631 title = layer->name();
634 QString
id = layer->id();
636 id = QStringLiteral(
"main" );
638 for (
int i = 0; i < mModel->rowCount(); ++i )
641 if ( mModel->item( i )->data( Qt::UserRole ) == layer->id() )
646 else if ( mModel->item( i )->data( Qt::UserRole ) == layer->source() )
648 id = layer->source();
653 addOption(
id, title,
false,
true );
656 switch ( mParameter->layerType() )
803QgsProcessingMultipleInputDialog::QgsProcessingMultipleInputDialog(
const QgsProcessingParameterMultipleLayers *parameter,
const QVariantList &selectedOptions,
const QList<QgsProcessingModelChildParameterSource> &modelSources, QgsProcessingModelAlgorithm *model, QWidget *parent, Qt::WindowFlags flags )
804 : QDialog( parent, flags )
806 setWindowTitle( tr(
"Multiple Selection" ) );
807 QVBoxLayout *vLayout =
new QVBoxLayout();
808 mWidget =
new QgsProcessingMultipleInputPanelWidget( parameter, selectedOptions, modelSources, model );
809 vLayout->addWidget( mWidget );
810 mWidget->buttonBox()->addButton( QDialogButtonBox::Cancel );
811 connect( mWidget->buttonBox(), &QDialogButtonBox::accepted,
this, &QDialog::accept );
812 connect( mWidget->buttonBox(), &QDialogButtonBox::rejected,
this, &QDialog::reject );
813 setLayout( vLayout );
814 setAcceptDrops(
true );
817QVariantList QgsProcessingMultipleInputDialog::selectedOptions()
const
819 return mWidget->selectedOptions();
822void QgsProcessingMultipleInputDialog::setProject(
QgsProject *project )
824 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 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...