Skip to content

Commit

Permalink
Add API to read/write QgsExpressionContextScope from/to XML
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 18, 2019
1 parent 4ff81e0 commit a0e91f3
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
18 changes: 18 additions & 0 deletions python/core/auto_generated/qgsexpressioncontext.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -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

};
Expand Down
26 changes: 26 additions & 0 deletions src/core/qgsexpressioncontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "qgslayoutreportcontext.h"
#include "qgsexpressionutils.h"
#include "qgslayoutrendercontext.h"
#include "qgsxmlutils.h"

#include <QSettings>
#include <QDir>
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions src/core/qgsexpressioncontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
33 changes: 33 additions & 0 deletions tests/src/core/testqgsexpressioncontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class TestQgsExpressionContext : public QObject

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

private:

Expand Down Expand Up @@ -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.