Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regenerate primary key when making layer permanent if layer property set #58954

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ QGIS interface.

QgsVectorFileWriterTask( QgsVectorLayer *layer,
const QString &fileName,
const QgsVectorFileWriter::SaveVectorOptions &options );
const QgsVectorFileWriter::SaveVectorOptions &options,
QgsFeatureSink::SinkFlags sinkFlags = QgsFeatureSink::SinkFlags() );
%Docstring
Constructor for QgsVectorFileWriterTask. Takes a source ``layer``, destination ``fileName``
and save ``options``.

Since QGIS 3.40 the ``sinkFlags`` can be specified.
%End

virtual void cancel();
Expand Down
5 changes: 4 additions & 1 deletion python/core/auto_generated/qgsvectorfilewritertask.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ QGIS interface.

QgsVectorFileWriterTask( QgsVectorLayer *layer,
const QString &fileName,
const QgsVectorFileWriter::SaveVectorOptions &options );
const QgsVectorFileWriter::SaveVectorOptions &options,
QgsFeatureSink::SinkFlags sinkFlags = QgsFeatureSink::SinkFlags() );
%Docstring
Constructor for QgsVectorFileWriterTask. Takes a source ``layer``, destination ``fileName``
and save ``options``.

Since QGIS 3.40 the ``sinkFlags`` can be specified.
%End

virtual void cancel();
Expand Down
12 changes: 11 additions & 1 deletion src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8328,6 +8328,10 @@ void QgisApp::makeMemoryLayerPermanent( QgsVectorLayer *layer )
source += QStringLiteral( "|layername=%1" ).arg( newLayerName );
vl->setDataSource( source, vl->name(), QStringLiteral( "ogr" ), options );
vl->triggerRepaint();
// we don't want this flag anymore -- the layer has changed format and from now on
// primary keys should be retained if it's exported to a new file
vl->removeCustomProperty( QStringLiteral( "OnConvertFormatRegeneratePrimaryKey" ) );

mLayerTreeView->refreshLayerSymbology( vl->id() );
this->visibleMessageBar()->pushMessage( tr( "Layer Saved" ),
tr( "Successfully saved scratch layer to <a href=\"%1\">%2</a>" ).arg( QUrl::fromLocalFile( newFilename ).toString(), QDir::toNativeSeparators( newFilename ) ),
Expand Down Expand Up @@ -8608,8 +8612,14 @@ QString QgisApp::saveAsVectorFileGeneral( QgsVectorLayer *vlayer, bool symbology
options.saveMetadata = dialog->persistMetadata();
options.layerMetadata = vlayer->metadata();

QgsFeatureSink::SinkFlags sinkFlags;
if ( vlayer->customProperty( QStringLiteral( "OnConvertFormatRegeneratePrimaryKey" ) ).toBool() )
{
sinkFlags.setFlag( QgsFeatureSink::RegeneratePrimaryKey, true );
}

bool addToCanvas = dialog->addToCanvas();
QgsVectorFileWriterTask *writerTask = new QgsVectorFileWriterTask( vlayer, vectorFilename, options );
QgsVectorFileWriterTask *writerTask = new QgsVectorFileWriterTask( vlayer, vectorFilename, options, sinkFlags );

// when writer is successful:
connect( writerTask, &QgsVectorFileWriterTask::completed, this, [onSuccess, addToCanvas, encoding, vectorFilename, format]( const QString & newFilename, const QString & newLayer )
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgsvectorfilewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3477,7 +3477,7 @@ QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormat( Prepa
return writeAsVectorFormatV2( details, fileName, QgsCoordinateTransformContext(), options, newFilename, newLayer, errorMessage );
}

QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormatV2( PreparedWriterDetails &details, const QString &fileName, const QgsCoordinateTransformContext &transformContext, const QgsVectorFileWriter::SaveVectorOptions &options, QString *newFilename, QString *newLayer, QString *errorMessage )
QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormatV2( PreparedWriterDetails &details, const QString &fileName, const QgsCoordinateTransformContext &transformContext, const QgsVectorFileWriter::SaveVectorOptions &options, QString *newFilename, QString *newLayer, QString *errorMessage, SinkFlags sinkFlags )
{
Qgis::WkbType destWkbType = details.destWkbType;

Expand Down Expand Up @@ -3545,7 +3545,7 @@ QgsVectorFileWriter::WriterError QgsVectorFileWriter::writeAsVectorFormatV2( Pre
newOptions.sourceDatabaseProviderConnection = details.sourceDatabaseProviderConnection.get();
}

std::unique_ptr< QgsVectorFileWriter > writer( create( fileName, details.outputFields, destWkbType, details.outputCrs, transformContext, newOptions, QgsFeatureSink::SinkFlags(), &tempNewFilename, &tempNewLayer ) );
std::unique_ptr< QgsVectorFileWriter > writer( create( fileName, details.outputFields, destWkbType, details.outputCrs, transformContext, newOptions, sinkFlags, &tempNewFilename, &tempNewLayer ) );
writer->setSymbologyScale( options.symbologyScale );

if ( newFilename )
Expand Down
4 changes: 3 additions & 1 deletion src/core/qgsvectorfilewriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,7 @@ class CORE_EXPORT QgsVectorFileWriter : public QgsFeatureSink
* \param newFilename potentially modified file name (output parameter)
* \param newLayer potentially modified layer name (output parameter)
* \param errorMessage will be set to the error message text, if an error occurs while writing the layer
* \param sinkFlags optional sink flags (since QGIS 3.40)
* \returns Error message code, or QgsVectorFileWriter.NoError if the write operation was successful
* \since QGIS 3.10.3
*/
Expand All @@ -1054,7 +1055,8 @@ class CORE_EXPORT QgsVectorFileWriter : public QgsFeatureSink
const QgsVectorFileWriter::SaveVectorOptions &options,
QString *newFilename = nullptr,
QString *newLayer = nullptr,
QString *errorMessage SIP_OUT = nullptr );
QString *errorMessage SIP_OUT = nullptr,
QgsFeatureSink::SinkFlags sinkFlags = QgsFeatureSink::SinkFlags() );

/**
* Writes a previously prepared PreparedWriterDetails \a details object.
Expand Down
5 changes: 3 additions & 2 deletions src/core/qgsvectorfilewritertask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
#include "qgsvectorfilewritertask.h"
#include "qgsvectorlayer.h"

QgsVectorFileWriterTask::QgsVectorFileWriterTask( QgsVectorLayer *layer, const QString &fileName, const QgsVectorFileWriter::SaveVectorOptions &options )
QgsVectorFileWriterTask::QgsVectorFileWriterTask( QgsVectorLayer *layer, const QString &fileName, const QgsVectorFileWriter::SaveVectorOptions &options, QgsFeatureSink::SinkFlags sinkFlags )
: QgsTask( tr( "Saving %1" ).arg( fileName ), QgsTask::CanCancel )
, mDestFileName( fileName )
, mSinkFlags( sinkFlags )
, mOptions( options )
{
if ( mOptions.fieldValueConverter )
Expand Down Expand Up @@ -59,7 +60,7 @@ bool QgsVectorFileWriterTask::run()


mError = QgsVectorFileWriter::writeAsVectorFormatV2(
mWriterDetails, mDestFileName, mTransformContext, mOptions, &mNewFilename, &mNewLayer, &mErrorMessage );
mWriterDetails, mDestFileName, mTransformContext, mOptions, &mNewFilename, &mNewLayer, &mErrorMessage, mSinkFlags );
return mError == QgsVectorFileWriter::NoError;
}

Expand Down
6 changes: 5 additions & 1 deletion src/core/qgsvectorfilewritertask.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ class CORE_EXPORT QgsVectorFileWriterTask : public QgsTask
/**
* Constructor for QgsVectorFileWriterTask. Takes a source \a layer, destination \a fileName
* and save \a options.
*
* Since QGIS 3.40 the \a sinkFlags can be specified.
*/
QgsVectorFileWriterTask( QgsVectorLayer *layer,
const QString &fileName,
const QgsVectorFileWriter::SaveVectorOptions &options );
const QgsVectorFileWriter::SaveVectorOptions &options,
QgsFeatureSink::SinkFlags sinkFlags = QgsFeatureSink::SinkFlags() );

void cancel() override;

Expand Down Expand Up @@ -79,6 +82,7 @@ class CORE_EXPORT QgsVectorFileWriterTask : public QgsTask
QString mDestFileName;

std::unique_ptr< QgsFeedback > mOwnedFeedback;
QgsFeatureSink::SinkFlags mSinkFlags;
QgsVectorFileWriter::WriterError mError = QgsVectorFileWriter::NoError;

QString mNewFilename;
Expand Down
Loading