Skip to content

Commit

Permalink
Merge pull request #46504 from kadas-albireo/master
Browse files Browse the repository at this point in the history
Automatically remove attached files used as layer sources when layer is deleted
  • Loading branch information
m-kuhn committed Dec 16, 2021
2 parents 132294b + 792ce7f commit 9500bfb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions python/core/auto_generated/project/qgsproject.sip.in
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions src/core/project/qgsproject.h
Expand Up @@ -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 );
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsmaplayer.cpp
Expand Up @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions tests/src/core/testqgsproject.cpp
Expand Up @@ -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() );
}

}
Expand Down Expand Up @@ -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() );
}

}
Expand Down

0 comments on commit 9500bfb

Please sign in to comment.