|
| 1 | +/*************************************************************************** |
| 2 | + qgsprocessingmaplayercombobox.cpp |
| 3 | + ------------------------------- |
| 4 | + begin : June 2019 |
| 5 | + copyright : (C) 2019 by Nyall Dawson |
| 6 | + email : nyall dot dawson at gmail dot com |
| 7 | + *************************************************************************** |
| 8 | + * * |
| 9 | + * This program is free software; you can redistribute it and/or modify * |
| 10 | + * it under the terms of the GNU General Public License as published by * |
| 11 | + * the Free Software Foundation; either version 2 of the License, or * |
| 12 | + * (at your option) any later version. * |
| 13 | + * * |
| 14 | + ***************************************************************************/ |
| 15 | + |
| 16 | +#include "qgsprocessingmaplayercombobox.h" |
| 17 | +#include "qgsmaplayercombobox.h" |
| 18 | +#include "qgsmimedatautils.h" |
| 19 | +#include "qgsprocessingparameters.h" |
| 20 | +#include "qgssettings.h" |
| 21 | +#include "qgsvectorlayer.h" |
| 22 | +#include "qgsfeatureid.h" |
| 23 | +#include <QHBoxLayout> |
| 24 | +#include <QVBoxLayout> |
| 25 | +#include <QToolButton> |
| 26 | +#include <QCheckBox> |
| 27 | +#include <QDragEnterEvent> |
| 28 | + |
| 29 | +///@cond PRIVATE |
| 30 | + |
| 31 | +QgsProcessingMapLayerComboBox::QgsProcessingMapLayerComboBox( QgsProcessingParameterDefinition *parameter, QWidget *parent ) |
| 32 | + : QWidget( parent ) |
| 33 | + , mParameter( parameter ) |
| 34 | +{ |
| 35 | + QHBoxLayout *layout = new QHBoxLayout(); |
| 36 | + layout->setMargin( 0 ); |
| 37 | + layout->setContentsMargins( 0, 0, 0, 0 ); |
| 38 | + layout->setSpacing( 6 ); |
| 39 | + |
| 40 | + mCombo = new QgsMapLayerComboBox(); |
| 41 | + layout->addWidget( mCombo ); |
| 42 | + layout->setAlignment( mCombo, Qt::AlignTop ); |
| 43 | + |
| 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 ); |
| 50 | + |
| 51 | + QVBoxLayout *vl = new QVBoxLayout(); |
| 52 | + vl->setMargin( 0 ); |
| 53 | + vl->setContentsMargins( 0, 0, 0, 0 ); |
| 54 | + vl->setSpacing( 6 ); |
| 55 | + vl->addLayout( layout ); |
| 56 | + |
| 57 | + QgsMapLayerProxyModel::Filters filters = nullptr; |
| 58 | + |
| 59 | + if ( mParameter->type() == QgsProcessingParameterFeatureSource::typeName() ) |
| 60 | + { |
| 61 | + mUseSelectionCheckBox = new QCheckBox( tr( "Selected features only" ) ); |
| 62 | + mUseSelectionCheckBox->setChecked( false ); |
| 63 | + mUseSelectionCheckBox->setEnabled( false ); |
| 64 | + vl->addWidget( mUseSelectionCheckBox ); |
| 65 | + } |
| 66 | + |
| 67 | + if ( mParameter->type() == QgsProcessingParameterFeatureSource::typeName() || mParameter->type() == QgsProcessingParameterVectorLayer::typeName() ) |
| 68 | + { |
| 69 | + QList<int> dataTypes; |
| 70 | + if ( mParameter->type() == QgsProcessingParameterFeatureSource::typeName() ) |
| 71 | + dataTypes = static_cast< QgsProcessingParameterFeatureSource *>( mParameter )->dataTypes(); |
| 72 | + else if ( mParameter->type() == QgsProcessingParameterVectorLayer::typeName() ) |
| 73 | + dataTypes = static_cast< QgsProcessingParameterVectorLayer *>( mParameter )->dataTypes(); |
| 74 | + |
| 75 | + if ( dataTypes.contains( QgsProcessing::TypeVectorAnyGeometry ) || dataTypes.isEmpty() ) |
| 76 | + filters = QgsMapLayerProxyModel::HasGeometry; |
| 77 | + if ( dataTypes.contains( QgsProcessing::TypeVectorPoint ) ) |
| 78 | + filters |= QgsMapLayerProxyModel::PointLayer; |
| 79 | + if ( dataTypes.contains( QgsProcessing::TypeVectorLine ) ) |
| 80 | + filters |= QgsMapLayerProxyModel::LineLayer; |
| 81 | + if ( dataTypes.contains( QgsProcessing::TypeVectorPolygon ) ) |
| 82 | + filters |= QgsMapLayerProxyModel::PolygonLayer; |
| 83 | + if ( !filters ) |
| 84 | + filters = QgsMapLayerProxyModel::VectorLayer; |
| 85 | + } |
| 86 | + else if ( mParameter->type() == QgsProcessingParameterRasterLayer::typeName() ) |
| 87 | + { |
| 88 | + filters = QgsMapLayerProxyModel::RasterLayer; |
| 89 | + } |
| 90 | + else if ( mParameter->type() == QgsProcessingParameterMeshLayer::typeName() ) |
| 91 | + { |
| 92 | + filters = QgsMapLayerProxyModel::MeshLayer; |
| 93 | + } |
| 94 | + |
| 95 | + QgsSettings settings; |
| 96 | + if ( settings.value( QStringLiteral( "Processing/Configuration/SHOW_CRS_DEF" ), true ).toBool() ) |
| 97 | + mCombo->setShowCrs( true ); |
| 98 | + |
| 99 | + if ( filters ) |
| 100 | + mCombo->setFilters( filters ); |
| 101 | + mCombo->setExcludedProviders( QStringList() << QStringLiteral( "grass" ) ); // not sure if this is still required... |
| 102 | + |
| 103 | + if ( mParameter->flags() & QgsProcessingParameterDefinition::FlagOptional ) |
| 104 | + { |
| 105 | + mCombo->setAllowEmptyLayer( true ); |
| 106 | + mCombo->setLayer( nullptr ); |
| 107 | + } |
| 108 | + |
| 109 | + connect( mCombo, &QgsMapLayerComboBox::layerChanged, this, &QgsProcessingMapLayerComboBox::onLayerChanged ); |
| 110 | + if ( mUseSelectionCheckBox ) |
| 111 | + connect( mUseSelectionCheckBox, &QCheckBox::toggled, this, [ = ] |
| 112 | + { |
| 113 | + if ( !mBlockChangedSignal ) |
| 114 | + emit valueChanged(); |
| 115 | + } ); |
| 116 | + |
| 117 | + setLayout( vl ); |
| 118 | + |
| 119 | + setAcceptDrops( true ); |
| 120 | +} |
| 121 | + |
| 122 | +void QgsProcessingMapLayerComboBox::setLayer( QgsMapLayer *layer ) |
| 123 | +{ |
| 124 | + if ( layer || mParameter->flags() & QgsProcessingParameterDefinition::FlagOptional ) |
| 125 | + mCombo->setLayer( layer ); |
| 126 | +} |
| 127 | + |
| 128 | +QgsMapLayer *QgsProcessingMapLayerComboBox::currentLayer() |
| 129 | +{ |
| 130 | + return mCombo->currentLayer(); |
| 131 | +} |
| 132 | + |
| 133 | +QString QgsProcessingMapLayerComboBox::currentText() |
| 134 | +{ |
| 135 | + return mCombo->currentText(); |
| 136 | +} |
| 137 | + |
| 138 | +void QgsProcessingMapLayerComboBox::setValue( const QVariant &value, QgsProcessingContext &context ) |
| 139 | +{ |
| 140 | + QVariant val = value; |
| 141 | + bool found = false; |
| 142 | + bool selectedOnly = false; |
| 143 | + if ( val.canConvert<QgsProcessingFeatureSourceDefinition>() ) |
| 144 | + { |
| 145 | + QgsProcessingFeatureSourceDefinition fromVar = qvariant_cast<QgsProcessingFeatureSourceDefinition>( val ); |
| 146 | + val = fromVar.source; |
| 147 | + selectedOnly = fromVar.selectedFeaturesOnly; |
| 148 | + } |
| 149 | + |
| 150 | + if ( val.canConvert<QgsProperty>() ) |
| 151 | + { |
| 152 | + if ( val.value< QgsProperty >().propertyType() == QgsProperty::StaticProperty ) |
| 153 | + { |
| 154 | + val = val.value< QgsProperty >().staticValue(); |
| 155 | + } |
| 156 | + else |
| 157 | + { |
| 158 | + val = val.value< QgsProperty >().valueAsString( context.expressionContext(), mParameter->defaultValue().toString() ); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( val.value< QObject * >() ); |
| 163 | + if ( !layer && val.type() == QVariant::String ) |
| 164 | + { |
| 165 | + layer = QgsProcessingUtils::mapLayerFromString( val.toString(), context, false ); |
| 166 | + } |
| 167 | + |
| 168 | + if ( layer ) |
| 169 | + { |
| 170 | + mBlockChangedSignal++; |
| 171 | + QgsMapLayer *prevLayer = currentLayer(); |
| 172 | + setLayer( layer ); |
| 173 | + found = static_cast< bool >( currentLayer() ); |
| 174 | + bool changed = found && ( currentLayer() != prevLayer ); |
| 175 | + if ( found && mUseSelectionCheckBox ) |
| 176 | + { |
| 177 | + const bool hasSelection = qobject_cast< QgsVectorLayer * >( layer ) && qobject_cast< QgsVectorLayer * >( layer )->selectedFeatureCount() > 0; |
| 178 | + changed = changed | ( ( hasSelection && selectedOnly ) != mUseSelectionCheckBox->isChecked() ); |
| 179 | + if ( hasSelection ) |
| 180 | + { |
| 181 | + mUseSelectionCheckBox->setEnabled( true ); |
| 182 | + mUseSelectionCheckBox->setChecked( selectedOnly ); |
| 183 | + } |
| 184 | + else |
| 185 | + { |
| 186 | + mUseSelectionCheckBox->setChecked( false ); |
| 187 | + mUseSelectionCheckBox->setEnabled( false ); |
| 188 | + } |
| 189 | + } |
| 190 | + mBlockChangedSignal--; |
| 191 | + if ( changed ) |
| 192 | + emit valueChanged(); // and ensure we only ever raise one |
| 193 | + } |
| 194 | + |
| 195 | + if ( !found ) |
| 196 | + { |
| 197 | + const QString string = val.toString(); |
| 198 | + if ( !string.isEmpty() ) |
| 199 | + { |
| 200 | + mBlockChangedSignal++; |
| 201 | + if ( mCombo->findText( string ) < 0 ) |
| 202 | + { |
| 203 | + QStringList additional = mCombo->additionalItems(); |
| 204 | + additional.append( string ); |
| 205 | + mCombo->setAdditionalItems( additional ); |
| 206 | + } |
| 207 | + mCombo->setCurrentIndex( mCombo->findText( string ) ); // this may or may not throw a signal, so let's block it.. |
| 208 | + mBlockChangedSignal--; |
| 209 | + if ( !mBlockChangedSignal ) |
| 210 | + emit valueChanged(); // and ensure we only ever raise one |
| 211 | + } |
| 212 | + else if ( mParameter->flags() & QgsProcessingParameterDefinition::FlagOptional ) |
| 213 | + { |
| 214 | + mCombo->setLayer( nullptr ); |
| 215 | + } |
| 216 | + } |
| 217 | +} |
| 218 | + |
| 219 | +QVariant QgsProcessingMapLayerComboBox::value() const |
| 220 | +{ |
| 221 | + if ( QgsMapLayer *layer = mCombo->currentLayer() ) |
| 222 | + { |
| 223 | + if ( mUseSelectionCheckBox && mUseSelectionCheckBox->isChecked() ) |
| 224 | + return QgsProcessingFeatureSourceDefinition( layer->id(), true ); |
| 225 | + else |
| 226 | + return layer->id(); |
| 227 | + } |
| 228 | + else |
| 229 | + { |
| 230 | + if ( !mCombo->currentText().isEmpty() ) |
| 231 | + { |
| 232 | + if ( mUseSelectionCheckBox && mUseSelectionCheckBox->isChecked() ) |
| 233 | + return QgsProcessingFeatureSourceDefinition( mCombo->currentText(), true ); |
| 234 | + else |
| 235 | + return mCombo->currentText(); |
| 236 | + } |
| 237 | + } |
| 238 | + return QVariant(); |
| 239 | +} |
| 240 | + |
| 241 | + |
| 242 | +QgsMapLayer *QgsProcessingMapLayerComboBox::compatibleMapLayerFromMimeData( const QMimeData *data, bool &incompatibleLayerSelected ) const |
| 243 | +{ |
| 244 | + incompatibleLayerSelected = false; |
| 245 | + const QgsMimeDataUtils::UriList uriList = QgsMimeDataUtils::decodeUriList( data ); |
| 246 | + for ( const QgsMimeDataUtils::Uri &u : uriList ) |
| 247 | + { |
| 248 | + // is this uri from the current project? |
| 249 | + if ( QgsMapLayer *layer = u.mapLayer() ) |
| 250 | + { |
| 251 | + if ( mCombo->mProxyModel->acceptsLayer( layer ) ) |
| 252 | + return layer; |
| 253 | + else |
| 254 | + { |
| 255 | + incompatibleLayerSelected = true; |
| 256 | + return nullptr; |
| 257 | + } |
| 258 | + } |
| 259 | + } |
| 260 | + return nullptr; |
| 261 | +} |
| 262 | + |
| 263 | + |
| 264 | +QString QgsProcessingMapLayerComboBox::compatibleUriFromMimeData( const QMimeData *data ) const |
| 265 | +{ |
| 266 | + const QgsMimeDataUtils::UriList uriList = QgsMimeDataUtils::decodeUriList( data ); |
| 267 | + for ( const QgsMimeDataUtils::Uri &u : uriList ) |
| 268 | + { |
| 269 | + if ( ( mParameter->type() == QgsProcessingParameterFeatureSource::typeName() |
| 270 | + || mParameter->type() == QgsProcessingParameterVectorLayer::typeName() |
| 271 | + || mParameter->type() == QgsProcessingParameterMapLayer::typeName() ) |
| 272 | + && u.layerType == QLatin1String( "vector" ) && u.providerKey == QLatin1String( "ogr" ) ) |
| 273 | + return u.uri; |
| 274 | + else if ( ( mParameter->type() == QgsProcessingParameterRasterLayer::typeName() |
| 275 | + || mParameter->type() == QgsProcessingParameterMapLayer::typeName() ) |
| 276 | + && u.layerType == QLatin1String( "raster" ) && u.providerKey == QLatin1String( "gdal" ) ) |
| 277 | + return u.uri; |
| 278 | + else if ( ( mParameter->type() == QgsProcessingParameterMeshLayer::typeName() |
| 279 | + || mParameter->type() == QgsProcessingParameterMapLayer::typeName() ) |
| 280 | + && u.layerType == QLatin1String( "mesh" ) && u.providerKey == QLatin1String( "mdal" ) ) |
| 281 | + return u.uri; |
| 282 | + } |
| 283 | + if ( !uriList.isEmpty() ) |
| 284 | + return QString(); |
| 285 | + |
| 286 | + // second chance -- files dragged from file explorer, outside of QGIS |
| 287 | + QStringList rawPaths; |
| 288 | + if ( data->hasUrls() ) |
| 289 | + { |
| 290 | + const QList< QUrl > urls = data->urls(); |
| 291 | + rawPaths.reserve( urls.count() ); |
| 292 | + for ( const QUrl &url : urls ) |
| 293 | + { |
| 294 | + const QString local = url.toLocalFile(); |
| 295 | + if ( !rawPaths.contains( local ) ) |
| 296 | + rawPaths.append( local ); |
| 297 | + } |
| 298 | + } |
| 299 | + if ( !data->text().isEmpty() && !rawPaths.contains( data->text() ) ) |
| 300 | + rawPaths.append( data->text() ); |
| 301 | + |
| 302 | + for ( const QString &path : qgis::as_const( rawPaths ) ) |
| 303 | + { |
| 304 | + QFileInfo file( path ); |
| 305 | + if ( file.isFile() ) |
| 306 | + { |
| 307 | + // TODO - we should check to see if it's a valid extension for the parameter, but that's non-trivial |
| 308 | + return path; |
| 309 | + } |
| 310 | + } |
| 311 | + |
| 312 | + return QString(); |
| 313 | +} |
| 314 | + |
| 315 | +void QgsProcessingMapLayerComboBox::dragEnterEvent( QDragEnterEvent *event ) |
| 316 | +{ |
| 317 | + if ( !( event->possibleActions() & Qt::CopyAction ) ) |
| 318 | + return; |
| 319 | + |
| 320 | + bool incompatibleLayerSelected = false; |
| 321 | + QgsMapLayer *layer = compatibleMapLayerFromMimeData( event->mimeData(), incompatibleLayerSelected ); |
| 322 | + const QString uri = compatibleUriFromMimeData( event->mimeData() ); |
| 323 | + if ( layer || ( !incompatibleLayerSelected && !uri.isEmpty() ) ) |
| 324 | + { |
| 325 | + // dragged an acceptable layer, phew |
| 326 | + event->setDropAction( Qt::CopyAction ); |
| 327 | + event->accept(); |
| 328 | + mDragActive = true; |
| 329 | + mCombo->mHighlight = true; |
| 330 | + update(); |
| 331 | + } |
| 332 | +} |
| 333 | + |
| 334 | +void QgsProcessingMapLayerComboBox::dragLeaveEvent( QDragLeaveEvent *event ) |
| 335 | +{ |
| 336 | + QWidget::dragLeaveEvent( event ); |
| 337 | + if ( mDragActive ) |
| 338 | + { |
| 339 | + event->accept(); |
| 340 | + mDragActive = false; |
| 341 | + mCombo->mHighlight = false; |
| 342 | + update(); |
| 343 | + } |
| 344 | +} |
| 345 | + |
| 346 | +void QgsProcessingMapLayerComboBox::dropEvent( QDropEvent *event ) |
| 347 | +{ |
| 348 | + if ( !( event->possibleActions() & Qt::CopyAction ) ) |
| 349 | + return; |
| 350 | + |
| 351 | + bool incompatibleLayerSelected = false; |
| 352 | + QgsMapLayer *layer = compatibleMapLayerFromMimeData( event->mimeData(), incompatibleLayerSelected ); |
| 353 | + const QString uri = compatibleUriFromMimeData( event->mimeData() ); |
| 354 | + if ( layer || ( !incompatibleLayerSelected && !uri.isEmpty() ) ) |
| 355 | + { |
| 356 | + // dropped an acceptable layer, phew |
| 357 | + setFocus( Qt::MouseFocusReason ); |
| 358 | + event->setDropAction( Qt::CopyAction ); |
| 359 | + event->accept(); |
| 360 | + QgsProcessingContext context; |
| 361 | + setValue( layer ? QVariant::fromValue( layer ) : QVariant::fromValue( uri ), context ); |
| 362 | + } |
| 363 | + mDragActive = false; |
| 364 | + mCombo->mHighlight = false; |
| 365 | + update(); |
| 366 | +} |
| 367 | + |
| 368 | +void QgsProcessingMapLayerComboBox::onLayerChanged( QgsMapLayer *layer ) |
| 369 | +{ |
| 370 | + if ( mParameter->type() == QgsProcessingParameterFeatureSource::typeName() ) |
| 371 | + { |
| 372 | + if ( QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( layer ) ) |
| 373 | + { |
| 374 | + if ( QgsVectorLayer *prevLayer = qobject_cast< QgsVectorLayer * >( mPrevLayer ) ) |
| 375 | + { |
| 376 | + disconnect( prevLayer, &QgsVectorLayer::selectionChanged, this, &QgsProcessingMapLayerComboBox::selectionChanged ); |
| 377 | + } |
| 378 | + if ( vl->selectedFeatureCount() == 0 ) |
| 379 | + mUseSelectionCheckBox->setChecked( false ); |
| 380 | + mUseSelectionCheckBox->setEnabled( vl->selectedFeatureCount() > 0 ); |
| 381 | + connect( vl, &QgsVectorLayer::selectionChanged, this, &QgsProcessingMapLayerComboBox::selectionChanged ); |
| 382 | + } |
| 383 | + } |
| 384 | + |
| 385 | + mPrevLayer = layer; |
| 386 | + if ( !mBlockChangedSignal ) |
| 387 | + emit valueChanged(); |
| 388 | +} |
| 389 | + |
| 390 | +void QgsProcessingMapLayerComboBox::selectionChanged( const QgsFeatureIds &selected, const QgsFeatureIds &, bool ) |
| 391 | +{ |
| 392 | + if ( selected.isEmpty() ) |
| 393 | + mUseSelectionCheckBox->setChecked( false ); |
| 394 | + mUseSelectionCheckBox->setEnabled( !selected.isEmpty() ); |
| 395 | +} |
| 396 | + |
| 397 | + |
| 398 | + |
| 399 | +///@endcond |
0 commit comments