diff --git a/python/core/auto_generated/project/qgsproject.sip.in b/python/core/auto_generated/project/qgsproject.sip.in index 6824d76674b..81d1054e3d2 100644 --- a/python/core/auto_generated/project/qgsproject.sip.in +++ b/python/core/auto_generated/project/qgsproject.sip.in @@ -1273,6 +1273,11 @@ Attaches a file to the project :return: The path to the file where the contents can be written to. +.. note:: + + If the attachment file is used as a source for a project layer, the attachment will be removed + automatically when the layer is deleted. + .. versionadded:: 3.22 %End diff --git a/src/core/project/qgsproject.h b/src/core/project/qgsproject.h index e6400a4ee3a..0271069fd7b 100644 --- a/src/core/project/qgsproject.h +++ b/src/core/project/qgsproject.h @@ -1341,6 +1341,8 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera * Attaches a file to the project * \param nameTemplate Any filename template, used as a basename for attachment file, i.e. "myfile.ext" * \return The path to the file where the contents can be written to. + * \note If the attachment file is used as a source for a project layer, the attachment will be removed + * automatically when the layer is deleted. * \since QGIS 3.22 */ QString createAttachedFile( const QString &nameTemplate ); diff --git a/src/core/qgsmaplayer.cpp b/src/core/qgsmaplayer.cpp index e0cf74fb807..56cd38d3c4d 100644 --- a/src/core/qgsmaplayer.cpp +++ b/src/core/qgsmaplayer.cpp @@ -98,6 +98,11 @@ QgsMapLayer::QgsMapLayer( QgsMapLayerType type, QgsMapLayer::~QgsMapLayer() { + if ( project() && project()->pathResolver().writePath( mDataSource ).startsWith( "attachment:" ) ) + { + project()->removeAttachedFile( mDataSource ); + } + delete m3DRenderer; delete mLegend; delete mStyleManager; diff --git a/tests/src/core/testqgsproject.cpp b/tests/src/core/testqgsproject.cpp index 051cb58915c..b27d2575aaf 100644 --- a/tests/src/core/testqgsproject.cpp +++ b/tests/src/core/testqgsproject.cpp @@ -812,6 +812,13 @@ void TestQgsProject::testAttachmentsQgs() QVERIFY( p2.attachedFiles().size() == 1 ); QVERIFY( p2.mapLayers().size() == 1 ); QVERIFY( p2.mapLayer( p2.mapLayers().firstKey() )->source() == p2.attachedFiles().first() ); + + // Verify that attachment file is removed when layer is deleted + QgsMapLayer *p2layer = p2.mapLayer( p2.mapLayers().firstKey() ); + QString path = p2layer->source(); + QVERIFY( QFile( path ).exists() ); + p2.removeMapLayer( p2layer->id() ); + QVERIFY( !QFile( path ).exists() ); } } @@ -882,6 +889,13 @@ void TestQgsProject::testAttachmentsQgz() QVERIFY( p2.attachedFiles().size() == 1 ); QVERIFY( p2.mapLayers().size() == 1 ); QVERIFY( p2.mapLayer( p2.mapLayers().firstKey() )->source() == p2.attachedFiles().first() ); + + // Verify that attachment file is removed when layer is deleted + QgsMapLayer *p2layer = p2.mapLayer( p2.mapLayers().firstKey() ); + QString path = p2layer->source(); + QVERIFY( QFile( path ).exists() ); + p2.removeMapLayer( p2layer->id() ); + QVERIFY( !QFile( path ).exists() ); } }