Skip to content

Commit

Permalink
Add project_basename to project expr context, project_path for custom…
Browse files Browse the repository at this point in the history
… impls
  • Loading branch information
wonder-sk committed Apr 7, 2018
1 parent 663a9ac commit 42969a4
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 13 deletions.
9 changes: 9 additions & 0 deletions python/core/qgsproject.sip.in
Expand Up @@ -111,6 +111,15 @@ Returns QFileInfo object for the project's associated file.

.. versionadded:: 2.9
\deprecated
%End

QgsProjectStorage *projectStorage() const;
%Docstring
Returns pointer to project storage implementation that handles read/write of the project file.
If the project file is stored in the local file system, returns null pointer.
The project storage object is inferred from fileName() of the project.

.. versionadded:: 3.2
%End

QDateTime lastModified() const;
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgisapp.cpp
Expand Up @@ -5738,7 +5738,7 @@ bool QgisApp::fileSave()
}
else
{
bool usingProjectStorage = QgsApplication::projectStorageRegistry()->projectStorageFromUri( QgsProject::instance()->fileName() );
bool usingProjectStorage = QgsProject::instance()->projectStorage();
bool fileExists = usingProjectStorage ? true : QFileInfo( QgsProject::instance()->fileName() ).exists();

if ( fileExists && !mProjectLastModified.isNull() && mProjectLastModified != QgsProject::instance()->lastModified() )
Expand Down
4 changes: 3 additions & 1 deletion src/core/qgsexpressioncontext.cpp
Expand Up @@ -778,15 +778,17 @@ QgsExpressionContextScope *QgsExpressionContextUtils::projectScope( const QgsPro
scope->setVariable( it.key(), it.value(), true );
}

QString projectPath = project->absoluteFilePath();
QString projectPath = project->projectStorage() ? project->fileName() : project->absoluteFilePath();
QString projectFolder = QFileInfo( projectPath ).path();
QString projectFilename = QFileInfo( projectPath ).fileName();
QString projectBasename = project->baseName();

//add other known project variables
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_title" ), project->title(), true, true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_path" ), QDir::toNativeSeparators( projectPath ), true, true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_folder" ), QDir::toNativeSeparators( projectFolder ), true, true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_filename" ), projectFilename, true, true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_basename" ), projectBasename, true, true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_home" ), QDir::toNativeSeparators( project->homePath() ), true, true ) );
QgsCoordinateReferenceSystem projectCrs = project->crs();
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_crs" ), projectCrs.authid(), true, true ) );
Expand Down
22 changes: 11 additions & 11 deletions src/core/qgsproject.cpp
Expand Up @@ -462,10 +462,14 @@ QFileInfo QgsProject::fileInfo() const
return QFileInfo( mFile );
}

QgsProjectStorage *QgsProject::projectStorage() const
{
return QgsApplication::projectStorageRegistry()->projectStorageFromUri( mFile.fileName() );
}

QDateTime QgsProject::lastModified() const
{
QgsProjectStorage *storage = QgsApplication::projectStorageRegistry()->projectStorageFromUri( mFile.fileName() );
if ( storage )
if ( QgsProjectStorage *storage = projectStorage() )
{
QgsProjectStorage::Metadata metadata;
storage->readProjectStorageMetadata( mFile.fileName(), metadata );
Expand All @@ -479,8 +483,7 @@ QDateTime QgsProject::lastModified() const

QString QgsProject::absoluteFilePath() const
{
QgsProjectStorage *storage = QgsApplication::projectStorageRegistry()->projectStorageFromUri( mFile.fileName() );
if ( storage )
if ( projectStorage() )
return QString();

if ( mFile.fileName().isEmpty() )
Expand All @@ -491,8 +494,7 @@ QString QgsProject::absoluteFilePath() const

QString QgsProject::baseName() const
{
QgsProjectStorage *storage = QgsApplication::projectStorageRegistry()->projectStorageFromUri( mFile.fileName() );
if ( storage )
if ( QgsProjectStorage *storage = projectStorage() )
{
QgsProjectStorage::Metadata metadata;
storage->readProjectStorageMetadata( mFile.fileName(), metadata );
Expand Down Expand Up @@ -866,8 +868,7 @@ bool QgsProject::read()
QString filename = mFile.fileName();
bool rc;

QgsProjectStorage *storage = QgsApplication::projectStorageRegistry()->projectStorageFromUri( filename );
if ( storage )
if ( QgsProjectStorage *storage = projectStorage() )
{
QTemporaryFile inDevice;
if ( !inDevice.open() )
Expand Down Expand Up @@ -1438,8 +1439,7 @@ bool QgsProject::write( const QString &filename )

bool QgsProject::write()
{
QgsProjectStorage *projectStorage = QgsApplication::projectStorageRegistry()->projectStorageFromUri( mFile.fileName() );
if ( projectStorage )
if ( QgsProjectStorage *storage = projectStorage() )
{
// for projects stored in a custom storage, we cannot use relative paths since the storage most likely
// will not be in a file system
Expand All @@ -1459,7 +1459,7 @@ bool QgsProject::write()
}

QgsReadWriteContext context;
if ( !projectStorage->writeProject( mFile.fileName(), &tmpZipFile, context ) )
if ( !storage->writeProject( mFile.fileName(), &tmpZipFile, context ) )
{
setError( tr( "Unable to save project to storage %1" ).arg( mFile.fileName() ) );
return false;
Expand Down
8 changes: 8 additions & 0 deletions src/core/qgsproject.h
Expand Up @@ -162,6 +162,14 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
*/
Q_DECL_DEPRECATED QFileInfo fileInfo() const SIP_DEPRECATED;

/**
* Returns pointer to project storage implementation that handles read/write of the project file.
* If the project file is stored in the local file system, returns null pointer.
* The project storage object is inferred from fileName() of the project.
* \since QGIS 3.2
*/
QgsProjectStorage *projectStorage() const;

/**
* Returns last modified time of the project file as returned by the file system (or other project storage).
* \since QGIS 3.2
Expand Down
7 changes: 7 additions & 0 deletions tests/src/core/testqgsprojectstorage.cpp
Expand Up @@ -16,6 +16,7 @@
#include "qgstest.h"

#include "qgsauxiliarystorage.h"
#include "qgsexpressioncontext.h"
#include "qgsproject.h"
#include "qgsprojectstorage.h"
#include "qgsprojectstorageregistry.h"
Expand Down Expand Up @@ -201,6 +202,12 @@ void TestQgsProjectStorage::testMemoryStorage()
QCOMPARE( prj2layer1->fields().count(), 7 );
QCOMPARE( prj2layer1->fields().at( 6 ).name(), QString( "auxiliary_storage_fld_aux" ) );

// test project-related variables for project storage
QgsExpressionContext expressionContext;
expressionContext.appendScope( QgsExpressionContextUtils::projectScope( &prj2 ) );
QCOMPARE( QgsExpression( "@project_path" ).evaluate( &expressionContext ).toString(), QString( "memory:project1" ) );
QCOMPARE( QgsExpression( "@project_basename" ).evaluate( &expressionContext ).toString(), QString( "project1" ) );

// test access of non-existent project

QgsProject prj3;
Expand Down

2 comments on commit 42969a4

@nyalldawson
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll also need to add the variable help text for the new variable too

@wonder-sk
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops... added in d6e7043

Please sign in to comment.