From 18408fa2c1ff94b89c25ca1e5a953a63e9530cff Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Tue, 20 Mar 2018 10:10:00 +1000 Subject: [PATCH] [FEATURE] Add expression context variables for project metadata - @project_author - @project_abstract - @project_creation_date - @project_identifier - @project_keywords Allows retrieval of project metadata through QGIS expressions Developed for Arpa Piemonte (Dipartimento Tematico Geologia e Dissesto) within ERIKUS project --- src/core/expression/qgsexpression.cpp | 5 +++++ src/core/qgsexpressioncontext.cpp | 15 +++++++++++++ tests/src/core/testqgsexpressioncontext.cpp | 24 +++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/src/core/expression/qgsexpression.cpp b/src/core/expression/qgsexpression.cpp index 9d71d4111c4..70fca464f1d 100644 --- a/src/core/expression/qgsexpression.cpp +++ b/src/core/expression/qgsexpression.cpp @@ -640,6 +640,11 @@ void QgsExpression::initVariableHelp() 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)." ) ); + sVariableHelpTexts.insert( QStringLiteral( "project_author" ), QCoreApplication::translate( "variable_help", "Project author, taken from project metadata." ) ); + sVariableHelpTexts.insert( QStringLiteral( "project_abstract" ), QCoreApplication::translate( "variable_help", "Project abstract, taken from project metadata." ) ); + sVariableHelpTexts.insert( QStringLiteral( "project_creation_date" ), QCoreApplication::translate( "variable_help", "Project creation date, taken from project metadata." ) ); + sVariableHelpTexts.insert( QStringLiteral( "project_identifier" ), QCoreApplication::translate( "variable_help", "Project identifier, taken from project metadata." ) ); + sVariableHelpTexts.insert( QStringLiteral( "project_keywords" ), QCoreApplication::translate( "variable_help", "Project keywords, taken from project metadata." ) ); //layer variables sVariableHelpTexts.insert( QStringLiteral( "layer_name" ), QCoreApplication::translate( "variable_help", "Name of current layer." ) ); diff --git a/src/core/qgsexpressioncontext.cpp b/src/core/qgsexpressioncontext.cpp index 93fc9565197..3a3ad46fd36 100644 --- a/src/core/qgsexpressioncontext.cpp +++ b/src/core/qgsexpressioncontext.cpp @@ -788,6 +788,21 @@ QgsExpressionContextScope *QgsExpressionContextUtils::projectScope( const QgsPro scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_crs" ), projectCrs.authid(), true, true ) ); scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_crs_definition" ), projectCrs.toProj4(), true, true ) ); + // metadata + scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_author" ), project->metadata().author(), true, true ) ); + scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_abstract" ), project->metadata().abstract(), true, true ) ); + scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_creation_date" ), project->metadata().creationDateTime(), true, true ) ); + scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_identifier" ), project->metadata().identifier(), true, true ) ); + + // keywords + QVariantMap keywords; + QgsAbstractMetadataBase::KeywordMap metadataKeywords = project->metadata().keywords(); + for ( auto it = metadataKeywords.constBegin(); it != metadataKeywords.constEnd(); ++it ) + { + keywords.insert( it.key(), it.value() ); + } + scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_keywords" ), keywords, true, true ) ); + scope->addFunction( QStringLiteral( "project_color" ), new GetNamedProjectColor( project ) ); return scope; } diff --git a/tests/src/core/testqgsexpressioncontext.cpp b/tests/src/core/testqgsexpressioncontext.cpp index 1df61d755e7..6a1595a7b1b 100644 --- a/tests/src/core/testqgsexpressioncontext.cpp +++ b/tests/src/core/testqgsexpressioncontext.cpp @@ -598,6 +598,18 @@ void TestQgsExpressionContext::globalScope() void TestQgsExpressionContext::projectScope() { QgsProject *project = QgsProject::instance(); + QgsProjectMetadata md; + md.setTitle( QStringLiteral( "project title" ) ); + md.setAuthor( QStringLiteral( "project author" ) ); + md.setAbstract( QStringLiteral( "project abstract" ) ); + md.setCreationDateTime( QDateTime( QDate( 2011, 3, 5 ), QTime( 9, 5, 4 ) ) ); + md.setIdentifier( QStringLiteral( "project identifier" ) ); + QgsAbstractMetadataBase::KeywordMap keywords; + keywords.insert( QStringLiteral( "voc1" ), QStringList() << "a" << "b" ); + keywords.insert( QStringLiteral( "voc2" ), QStringList() << "c" << "d" ); + md.setKeywords( keywords ); + project->setMetadata( md ); + QgsExpressionContextUtils::setProjectVariable( project, QStringLiteral( "test" ), "testval" ); QgsExpressionContextUtils::setProjectVariable( project, QStringLiteral( "testdouble" ), 5.2 ); @@ -606,6 +618,18 @@ void TestQgsExpressionContext::projectScope() context << scope; QCOMPARE( scope->name(), tr( "Project" ) ); + // metadata variables + QCOMPARE( context.variable( "project_title" ).toString(), QStringLiteral( "project title" ) ); + QCOMPARE( context.variable( "project_author" ).toString(), QStringLiteral( "project author" ) ); + QCOMPARE( context.variable( "project_abstract" ).toString(), QStringLiteral( "project abstract" ) ); + QCOMPARE( context.variable( "project_creation_date" ).toDateTime(), QDateTime( QDate( 2011, 3, 5 ), QTime( 9, 5, 4 ) ) ); + QCOMPARE( context.variable( "project_identifier" ).toString(), QStringLiteral( "project identifier" ) ); + QVariantMap keywordsExpected; + keywordsExpected.insert( QStringLiteral( "voc1" ), QStringList() << "a" << "b" ); + keywordsExpected.insert( QStringLiteral( "voc2" ), QStringList() << "c" << "d" ); + QVariantMap keywordsResult = context.variable( "project_keywords" ).toMap(); + QCOMPARE( keywordsResult, keywordsExpected ); + QCOMPARE( context.variable( "test" ).toString(), QString( "testval" ) ); QCOMPARE( context.variable( "testdouble" ).toDouble(), 5.2 );