29#include <QInputDialog>
35QgsProcessingLayerOutputDestinationWidget::QgsProcessingLayerOutputDestinationWidget(
const QgsProcessingDestinationParameter *param,
bool defaultSelection, QWidget *parent )
38 , mDefaultSelection( defaultSelection )
40 Q_ASSERT( mParameter );
44 leText->setClearButtonEnabled(
false );
46 connect( leText, &QLineEdit::textEdited,
this, &QgsProcessingLayerOutputDestinationWidget::textChanged );
48 mMenu =
new QMenu(
this );
49 connect( mMenu, &QMenu::aboutToShow,
this, &QgsProcessingLayerOutputDestinationWidget::menuAboutToShow );
50 mSelectButton->setMenu( mMenu );
51 mSelectButton->setPopupMode( QToolButton::InstantPopup );
54 mEncoding = settings.
value( QStringLiteral(
"/Processing/encoding" ), QStringLiteral(
"System" ) ).toString();
56 if ( !mParameter->defaultValueForGui().isValid() )
60 setValue( QVariant() );
66 setValue( mParameter->defaultValueForGui() );
69 setToolTip( mParameter->toolTip() );
71 setAcceptDrops(
true );
72 leText->setAcceptDrops(
false );
75bool QgsProcessingLayerOutputDestinationWidget::outputIsSkipped()
const
77 return leText->text().isEmpty() && !mUseTemporary;
80void QgsProcessingLayerOutputDestinationWidget::setValue(
const QVariant &value )
82 const bool prevSkip = outputIsSkipped();
83 mUseRemapping =
false;
84 if ( !value.isValid() || ( value.type() == QVariant::String && value.toString().isEmpty() ) )
97 else if ( value.userType() == QMetaType::type(
"QgsProcessingOutputLayerDefinition" ) )
106 const QVariant prev = QgsProcessingLayerOutputDestinationWidget::value();
108 mUseTemporary =
false;
110 emit skipOutputChanged(
false );
111 if ( prev != QgsProcessingLayerOutputDestinationWidget::value() )
112 emit destinationChanged();
116 mEncoding = def.
createOptions.value( QStringLiteral(
"fileEncoding" ) ).toString();
120 const QVariant prev = QgsProcessingLayerOutputDestinationWidget::value();
121 leText->setText( value.toString() );
122 mUseTemporary =
false;
124 emit skipOutputChanged(
false );
128 if ( prev.toString() != QgsProcessingLayerOutputDestinationWidget::value().toString() )
129 emit destinationChanged();
133 if ( prev.userType() != QMetaType::type(
"QgsProcessingOutputLayerDefinition" ) ||
135 emit destinationChanged();
141QVariant QgsProcessingLayerOutputDestinationWidget::value()
const
149 else if ( mUseTemporary && !mDefaultSelection )
155 key = leText->text();
164 && !key.startsWith( QLatin1String(
"memory:" ) )
165 && !key.startsWith( QLatin1String(
"ogr:" ) )
166 && !key.startsWith( QLatin1String(
"postgres:" ) )
167 && !key.startsWith( QLatin1String(
"postgis:" ) )
171 QString folder = QFileInfo( key ).path();
175 QString defaultFolder = settings.
value( QStringLiteral(
"/Processing/Configuration/OUTPUTS_FOLDER" ) ).toString();
176 key = QDir( defaultFolder ).filePath( key );
186 value.createOptions.insert( QStringLiteral(
"fileEncoding" ), mEncoding );
188 value.setRemappingDefinition( mRemapDefinition );
204 mParametersGenerator = generator;
207void QgsProcessingLayerOutputDestinationWidget::addOpenAfterRunningOption()
209 Q_ASSERT( mOpenAfterRunningCheck ==
nullptr );
210 mOpenAfterRunningCheck =
new QCheckBox( tr(
"Open output file after running algorithm" ) );
211 mOpenAfterRunningCheck->setChecked( !outputIsSkipped() );
212 mOpenAfterRunningCheck->setEnabled( !outputIsSkipped() );
213 gridLayout->addWidget( mOpenAfterRunningCheck, 1, 0, 1, 2 );
215 connect(
this, &QgsProcessingLayerOutputDestinationWidget::skipOutputChanged,
this, [ = ](
bool skipped )
217 bool enabled = !skipped;
218 mOpenAfterRunningCheck->setEnabled( enabled );
219 mOpenAfterRunningCheck->setChecked( enabled );
223bool QgsProcessingLayerOutputDestinationWidget::openAfterRunning()
const
225 return mOpenAfterRunningCheck && mOpenAfterRunningCheck->isChecked();
228void QgsProcessingLayerOutputDestinationWidget::menuAboutToShow()
232 if ( !mDefaultSelection )
236 QAction *actionSkipOutput =
new QAction( tr(
"Skip Output" ),
this );
237 connect( actionSkipOutput, &QAction::triggered,
this, &QgsProcessingLayerOutputDestinationWidget::skipOutput );
238 mMenu->addAction( actionSkipOutput );
241 QAction *actionSaveToTemp =
nullptr;
245 actionSaveToTemp =
new QAction( tr(
"Create Temporary Layer" ),
this );
249 actionSaveToTemp =
new QAction( tr(
"Save to a Temporary Directory" ),
this );
253 actionSaveToTemp =
new QAction( tr(
"Save to a Temporary File" ),
this );
256 connect( actionSaveToTemp, &QAction::triggered,
this, &QgsProcessingLayerOutputDestinationWidget::saveToTemporary );
257 mMenu->addAction( actionSaveToTemp );
260 QAction *actionSaveToFile =
nullptr;
263 actionSaveToFile =
new QAction( tr(
"Save to Directory…" ),
this );
264 connect( actionSaveToFile, &QAction::triggered,
this, &QgsProcessingLayerOutputDestinationWidget::selectDirectory );
268 actionSaveToFile =
new QAction( tr(
"Save to File…" ),
this );
269 connect( actionSaveToFile, &QAction::triggered,
this, &QgsProcessingLayerOutputDestinationWidget::selectFile );
271 mMenu->addAction( actionSaveToFile );
275 QAction *actionSaveToGpkg =
new QAction( tr(
"Save to GeoPackage…" ),
this );
276 connect( actionSaveToGpkg, &QAction::triggered,
this, &QgsProcessingLayerOutputDestinationWidget::saveToGeopackage );
277 mMenu->addAction( actionSaveToGpkg );
279 QAction *actionSaveToDatabase =
new QAction( tr(
"Save to Database Table…" ),
this );
280 connect( actionSaveToDatabase, &QAction::triggered,
this, &QgsProcessingLayerOutputDestinationWidget::saveToDatabase );
281 mMenu->addAction( actionSaveToDatabase );
283 if ( mParameter->algorithm() && qgis::down_cast< const QgsProcessingParameterFeatureSink * >( mParameter )->supportsAppend() )
285 mMenu->addSeparator();
286 QAction *actionAppendToLayer =
new QAction( tr(
"Append to Layer…" ),
this );
287 connect( actionAppendToLayer, &QAction::triggered,
this, &QgsProcessingLayerOutputDestinationWidget::appendToLayer );
288 mMenu->addAction( actionAppendToLayer );
291 QAction *editMappingAction =
new QAction( tr(
"Edit Field Mapping…" ),
this );
292 connect( editMappingAction, &QAction::triggered,
this, [ = ]
294 setAppendDestination( value().value< QgsProcessingOutputLayerDefinition >().sink.staticValue().toString(), mRemapDefinition.destinationFields() );
296 mMenu->addAction( editMappingAction );
303 mMenu->addSeparator();
304 QAction *actionSetEncoding =
new QAction( tr(
"Change File Encoding (%1)…" ).arg( mEncoding ),
this );
305 connect( actionSetEncoding, &QAction::triggered,
this, &QgsProcessingLayerOutputDestinationWidget::selectEncoding );
306 mMenu->addAction( actionSetEncoding );
310void QgsProcessingLayerOutputDestinationWidget::skipOutput()
312 leText->setPlaceholderText( tr(
"[Skip output]" ) );
314 mUseTemporary =
false;
315 mUseRemapping =
false;
317 emit skipOutputChanged(
true );
318 emit destinationChanged();
321void QgsProcessingLayerOutputDestinationWidget::saveToTemporary()
323 const bool prevSkip = outputIsSkipped();
327 leText->setPlaceholderText( tr(
"[Create temporary layer]" ) );
331 leText->setPlaceholderText( tr(
"[Save to temporary folder]" ) );
335 leText->setPlaceholderText( tr(
"[Save to temporary file]" ) );
342 mUseTemporary =
true;
343 mUseRemapping =
false;
345 emit skipOutputChanged(
false );
346 emit destinationChanged();
349void QgsProcessingLayerOutputDestinationWidget::selectDirectory()
351 QString lastDir = leText->text();
353 if ( lastDir.isEmpty() )
354 lastDir = settings.
value( QStringLiteral(
"/Processing/LastOutputPath" ), QDir::homePath() ).toString();
356 const QString dirName = QFileDialog::getExistingDirectory(
this, tr(
"Select Directory" ), lastDir, QFileDialog::Options() );
357 if ( !dirName.isEmpty() )
359 leText->setText( QDir::toNativeSeparators( dirName ) );
360 settings.
setValue( QStringLiteral(
"/Processing/LastOutputPath" ), dirName );
361 mUseTemporary =
false;
362 mUseRemapping =
false;
363 emit skipOutputChanged(
false );
364 emit destinationChanged();
368void QgsProcessingLayerOutputDestinationWidget::selectFile()
370 const QString fileFilter = mParameter->createFileFilter();
378 lastExtPath = QStringLiteral(
"/Processing/LastVectorOutputExt" );
379 lastExt = settings.
value( lastExtPath, QStringLiteral(
".%1" ).arg( mParameter->defaultFileExtension() ) ).toString() ;
383 lastExtPath = QStringLiteral(
"/Processing/LastRasterOutputExt" );
384 lastExt = settings.
value( lastExtPath, QStringLiteral(
".%1" ).arg( mParameter->defaultFileExtension() ) ).toString();
388 lastExtPath = QStringLiteral(
"/Processing/LastPointCloudOutputExt" );
389 lastExt = settings.
value( lastExtPath, QStringLiteral(
".%1" ).arg( mParameter->defaultFileExtension() ) ).toString();
393 const QStringList filters = fileFilter.split( QStringLiteral(
";;" ) );
395 for (
const QString &f : filters )
397 if ( f.contains( QStringLiteral(
"*.%1" ).arg( lastExt ), Qt::CaseInsensitive ) )
405 if ( settings.
contains( QStringLiteral(
"/Processing/LastOutputPath" ) ) )
406 path = settings.
value( QStringLiteral(
"/Processing/LastOutputPath" ) ).toString();
408 path = settings.
value( QStringLiteral(
"/Processing/Configuration/OUTPUTS_FOLDER" ) ).toString();
410 const bool dontConfirmOverwrite = mParameter->metadata().value( QStringLiteral(
"widget_wrapper" ) ).toMap().value( QStringLiteral(
"dontconfirmoverwrite" ),
false ).toBool();
412 QString filename = QFileDialog::getSaveFileName(
this, tr(
"Save file" ), path, fileFilter, &lastFilter, dontConfirmOverwrite ? QFileDialog::Options( QFileDialog::DontConfirmOverwrite ) : QFileDialog::Options() );
413 if ( !filename.isEmpty() )
415 mUseTemporary =
false;
416 mUseRemapping =
false;
419 leText->setText( filename );
420 settings.
setValue( QStringLiteral(
"/Processing/LastOutputPath" ), QFileInfo( filename ).path() );
421 if ( !lastExtPath.isEmpty() )
422 settings.
setValue( lastExtPath, QFileInfo( filename ).suffix().toLower() );
424 emit skipOutputChanged(
false );
425 emit destinationChanged();
429void QgsProcessingLayerOutputDestinationWidget::saveToGeopackage()
432 QString lastPath = settings.
value( QStringLiteral(
"/Processing/LastOutputPath" ), QString() ).toString();
433 if ( lastPath.isEmpty() )
434 lastPath = settings.
value( QStringLiteral(
"/Processing/Configuration/OUTPUTS_FOLDER" ), QString() ).toString();
436 QString filename = QFileDialog::getSaveFileName(
this, tr(
"Save to GeoPackage" ), lastPath, tr(
"GeoPackage files (*.gpkg);;All files (*.*)" ),
nullptr, QFileDialog::DontConfirmOverwrite );
438 if ( filename.isEmpty() )
441 const QString layerName = QInputDialog::getText(
this, tr(
"Save to GeoPackage" ), tr(
"Layer name" ), QLineEdit::Normal, mParameter->name().toLower() );
442 if ( layerName.isEmpty() )
445 mUseTemporary =
false;
446 mUseRemapping =
false;
450 settings.
setValue( QStringLiteral(
"/Processing/LastOutputPath" ), QFileInfo( filename ).path() );
459 if ( sink->hasGeometry() )
460 geomColumn = QStringLiteral(
"geom" );
464 leText->setText( QStringLiteral(
"ogr:%1" ).arg( uri.
uri() ) );
466 emit skipOutputChanged(
false );
467 emit destinationChanged();
470void QgsProcessingLayerOutputDestinationWidget::saveToDatabase()
476 << QStringLiteral(
"mssql" )
477 << QStringLiteral(
"ogr" )
478 << QStringLiteral(
"hana" )
479 << QStringLiteral(
"spatialite" )
480 << QStringLiteral(
"oracle" ),
this );
481 widget->
setPanelTitle( tr(
"Save “%1” to Database Table" ).arg( mParameter->description() ) );
484 panel->openPanel( widget );
488 mUseTemporary =
false;
489 mUseRemapping =
false;
494 if ( sink->hasGeometry() )
495 geomColumn = widget->
dataProviderKey() == QLatin1String(
"oracle" ) ? QStringLiteral(
"GEOM" ) : QStringLiteral(
"geom" );
504 leText->setText( QStringLiteral(
"ogr:%1" ).arg( uri.
uri() ) );
513 emit skipOutputChanged(
false );
514 emit destinationChanged();
529void QgsProcessingLayerOutputDestinationWidget::appendToLayer()
534 widget->
setPanelTitle( tr(
"Append \"%1\" to Layer" ).arg( mParameter->description() ) );
536 panel->openPanel( widget );
544 if ( widget->
uri().
uri.isEmpty() )
545 setValue( QVariant() );
549 std::unique_ptr< QgsVectorLayer > dest = std::make_unique< QgsVectorLayer >( widget->
uri().
uri, QString(), widget->
uri().
providerKey );
551 setAppendDestination( widget->
uri().
uri, dest->fields() );
560void QgsProcessingLayerOutputDestinationWidget::setAppendDestination(
const QString &uri,
const QgsFields &destFields )
564 if ( mParametersGenerator )
565 props = mParametersGenerator->createProcessingParameters();
566 props.insert( mParameter->name(), uri );
575 widget->
setPanelTitle( tr(
"Append \"%1\" to Layer" ).arg( mParameter->description() ) );
576 if ( !mRemapDefinition.fieldMap().isEmpty() )
579 panel->openPanel( widget );
595void QgsProcessingLayerOutputDestinationWidget::selectEncoding()
600 mEncoding = dialog.encoding();
602 settings.
setValue( QStringLiteral(
"/Processing/encoding" ), mEncoding );
603 emit destinationChanged();
607void QgsProcessingLayerOutputDestinationWidget::textChanged(
const QString &text )
609 mUseTemporary = text.isEmpty();
610 mUseRemapping =
false;
611 emit destinationChanged();
615QString QgsProcessingLayerOutputDestinationWidget::mimeDataToPath(
const QMimeData *data )
623 && u.layerType == QLatin1String(
"vector" ) && u.providerKey == QLatin1String(
"ogr" ) )
629 && u.layerType == QLatin1String(
"raster" ) && u.providerKey == QLatin1String(
"gdal" ) )
635 && u.layerType == QLatin1String(
"pointcloud" ) && ( u.providerKey == QLatin1String(
"ept" ) || u.providerKey == QLatin1String(
"pdal" ) ) )
642 && u.layerType == QLatin1String(
"mesh" ) && u.providerKey == QLatin1String(
"mdal" ) )
647 && u.layerType == QLatin1String(
"directory" ) )
652 if ( !uriList.isEmpty() )
656 QStringList rawPaths;
657 if ( data->hasUrls() )
659 const QList< QUrl > urls = data->urls();
660 rawPaths.reserve( urls.count() );
661 for (
const QUrl &url : urls )
663 const QString local = url.toLocalFile();
664 if ( !rawPaths.contains( local ) )
665 rawPaths.append( local );
668 if ( !data->text().isEmpty() && !rawPaths.contains( data->text() ) )
669 rawPaths.append( data->text() );
671 for (
const QString &path : std::as_const( rawPaths ) )
673 QFileInfo file( path );
691void QgsProcessingLayerOutputDestinationWidget::dragEnterEvent( QDragEnterEvent *event )
693 if ( !( event->possibleActions() & Qt::CopyAction ) )
696 const QString path = mimeDataToPath( event->mimeData() );
697 if ( !path.isEmpty() )
700 event->setDropAction( Qt::CopyAction );
702 leText->setHighlighted(
true );
706void QgsProcessingLayerOutputDestinationWidget::dragLeaveEvent( QDragLeaveEvent *event )
708 QWidget::dragLeaveEvent( event );
709 if ( leText->isHighlighted() )
712 leText->setHighlighted(
false );
716void QgsProcessingLayerOutputDestinationWidget::dropEvent( QDropEvent *event )
718 if ( !( event->possibleActions() & Qt::CopyAction ) )
721 const QString path = mimeDataToPath( event->mimeData() );
722 if ( !path.isEmpty() )
725 setFocus( Qt::MouseFocusReason );
726 event->setDropAction( Qt::CopyAction );
730 leText->setHighlighted(
false );
Class for storing the component parts of a RDBMS data source URI (e.g.
void setTable(const QString &table)
Sets table to table.
void setGeometryColumn(const QString &geometryColumn)
Sets geometry column name to geometryColumn.
QString uri(bool expandAuthConfig=true) const
Returns the complete URI as a string.
void setDatabase(const QString &database)
Sets the URI database name.
A dialog which presents the user with a choice of file encodings.
Container of fields for a vector layer.
static QString ensureFileNameHasExtension(const QString &fileName, const QStringList &extensions)
Ensures that a fileName ends with an extension from the provided list of extensions.
static QString addExtensionFromFilter(const QString &fileName, const QString &filter)
Ensures that a fileName ends with an extension from the specified filter string.
QList< QgsMimeDataUtils::Uri > UriList
static UriList decodeUriList(const QMimeData *data)
Abstract base class for processing algorithms.
@ Available
Properties are available.
virtual QgsProcessingAlgorithm::VectorProperties sinkProperties(const QString &sink, const QVariantMap ¶meters, QgsProcessingContext &context, const QMap< QString, QgsProcessingAlgorithm::VectorProperties > &sourceProperties) const
Returns the vector properties which will be used for the sink with matching name.
Contains information about the context in which a processing algorithm is executed.
Base class for all parameter definitions which represent file or layer destinations,...
Encapsulates settings relating to a feature sink or output raster layer for a processing algorithm.
QgsProperty sink
Sink/layer definition.
bool useRemapping() const
Returns true if the output uses a remapping definition.
QgsRemappingSinkDefinition remappingDefinition() const
Returns the output remapping definition, if useRemapping() is true.
QVariantMap createOptions
Map of optional sink/layer creation options, which are passed to the underlying provider when creatin...
void setRemappingDefinition(const QgsRemappingSinkDefinition &definition)
Sets the remapping definition to use when adding features to the output layer.
@ FlagOptional
Parameter is optional.
A feature sink output for processing algorithms.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
Contains settings which reflect the context in which a Processing parameter widget is shown,...
QgsBrowserGuiModel * browserModel() const
Returns the browser model associated with the widget.
An interface for objects which can create sets of parameter values for processing algorithms.
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 bool decodeProviderKeyAndUri(const QString &string, QString &providerKey, QString &uri)
Decodes a provider key and layer uri from an encoded string, for use with encodeProviderKeyAndUri()
static const QString TEMPORARY_OUTPUT
Constant used to indicate that a Processing algorithm output should be a temporary layer/file.
QVariant staticValue() const
Returns the current static value for the property.
Defines the parameters used to remap features when creating a QgsRemappingProxyFeatureSink.
void setFieldMap(const QMap< QString, QgsProperty > &map)
Sets the field mapping, which defines how to map the values from incoming features to destination fie...
void setDestinationFields(const QgsFields &fields)
Sets the fields for the destination sink.
void setSourceCrs(const QgsCoordinateReferenceSystem &source)
Sets the source crs used for reprojecting incoming features to the sink's destination CRS.
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.
bool contains(const QString &key, QgsSettings::Section section=QgsSettings::NoSection) const
Returns true if there exists a setting called key; returns false otherwise.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
QString uri
Identifier of the data source recognized by its providerKey.
QString providerKey
For "vector" / "raster" type: provider id.
Properties of a vector source or sink used in an algorithm.
QgsCoordinateReferenceSystem crs
Coordinate Reference System.
QgsProcessingAlgorithm::PropertyAvailability availability
Availability of the properties. By default properties are not available.