Skip to content

Commit

Permalink
Implement invokable functions for plugins to handle global/project/la…
Browse files Browse the repository at this point in the history
…yer variables
  • Loading branch information
nirvn committed Jul 5, 2024
1 parent 4bd6bf5 commit 78a4fee
Show file tree
Hide file tree
Showing 3 changed files with 270 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/core/qgismobileapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include "digitizinglogger.h"
#include "distancearea.h"
#include "drawingcanvas.h"
#include "expressioncontextutils.h"
#include "expressionevaluator.h"
#include "expressionvariablemodel.h"
#include "featurechecklistmodel.h"
Expand Down Expand Up @@ -518,6 +519,7 @@ void QgisMobileapp::initDeclarative()
qRegisterMetaType<GnssPositionInformation>( "GnssPositionInformation" );
qRegisterMetaType<PluginInformation>( "PluginInformation" );

REGISTER_SINGLETON( "org.qfield", ExpressionContextUtils, "ExpressionContextUtils" );
REGISTER_SINGLETON( "org.qfield", GeometryEditorsModel, "GeometryEditorsModelSingleton" );
REGISTER_SINGLETON( "org.qfield", GeometryUtils, "GeometryUtils" );
REGISTER_SINGLETON( "org.qfield", FeatureUtils, "FeatureUtils" );
Expand Down
149 changes: 148 additions & 1 deletion src/core/utils/expressioncontextutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@


#include "expressioncontextutils.h"
#include "qgsgeometry.h"

#include <qgsapplication.h>
#include <qgsgeometry.h>
#include <qgsmaplayer.h>
#include <qgsproject.h>


void addPositionVariable( QgsExpressionContextScope *scope, const QString &name, const QVariant &value, bool positionLocked, const QVariant &defaultValue = QVariant() )
Expand All @@ -29,6 +33,10 @@ void addPositionVariable( QgsExpressionContextScope *scope, const QString &name,
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "position_%1" ).arg( name ), defaultValue, true, true ) );
}

ExpressionContextUtils::ExpressionContextUtils( QObject *parent )
: QObject( parent )
{
}

QgsExpressionContextScope *ExpressionContextUtils::positionScope( const GnssPositionInformation &positionInformation, bool positionLocked )
{
Expand Down Expand Up @@ -111,3 +119,142 @@ QgsExpressionContextScope *ExpressionContextUtils::cloudUserScope( const CloudUs
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "cloud_useremail" ), cloudUserInformation.email, true, true ) );
return scope;
}

QVariantMap ExpressionContextUtils::layerVariables( QgsMapLayer *layer )
{
if ( !layer )
{
return QVariantMap();
}

const QStringList variableNames = layer->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
const QStringList variableValues = layer->customProperty( QStringLiteral( "variableValues" ) ).toStringList();

QVariantMap variables;
for ( int i = 0; i < variableNames.size(); i++ )
{
variables[variableNames.at( i )] = variableValues.at( i );
}
return variables;
}

void ExpressionContextUtils::setLayerVariable( QgsMapLayer *layer, const QString &name, const QVariant &value )
{
if ( !layer )
{
return;
}

QStringList variableNames = layer->customProperty( QStringLiteral( "variableNames" ) ).toStringList();
QStringList variableValues = layer->customProperty( QStringLiteral( "variableValues" ) ).toStringList();

variableNames << name;
variableValues << value.toString();

layer->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
layer->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
}

void ExpressionContextUtils::setLayerVariables( QgsMapLayer *layer, const QVariantMap &variables )
{
if ( !layer )
{
return;
}

QStringList variableNames;
QStringList variableValues;

QVariantMap::const_iterator it = variables.constBegin();
while ( ++it != variables.constEnd() )
{
variableNames << it.key();
variableValues << it.value().toString();
}

layer->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
layer->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
}

void ExpressionContextUtils::removeLayerVariable( QgsMapLayer *layer, const QString &name )
{
if ( !layer )
{
return;
}

QVariantMap variables = layerVariables( layer );
if ( variables.remove( name ) )
{
setLayerVariables( layer, variables );
}
}

QVariantMap ExpressionContextUtils::projectVariables( QgsProject *project )
{
if ( !project )
{
return QVariantMap();
}

return project->customVariables();
}

void ExpressionContextUtils::setProjectVariable( QgsProject *project, const QString &name, const QVariant &value )
{
if ( !project )
{
return;
}

QVariantMap variables = project->customVariables();
variables.insert( name, value );
project->setCustomVariables( variables );
}
void ExpressionContextUtils::setProjectVariables( QgsProject *project, const QVariantMap &variables )
{
if ( !project )
{
return;
}

project->setCustomVariables( variables );
}

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

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

QVariantMap ExpressionContextUtils::globalVariables()
{
return QgsApplication::customVariables();
}

void ExpressionContextUtils::setGlobalVariable( const QString &name, const QVariant &value )
{
QgsApplication::setCustomVariable( name, value );
}

void ExpressionContextUtils::setGlobalVariables( const QVariantMap &variables )
{
QgsApplication::setCustomVariables( variables );
}

void ExpressionContextUtils::removeGlobalVariable( const QString &name )
{
QVariantMap variables = QgsApplication::customVariables();
if ( variables.remove( name ) )
{
QgsApplication::setCustomVariables( variables );
}
}
123 changes: 120 additions & 3 deletions src/core/utils/expressioncontextutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,132 @@

#include <qgsexpressioncontext.h>

class ExpressionContextUtils
class ExpressionContextUtils : public QObject
{
Q_OBJECT

public:
explicit ExpressionContextUtils( QObject *parent = nullptr );

static QgsExpressionContextScope *positionScope( const GnssPositionInformation &positionInformation, bool positionLocked );
static QgsExpressionContextScope *mapToolCaptureScope( const SnappingResult &topSnappingResult );
static QgsExpressionContextScope *cloudUserScope( const CloudUserInformation &cloudUserInformation );

private:
ExpressionContextUtils() = default;
/**
* Returns a layer context variables.
* \param layer map layer
* \see setLayerVariable()
* \see setLayerVariables()
* \see removeLayerVariable()
*/
Q_INVOKABLE static QVariantMap layerVariables( QgsMapLayer *layer );

/**
* Sets a layer context variable.
* \param layer map layer
* \param name variable name
* \param value variable value
* \see layerVariables()
* \see setLayerVariables()
* \see removeLayerVariable()
*/
Q_INVOKABLE static void setLayerVariable( QgsMapLayer *layer, const QString &name, const QVariant &value );

/**
* Sets a layer context variables.
* \param layer map layer
* \param variables new set of layer variables
* \see layerVariables()
* \see setLayerVariable()
* \see removeLayerVariable()
*/
Q_INVOKABLE static void setLayerVariables( QgsMapLayer *layer, const QVariantMap &variables );

/**
* Removes a layer context variable.
* \param layer map layer
* \param name variable name
* \see layerVariables()
* \see setLayerVariable()
* \see setLayerVariables()
*/
Q_INVOKABLE static void removeLayerVariable( QgsMapLayer *layer, const QString &name );

/**
* Returns a project context variables.
* \param project project
* \see setProjectVariable()
* \see setProjectVariables()
* \see removeProjectVariable()
*/
Q_INVOKABLE static QVariantMap projectVariables( QgsProject *project );

/**
* Sets a project context variable.
* \param project project
* \param name variable name
* \param value variable value
* \see projectVariables()
* \see setProjectVariables()
* \see removeProjectVariable()
*/
Q_INVOKABLE static void setProjectVariable( QgsProject *project, const QString &name, const QVariant &value );

/**
* Sets a project context variables.
* \param project project
* \param variables new set of project variables
* \see projectVariables()
* \see setProjectVariable()
* \see removeProjectVariable()
*/
Q_INVOKABLE static void setProjectVariables( QgsProject *project, const QVariantMap &variables );

/**
* Removes a project context variable.
* \param project project
* \param name variable name
* \see projectVariables()
* \see setProjectVariable()
* \see setProjectVariables()
*/
Q_INVOKABLE static void removeProjectVariable( QgsProject *project, const QString &name );

/**
* Returns the global context variables.
* \see setGlobalVariable()
* \see setGlobalVariables()
* \see removeGlobalVariable()
*/
Q_INVOKABLE static QVariantMap globalVariables();

/**
* Sets a global context variable.
* \param name variable name
* \param value variable value
* \see globalVariables()
* \see setGlobalVariables()
* \see removeGlobalVariable()
*/
Q_INVOKABLE static void setGlobalVariable( const QString &name, const QVariant &value );

/**
* Sets the global context variables.
* \param variables new set of global variables
* \see globalVariables()
* \see setGlobalVariable()
* \see removeGlobalVariable()
*/
Q_INVOKABLE static void setGlobalVariables( const QVariantMap &variables );

/**
* Removes a global context variable.
* \param name variable name
* \see globalVariables()
* \see setGlobalVariable()
* \see setGlobalVariables()
*/
Q_INVOKABLE static void removeGlobalVariable( const QString &name );
};

#endif // EXPRESSIONCONTEXTUTILS_H

1 comment on commit 78a4fee

@qfield-fairy
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.