Skip to content
Permalink
Browse files

Let QgsApplication manage global custom variables

  • Loading branch information
m-kuhn committed Dec 22, 2016
1 parent 94cbc60 commit b07ecfa106c19358835f31d0484e58a063a57e95
@@ -406,24 +406,13 @@ static void qtgui_UpdatePyArgv(PyObject *argvlist, int argc, char **argv)
static QgsFieldFormatterRegistry* fieldFormatterRegistry();
static QString nullRepresentation();
static void setNullRepresentation( const QString& nullRepresentation );
public slots:

/** Causes the application instance to emit the settingsChanged() signal. This should
* be called whenever global, application-wide settings are altered to advise listeners
* that they may need to update their state.
* @see settingsChanged()
* @note added in QGIS 3.0
*/
void emitSettingsChanged();

static QgsStringMap customVariables();
static void setCustomVariables(const QgsStringMap& customVariables);
static void setCustomVariable(const QString& name, const QString& value);
signals:
//! @note not available in python bindings
// void preNotify( QObject * receiver, QEvent * event, bool * done );

/** Emitted whenever any global, application-wide settings are changed.
* @note added in QGIS 3.0
* @see emitSettingsChanged()
*/
void settingsChanged();
void customVariablesChanged();
void nullRepresentationChanged();
};
@@ -177,12 +177,9 @@ QgsComposerItemWidget::QgsComposerItemWidget( QWidget* parent, QgsComposerItem*
updateVariables();
connect( mVariableEditor, SIGNAL( scopeChanged() ), this, SLOT( variablesChanged() ) );
// listen out for variable edits
QgsApplication* app = qobject_cast<QgsApplication*>( QgsApplication::instance() );
if ( app )
{
connect( app, SIGNAL( settingsChanged() ), this, SLOT( updateVariables() ) );
}
connect( QgsProject::instance(), SIGNAL( variablesChanged() ), this, SLOT( updateVariables() ) );
connect( QgsApplication::instance(), &QgsApplication::customVariablesChanged, this, &QgsComposerItemWidget::updateVariables );
connect( QgsProject::instance(), &QgsProject::variablesChanged, this, &QgsComposerItemWidget::updateVariables );

if ( mItem->composition() )
connect( mItem->composition(), SIGNAL( variablesChanged() ), this, SLOT( updateVariables() ) );
}
@@ -28,10 +28,9 @@
#include <QWidget>
#include <QPrinter> //for screen resolution

QgsCompositionWidget::QgsCompositionWidget( QWidget* parent, QgsComposition* c )\
:
QgsPanelWidget( parent )
, mComposition( c )
QgsCompositionWidget::QgsCompositionWidget( QWidget* parent, QgsComposition* c )
: QgsPanelWidget( parent )
, mComposition( c )
{
setupUi( this );
setPanelTitle( tr( "Composition properties" ) );
@@ -53,12 +52,8 @@ QgsPanelWidget( parent )
updateVariables();
connect( mVariableEditor, SIGNAL( scopeChanged() ), this, SLOT( variablesChanged() ) );
// listen out for variable edits
QgsApplication* app = qobject_cast<QgsApplication*>( QgsApplication::instance() );
if ( app )
{
connect( app, SIGNAL( settingsChanged() ), this, SLOT( updateVariables() ) );
}
connect( QgsProject::instance(), SIGNAL( variablesChanged() ), this, SLOT( updateVariables() ) );
connect( QgsApplication::instance(), &QgsApplication::customVariablesChanged, this, &QgsCompositionWidget::updateVariables );
connect( QgsProject::instance(), &QgsProject::variablesChanged, this, &QgsCompositionWidget::updateVariables );

if ( mComposition )
{
@@ -1486,12 +1486,6 @@ void QgsOptions::saveOptions()
}

saveDefaultDatumTransformations();

QgsApplication* app = qobject_cast<QgsApplication*>( QgsApplication::instance() );
if ( app )
{
app->emitSettingsChanged();
}
}

void QgsOptions::rejectOptions()
@@ -1258,6 +1258,75 @@ void QgsApplication::copyPath( const QString& src, const QString& dst )
}
}

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

QgsStringMap variables;

//check if settings contains any variables
if ( settings.contains( QStringLiteral( "/variables/values" ) ) )
{
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.toString() );
variableIndex++;
}
}

return variables;
}

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

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

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

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

emit instance()->customVariablesChanged();
}

void QgsApplication::setCustomVariable( const QString& name, const QString& value )
{
// save variable to settings
QSettings 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 );

emit instance()->customVariablesChanged();
}

QString QgsApplication::nullRepresentation()
{
QgsApplication* app = instance();
@@ -1439,12 +1508,6 @@ QgsTaskManager* QgsApplication::taskManager()
return instance()->mTaskManager;
}

void QgsApplication::emitSettingsChanged()
{
emit settingsChanged();
}


QgsFieldFormatterRegistry* QgsApplication::fieldFormatterRegistry()
{
return instance()->mFieldFormatterRegistry;
@@ -417,25 +417,42 @@ class CORE_EXPORT QgsApplication : public QApplication
*/
static void setNullRepresentation( const QString& nullRepresentation );

public slots:
/**
* Custom expression variables for this application.
* This does not include generated variables (like system name, user name etc.)
*
* \see QgsExpressionContextUtils::globalVariables().
* \note Added in QGIS 3.0
*/
static QgsStringMap customVariables();

/** Causes the application instance to emit the settingsChanged() signal. This should
* be called whenever global, application-wide settings are altered to advise listeners
* that they may need to update their state.
* @see settingsChanged()
* @note added in QGIS 3.0
/**
* Custom expression variables for this application.
* Do not include generated variables (like system name, user name etc.)
*
* \see QgsExpressionContextUtils::globalVariables().
* \note Added in QGIS 3.0
*/
void emitSettingsChanged();
static void setCustomVariables( const QgsStringMap& customVariables );


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

signals:
//! @note not available in python bindings
void preNotify( QObject * receiver, QEvent * event, bool * done );

/** Emitted whenever any global, application-wide settings are changed.
/**
* Emitted whenever a custom global variable changes.
* @note added in QGIS 3.0
* @see emitSettingsChanged()
*/
void settingsChanged();
void customVariablesChanged();


/**
* \copydoc nullRepresentation()
@@ -501,29 +501,11 @@ QgsExpressionContextScope* QgsExpressionContextUtils::globalScope()
{
QgsExpressionContextScope* scope = new QgsExpressionContextScope( QObject::tr( "Global" ) );

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

//check if settings contains any variables
if ( settings.contains( QStringLiteral( "/variables/values" ) ) )
for ( QgsStringMap::const_iterator it = customVariables.constBegin(); it != customVariables.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();

scope->setVariable( name, value );
variableIndex++;
}
scope->setVariable( it.key(), it.value() );
}

//add some extra global variables
@@ -540,35 +522,12 @@ QgsExpressionContextScope* QgsExpressionContextUtils::globalScope()

void QgsExpressionContextUtils::setGlobalVariable( const QString& name, const QVariant& value )
{
// save variable to settings
QSettings 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 );
QgsApplication::setCustomVariable( name, value.toString() );
}

void QgsExpressionContextUtils::setGlobalVariables( const QgsStringMap &variables )
{
QSettings settings;

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

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

settings.setValue( QStringLiteral( "/variables/names" ), customVariableNames );
settings.setValue( QStringLiteral( "/variables/values" ), customVariableVariants );
QgsApplication::setCustomVariables( variables );
}

/// @cond PRIVATE

0 comments on commit b07ecfa

Please sign in to comment.
You can’t perform that action at this time.