@@ -7166,7 +7166,7 @@ void QgisApp::saveAsRasterFile( QgsRasterLayer *rasterLayer )
7166
7166
emit layerSavedAs ( rlWeakPointer, fileName );
7167
7167
7168
7168
messageBar ()->pushMessage ( tr ( " Layer Exported" ),
7169
- tr ( " Successfully saved raster layer to <a href=\" %1\" >%2</a>" ).arg ( QUrl::fromLocalFile ( newFilename ).toString (), QDir::toNativeSeparators ( newFilename ) ),
7169
+ tr ( " Successfully saved raster layer to <a href=\" %1\" >%2</a>" ).arg ( QUrl::fromLocalFile ( QFileInfo ( newFilename ). path () ).toString (), QDir::toNativeSeparators ( newFilename ) ),
7170
7170
Qgis::Success, messageTimeout () );
7171
7171
} );
7172
7172
@@ -7204,6 +7204,45 @@ void QgisApp::saveAsFile( QgsMapLayer *layer, bool onlySelected )
7204
7204
}
7205
7205
}
7206
7206
7207
+ void QgisApp::makeMemoryLayerPermanent ( QgsVectorLayer *layer )
7208
+ {
7209
+ if ( !layer )
7210
+ return ;
7211
+
7212
+ const QString layerId = layer->id ();
7213
+
7214
+ auto onSuccess = [this , layerId]( const QString & newFilename,
7215
+ bool ,
7216
+ const QString &,
7217
+ const QString &,
7218
+ const QString & )
7219
+ {
7220
+ // we have to re-retrieve the layer, in case it's been removed during the lifetime of the writer task
7221
+ QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( QgsProject::instance ()->mapLayer ( layerId ) );
7222
+ if ( vl )
7223
+ {
7224
+ QgsDataProvider::ProviderOptions options;
7225
+ vl->setDataSource ( QStringLiteral ( " %1" ).arg ( newFilename ), vl->name (), QStringLiteral ( " ogr" ), options );
7226
+ this ->messageBar ()->pushMessage ( tr ( " Layer Saved" ),
7227
+ tr ( " Successfully saved scratch layer to <a href=\" %1\" >%2</a>" ).arg ( QUrl::fromLocalFile ( QFileInfo ( newFilename ).path () ).toString (), QDir::toNativeSeparators ( newFilename ) ),
7228
+ Qgis::Success, messageTimeout () );
7229
+ }
7230
+ };
7231
+
7232
+ auto onFailure = []( int error, const QString & errorMessage )
7233
+ {
7234
+ if ( error != QgsVectorFileWriter::Canceled )
7235
+ {
7236
+ QgsMessageViewer *m = new QgsMessageViewer ( nullptr );
7237
+ m->setWindowTitle ( tr ( " Save Error" ) );
7238
+ m->setMessageAsPlainText ( tr ( " Could not make temporary scratch layer permanent.\n Error: %1" ).arg ( errorMessage ) );
7239
+ m->exec ();
7240
+ }
7241
+ };
7242
+
7243
+ saveAsVectorFileGeneral ( layer, true , false , onSuccess, onFailure );
7244
+ }
7245
+
7207
7246
void QgisApp::saveAsLayerDefinition ()
7208
7247
{
7209
7248
QString path = QFileDialog::getSaveFileName ( this , QStringLiteral ( " Save as Layer Definition File" ), QDir::home ().path (), QStringLiteral ( " *.qlr" ) );
@@ -7310,6 +7349,42 @@ void QgisApp::saveAsVectorFileGeneral( QgsVectorLayer *vlayer, bool symbologyOpt
7310
7349
if ( !vlayer )
7311
7350
return ;
7312
7351
7352
+ auto onSuccess = [this , vlayer]( const QString & newFilename,
7353
+ bool addToCanvas,
7354
+ const QString & layerName,
7355
+ const QString & encoding,
7356
+ const QString & vectorFileName )
7357
+ {
7358
+ if ( addToCanvas )
7359
+ {
7360
+ QString uri ( newFilename );
7361
+ if ( !layerName.isEmpty () )
7362
+ uri += " |layername=" + layerName;
7363
+ this ->addVectorLayers ( QStringList ( uri ), encoding, QStringLiteral ( " file" ) );
7364
+ }
7365
+
7366
+ this ->emit layerSavedAs ( vlayer, vectorFilename );
7367
+ this ->messageBar ()->pushMessage ( tr ( " Layer Exported" ),
7368
+ tr ( " Successfully saved vector layer to <a href=\" %1\" >%2</a>" ).arg ( QUrl::fromLocalFile ( newFilename ).toString (), QDir::toNativeSeparators ( newFilename ) ),
7369
+ Qgis::Success, messageTimeout () );
7370
+ };
7371
+
7372
+ auto onFailure = []( int error, const QString & errorMessage )
7373
+ {
7374
+ if ( error != QgsVectorFileWriter::Canceled )
7375
+ {
7376
+ QgsMessageViewer *m = new QgsMessageViewer ( nullptr );
7377
+ m->setWindowTitle ( tr ( " Save Error" ) );
7378
+ m->setMessageAsPlainText ( tr ( " Export to vector file failed.\n Error: %1" ).arg ( errorMessage ) );
7379
+ m->exec ();
7380
+ }
7381
+ };
7382
+
7383
+ saveAsVectorFileGeneral ( vlayer, symbologyOption, onlySelected, onSuccess, onFailure );
7384
+ }
7385
+
7386
+ void QgisApp::saveAsVectorFileGeneral ( QgsVectorLayer *vlayer, bool symbologyOption, bool onlySelected, const std::function<void ( const QString &, bool , const QString &, const QString &, const QString & )> &onSuccess, const std::function<void ( int , const QString & )> &onFailure )
7387
+ {
7313
7388
QgsCoordinateReferenceSystem destCRS;
7314
7389
7315
7390
int options = QgsVectorLayerSaveAsDialog::AllOptions;
@@ -7381,21 +7456,10 @@ void QgisApp::saveAsVectorFileGeneral( QgsVectorLayer *vlayer, bool symbologyOpt
7381
7456
QgsVectorFileWriterTask *writerTask = new QgsVectorFileWriterTask ( vlayer, vectorFilename, options );
7382
7457
7383
7458
// when writer is successful:
7384
- connect ( writerTask, &QgsVectorFileWriterTask::writeComplete, this , [this , addToCanvas, layerName, encoding, vectorFilename, vlayer ]( const QString & newFilename )
7459
+ connect ( writerTask, &QgsVectorFileWriterTask::writeComplete, this , [onSuccess , addToCanvas, layerName, encoding, vectorFilename]( const QString & newFilename )
7385
7460
{
7386
- if ( addToCanvas )
7387
- {
7388
- QString uri ( newFilename );
7389
- if ( !layerName.isEmpty () )
7390
- uri += " |layername=" + layerName;
7391
- this ->addVectorLayers ( QStringList ( uri ), encoding, QStringLiteral ( " file" ) );
7392
- }
7393
- this ->emit layerSavedAs ( vlayer, vectorFilename );
7394
- this ->messageBar ()->pushMessage ( tr ( " Layer Exported" ),
7395
- tr ( " Successfully saved vector layer to <a href=\" %1\" >%2</a>" ).arg ( QUrl::fromLocalFile ( newFilename ).toString (), QDir::toNativeSeparators ( newFilename ) ),
7396
- Qgis::Success, messageTimeout () );
7397
- }
7398
- );
7461
+ onSuccess ( newFilename, addToCanvas, layerName, encoding, vectorFilename );
7462
+ } );
7399
7463
7400
7464
// when an error occurs:
7401
7465
connect ( writerTask, &QgsVectorFileWriterTask::errorOccurred, this , [ = ]( int error, const QString & errorMessage )
0 commit comments