Skip to content
Permalink
Browse files

Add @project_home expression variable

  • Loading branch information
nyalldawson committed Mar 8, 2018
1 parent 4e5c08e commit 8c780dc12fc2463d4d43f4b8db88dcdfddaf7521
@@ -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)." ) );

@@ -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 ) );
@@ -24,7 +24,8 @@
QgsCoordinateReferenceSystem,
QgsVectorLayer,
QgsRasterLayer,
QgsMapLayer)
QgsMapLayer,
QgsExpressionContextUtils)
from qgis.gui import (QgsLayerTreeMapCanvasBridge,
QgsMapCanvas)

@@ -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')
@@ -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.
You can’t perform that action at this time.