Navigation Menu

Skip to content

Commit

Permalink
Add methods to remove variables from global/project scope
Browse files Browse the repository at this point in the history
  • Loading branch information
ismailsunni authored and nyalldawson committed Oct 9, 2017
1 parent 42676dc commit 1be2f3e
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
23 changes: 23 additions & 0 deletions python/core/qgsexpressioncontext.sip
Expand Up @@ -726,6 +726,7 @@ class QgsExpressionContextUtils
\param value variable value
.. seealso:: setGlobalVariable()
.. seealso:: globalScope()
.. seealso:: removeGlobalVariable()
%End

static void setGlobalVariables( const QVariantMap &variables );
Expand All @@ -735,6 +736,16 @@ class QgsExpressionContextUtils
\param variables new set of global variables
.. seealso:: setGlobalVariable()
.. seealso:: globalScope()
.. seealso:: removeGlobalVariable()
%End

static void removeGlobalVariable( const QString &name );
%Docstring
Remove a global context variable.
\param name variable name
.. seealso:: setGlobalVariable()
.. seealso:: setGlobalVariables()
.. seealso:: globalScope()
%End

static QgsExpressionContextScope *projectScope( const QgsProject *project ) /Factory/;
Expand All @@ -754,6 +765,7 @@ class QgsExpressionContextUtils
\param name variable name
\param value variable value
.. seealso:: setProjectVariables()
.. seealso:: removeProjectVariable()
.. seealso:: projectScope()
%End

Expand All @@ -764,6 +776,17 @@ class QgsExpressionContextUtils
\param project Project to apply changes to
\param variables new set of project variables
.. seealso:: setProjectVariable()
.. seealso:: removeProjectVariable()
.. seealso:: projectScope()
%End

static void removeProjectVariable( QgsProject *project, const QString &name );
%Docstring
Remove project context variable.
\param project Project to apply changes to
\param name variable name
.. seealso:: setProjectVariable()
.. seealso:: setProjectVariables()
.. seealso:: projectScope()
%End

Expand Down
20 changes: 20 additions & 0 deletions src/core/qgsexpressioncontext.cpp
Expand Up @@ -593,6 +593,14 @@ void QgsExpressionContextUtils::setGlobalVariables( const QVariantMap &variables
QgsApplication::setCustomVariables( variables );
}

void QgsExpressionContextUtils::removeGlobalVariable( const QString &name )
{
QVariantMap vars = QgsApplication::customVariables();
if ( vars.remove( name ) )
QgsApplication::setCustomVariables( vars );
}


/// @cond PRIVATE

class GetNamedProjectColor : public QgsScopedExpressionFunction
Expand Down Expand Up @@ -796,6 +804,18 @@ void QgsExpressionContextUtils::setProjectVariables( QgsProject *project, const
project->setCustomVariables( variables );
}

void QgsExpressionContextUtils::removeProjectVariable( QgsProject *project, const QString &name )
{
if ( !project )
{
return;
}

QVariantMap vars = project->customVariables();
if ( vars.remove( name ) )
project->setCustomVariables( vars );
}

QgsExpressionContextScope *QgsExpressionContextUtils::layerScope( const QgsMapLayer *layer )
{
QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Layer" ) );
Expand Down
23 changes: 23 additions & 0 deletions src/core/qgsexpressioncontext.h
Expand Up @@ -733,6 +733,7 @@ class CORE_EXPORT QgsExpressionContextUtils
* \param value variable value
* \see setGlobalVariable()
* \see globalScope()
* \see removeGlobalVariable()
*/
static void setGlobalVariable( const QString &name, const QVariant &value );

Expand All @@ -742,9 +743,19 @@ class CORE_EXPORT QgsExpressionContextUtils
* \param variables new set of global variables
* \see setGlobalVariable()
* \see globalScope()
* \see removeGlobalVariable()
*/
static void setGlobalVariables( const QVariantMap &variables );

/**
* Remove a global context variable.
* \param name variable name
* \see setGlobalVariable()
* \see setGlobalVariables()
* \see globalScope()
*/
static void removeGlobalVariable( const QString &name );

/**
* Creates a new scope which contains variables and functions relating to a QGIS project.
* For instance, project path and title, and variables specified through the project properties.
Expand All @@ -760,6 +771,7 @@ class CORE_EXPORT QgsExpressionContextUtils
* \param name variable name
* \param value variable value
* \see setProjectVariables()
* \see removeProjectVariable()
* \see projectScope()
*/
static void setProjectVariable( QgsProject *project, const QString &name, const QVariant &value );
Expand All @@ -770,10 +782,21 @@ class CORE_EXPORT QgsExpressionContextUtils
* \param project Project to apply changes to
* \param variables new set of project variables
* \see setProjectVariable()
* \see removeProjectVariable()
* \see projectScope()
*/
static void setProjectVariables( QgsProject *project, const QVariantMap &variables );

/**
* Remove project context variable.
* \param project Project to apply changes to
* \param name variable name
* \see setProjectVariable()
* \see setProjectVariables()
* \see projectScope()
*/
static void removeProjectVariable( QgsProject *project, const QString &name );

/**
* Creates a new scope which contains variables and functions relating to a QgsMapLayer.
* For instance, layer name, id and fields.
Expand Down
18 changes: 18 additions & 0 deletions tests/src/core/testqgsexpressioncontext.cpp
Expand Up @@ -554,6 +554,15 @@ void TestQgsExpressionContext::globalScope()
QCOMPARE( globalScope2->variable( "newvar2" ).toString(), QString( "val2" ) );

delete globalScope2;

//test removeGlobalVariables
QgsExpressionContextUtils::setGlobalVariable( QStringLiteral( "key" ), "value" );
QgsExpressionContextScope *globalScope3 = QgsExpressionContextUtils::globalScope();
QVERIFY( globalScope3->hasVariable( "key" ) );
QgsExpressionContextUtils::removeGlobalVariable( QStringLiteral( "key" ) );
globalScope3 = QgsExpressionContextUtils::globalScope();
QVERIFY( !globalScope3->hasVariable( "key" ) );
delete globalScope3;
}

void TestQgsExpressionContext::projectScope()
Expand Down Expand Up @@ -599,6 +608,15 @@ void TestQgsExpressionContext::projectScope()
QCOMPARE( projectScope->variable( "newvar1" ).toString(), QString( "val1" ) );
QCOMPARE( projectScope->variable( "newvar2" ).toString(), QString( "val2" ) );
delete projectScope;

//test removeProjectVariable
QgsExpressionContextUtils::setProjectVariable( project, QStringLiteral( "key" ), "value" );
projectScope = QgsExpressionContextUtils::projectScope( project );
QVERIFY( projectScope->hasVariable( "key" ) );
QgsExpressionContextUtils::removeProjectVariable( project, QStringLiteral( "key" ) );
projectScope = QgsExpressionContextUtils::projectScope( project );
QVERIFY( !projectScope->hasVariable( "key" ) );
delete projectScope;
projectScope = 0;

//test project scope functions
Expand Down

0 comments on commit 1be2f3e

Please sign in to comment.