Skip to content
Permalink
Browse files

Add API to read/write QgsExpressionContextScope from/to XML

  • Loading branch information
nyalldawson committed Jan 18, 2019
1 parent 4ff81e0 commit a0e91f37981cff414c76000877d9a92096eacb64
@@ -341,6 +341,24 @@ Convenience function for setting a fields for the scope. Any existing
fields set by the scope will be overwritten.

:param fields: fields for scope
%End

void readXml( const QDomElement &element, const QgsReadWriteContext &context );
%Docstring
Reads scope variables from an XML element.

.. seealso:: :py:func:`writeXml`

.. versionadded:: 3.6
%End

bool writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const;
%Docstring
Writes scope variables to an XML ``element``.

.. seealso:: :py:func:`readXml`

.. versionadded:: 3.6
%End

};
@@ -34,6 +34,7 @@
#include "qgslayoutreportcontext.h"
#include "qgsexpressionutils.h"
#include "qgslayoutrendercontext.h"
#include "qgsxmlutils.h"

#include <QSettings>
#include <QDir>
@@ -222,6 +223,31 @@ void QgsExpressionContextScope::setFields( const QgsFields &fields )
addVariable( StaticVariable( QgsExpressionContext::EXPR_FIELDS, QVariant::fromValue( fields ), true ) );
}

void QgsExpressionContextScope::readXml( const QDomElement &element, const QgsReadWriteContext & )
{
const QDomNodeList variablesNodeList = element.childNodes();
for ( int i = 0; i < variablesNodeList.size(); ++i )
{
const QDomElement variableElement = variablesNodeList.at( i ).toElement();
const QString key = variableElement.attribute( QStringLiteral( "name" ) );
const QVariant value = QgsXmlUtils::readVariant( variableElement.firstChildElement( QStringLiteral( "Option" ) ) );
setVariable( key, value );
}
}

bool QgsExpressionContextScope::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext & ) const
{
for ( auto it = mVariables.constBegin(); it != mVariables.constEnd(); ++it )
{
QDomElement varElem = document.createElement( QStringLiteral( "Variable" ) );
varElem.setAttribute( QStringLiteral( "name" ), it.key() );
QDomElement valueElem = QgsXmlUtils::writeVariant( it.value().value, document );
varElem.appendChild( valueElem );
element.appendChild( varElem );
}
return true;
}


//
// QgsExpressionContext
@@ -349,6 +349,20 @@ class CORE_EXPORT QgsExpressionContextScope
*/
void setFields( const QgsFields &fields );

/**
* Reads scope variables from an XML element.
* \see writeXml()
* \since QGIS 3.6
*/
void readXml( const QDomElement &element, const QgsReadWriteContext &context );

/**
* Writes scope variables to an XML \a element.
* \see readXml()
* \since QGIS 3.6
*/
bool writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const;

private:
QString mName;
QHash<QString, StaticVariable> mVariables;
@@ -55,6 +55,7 @@ class TestQgsExpressionContext : public QObject

void valuesAsMap();
void description();
void readWriteScope();

private:

@@ -854,5 +855,37 @@ void TestQgsExpressionContext::description()
QCOMPARE( context.description( "project_title" ), QStringLiteral( "my desc" ) );
}

void TestQgsExpressionContext::readWriteScope()
{
QDomImplementation DomImplementation;
QDomDocumentType documentType =
DomImplementation.createDocumentType(
QStringLiteral( "qgis" ), QStringLiteral( "http://mrcc.com/qgis.dtd" ), QStringLiteral( "SYSTEM" ) );
QDomDocument doc( documentType );
QDomElement rootNode = doc.createElement( QStringLiteral( "qgis" ) );

QgsExpressionContextScope s1;
s1.setVariable( QStringLiteral( "v1" ), "t1" );
s1.setVariable( QStringLiteral( "v2" ), 55 );
QDomElement e = doc.createElement( QStringLiteral( "scope_test" ) );
s1.writeXml( e, doc, QgsReadWriteContext() );
doc.appendChild( e );

QgsExpressionContextScope s2;
QCOMPARE( s2.variableCount(), 0 );

// invalid xml element
QDomElement e2 = doc.createElement( QStringLiteral( "empty" ) );
s2.readXml( e2, QgsReadWriteContext() );
QCOMPARE( s2.variableCount(), 0 );

// valid element
s2.readXml( e, QgsReadWriteContext() );
QCOMPARE( s2.variableCount(), 2 );
QCOMPARE( s2.variableNames().toSet(), QSet< QString >() << QStringLiteral( "v1" ) << QStringLiteral( "v2" ) );
QCOMPARE( s2.variable( QStringLiteral( "v1" ) ).toString(), QStringLiteral( "t1" ) );
QCOMPARE( s2.variable( QStringLiteral( "v2" ) ).toInt(), 55 );
}

QGSTEST_MAIN( TestQgsExpressionContext )
#include "testqgsexpressioncontext.moc"

0 comments on commit a0e91f3

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