Skip to content

Commit

Permalink
Add @project_home expression variable
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Mar 9, 2018
1 parent 4e5c08e commit 8c780dc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/core/expression/qgsexpression.cpp
Expand Up @@ -637,6 +637,7 @@ void QgsExpression::initVariableHelp()
sVariableHelpTexts.insert( QStringLiteral( "project_path" ), QCoreApplication::translate( "variable_help", "Full path (including file name) of current project." ) );
sVariableHelpTexts.insert( QStringLiteral( "project_folder" ), QCoreApplication::translate( "variable_help", "Folder for current project." ) );
sVariableHelpTexts.insert( QStringLiteral( "project_filename" ), QCoreApplication::translate( "variable_help", "Filename of current project." ) );
sVariableHelpTexts.insert( QStringLiteral( "project_home" ), QCoreApplication::translate( "variable_help", "Home path of current project." ) );
sVariableHelpTexts.insert( QStringLiteral( "project_crs" ), QCoreApplication::translate( "variable_help", "Coordinate reference system of project (e.g., 'EPSG:4326')." ) );
sVariableHelpTexts.insert( QStringLiteral( "project_crs_definition" ), QCoreApplication::translate( "variable_help", "Coordinate reference system of project (full definition)." ) );

Expand Down
1 change: 1 addition & 0 deletions src/core/qgsexpressioncontext.cpp
Expand Up @@ -783,6 +783,7 @@ QgsExpressionContextScope *QgsExpressionContextUtils::projectScope( const QgsPro
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_path" ), QDir::toNativeSeparators( project->fileInfo().filePath() ), true, true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_folder" ), QDir::toNativeSeparators( project->fileInfo().dir().path() ), true, true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_filename" ), project->fileInfo().fileName(), 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 ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_crs_definition" ), projectCrs.toProj4(), true, true ) );
Expand Down
18 changes: 17 additions & 1 deletion tests/src/python/test_qgsproject.py
Expand Up @@ -24,7 +24,8 @@
QgsCoordinateReferenceSystem,
QgsVectorLayer,
QgsRasterLayer,
QgsMapLayer)
QgsMapLayer,
QgsExpressionContextUtils)
from qgis.gui import (QgsLayerTreeMapCanvasBridge,
QgsMapCanvas)

Expand Down Expand Up @@ -880,6 +881,9 @@ def testHomePath(self):
self.assertEqual(p.homePath(), '/tmp/my_path')
self.assertEqual(p.presetHomePath(), '/tmp/my_path')
self.assertEqual(len(path_changed_spy), 2)
# check project scope
scope = QgsExpressionContextUtils.projectScope(p)
self.assertEqual(scope.variable('project_home'), '/tmp/my_path')

# no extra signal if path is unchanged
p.setPresetHomePath('/tmp/my_path')
Expand All @@ -897,23 +901,35 @@ def testHomePath(self):
self.assertEqual(p.presetHomePath(), '/tmp/my_path')
self.assertEqual(len(path_changed_spy), 2)

scope = QgsExpressionContextUtils.projectScope(p)
self.assertEqual(scope.variable('project_home'), '/tmp/my_path')

# clear manual path
p.setPresetHomePath('')
self.assertEqual(p.homePath(), tmp_dir.path() + '/project')
self.assertFalse(p.presetHomePath())
self.assertEqual(len(path_changed_spy), 3)

scope = QgsExpressionContextUtils.projectScope(p)
self.assertEqual(scope.variable('project_home'), tmp_dir.path() + '/project')

# relative path
p.setPresetHomePath('../home')
self.assertEqual(p.homePath(), tmp_dir.path() + '/home')
self.assertEqual(p.presetHomePath(), '../home')
self.assertEqual(len(path_changed_spy), 4)

scope = QgsExpressionContextUtils.projectScope(p)
self.assertEqual(scope.variable('project_home'), tmp_dir.path() + '/home')

# relative path, no filename
p.setFileName('')
self.assertEqual(p.homePath(), '../home')
self.assertEqual(p.presetHomePath(), '../home')

scope = QgsExpressionContextUtils.projectScope(p)
self.assertEqual(scope.variable('project_home'), '../home')


if __name__ == '__main__':
unittest.main()

0 comments on commit 8c780dc

Please sign in to comment.