Skip to content
Permalink
Browse files

Add project_basename to project expr context, project_path for custom…

… impls
  • Loading branch information
wonder-sk committed Apr 7, 2018
1 parent 663a9ac commit 42969a479416c84c1a318beedb8a5d57418fa670
@@ -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;
@@ -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() )
@@ -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 ) );
@@ -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 );
@@ -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() )
@@ -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 );
@@ -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() )
@@ -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
@@ -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;
@@ -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
@@ -16,6 +16,7 @@
#include "qgstest.h"

#include "qgsauxiliarystorage.h"
#include "qgsexpressioncontext.h"
#include "qgsproject.h"
#include "qgsprojectstorage.h"
#include "qgsprojectstorageregistry.h"
@@ -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;

2 comments on commit 42969a4

@nyalldawson

This comment has been minimized.

Copy link
Collaborator

@nyalldawson nyalldawson replied Apr 7, 2018

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

@wonder-sk

This comment has been minimized.

Copy link
Member Author

@wonder-sk wonder-sk replied Apr 7, 2018

oops... added in d6e7043

Please sign in to comment.
You can’t perform that action at this time.