Add methods to remove variables from global/project scope

This commit is contained in:
Ismail Sunni 2017-10-09 09:03:37 +07:00 committed by Nyall Dawson
parent 42676dc93c
commit 1be2f3ee66
4 changed files with 84 additions and 0 deletions

View File

@ -726,6 +726,7 @@ class QgsExpressionContextUtils
\param value variable value \param value variable value
.. seealso:: setGlobalVariable() .. seealso:: setGlobalVariable()
.. seealso:: globalScope() .. seealso:: globalScope()
.. seealso:: removeGlobalVariable()
%End %End
static void setGlobalVariables( const QVariantMap &variables ); static void setGlobalVariables( const QVariantMap &variables );
@ -735,6 +736,16 @@ class QgsExpressionContextUtils
\param variables new set of global variables \param variables new set of global variables
.. seealso:: setGlobalVariable() .. seealso:: setGlobalVariable()
.. seealso:: globalScope() .. 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 %End
static QgsExpressionContextScope *projectScope( const QgsProject *project ) /Factory/; static QgsExpressionContextScope *projectScope( const QgsProject *project ) /Factory/;
@ -754,6 +765,7 @@ class QgsExpressionContextUtils
\param name variable name \param name variable name
\param value variable value \param value variable value
.. seealso:: setProjectVariables() .. seealso:: setProjectVariables()
.. seealso:: removeProjectVariable()
.. seealso:: projectScope() .. seealso:: projectScope()
%End %End
@ -764,6 +776,17 @@ class QgsExpressionContextUtils
\param project Project to apply changes to \param project Project to apply changes to
\param variables new set of project variables \param variables new set of project variables
.. seealso:: setProjectVariable() .. 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() .. seealso:: projectScope()
%End %End

View File

@ -593,6 +593,14 @@ void QgsExpressionContextUtils::setGlobalVariables( const QVariantMap &variables
QgsApplication::setCustomVariables( variables ); QgsApplication::setCustomVariables( variables );
} }
void QgsExpressionContextUtils::removeGlobalVariable( const QString &name )
{
QVariantMap vars = QgsApplication::customVariables();
if ( vars.remove( name ) )
QgsApplication::setCustomVariables( vars );
}
/// @cond PRIVATE /// @cond PRIVATE
class GetNamedProjectColor : public QgsScopedExpressionFunction class GetNamedProjectColor : public QgsScopedExpressionFunction
@ -796,6 +804,18 @@ void QgsExpressionContextUtils::setProjectVariables( QgsProject *project, const
project->setCustomVariables( variables ); 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 *QgsExpressionContextUtils::layerScope( const QgsMapLayer *layer )
{ {
QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Layer" ) ); QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Layer" ) );

View File

@ -733,6 +733,7 @@ class CORE_EXPORT QgsExpressionContextUtils
* \param value variable value * \param value variable value
* \see setGlobalVariable() * \see setGlobalVariable()
* \see globalScope() * \see globalScope()
* \see removeGlobalVariable()
*/ */
static void setGlobalVariable( const QString &name, const QVariant &value ); static void setGlobalVariable( const QString &name, const QVariant &value );
@ -742,9 +743,19 @@ class CORE_EXPORT QgsExpressionContextUtils
* \param variables new set of global variables * \param variables new set of global variables
* \see setGlobalVariable() * \see setGlobalVariable()
* \see globalScope() * \see globalScope()
* \see removeGlobalVariable()
*/ */
static void setGlobalVariables( const QVariantMap &variables ); 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. * 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. * For instance, project path and title, and variables specified through the project properties.
@ -760,6 +771,7 @@ class CORE_EXPORT QgsExpressionContextUtils
* \param name variable name * \param name variable name
* \param value variable value * \param value variable value
* \see setProjectVariables() * \see setProjectVariables()
* \see removeProjectVariable()
* \see projectScope() * \see projectScope()
*/ */
static void setProjectVariable( QgsProject *project, const QString &name, const QVariant &value ); static void setProjectVariable( QgsProject *project, const QString &name, const QVariant &value );
@ -770,10 +782,21 @@ class CORE_EXPORT QgsExpressionContextUtils
* \param project Project to apply changes to * \param project Project to apply changes to
* \param variables new set of project variables * \param variables new set of project variables
* \see setProjectVariable() * \see setProjectVariable()
* \see removeProjectVariable()
* \see projectScope() * \see projectScope()
*/ */
static void setProjectVariables( QgsProject *project, const QVariantMap &variables ); 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. * Creates a new scope which contains variables and functions relating to a QgsMapLayer.
* For instance, layer name, id and fields. * For instance, layer name, id and fields.

View File

@ -554,6 +554,15 @@ void TestQgsExpressionContext::globalScope()
QCOMPARE( globalScope2->variable( "newvar2" ).toString(), QString( "val2" ) ); QCOMPARE( globalScope2->variable( "newvar2" ).toString(), QString( "val2" ) );
delete globalScope2; 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() void TestQgsExpressionContext::projectScope()
@ -599,6 +608,15 @@ void TestQgsExpressionContext::projectScope()
QCOMPARE( projectScope->variable( "newvar1" ).toString(), QString( "val1" ) ); QCOMPARE( projectScope->variable( "newvar1" ).toString(), QString( "val1" ) );
QCOMPARE( projectScope->variable( "newvar2" ).toString(), QString( "val2" ) ); QCOMPARE( projectScope->variable( "newvar2" ).toString(), QString( "val2" ) );
delete projectScope; 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; projectScope = 0;
//test project scope functions //test project scope functions