Skip to content

Commit

Permalink
Save variables are key value in settings (#6411)
Browse files Browse the repository at this point in the history
* Save variables are key value in settings

This allows variables to be set in qgis_global_settings.ini correctly.

This used to be a list of names and then a list of values making it
impossible to set and override.
  • Loading branch information
NathanW2 committed Feb 22, 2018
1 parent 6a8e423 commit 5eb3e6b
Showing 1 changed file with 9 additions and 34 deletions.
43 changes: 9 additions & 34 deletions src/core/qgsapplication.cpp
Expand Up @@ -1381,25 +1381,12 @@ QVariantMap QgsApplication::customVariables()
QVariantMap variables;

//check if settings contains any variables
if ( settings.contains( QStringLiteral( "/variables/values" ) ) )
settings.beginGroup( "variables" );
QStringList childKeys = settings.childKeys();
for ( QStringList::const_iterator it = childKeys.constBegin(); it != childKeys.constEnd(); ++it )
{
QList< QVariant > customVariableVariants = settings.value( QStringLiteral( "variables/values" ) ).toList();
QList< QVariant > customVariableNames = settings.value( QStringLiteral( "variables/names" ) ).toList();
int variableIndex = 0;
for ( QList< QVariant >::const_iterator it = customVariableVariants.constBegin();
it != customVariableVariants.constEnd(); ++it )
{
if ( variableIndex >= customVariableNames.length() )
{
break;
}

QVariant value = ( *it );
QString name = customVariableNames.at( variableIndex ).toString();

variables.insert( name, value );
variableIndex++;
}
QString name = *it;
variables.insert( name, settings.value( name ) );
}

return variables;
Expand All @@ -1409,19 +1396,14 @@ void QgsApplication::setCustomVariables( const QVariantMap &variables )
{
QgsSettings settings;

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

QVariantMap::const_iterator it = variables.constBegin();
settings.beginGroup("variables");
settings.remove("");
for ( ; it != variables.constEnd(); ++it )
{
customVariableNames << it.key();
customVariableValues << it.value();
settings.setValue( it.key(), it.value() );
}

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

emit instance()->customVariablesChanged();
}

Expand All @@ -1430,14 +1412,7 @@ void QgsApplication::setCustomVariable( const QString &name, const QVariant &val
// save variable to settings
QgsSettings settings;

QList< QVariant > customVariableVariants = settings.value( QStringLiteral( "variables/values" ) ).toList();
QList< QVariant > customVariableNames = settings.value( QStringLiteral( "variables/names" ) ).toList();

customVariableVariants << value;
customVariableNames << name;

settings.setValue( QStringLiteral( "variables/names" ), customVariableNames );
settings.setValue( QStringLiteral( "variables/values" ), customVariableVariants );
settings.setValue( QStringLiteral( "variables/" ) + name, value );

emit instance()->customVariablesChanged();
}
Expand Down

0 comments on commit 5eb3e6b

Please sign in to comment.