Skip to content

Commit

Permalink
Pass variable values as QVariant
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Dec 22, 2016
1 parent 79f32ab commit 33abb78
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 157 deletions.
6 changes: 3 additions & 3 deletions python/core/qgsapplication.sip
Expand Up @@ -406,9 +406,9 @@ static void qtgui_UpdatePyArgv(PyObject *argvlist, int argc, char **argv)
static QgsFieldFormatterRegistry* fieldFormatterRegistry();
static QString nullRepresentation();
static void setNullRepresentation( const QString& nullRepresentation );
static QgsStringMap customVariables();
static void setCustomVariables(const QgsStringMap& customVariables);
static void setCustomVariable(const QString& name, const QString& value);
static QVariantMap customVariables();
static void setCustomVariables( const QVariantMap& customVariables );
static void setCustomVariable( const QString& name, const QVariant& value );
signals:
//! @note not available in python bindings
// void preNotify( QObject * receiver, QEvent * event, bool * done );
Expand Down
84 changes: 5 additions & 79 deletions python/core/qgsexpressioncontext.sip
Expand Up @@ -482,46 +482,11 @@ class QgsExpressionContextUtils
* @see setGlobalVariable()
*/
static QgsExpressionContextScope* globalScope() /Factory/;

/** Sets a global context variable. This variable will be contained within scopes retrieved via
* globalScope().
* @param name variable name
* @param value variable value
* @see setGlobalVariable()
* @see globalScope()
*/
static void setGlobalVariable( const QString& name, const QVariant& value );

/** Sets all global context variables. Existing global variables will be removed and replaced
* with the variables specified.
* @param variables new set of global variables
* @see setGlobalVariable()
* @see globalScope()
*/
static void setGlobalVariables( const QgsStringMap& variables );

/** Creates a new scope which contains variables and functions relating to the current QGIS project.
* For instance, project path and title, and variables specified through the project properties.
* @see setProjectVariable()
*/
static void setGlobalVariables( const QVariantMap& variables );
static QgsExpressionContextScope* projectScope() /Factory/;

/** Sets a project context variable. This variable will be contained within scopes retrieved via
* projectScope().
* @param name variable name
* @param value variable value
* @see setProjectVariables()
* @see projectScope()
*/
static void setProjectVariable( const QString& name, const QVariant& value );

/** Sets all project context variables. Existing project variables will be removed and replaced
* with the variables specified.
* @param variables new set of project variables
* @see setProjectVariable()
* @see projectScope()
*/
static void setProjectVariables( const QgsStringMap& variables );
static void setProjectVariables( const QVariantMap& variables );

/** Creates a new scope which contains variables and functions relating to a QgsMapLayer.
* For instance, layer name, id and fields.
Expand All @@ -537,15 +502,7 @@ class QgsExpressionContextUtils
* @see layerScope()
*/
static void setLayerVariable( QgsMapLayer* layer, const QString& name, const QVariant& value );

/** Sets all layer context variables. Existing layer variables will be removed and replaced
* with the variables specified.
* @param layer map layer
* @param variables new set of layer variables
* @see setLayerVariable()
* @see layerScope()
*/
static void setLayerVariables( QgsMapLayer* layer, const QgsStringMap& variables );
static void setLayerVariables( QgsMapLayer* layer, const QVariantMap& variables );

/** Creates a new scope which contains variables and functions relating to a QgsMapSettings object.
* For instance, map scale and rotation.
Expand Down Expand Up @@ -575,26 +532,8 @@ class QgsExpressionContextUtils
* @see compositionScope()
*/
static void setCompositionVariable( QgsComposition* composition, const QString& name, const QVariant& value );

/** Sets all composition context variables. Existing composition variables will be removed and replaced
* with the variables specified.
* @param composition target composition
* @param variables new set of layer variables
* @see setCompositionVariable()
* @see compositionScope()
*/
static void setCompositionVariables( QgsComposition* composition, const QgsStringMap& variables );

/** Creates a new scope which contains variables and functions relating to a QgsAtlasComposition.
* For instance, current page name and number.
* @param atlas source atlas. If null, a set of default atlas variables will be added to the scope.
*/
static void setCompositionVariables( QgsComposition* composition, const QVariantMap& variables );
static QgsExpressionContextScope* atlasScope( const QgsAtlasComposition* atlas ) /Factory/;

/** Creates a new scope which contains variables and functions relating to a QgsComposerItem.
* For instance, item size and position.
* @param composerItem source composer item
*/
static QgsExpressionContextScope* composerItemScope( const QgsComposerItem *composerItem ) /Factory/;

/** Sets a composer item context variable. This variable will be contained within scopes retrieved via
Expand All @@ -606,20 +545,7 @@ class QgsExpressionContextUtils
* @see composerItemScope()
*/
static void setComposerItemVariable( QgsComposerItem* composerItem, const QString& name, const QVariant& value );

/** Sets all composition context variables. Existing compositoin variables will be removed and replaced
* with the variables specified.
* @param composerItem target composer item
* @param variables new set of layer variables
* @see setComposerItemVariable()
* @see composerItemScope()
*/
static void setComposerItemVariables( QgsComposerItem* composerItem, const QgsStringMap& variables );

/** Helper function for creating an expression context which contains just a feature and fields
* collection. Generally this method should not be used as the created context does not include
* standard scopes such as the global and project scopes.
*/
static void setComposerItemVariables( QgsComposerItem* composerItem, const QVariantMap& variables );
static QgsExpressionContext createFeatureBasedContext( const QgsFeature& feature, const QgsFields& fields );

/** Registers all known core functions provided by QgsExpressionContextScope objects.
Expand Down
21 changes: 2 additions & 19 deletions python/core/qgsproject.sip
Expand Up @@ -420,25 +420,8 @@ class QgsProject : QObject, QgsExpressionContextGenerator
* @note Added in QGIS 3.0
*/
void setAvoidIntersectionsList( const QStringList& avoidIntersectionsList );

/**
* A map of custom project variables.
* To get all available variables including generated ones
* use QgsExpressionContextUtils::projectScope() instead.
*/
QgsStringMap customVariables() const;

/**
* A map of custom project variables.
* Be careful not to set generated variables.
*/
void setCustomVariables( const QgsStringMap& customVariables );

//
// Functionality from QgsMapLayerRegistry
//

//! Returns the number of registered layers.
QVariantMap customVariables() const;
void setCustomVariables( const QVariantMap& customVariables );
int count() const;

/** Retrieve a pointer to a registered layer by layer ID.
Expand Down
7 changes: 1 addition & 6 deletions python/gui/qgsvariableeditorwidget.sip
Expand Up @@ -70,12 +70,7 @@ class QgsVariableEditorWidget : QWidget
* @see setSettingGroup()
*/
QString settingGroup() const;

/** Returns a map variables set within the editable scope. Read only variables are not
* returned. This method can be used to retrieve the variables edited an added by
* users via the widget.
*/
QgsStringMap variablesInActiveScope() const;
QVariantMap variablesInActiveScope() const;

signals:

Expand Down
18 changes: 9 additions & 9 deletions src/core/qgsapplication.cpp
Expand Up @@ -1258,12 +1258,12 @@ void QgsApplication::copyPath( const QString& src, const QString& dst )
}
}

QgsStringMap QgsApplication::customVariables()
QVariantMap QgsApplication::customVariables()
{
//read values from QSettings
QSettings settings;

QgsStringMap variables;
QVariantMap variables;

//check if settings contains any variables
if ( settings.contains( QStringLiteral( "/variables/values" ) ) )
Expand All @@ -1282,35 +1282,35 @@ QgsStringMap QgsApplication::customVariables()
QVariant value = ( *it );
QString name = customVariableNames.at( variableIndex ).toString();

variables.insert( name, value.toString() );
variables.insert( name, value );
variableIndex++;
}
}

return variables;
}

void QgsApplication::setCustomVariables( const QgsStringMap& variables )
void QgsApplication::setCustomVariables( const QVariantMap& variables )
{
QSettings settings;

QList< QVariant > customVariableVariants;
QList< QVariant > customVariableValues;
QList< QVariant > customVariableNames;

QMap< QString, QString >::const_iterator it = variables.constBegin();
QVariantMap::const_iterator it = variables.constBegin();
for ( ; it != variables.constEnd(); ++it )
{
customVariableNames << it.key();
customVariableVariants << it.value();
customVariableValues << it.value();
}

settings.setValue( QStringLiteral( "/variables/names" ), customVariableNames );
settings.setValue( QStringLiteral( "/variables/values" ), customVariableVariants );
settings.setValue( QStringLiteral( "/variables/values" ), customVariableValues );

emit instance()->customVariablesChanged();
}

void QgsApplication::setCustomVariable( const QString& name, const QString& value )
void QgsApplication::setCustomVariable( const QString& name, const QVariant& value )
{
// save variable to settings
QSettings settings;
Expand Down
6 changes: 3 additions & 3 deletions src/core/qgsapplication.h
Expand Up @@ -424,7 +424,7 @@ class CORE_EXPORT QgsApplication : public QApplication
* \see QgsExpressionContextUtils::globalVariables().
* \note Added in QGIS 3.0
*/
static QgsStringMap customVariables();
static QVariantMap customVariables();

/**
* Custom expression variables for this application.
Expand All @@ -433,15 +433,15 @@ class CORE_EXPORT QgsApplication : public QApplication
* \see QgsExpressionContextUtils::globalVariables().
* \note Added in QGIS 3.0
*/
static void setCustomVariables( const QgsStringMap& customVariables );
static void setCustomVariables( const QVariantMap& customVariables );


/**
* Set a single custom expression variable.
*
* \note Added in QGIS 3.0
*/
static void setCustomVariable( const QString& name, const QString& value );
static void setCustomVariable( const QString& name, const QVariant& value );

signals:
//! @note not available in python bindings
Expand Down
36 changes: 18 additions & 18 deletions src/core/qgsexpressioncontext.cpp
Expand Up @@ -501,9 +501,9 @@ QgsExpressionContextScope* QgsExpressionContextUtils::globalScope()
{
QgsExpressionContextScope* scope = new QgsExpressionContextScope( QObject::tr( "Global" ) );

QgsStringMap customVariables = QgsApplication::customVariables();
QVariantMap customVariables = QgsApplication::customVariables();

for ( QgsStringMap::const_iterator it = customVariables.constBegin(); it != customVariables.constEnd(); ++it )
for ( QVariantMap::const_iterator it = customVariables.constBegin(); it != customVariables.constEnd(); ++it )
{
scope->setVariable( it.key(), it.value() );
}
Expand All @@ -522,10 +522,10 @@ QgsExpressionContextScope* QgsExpressionContextUtils::globalScope()

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

void QgsExpressionContextUtils::setGlobalVariables( const QgsStringMap &variables )
void QgsExpressionContextUtils::setGlobalVariables( const QVariantMap &variables )
{
QgsApplication::setCustomVariables( variables );
}
Expand Down Expand Up @@ -589,9 +589,9 @@ QgsExpressionContextScope* QgsExpressionContextUtils::projectScope()

QgsExpressionContextScope* scope = new QgsExpressionContextScope( QObject::tr( "Project" ) );

const QgsStringMap vars = QgsProject::instance()->customVariables();
const QVariantMap vars = QgsProject::instance()->customVariables();

QgsStringMap::const_iterator it = vars.constBegin();
QVariantMap::const_iterator it = vars.constBegin();

for ( ; it != vars.constEnd(); ++it )
{
Expand All @@ -615,14 +615,14 @@ void QgsExpressionContextUtils::setProjectVariable( const QString& name, const Q
{
QgsProject* project = QgsProject::instance();

QgsStringMap vars = project->customVariables();
QVariantMap vars = project->customVariables();

vars.insert( name, value.toString() );
vars.insert( name, value );

project->setCustomVariables( vars );
}

void QgsExpressionContextUtils::setProjectVariables( const QgsStringMap& variables )
void QgsExpressionContextUtils::setProjectVariables( const QVariantMap& variables )
{
QgsProject::instance()->setCustomVariables( variables );
}
Expand Down Expand Up @@ -684,19 +684,19 @@ void QgsExpressionContextUtils::setLayerVariable( QgsMapLayer* layer, const QStr
layer->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
}

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

QStringList variableNames;
QStringList variableValues;

QMap< QString, QString >::const_iterator it = variables.constBegin();
QVariantMap::const_iterator it = variables.constBegin();
for ( ; it != variables.constEnd(); ++it )
{
variableNames << it.key();
variableValues << it.value();
variableValues << it.value().toString();
}

layer->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
Expand Down Expand Up @@ -790,19 +790,19 @@ void QgsExpressionContextUtils::setCompositionVariable( QgsComposition* composit
composition->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
}

void QgsExpressionContextUtils::setCompositionVariables( QgsComposition* composition, const QgsStringMap& variables )
void QgsExpressionContextUtils::setCompositionVariables( QgsComposition* composition, const QVariantMap& variables )
{
if ( !composition )
return;

QStringList variableNames;
QStringList variableValues;

QMap< QString, QString >::const_iterator it = variables.constBegin();
QVariantMap::const_iterator it = variables.constBegin();
for ( ; it != variables.constEnd(); ++it )
{
variableNames << it.key();
variableValues << it.value();
variableValues << it.value().toString();
}

composition->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
Expand Down Expand Up @@ -893,19 +893,19 @@ void QgsExpressionContextUtils::setComposerItemVariable( QgsComposerItem* compos
composerItem->setCustomProperty( QStringLiteral( "variableValues" ), variableValues );
}

void QgsExpressionContextUtils::setComposerItemVariables( QgsComposerItem* composerItem, const QgsStringMap& variables )
void QgsExpressionContextUtils::setComposerItemVariables( QgsComposerItem* composerItem, const QVariantMap& variables )
{
if ( !composerItem )
return;

QStringList variableNames;
QStringList variableValues;

QMap< QString, QString >::const_iterator it = variables.constBegin();
QVariantMap::const_iterator it = variables.constBegin();
for ( ; it != variables.constEnd(); ++it )
{
variableNames << it.key();
variableValues << it.value();
variableValues << it.value().toString();
}

composerItem->setCustomProperty( QStringLiteral( "variableNames" ), variableNames );
Expand Down

0 comments on commit 33abb78

Please sign in to comment.