Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c57509d

Browse files
committedJul 26, 2021
Add QgsProject::getAttachmentIdentifier and QgsProject::resolveAttachementIdentifier
1 parent ea29575 commit c57509d

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed
 

‎python/core/auto_generated/project/qgsproject.sip.in

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,28 @@ Removes the attached file
12951295

12961296
.. seealso:: :py:func:`createAttachedFile`
12971297

1298+
.. versionadded:: 3.22
1299+
%End
1300+
1301+
QString getAttachmentIdentifier( const QString &attachedFile ) const;
1302+
%Docstring
1303+
Returns an identifier for an attachment file path
1304+
1305+
:param attachedFile: An attachment file path
1306+
1307+
:return: An identifier for the attached file
1308+
1309+
.. versionadded:: 3.22
1310+
%End
1311+
1312+
QString resolveAttachementIdentifier( const QString &identifier ) const;
1313+
%Docstring
1314+
Resolves an attachment identifier to a attachment file path
1315+
1316+
:param identifier: An attachment identifier
1317+
1318+
:return: The attachment file path, or an empty string if the identifier is invalid
1319+
12981320
.. versionadded:: 3.22
12991321
%End
13001322

‎src/core/project/qgsproject.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3700,6 +3700,20 @@ bool QgsProject::removeAttachedFile( const QString &path )
37003700
return mArchive->removeFile( path );
37013701
}
37023702

3703+
QString QgsProject::getAttachmentIdentifier( const QString &attachedFile ) const
3704+
{
3705+
return QStringLiteral( "attachment:///%1" ).arg( QFileInfo( attachedFile ).fileName() );
3706+
}
3707+
3708+
QString QgsProject::resolveAttachementIdentifier( const QString &identifier ) const
3709+
{
3710+
if ( identifier.startsWith( "attachment:///" ) )
3711+
{
3712+
return QDir( mArchive->dir() ).absoluteFilePath( identifier.mid( 14 ) );
3713+
}
3714+
return QString();
3715+
}
3716+
37033717
const QgsProjectMetadata &QgsProject::metadata() const
37043718
{
37053719
return mMetadata;

‎src/core/project/qgsproject.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,6 +1362,22 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
13621362
*/
13631363
bool removeAttachedFile( const QString &path );
13641364

1365+
/**
1366+
* Returns an identifier for an attachment file path
1367+
* \param attachedFile An attachment file path
1368+
* \return An identifier for the attached file
1369+
* \since QGIS 3.22
1370+
*/
1371+
QString getAttachmentIdentifier( const QString &attachedFile ) const;
1372+
1373+
/**
1374+
* Resolves an attachment identifier to a attachment file path
1375+
* \param identifier An attachment identifier
1376+
* \return The attachment file path, or an empty string if the identifier is invalid
1377+
* \since QGIS 3.22
1378+
*/
1379+
QString resolveAttachementIdentifier( const QString &identifier ) const;
1380+
13651381
/**
13661382
* Returns a reference to the project's metadata store.
13671383
* \see setMetadata()

‎tests/src/core/testqgsproject.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class TestQgsProject : public QObject
5858
void testDefaultRelativePaths();
5959
void testAttachmentsQgs();
6060
void testAttachmentsQgz();
61+
void testAttachmentIdentifier();
6162
};
6263

6364
void TestQgsProject::init()
@@ -884,6 +885,18 @@ void TestQgsProject::testAttachmentsQgz()
884885

885886
}
886887

888+
void TestQgsProject::testAttachmentIdentifier()
889+
{
890+
// Verify attachment identifiers
891+
{
892+
QgsProject p;
893+
QString attachmentFileName = p.createAttachedFile( "test.jpg" );
894+
QString attachmentId = p.getAttachmentIdentifier( attachmentFileName );
895+
QCOMPARE( p.resolveAttachementIdentifier( attachmentId ), attachmentFileName );
896+
}
897+
898+
}
899+
887900

888901
QGSTEST_MAIN( TestQgsProject )
889902
#include "testqgsproject.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.