17#include "moc_qgsprocessingmultipleselectiondialog.cpp"
30#include <QStandardItemModel>
31#include <QStandardItem>
36#include <QDirIterator>
38#include <QDragEnterEvent>
42QgsProcessingMultipleSelectionPanelWidget::QgsProcessingMultipleSelectionPanelWidget(
const QVariantList &availableOptions,
const QVariantList &selectedOptions, QWidget *parent )
44 , mValueFormatter( []( const QVariant &v ) -> QString {
45 if ( v.userType() == qMetaTypeId<QgsProcessingModelChildParameterSource>() )
46 return v.value<QgsProcessingModelChildParameterSource>().staticValue().toString();
55 mSelectionList->setSelectionBehavior( QAbstractItemView::SelectRows );
56 mSelectionList->setSelectionMode( QAbstractItemView::ExtendedSelection );
57 mSelectionList->setDragDropMode( QAbstractItemView::InternalMove );
59 mButtonSelectAll =
new QPushButton( tr(
"Select All" ) );
60 mButtonBox->addButton( mButtonSelectAll, QDialogButtonBox::ActionRole );
62 mButtonClearSelection =
new QPushButton( tr(
"Clear Selection" ) );
63 mButtonBox->addButton( mButtonClearSelection, QDialogButtonBox::ActionRole );
65 mButtonToggleSelection =
new QPushButton( tr(
"Toggle Selection" ) );
66 mButtonBox->addButton( mButtonToggleSelection, QDialogButtonBox::ActionRole );
68 connect( mButtonSelectAll, &QPushButton::clicked,
this, [=] { selectAll(
true ); } );
69 connect( mButtonClearSelection, &QPushButton::clicked,
this, [=] { selectAll(
false ); } );
70 connect( mButtonToggleSelection, &QPushButton::clicked,
this, &QgsProcessingMultipleSelectionPanelWidget::toggleSelection );
72 connect( mButtonBox, &QDialogButtonBox::accepted,
this, &QgsProcessingMultipleSelectionPanelWidget::acceptClicked );
73 populateList( availableOptions, selectedOptions );
75 connect( mModel, &QStandardItemModel::itemChanged,
this, &QgsProcessingMultipleSelectionPanelWidget::selectionChanged );
79 connect( mModel, &QStandardItemModel::rowsRemoved,
this, &QgsProcessingMultipleSelectionPanelWidget::selectionChanged );
82void QgsProcessingMultipleSelectionPanelWidget::setValueFormatter(
const std::function<QString(
const QVariant & )> &formatter )
84 mValueFormatter = formatter;
86 for (
int i = 0; i < mModel->rowCount(); ++i )
88 mModel->item( i )->setText( mValueFormatter( mModel->item( i )->data( Qt::UserRole ) ) );
92QVariantList QgsProcessingMultipleSelectionPanelWidget::selectedOptions()
const
95 options.reserve( mModel->rowCount() );
96 bool hasModelSources =
false;
97 for (
int i = 0; i < mModel->rowCount(); ++i )
99 QStandardItem *item = mModel->item( i );
105 if ( item->checkState() == Qt::Checked )
107 const QVariant option = item->data( Qt::UserRole );
109 if ( option.userType() == qMetaTypeId<QgsProcessingModelChildParameterSource>() )
110 hasModelSources =
true;
116 if ( hasModelSources )
119 QVariantList originalOptions = options;
121 for (
const QVariant &option : originalOptions )
123 if ( option.userType() == qMetaTypeId<QgsProcessingModelChildParameterSource>() )
126 options << QVariant::fromValue( QgsProcessingModelChildParameterSource::fromStaticValue( option ) );
134void QgsProcessingMultipleSelectionPanelWidget::selectAll(
const bool checked )
136 const QList<QStandardItem *> items = currentItems();
137 for ( QStandardItem *item : items )
139 item->setCheckState( checked ? Qt::Checked : Qt::Unchecked );
143void QgsProcessingMultipleSelectionPanelWidget::toggleSelection()
145 const QList<QStandardItem *> items = currentItems();
146 for ( QStandardItem *item : items )
148 item->setCheckState( item->checkState() == Qt::Unchecked ? Qt::Checked : Qt::Unchecked );
152QList<QStandardItem *> QgsProcessingMultipleSelectionPanelWidget::currentItems()
154 QList<QStandardItem *> items;
155 const QModelIndexList selection = mSelectionList->selectionModel()->selectedIndexes();
156 if ( selection.size() > 1 )
158 items.reserve( selection.size() );
159 for (
const QModelIndex &index : selection )
161 items << mModel->itemFromIndex( index );
166 items.reserve( mModel->rowCount() );
167 for (
int i = 0; i < mModel->rowCount(); ++i )
169 items << mModel->item( i );
175void QgsProcessingMultipleSelectionPanelWidget::populateList(
const QVariantList &availableOptions,
const QVariantList &selectedOptions )
177 mModel =
new QStandardItemModel(
this );
179 QVariantList remainingOptions = availableOptions;
182 for (
const QVariant &option : selectedOptions )
188 addOption( option, mValueFormatter( option ),
true );
189 remainingOptions.removeAll( option );
192 for (
const QVariant &option : std::as_const( remainingOptions ) )
194 addOption( option, mValueFormatter( option ),
false );
197 mSelectionList->setModel( mModel );
200QList<int> QgsProcessingMultipleSelectionPanelWidget::existingMapLayerFromMimeData(
const QMimeData *data )
const
209 for (
int i = 0; i < mModel->rowCount(); ++i )
212 QString userRole = mModel->item( i )->data( Qt::UserRole ).toString();
213 if ( userRole == layer->id() || userRole == layer->source() )
223void QgsProcessingMultipleSelectionPanelWidget::dragEnterEvent( QDragEnterEvent *event )
225 if ( !( event->possibleActions() & Qt::CopyAction ) )
228 const QList<int> indexes = existingMapLayerFromMimeData( event->mimeData() );
229 if ( !indexes.isEmpty() )
232 event->setDropAction( Qt::CopyAction );
237void QgsProcessingMultipleSelectionPanelWidget::dropEvent( QDropEvent *event )
239 if ( !( event->possibleActions() & Qt::CopyAction ) )
242 const QList<int> indexes = existingMapLayerFromMimeData( event->mimeData() );
243 if ( !indexes.isEmpty() )
246 setFocus( Qt::MouseFocusReason );
247 event->setDropAction( Qt::CopyAction );
250 for (
const int i : indexes )
252 mModel->item( i )->setCheckState( Qt::Checked );
254 emit selectionChanged();
258void QgsProcessingMultipleSelectionPanelWidget::addOption(
const QVariant &value,
const QString &title,
bool selected,
bool updateExistingTitle )
261 for (
int i = 0; i < mModel->rowCount(); ++i )
263 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>() ) )
265 if ( updateExistingTitle )
266 mModel->item( i )->setText( title );
271 std::unique_ptr<QStandardItem> item = std::make_unique<QStandardItem>( title );
272 item->setData( value, Qt::UserRole );
273 item->setCheckState( selected ? Qt::Checked : Qt::Unchecked );
274 item->setCheckable(
true );
275 item->setDropEnabled(
false );
276 mModel->appendRow( item.release() );
284QgsProcessingMultipleSelectionDialog::QgsProcessingMultipleSelectionDialog(
const QVariantList &availableOptions,
const QVariantList &selectedOptions, QWidget *parent, Qt::WindowFlags flags )
285 : QDialog( parent, flags )
287 setWindowTitle( tr(
"Multiple Selection" ) );
288 QVBoxLayout *vLayout =
new QVBoxLayout();
289 mWidget =
new QgsProcessingMultipleSelectionPanelWidget( availableOptions, selectedOptions );
290 vLayout->addWidget( mWidget );
291 mWidget->buttonBox()->addButton( QDialogButtonBox::Cancel );
292 connect( mWidget->buttonBox(), &QDialogButtonBox::accepted,
this, &QDialog::accept );
293 connect( mWidget->buttonBox(), &QDialogButtonBox::rejected,
this, &QDialog::reject );
294 setLayout( vLayout );
297void QgsProcessingMultipleSelectionDialog::setValueFormatter(
const std::function<QString(
const QVariant & )> &formatter )
299 mWidget->setValueFormatter( formatter );
302QVariantList QgsProcessingMultipleSelectionDialog::selectedOptions()
const
304 return mWidget->selectedOptions();
312QgsProcessingMultipleInputPanelWidget::QgsProcessingMultipleInputPanelWidget(
const QgsProcessingParameterMultipleLayers *parameter,
const QVariantList &selectedOptions,
const QList<QgsProcessingModelChildParameterSource> &modelSources, QgsProcessingModelAlgorithm *model, QWidget *parent )
313 : QgsProcessingMultipleSelectionPanelWidget( QVariantList(), selectedOptions, parent )
314 , mParameter( parameter )
316 QPushButton *addFileButton =
new QPushButton( tr(
"Add File(s)…" ) );
317 connect( addFileButton, &QPushButton::clicked,
this, &QgsProcessingMultipleInputPanelWidget::addFiles );
318 buttonBox()->addButton( addFileButton, QDialogButtonBox::ActionRole );
320 QPushButton *addDirButton =
new QPushButton( tr(
"Add Directory…" ) );
321 connect( addDirButton, &QPushButton::clicked,
this, &QgsProcessingMultipleInputPanelWidget::addDirectory );
322 buttonBox()->addButton( addDirButton, QDialogButtonBox::ActionRole );
323 setAcceptDrops(
true );
324 for (
const QgsProcessingModelChildParameterSource &source : modelSources )
326 addOption( QVariant::fromValue( source ), source.friendlyIdentifier( model ),
false,
true );
330void QgsProcessingMultipleInputPanelWidget::setProject(
QgsProject *project )
333 populateFromProject( project );
336void QgsProcessingMultipleInputPanelWidget::addFiles()
339 QString path = settings.
value( QStringLiteral(
"/Processing/LastInputPath" ), QDir::homePath() ).toString();
343 filter = generator->createFileFilter();
345 filter = QObject::tr(
"All files (*.*)" );
347 const QStringList filenames = QFileDialog::getOpenFileNames(
this, tr(
"Select File(s)" ), path, filter );
348 if ( filenames.empty() )
351 settings.
setValue( QStringLiteral(
"/Processing/LastInputPath" ), QFileInfo( filenames.at( 0 ) ).path() );
353 for (
const QString &file : filenames )
355 addOption( file, file,
true );
358 emit selectionChanged();
361void QgsProcessingMultipleInputPanelWidget::addDirectory()
364 const QString path = settings.
value( QStringLiteral(
"/Processing/LastInputPath" ), QDir::homePath() ).toString();
366 const QString dir = QFileDialog::getExistingDirectory(
this, tr(
"Select Directory" ), path );
370 settings.
setValue( QStringLiteral(
"/Processing/LastInputPath" ), dir );
372 QStringList nameFilters;
376 for (
const QString &extension : extensions )
378 nameFilters << QStringLiteral(
"*.%1" ).arg( extension );
379 nameFilters << QStringLiteral(
"*.%1" ).arg( extension.toUpper() );
380 nameFilters << QStringLiteral(
"*.%1" ).arg( extension.toLower() );
384 QDirIterator it( dir, nameFilters, QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot, QDirIterator::Subdirectories );
385 while ( it.hasNext() )
387 const QString fullPath = it.next();
388 if ( fullPath.endsWith( QLatin1String(
".dbf" ), Qt::CaseInsensitive ) )
390 if ( QFileInfo::exists( QStringLiteral(
"%1.shp" ).arg( fullPath.chopped( 4 ) ) ) || QFileInfo::exists( QStringLiteral(
"%1.SHP" ).arg( fullPath.chopped( 4 ) ) ) )
396 else if ( fullPath.endsWith( QLatin1String(
".aux.xml" ), Qt::CaseInsensitive ) || fullPath.endsWith( QLatin1String(
".shp.xml" ), Qt::CaseInsensitive ) )
401 addOption( fullPath, fullPath,
true );
403 emit selectionChanged();
406QList<int> QgsProcessingMultipleInputPanelWidget::existingMapLayerFromMimeData(
const QMimeData *data,
QgsMimeDataUtils::UriList &handledUrls )
const
414 bool matched =
false;
417 for (
int i = 0; i < mModel->rowCount(); ++i )
420 const QString userRole = mModel->item( i )->data( Qt::UserRole ).toString();
421 if ( userRole == layer->id() || userRole == layer->source() )
431 handledUrls.append( u );
440 QStringList skipUrlData;
441 skipUrlData.reserve( skipUrls.size() );
444 skipUrlData.append( u.data() );
452 if ( skipUrlData.contains( u.data() ) )
463 && u.layerType == QLatin1String(
"vector" ) )
465 bool acceptable =
false;
496 && u.layerType == QLatin1String(
"raster" ) && u.providerKey == QLatin1String(
"gdal" ) )
499 && u.layerType == QLatin1String(
"mesh" ) && u.providerKey == QLatin1String(
"mdal" ) )
502 && u.layerType == QLatin1String(
"pointcloud" ) )
505 && u.layerType == QLatin1String(
"vector-tile" ) )
509 if ( !uriList.isEmpty() )
513 QStringList rawPaths;
514 if ( data->hasUrls() )
516 const QList<QUrl> urls = data->urls();
517 rawPaths.reserve( urls.count() );
518 for (
const QUrl &url : urls )
520 const QString local = url.toLocalFile();
521 if ( !rawPaths.contains( local ) )
522 rawPaths.append( local );
525 if ( !data->text().isEmpty() && !rawPaths.contains( data->text() ) )
526 rawPaths.append( data->text() );
528 for (
const QString &path : std::as_const( rawPaths ) )
530 QFileInfo file( path );
541void QgsProcessingMultipleInputPanelWidget::dragEnterEvent( QDragEnterEvent *event )
543 if ( !( event->possibleActions() & Qt::CopyAction ) )
548 const QList<int> indexes = existingMapLayerFromMimeData( event->mimeData(), handledUris );
549 if ( !indexes.isEmpty() )
552 event->setDropAction( Qt::CopyAction );
558 const QStringList uris = compatibleUrisFromMimeData( mParameter, event->mimeData(), handledUris );
559 if ( !uris.isEmpty() )
562 event->setDropAction( Qt::CopyAction );
567void QgsProcessingMultipleInputPanelWidget::dropEvent( QDropEvent *event )
569 if ( !( event->possibleActions() & Qt::CopyAction ) )
573 const QList<int> indexes = existingMapLayerFromMimeData( event->mimeData(), handledUris );
574 if ( !indexes.isEmpty() )
577 setFocus( Qt::MouseFocusReason );
578 event->setDropAction( Qt::CopyAction );
581 for (
const int i : indexes )
583 mModel->item( i )->setCheckState( Qt::Checked );
585 emit selectionChanged();
589 const QStringList uris = compatibleUrisFromMimeData( mParameter, event->mimeData(), handledUris );
590 if ( !uris.isEmpty() )
592 for (
const QString &uri : uris )
594 addOption( uri, uri,
true );
596 emit selectionChanged();
600void QgsProcessingMultipleInputPanelWidget::populateFromProject(
QgsProject *project )
603 for (
int i = 0; i < mModel->rowCount(); ++i )
605 const QStandardItem *item = mModel->item( i );
606 if ( item->data( Qt::UserRole ) == layerId )
608 bool isChecked = ( item->checkState() == Qt::Checked );
609 mModel->removeRow( i );
612 emit selectionChanged();
621 const QString authid = layer->crs().authid();
623 if ( settings.value( QStringLiteral(
"Processing/Configuration/SHOW_CRS_DEF" ),
true ).toBool() && !authid.isEmpty() )
624 title = QStringLiteral(
"%1 [%2]" ).arg( layer->name(), authid );
626 title = layer->name();
629 QString
id = layer->id();
631 id = QStringLiteral(
"main" );
633 for (
int i = 0; i < mModel->rowCount(); ++i )
636 if ( mModel->item( i )->data( Qt::UserRole ) == layer->id() )
641 else if ( mModel->item( i )->data( Qt::UserRole ) == layer->source() )
643 id = layer->source();
648 addOption(
id, title,
false,
true );
651 switch ( mParameter->layerType() )
787QgsProcessingMultipleInputDialog::QgsProcessingMultipleInputDialog(
const QgsProcessingParameterMultipleLayers *parameter,
const QVariantList &selectedOptions,
const QList<QgsProcessingModelChildParameterSource> &modelSources, QgsProcessingModelAlgorithm *model, QWidget *parent, Qt::WindowFlags flags )
788 : QDialog( parent, flags )
790 setWindowTitle( tr(
"Multiple Selection" ) );
791 QVBoxLayout *vLayout =
new QVBoxLayout();
792 mWidget =
new QgsProcessingMultipleInputPanelWidget( parameter, selectedOptions, modelSources, model );
793 vLayout->addWidget( mWidget );
794 mWidget->buttonBox()->addButton( QDialogButtonBox::Cancel );
795 connect( mWidget->buttonBox(), &QDialogButtonBox::accepted,
this, &QDialog::accept );
796 connect( mWidget->buttonBox(), &QDialogButtonBox::rejected,
this, &QDialog::reject );
797 setLayout( vLayout );
798 setAcceptDrops(
true );
801QVariantList QgsProcessingMultipleInputDialog::selectedOptions()
const
803 return mWidget->selectedOptions();
806void QgsProcessingMultipleInputDialog::setProject(
QgsProject *project )
808 mWidget->setProject( project );
@ File
Files (i.e. non map layer sources, such as text files)
@ 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.
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...
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< 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.
This class is a composition of two QSettings instances:
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 vector layer which manages a vector based data sets.
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...