mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-14 00:07:35 -04:00
[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
This commit is contained in:
parent
288bb1a872
commit
18408fa2c1
@ -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." ) );
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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 );
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user