23 #include <QHBoxLayout> 24 #include <QVBoxLayout> 25 #include <QToolButton> 27 #include <QDragEnterEvent> 33 , mParameter( parameter->clone() )
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.get() )->dataTypes();
73 dataTypes = static_cast< QgsProcessingParameterVectorLayer *>( mParameter.get() )->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 );
121 onLayerChanged( mCombo->currentLayer() );
124 QgsProcessingMapLayerComboBox::~QgsProcessingMapLayerComboBox() =
default;
126 void QgsProcessingMapLayerComboBox::setLayer(
QgsMapLayer *layer )
129 mCombo->setLayer( layer );
132 QgsMapLayer *QgsProcessingMapLayerComboBox::currentLayer()
134 return mCombo->currentLayer();
137 QString QgsProcessingMapLayerComboBox::currentText()
139 return mCombo->currentText();
142 void QgsProcessingMapLayerComboBox::setValue(
const QVariant &value,
QgsProcessingContext &context )
144 QVariant val = value;
146 bool selectedOnly =
false;
167 if ( !layer && val.
type() == QVariant::String )
174 mBlockChangedSignal++;
177 found =
static_cast< bool >( currentLayer() );
178 bool changed = found && ( currentLayer() != prevLayer );
179 if ( found && mUseSelectionCheckBox )
182 changed = changed | ( ( hasSelection && selectedOnly ) != mUseSelectionCheckBox->isChecked() );
185 mUseSelectionCheckBox->setEnabled(
true );
186 mUseSelectionCheckBox->setChecked( selectedOnly );
190 mUseSelectionCheckBox->setChecked(
false );
191 mUseSelectionCheckBox->setEnabled(
false );
194 mBlockChangedSignal--;
201 const QString
string = val.toString();
202 if ( mUseSelectionCheckBox )
204 mUseSelectionCheckBox->setChecked(
false );
205 mUseSelectionCheckBox->setEnabled(
false );
207 if ( !
string.isEmpty() )
209 mBlockChangedSignal++;
210 if ( mCombo->findText(
string ) < 0 )
212 QStringList additional = mCombo->additionalItems();
213 additional.append(
string );
214 mCombo->setAdditionalItems( additional );
216 mCombo->setCurrentIndex( mCombo->findText(
string ) );
217 mBlockChangedSignal--;
218 if ( !mBlockChangedSignal )
223 mCombo->setLayer(
nullptr );
228 QVariant QgsProcessingMapLayerComboBox::value()
const 232 if ( mUseSelectionCheckBox && mUseSelectionCheckBox->isChecked() )
239 if ( !mCombo->currentText().isEmpty() )
241 if ( mUseSelectionCheckBox && mUseSelectionCheckBox->isChecked() )
244 return mCombo->currentText();
251 QgsMapLayer *QgsProcessingMapLayerComboBox::compatibleMapLayerFromMimeData(
const QMimeData *data,
bool &incompatibleLayerSelected )
const 253 incompatibleLayerSelected =
false;
260 if ( mCombo->mProxyModel->acceptsLayer( layer ) )
264 incompatibleLayerSelected =
true;
273 QString QgsProcessingMapLayerComboBox::compatibleUriFromMimeData(
const QMimeData *data )
const 281 && u.layerType == QLatin1String(
"vector" ) && u.providerKey == QLatin1String(
"ogr" ) )
314 && u.layerType == QLatin1String(
"raster" ) && u.providerKey == QLatin1String(
"gdal" ) )
318 && u.layerType == QLatin1String(
"mesh" ) && u.providerKey == QLatin1String(
"mdal" ) )
321 if ( !uriList.isEmpty() )
325 QStringList rawPaths;
326 if ( data->hasUrls() )
328 const QList< QUrl > urls = data->urls();
329 rawPaths.reserve( urls.count() );
330 for (
const QUrl &url : urls )
332 const QString local = url.toLocalFile();
333 if ( !rawPaths.contains( local ) )
334 rawPaths.append( local );
337 if ( !data->text().isEmpty() && !rawPaths.contains( data->text() ) )
338 rawPaths.append( data->text() );
340 for (
const QString &path : qgis::as_const( rawPaths ) )
342 QFileInfo file( path );
353 void QgsProcessingMapLayerComboBox::dragEnterEvent( QDragEnterEvent *event )
355 if ( !( event->possibleActions() & Qt::CopyAction ) )
358 bool incompatibleLayerSelected =
false;
359 QgsMapLayer *layer = compatibleMapLayerFromMimeData( event->mimeData(), incompatibleLayerSelected );
360 const QString uri = compatibleUriFromMimeData( event->mimeData() );
361 if ( layer || ( !incompatibleLayerSelected && !uri.isEmpty() ) )
364 event->setDropAction( Qt::CopyAction );
367 mCombo->mHighlight =
true;
372 void QgsProcessingMapLayerComboBox::dragLeaveEvent( QDragLeaveEvent *event )
374 QWidget::dragLeaveEvent( event );
379 mCombo->mHighlight =
false;
384 void QgsProcessingMapLayerComboBox::dropEvent( QDropEvent *event )
386 if ( !( event->possibleActions() & Qt::CopyAction ) )
389 bool incompatibleLayerSelected =
false;
390 QgsMapLayer *layer = compatibleMapLayerFromMimeData( event->mimeData(), incompatibleLayerSelected );
391 const QString uri = compatibleUriFromMimeData( event->mimeData() );
392 if ( layer || ( !incompatibleLayerSelected && !uri.isEmpty() ) )
395 setFocus( Qt::MouseFocusReason );
396 event->setDropAction( Qt::CopyAction );
399 setValue( layer ? QVariant::fromValue( layer ) : QVariant::fromValue( uri ), context );
402 mCombo->mHighlight =
false;
406 void QgsProcessingMapLayerComboBox::onLayerChanged(
QgsMapLayer *layer )
410 if (
QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( layer ) )
412 if (
QgsVectorLayer *prevLayer = qobject_cast< QgsVectorLayer * >( mPrevLayer ) )
416 if ( vl->selectedFeatureCount() == 0 )
417 mUseSelectionCheckBox->setChecked(
false );
418 mUseSelectionCheckBox->setEnabled( vl->selectedFeatureCount() > 0 );
424 if ( !mBlockChangedSignal )
430 if ( selected.isEmpty() )
431 mUseSelectionCheckBox->setChecked(
false );
432 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.
virtual bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified symbology visitor, causing it to visit all symbols associated with the layer...
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.