23 #include <QHBoxLayout> 24 #include <QVBoxLayout> 25 #include <QToolButton> 27 #include <QDragEnterEvent> 33 , mParameter( parameter )
35 QHBoxLayout *layout =
new QHBoxLayout();
36 layout->setMargin( 0 );
37 layout->setContentsMargins( 0, 0, 0, 0 );
38 layout->setSpacing( 6 );
41 layout->addWidget( mCombo );
42 layout->setAlignment( mCombo, Qt::AlignTop );
44 mSelectButton =
new QToolButton();
45 mSelectButton->setText( QStringLiteral(
"…" ) );
46 mSelectButton->setToolTip( tr(
"Select file" ) );
47 connect( mSelectButton, &QToolButton::clicked,
this, &QgsProcessingMapLayerComboBox::triggerFileSelection );
48 layout->addWidget( mSelectButton );
49 layout->setAlignment( mSelectButton, Qt::AlignTop );
51 QVBoxLayout *vl =
new QVBoxLayout();
53 vl->setContentsMargins( 0, 0, 0, 0 );
55 vl->addLayout( layout );
57 QgsMapLayerProxyModel::Filters filters =
nullptr;
61 mUseSelectionCheckBox =
new QCheckBox( tr(
"Selected features only" ) );
62 mUseSelectionCheckBox->setChecked(
false );
63 mUseSelectionCheckBox->setEnabled(
false );
64 vl->addWidget( mUseSelectionCheckBox );
71 dataTypes = static_cast< QgsProcessingParameterFeatureSource *>( mParameter )->dataTypes();
73 dataTypes = static_cast< QgsProcessingParameterVectorLayer *>( mParameter )->dataTypes();
96 if ( settings.
value( QStringLiteral(
"Processing/Configuration/SHOW_CRS_DEF" ),
true ).toBool() )
97 mCombo->setShowCrs(
true );
100 mCombo->setFilters( filters );
101 mCombo->setExcludedProviders( QStringList() << QStringLiteral(
"grass" ) );
105 mCombo->setAllowEmptyLayer(
true );
106 mCombo->setLayer(
nullptr );
110 if ( mUseSelectionCheckBox )
111 connect( mUseSelectionCheckBox, &QCheckBox::toggled,
this, [ = ]
113 if ( !mBlockChangedSignal )
119 setAcceptDrops(
true );
122 void QgsProcessingMapLayerComboBox::setLayer(
QgsMapLayer *layer )
125 mCombo->setLayer( layer );
128 QgsMapLayer *QgsProcessingMapLayerComboBox::currentLayer()
130 return mCombo->currentLayer();
133 QString QgsProcessingMapLayerComboBox::currentText()
135 return mCombo->currentText();
138 void QgsProcessingMapLayerComboBox::setValue(
const QVariant &value,
QgsProcessingContext &context )
140 QVariant val = value;
142 bool selectedOnly =
false;
163 if ( !layer && val.
type() == QVariant::String )
170 mBlockChangedSignal++;
173 found =
static_cast< bool >( currentLayer() );
174 bool changed = found && ( currentLayer() != prevLayer );
175 if ( found && mUseSelectionCheckBox )
178 changed = changed | ( ( hasSelection && selectedOnly ) != mUseSelectionCheckBox->isChecked() );
181 mUseSelectionCheckBox->setEnabled(
true );
182 mUseSelectionCheckBox->setChecked( selectedOnly );
186 mUseSelectionCheckBox->setChecked(
false );
187 mUseSelectionCheckBox->setEnabled(
false );
190 mBlockChangedSignal--;
197 const QString
string = val.toString();
198 if ( !
string.isEmpty() )
200 mBlockChangedSignal++;
201 if ( mCombo->findText(
string ) < 0 )
203 QStringList additional = mCombo->additionalItems();
204 additional.append(
string );
205 mCombo->setAdditionalItems( additional );
207 mCombo->setCurrentIndex( mCombo->findText(
string ) );
208 mBlockChangedSignal--;
209 if ( !mBlockChangedSignal )
214 mCombo->setLayer(
nullptr );
219 QVariant QgsProcessingMapLayerComboBox::value()
const 223 if ( mUseSelectionCheckBox && mUseSelectionCheckBox->isChecked() )
230 if ( !mCombo->currentText().isEmpty() )
232 if ( mUseSelectionCheckBox && mUseSelectionCheckBox->isChecked() )
235 return mCombo->currentText();
242 QgsMapLayer *QgsProcessingMapLayerComboBox::compatibleMapLayerFromMimeData(
const QMimeData *data,
bool &incompatibleLayerSelected )
const 244 incompatibleLayerSelected =
false;
251 if ( mCombo->mProxyModel->acceptsLayer( layer ) )
255 incompatibleLayerSelected =
true;
264 QString QgsProcessingMapLayerComboBox::compatibleUriFromMimeData(
const QMimeData *data )
const 272 && u.layerType == QLatin1String(
"vector" ) && u.providerKey == QLatin1String(
"ogr" ) )
305 && u.layerType == QLatin1String(
"raster" ) && u.providerKey == QLatin1String(
"gdal" ) )
309 && u.layerType == QLatin1String(
"mesh" ) && u.providerKey == QLatin1String(
"mdal" ) )
312 if ( !uriList.isEmpty() )
316 QStringList rawPaths;
317 if ( data->hasUrls() )
319 const QList< QUrl > urls = data->urls();
320 rawPaths.reserve( urls.count() );
321 for (
const QUrl &url : urls )
323 const QString local = url.toLocalFile();
324 if ( !rawPaths.contains( local ) )
325 rawPaths.append( local );
328 if ( !data->text().isEmpty() && !rawPaths.contains( data->text() ) )
329 rawPaths.append( data->text() );
331 for (
const QString &path : qgis::as_const( rawPaths ) )
333 QFileInfo file( path );
344 void QgsProcessingMapLayerComboBox::dragEnterEvent( QDragEnterEvent *event )
346 if ( !( event->possibleActions() & Qt::CopyAction ) )
349 bool incompatibleLayerSelected =
false;
350 QgsMapLayer *layer = compatibleMapLayerFromMimeData( event->mimeData(), incompatibleLayerSelected );
351 const QString uri = compatibleUriFromMimeData( event->mimeData() );
352 if ( layer || ( !incompatibleLayerSelected && !uri.isEmpty() ) )
355 event->setDropAction( Qt::CopyAction );
358 mCombo->mHighlight =
true;
363 void QgsProcessingMapLayerComboBox::dragLeaveEvent( QDragLeaveEvent *event )
365 QWidget::dragLeaveEvent( event );
370 mCombo->mHighlight =
false;
375 void QgsProcessingMapLayerComboBox::dropEvent( QDropEvent *event )
377 if ( !( event->possibleActions() & Qt::CopyAction ) )
380 bool incompatibleLayerSelected =
false;
381 QgsMapLayer *layer = compatibleMapLayerFromMimeData( event->mimeData(), incompatibleLayerSelected );
382 const QString uri = compatibleUriFromMimeData( event->mimeData() );
383 if ( layer || ( !incompatibleLayerSelected && !uri.isEmpty() ) )
386 setFocus( Qt::MouseFocusReason );
387 event->setDropAction( Qt::CopyAction );
390 setValue( layer ? QVariant::fromValue( layer ) : QVariant::fromValue( uri ), context );
393 mCombo->mHighlight =
false;
397 void QgsProcessingMapLayerComboBox::onLayerChanged(
QgsMapLayer *layer )
401 if (
QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( layer ) )
403 if (
QgsVectorLayer *prevLayer = qobject_cast< QgsVectorLayer * >( mPrevLayer ) )
407 if ( vl->selectedFeatureCount() == 0 )
408 mUseSelectionCheckBox->setChecked(
false );
409 mUseSelectionCheckBox->setEnabled( vl->selectedFeatureCount() > 0 );
415 if ( !mBlockChangedSignal )
421 if ( selected.isEmpty() )
422 mUseSelectionCheckBox->setChecked(
false );
423 mUseSelectionCheckBox->setEnabled( !selected.isEmpty() );
Base class for all map layer types.
QSet< QgsFeatureId > QgsFeatureIds
QgsMapLayerType type() const
Returns the type of the layer.
This class is a composition of two QSettings instances:
static QString typeName()
Returns the type name for the parameter class.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
static QString typeName()
Returns the type name for the parameter class.
static UriList decodeUriList(const QMimeData *data)
int selectedFeatureCount() const
Returns the number of features that are selected in this layer.
static QString typeName()
Returns the type name for the parameter class.
QgsMapLayer::LayerFlags flags() const
Returns the flags for this layer.
void layerChanged(QgsMapLayer *layer)
Emitted whenever the currently selected layer changes.
bool selectedFeaturesOnly
true if only selected features in the source should be used by algorithms.
static QString typeName()
Returns the type name for the parameter class.
static QgsMapLayer * mapLayerFromString(const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers=true, QgsProcessingUtils::LayerHint typeHint=QgsProcessingUtils::LayerHint::UnknownType)
Interprets a string as a map layer within the supplied context.
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
QgsProperty source
Source definition.
Type propertyType() const
Returns the property type.
static QString typeName()
Returns the type name for the parameter class.
static GeometryType geometryType(Type type)
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
The QgsMapLayerComboBox class is a combo box which displays the list of layers.
A vector layer (with or without geometry) parameter for processing algorithms.
void selectionChanged(const QgsFeatureIds &selected, const QgsFeatureIds &deselected, bool clearAndSelect)
Emitted when selection was changed.
A store for object properties.
QgsExpressionContext & expressionContext()
Returns the expression context.
Encapsulates settings relating to a feature source input to a processing algorithm.
An input feature source (such as vector layers) parameter for processing algorithms.
Base class for the definition of processing parameters.
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
QList< QgsMimeDataUtils::Uri > UriList
Represents a vector layer which manages a vector based data sets.
Static property (QgsStaticProperty)
Contains information about the context in which a processing algorithm is executed.
Any vector layer with geometry.