Skip to content

Commit 1be2f3e

Browse files
ismailsunninyalldawson
authored andcommittedOct 9, 2017
Add methods to remove variables from global/project scope
1 parent 42676dc commit 1be2f3e

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed
 

‎python/core/qgsexpressioncontext.sip

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,7 @@ class QgsExpressionContextUtils
726726
\param value variable value
727727
.. seealso:: setGlobalVariable()
728728
.. seealso:: globalScope()
729+
.. seealso:: removeGlobalVariable()
729730
%End
730731

731732
static void setGlobalVariables( const QVariantMap &variables );
@@ -735,6 +736,16 @@ class QgsExpressionContextUtils
735736
\param variables new set of global variables
736737
.. seealso:: setGlobalVariable()
737738
.. seealso:: globalScope()
739+
.. seealso:: removeGlobalVariable()
740+
%End
741+
742+
static void removeGlobalVariable( const QString &name );
743+
%Docstring
744+
Remove a global context variable.
745+
\param name variable name
746+
.. seealso:: setGlobalVariable()
747+
.. seealso:: setGlobalVariables()
748+
.. seealso:: globalScope()
738749
%End
739750

740751
static QgsExpressionContextScope *projectScope( const QgsProject *project ) /Factory/;
@@ -754,6 +765,7 @@ class QgsExpressionContextUtils
754765
\param name variable name
755766
\param value variable value
756767
.. seealso:: setProjectVariables()
768+
.. seealso:: removeProjectVariable()
757769
.. seealso:: projectScope()
758770
%End
759771

@@ -764,6 +776,17 @@ class QgsExpressionContextUtils
764776
\param project Project to apply changes to
765777
\param variables new set of project variables
766778
.. seealso:: setProjectVariable()
779+
.. seealso:: removeProjectVariable()
780+
.. seealso:: projectScope()
781+
%End
782+
783+
static void removeProjectVariable( QgsProject *project, const QString &name );
784+
%Docstring
785+
Remove project context variable.
786+
\param project Project to apply changes to
787+
\param name variable name
788+
.. seealso:: setProjectVariable()
789+
.. seealso:: setProjectVariables()
767790
.. seealso:: projectScope()
768791
%End
769792

‎src/core/qgsexpressioncontext.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,14 @@ void QgsExpressionContextUtils::setGlobalVariables( const QVariantMap &variables
593593
QgsApplication::setCustomVariables( variables );
594594
}
595595

596+
void QgsExpressionContextUtils::removeGlobalVariable( const QString &name )
597+
{
598+
QVariantMap vars = QgsApplication::customVariables();
599+
if ( vars.remove( name ) )
600+
QgsApplication::setCustomVariables( vars );
601+
}
602+
603+
596604
/// @cond PRIVATE
597605

598606
class GetNamedProjectColor : public QgsScopedExpressionFunction
@@ -796,6 +804,18 @@ void QgsExpressionContextUtils::setProjectVariables( QgsProject *project, const
796804
project->setCustomVariables( variables );
797805
}
798806

807+
void QgsExpressionContextUtils::removeProjectVariable( QgsProject *project, const QString &name )
808+
{
809+
if ( !project )
810+
{
811+
return;
812+
}
813+
814+
QVariantMap vars = project->customVariables();
815+
if ( vars.remove( name ) )
816+
project->setCustomVariables( vars );
817+
}
818+
799819
QgsExpressionContextScope *QgsExpressionContextUtils::layerScope( const QgsMapLayer *layer )
800820
{
801821
QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Layer" ) );

‎src/core/qgsexpressioncontext.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,7 @@ class CORE_EXPORT QgsExpressionContextUtils
733733
* \param value variable value
734734
* \see setGlobalVariable()
735735
* \see globalScope()
736+
* \see removeGlobalVariable()
736737
*/
737738
static void setGlobalVariable( const QString &name, const QVariant &value );
738739

@@ -742,9 +743,19 @@ class CORE_EXPORT QgsExpressionContextUtils
742743
* \param variables new set of global variables
743744
* \see setGlobalVariable()
744745
* \see globalScope()
746+
* \see removeGlobalVariable()
745747
*/
746748
static void setGlobalVariables( const QVariantMap &variables );
747749

750+
/**
751+
* Remove a global context variable.
752+
* \param name variable name
753+
* \see setGlobalVariable()
754+
* \see setGlobalVariables()
755+
* \see globalScope()
756+
*/
757+
static void removeGlobalVariable( const QString &name );
758+
748759
/**
749760
* Creates a new scope which contains variables and functions relating to a QGIS project.
750761
* For instance, project path and title, and variables specified through the project properties.
@@ -760,6 +771,7 @@ class CORE_EXPORT QgsExpressionContextUtils
760771
* \param name variable name
761772
* \param value variable value
762773
* \see setProjectVariables()
774+
* \see removeProjectVariable()
763775
* \see projectScope()
764776
*/
765777
static void setProjectVariable( QgsProject *project, const QString &name, const QVariant &value );
@@ -770,10 +782,21 @@ class CORE_EXPORT QgsExpressionContextUtils
770782
* \param project Project to apply changes to
771783
* \param variables new set of project variables
772784
* \see setProjectVariable()
785+
* \see removeProjectVariable()
773786
* \see projectScope()
774787
*/
775788
static void setProjectVariables( QgsProject *project, const QVariantMap &variables );
776789

790+
/**
791+
* Remove project context variable.
792+
* \param project Project to apply changes to
793+
* \param name variable name
794+
* \see setProjectVariable()
795+
* \see setProjectVariables()
796+
* \see projectScope()
797+
*/
798+
static void removeProjectVariable( QgsProject *project, const QString &name );
799+
777800
/**
778801
* Creates a new scope which contains variables and functions relating to a QgsMapLayer.
779802
* For instance, layer name, id and fields.

‎tests/src/core/testqgsexpressioncontext.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,15 @@ void TestQgsExpressionContext::globalScope()
554554
QCOMPARE( globalScope2->variable( "newvar2" ).toString(), QString( "val2" ) );
555555

556556
delete globalScope2;
557+
558+
//test removeGlobalVariables
559+
QgsExpressionContextUtils::setGlobalVariable( QStringLiteral( "key" ), "value" );
560+
QgsExpressionContextScope *globalScope3 = QgsExpressionContextUtils::globalScope();
561+
QVERIFY( globalScope3->hasVariable( "key" ) );
562+
QgsExpressionContextUtils::removeGlobalVariable( QStringLiteral( "key" ) );
563+
globalScope3 = QgsExpressionContextUtils::globalScope();
564+
QVERIFY( !globalScope3->hasVariable( "key" ) );
565+
delete globalScope3;
557566
}
558567

559568
void TestQgsExpressionContext::projectScope()
@@ -599,6 +608,15 @@ void TestQgsExpressionContext::projectScope()
599608
QCOMPARE( projectScope->variable( "newvar1" ).toString(), QString( "val1" ) );
600609
QCOMPARE( projectScope->variable( "newvar2" ).toString(), QString( "val2" ) );
601610
delete projectScope;
611+
612+
//test removeProjectVariable
613+
QgsExpressionContextUtils::setProjectVariable( project, QStringLiteral( "key" ), "value" );
614+
projectScope = QgsExpressionContextUtils::projectScope( project );
615+
QVERIFY( projectScope->hasVariable( "key" ) );
616+
QgsExpressionContextUtils::removeProjectVariable( project, QStringLiteral( "key" ) );
617+
projectScope = QgsExpressionContextUtils::projectScope( project );
618+
QVERIFY( !projectScope->hasVariable( "key" ) );
619+
delete projectScope;
602620
projectScope = 0;
603621

604622
//test project scope functions

0 commit comments

Comments
 (0)
Please sign in to comment.