Skip to content

Commit 18408fa

Browse files
committed
[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
1 parent 288bb1a commit 18408fa

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

src/core/expression/qgsexpression.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,11 @@ void QgsExpression::initVariableHelp()
640640
sVariableHelpTexts.insert( QStringLiteral( "project_home" ), QCoreApplication::translate( "variable_help", "Home path of current project." ) );
641641
sVariableHelpTexts.insert( QStringLiteral( "project_crs" ), QCoreApplication::translate( "variable_help", "Coordinate reference system of project (e.g., 'EPSG:4326')." ) );
642642
sVariableHelpTexts.insert( QStringLiteral( "project_crs_definition" ), QCoreApplication::translate( "variable_help", "Coordinate reference system of project (full definition)." ) );
643+
sVariableHelpTexts.insert( QStringLiteral( "project_author" ), QCoreApplication::translate( "variable_help", "Project author, taken from project metadata." ) );
644+
sVariableHelpTexts.insert( QStringLiteral( "project_abstract" ), QCoreApplication::translate( "variable_help", "Project abstract, taken from project metadata." ) );
645+
sVariableHelpTexts.insert( QStringLiteral( "project_creation_date" ), QCoreApplication::translate( "variable_help", "Project creation date, taken from project metadata." ) );
646+
sVariableHelpTexts.insert( QStringLiteral( "project_identifier" ), QCoreApplication::translate( "variable_help", "Project identifier, taken from project metadata." ) );
647+
sVariableHelpTexts.insert( QStringLiteral( "project_keywords" ), QCoreApplication::translate( "variable_help", "Project keywords, taken from project metadata." ) );
643648

644649
//layer variables
645650
sVariableHelpTexts.insert( QStringLiteral( "layer_name" ), QCoreApplication::translate( "variable_help", "Name of current layer." ) );

src/core/qgsexpressioncontext.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,21 @@ QgsExpressionContextScope *QgsExpressionContextUtils::projectScope( const QgsPro
788788
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_crs" ), projectCrs.authid(), true, true ) );
789789
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_crs_definition" ), projectCrs.toProj4(), true, true ) );
790790

791+
// metadata
792+
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_author" ), project->metadata().author(), true, true ) );
793+
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_abstract" ), project->metadata().abstract(), true, true ) );
794+
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_creation_date" ), project->metadata().creationDateTime(), true, true ) );
795+
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_identifier" ), project->metadata().identifier(), true, true ) );
796+
797+
// keywords
798+
QVariantMap keywords;
799+
QgsAbstractMetadataBase::KeywordMap metadataKeywords = project->metadata().keywords();
800+
for ( auto it = metadataKeywords.constBegin(); it != metadataKeywords.constEnd(); ++it )
801+
{
802+
keywords.insert( it.key(), it.value() );
803+
}
804+
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "project_keywords" ), keywords, true, true ) );
805+
791806
scope->addFunction( QStringLiteral( "project_color" ), new GetNamedProjectColor( project ) );
792807
return scope;
793808
}

tests/src/core/testqgsexpressioncontext.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,18 @@ void TestQgsExpressionContext::globalScope()
598598
void TestQgsExpressionContext::projectScope()
599599
{
600600
QgsProject *project = QgsProject::instance();
601+
QgsProjectMetadata md;
602+
md.setTitle( QStringLiteral( "project title" ) );
603+
md.setAuthor( QStringLiteral( "project author" ) );
604+
md.setAbstract( QStringLiteral( "project abstract" ) );
605+
md.setCreationDateTime( QDateTime( QDate( 2011, 3, 5 ), QTime( 9, 5, 4 ) ) );
606+
md.setIdentifier( QStringLiteral( "project identifier" ) );
607+
QgsAbstractMetadataBase::KeywordMap keywords;
608+
keywords.insert( QStringLiteral( "voc1" ), QStringList() << "a" << "b" );
609+
keywords.insert( QStringLiteral( "voc2" ), QStringList() << "c" << "d" );
610+
md.setKeywords( keywords );
611+
project->setMetadata( md );
612+
601613
QgsExpressionContextUtils::setProjectVariable( project, QStringLiteral( "test" ), "testval" );
602614
QgsExpressionContextUtils::setProjectVariable( project, QStringLiteral( "testdouble" ), 5.2 );
603615

@@ -606,6 +618,18 @@ void TestQgsExpressionContext::projectScope()
606618
context << scope;
607619
QCOMPARE( scope->name(), tr( "Project" ) );
608620

621+
// metadata variables
622+
QCOMPARE( context.variable( "project_title" ).toString(), QStringLiteral( "project title" ) );
623+
QCOMPARE( context.variable( "project_author" ).toString(), QStringLiteral( "project author" ) );
624+
QCOMPARE( context.variable( "project_abstract" ).toString(), QStringLiteral( "project abstract" ) );
625+
QCOMPARE( context.variable( "project_creation_date" ).toDateTime(), QDateTime( QDate( 2011, 3, 5 ), QTime( 9, 5, 4 ) ) );
626+
QCOMPARE( context.variable( "project_identifier" ).toString(), QStringLiteral( "project identifier" ) );
627+
QVariantMap keywordsExpected;
628+
keywordsExpected.insert( QStringLiteral( "voc1" ), QStringList() << "a" << "b" );
629+
keywordsExpected.insert( QStringLiteral( "voc2" ), QStringList() << "c" << "d" );
630+
QVariantMap keywordsResult = context.variable( "project_keywords" ).toMap();
631+
QCOMPARE( keywordsResult, keywordsExpected );
632+
609633
QCOMPARE( context.variable( "test" ).toString(), QString( "testval" ) );
610634
QCOMPARE( context.variable( "testdouble" ).toDouble(), 5.2 );
611635

0 commit comments

Comments
 (0)