@@ -90,17 +90,18 @@ QgsExpressionContextScope::~QgsExpressionContextScope()
90
90
qDeleteAll ( mFunctions );
91
91
}
92
92
93
- void QgsExpressionContextScope::setVariable ( const QString &name, const QVariant &value )
93
+ void QgsExpressionContextScope::setVariable ( const QString &name, const QVariant &value, bool isStatic )
94
94
{
95
95
if ( mVariables .contains ( name ) )
96
96
{
97
97
StaticVariable existing = mVariables .value ( name );
98
98
existing.value = value;
99
+ existing.isStatic = isStatic;
99
100
addVariable ( existing );
100
101
}
101
102
else
102
103
{
103
- addVariable ( QgsExpressionContextScope::StaticVariable ( name, value ) );
104
+ addVariable ( QgsExpressionContextScope::StaticVariable ( name, value, false , isStatic ) );
104
105
}
105
106
}
106
107
@@ -179,6 +180,11 @@ bool QgsExpressionContextScope::isReadOnly( const QString &name ) const
179
180
return hasVariable ( name ) ? mVariables .value ( name ).readOnly : false ;
180
181
}
181
182
183
+ bool QgsExpressionContextScope::isStatic ( const QString &name ) const
184
+ {
185
+ return hasVariable ( name ) ? mVariables .value ( name ).isStatic : false ;
186
+ }
187
+
182
188
bool QgsExpressionContextScope::hasFunction ( const QString &name ) const
183
189
{
184
190
return mFunctions .contains ( name );
@@ -545,19 +551,19 @@ QgsExpressionContextScope *QgsExpressionContextUtils::globalScope()
545
551
546
552
for ( QVariantMap::const_iterator it = customVariables.constBegin (); it != customVariables.constEnd (); ++it )
547
553
{
548
- scope->setVariable ( it.key (), it.value () );
554
+ scope->setVariable ( it.key (), it.value (), true );
549
555
}
550
556
551
557
// add some extra global variables
552
- scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " qgis_version" ), Qgis::QGIS_VERSION, true ) );
553
- scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " qgis_version_no" ), Qgis::QGIS_VERSION_INT, true ) );
554
- scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " qgis_short_version" ), QStringLiteral ( " %1.%2" ).arg ( Qgis::QGIS_VERSION_INT / 10000 ).arg ( Qgis::QGIS_VERSION_INT / 100 % 100 ), true ) );
555
- scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " qgis_release_name" ), Qgis::QGIS_RELEASE_NAME, true ) );
556
- scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " qgis_platform" ), QgsApplication::platform (), true ) );
557
- scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " qgis_os_name" ), QgsApplication::osName (), true ) );
558
- scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " qgis_locale" ), QgsApplication::locale (), true ) );
559
- scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " user_account_name" ), QgsApplication::userLoginName (), true ) );
560
- scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " user_full_name" ), QgsApplication::userFullName (), true ) );
558
+ scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " qgis_version" ), Qgis::QGIS_VERSION, true , true ) );
559
+ scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " qgis_version_no" ), Qgis::QGIS_VERSION_INT, true , true ) );
560
+ scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " qgis_short_version" ), QStringLiteral ( " %1.%2" ).arg ( Qgis::QGIS_VERSION_INT / 10000 ).arg ( Qgis::QGIS_VERSION_INT / 100 % 100 ), true , true ) );
561
+ scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " qgis_release_name" ), Qgis::QGIS_RELEASE_NAME, true , true ) );
562
+ scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " qgis_platform" ), QgsApplication::platform (), true , true ) );
563
+ scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " qgis_os_name" ), QgsApplication::osName (), true , true ) );
564
+ scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " qgis_locale" ), QgsApplication::locale (), true , true ) );
565
+ scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " user_account_name" ), QgsApplication::userLoginName (), true , true ) );
566
+ scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " user_full_name" ), QgsApplication::userFullName (), true , true ) );
561
567
562
568
return scope;
563
569
}
@@ -715,17 +721,17 @@ QgsExpressionContextScope *QgsExpressionContextUtils::projectScope( const QgsPro
715
721
716
722
for ( ; it != vars.constEnd (); ++it )
717
723
{
718
- scope->setVariable ( it.key (), it.value () );
724
+ scope->setVariable ( it.key (), it.value (), true );
719
725
}
720
726
721
727
// add other known project variables
722
- scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " project_title" ), project->title (), true ) );
723
- scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " project_path" ), project->fileInfo ().filePath (), true ) );
724
- scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " project_folder" ), project->fileInfo ().dir ().path (), true ) );
725
- scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " project_filename" ), project->fileInfo ().fileName (), true ) );
728
+ scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " project_title" ), project->title (), true , true ) );
729
+ scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " project_path" ), project->fileInfo ().filePath (), true , true ) );
730
+ scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " project_folder" ), project->fileInfo ().dir ().path (), true , true ) );
731
+ scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " project_filename" ), project->fileInfo ().fileName (), true , true ) );
726
732
QgsCoordinateReferenceSystem projectCrs = project->crs ();
727
- scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " project_crs" ), projectCrs.authid (), true ) );
728
- scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " project_crs_definition" ), projectCrs.toProj4 (), true ) );
733
+ scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " project_crs" ), projectCrs.authid (), true , true ) );
734
+ scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " project_crs_definition" ), projectCrs.toProj4 (), true , true ) );
729
735
730
736
scope->addFunction ( QStringLiteral ( " project_color" ), new GetNamedProjectColor ( project ) );
731
737
return scope;
@@ -772,14 +778,14 @@ QgsExpressionContextScope *QgsExpressionContextUtils::layerScope( const QgsMapLa
772
778
773
779
QVariant varValue = variableValues.at ( varIndex );
774
780
varIndex++;
775
- scope->setVariable ( variableName, varValue );
781
+ scope->setVariable ( variableName, varValue, true );
776
782
}
777
783
778
- scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " layer_name" ), layer->name (), true ) );
779
- scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " layer_id" ), layer->id (), true ) );
780
- scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " layer" ), QVariant::fromValue<QgsWeakMapLayerPointer >( QgsWeakMapLayerPointer ( const_cast <QgsMapLayer *>( layer ) ) ), true ) );
784
+ scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " layer_name" ), layer->name (), true , true ) );
785
+ scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " layer_id" ), layer->id (), true , true ) );
786
+ scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " layer" ), QVariant::fromValue<QgsWeakMapLayerPointer >( QgsWeakMapLayerPointer ( const_cast <QgsMapLayer *>( layer ) ) ), true , true ) );
781
787
782
- const QgsVectorLayer *vLayer = dynamic_cast < const QgsVectorLayer * >( layer );
788
+ const QgsVectorLayer *vLayer = qobject_cast < const QgsVectorLayer * >( layer );
783
789
if ( vLayer )
784
790
{
785
791
scope->setFields ( vLayer->fields () );
0 commit comments