Skip to content

Commit a0e91f3

Browse files
committedJan 18, 2019
Add API to read/write QgsExpressionContextScope from/to XML
1 parent 4ff81e0 commit a0e91f3

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed
 

‎python/core/auto_generated/qgsexpressioncontext.sip.in

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,24 @@ Convenience function for setting a fields for the scope. Any existing
341341
fields set by the scope will be overwritten.
342342

343343
:param fields: fields for scope
344+
%End
345+
346+
void readXml( const QDomElement &element, const QgsReadWriteContext &context );
347+
%Docstring
348+
Reads scope variables from an XML element.
349+
350+
.. seealso:: :py:func:`writeXml`
351+
352+
.. versionadded:: 3.6
353+
%End
354+
355+
bool writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const;
356+
%Docstring
357+
Writes scope variables to an XML ``element``.
358+
359+
.. seealso:: :py:func:`readXml`
360+
361+
.. versionadded:: 3.6
344362
%End
345363

346364
};

‎src/core/qgsexpressioncontext.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "qgslayoutreportcontext.h"
3535
#include "qgsexpressionutils.h"
3636
#include "qgslayoutrendercontext.h"
37+
#include "qgsxmlutils.h"
3738

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

226+
void QgsExpressionContextScope::readXml( const QDomElement &element, const QgsReadWriteContext & )
227+
{
228+
const QDomNodeList variablesNodeList = element.childNodes();
229+
for ( int i = 0; i < variablesNodeList.size(); ++i )
230+
{
231+
const QDomElement variableElement = variablesNodeList.at( i ).toElement();
232+
const QString key = variableElement.attribute( QStringLiteral( "name" ) );
233+
const QVariant value = QgsXmlUtils::readVariant( variableElement.firstChildElement( QStringLiteral( "Option" ) ) );
234+
setVariable( key, value );
235+
}
236+
}
237+
238+
bool QgsExpressionContextScope::writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext & ) const
239+
{
240+
for ( auto it = mVariables.constBegin(); it != mVariables.constEnd(); ++it )
241+
{
242+
QDomElement varElem = document.createElement( QStringLiteral( "Variable" ) );
243+
varElem.setAttribute( QStringLiteral( "name" ), it.key() );
244+
QDomElement valueElem = QgsXmlUtils::writeVariant( it.value().value, document );
245+
varElem.appendChild( valueElem );
246+
element.appendChild( varElem );
247+
}
248+
return true;
249+
}
250+
225251

226252
//
227253
// QgsExpressionContext

‎src/core/qgsexpressioncontext.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,20 @@ class CORE_EXPORT QgsExpressionContextScope
349349
*/
350350
void setFields( const QgsFields &fields );
351351

352+
/**
353+
* Reads scope variables from an XML element.
354+
* \see writeXml()
355+
* \since QGIS 3.6
356+
*/
357+
void readXml( const QDomElement &element, const QgsReadWriteContext &context );
358+
359+
/**
360+
* Writes scope variables to an XML \a element.
361+
* \see readXml()
362+
* \since QGIS 3.6
363+
*/
364+
bool writeXml( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const;
365+
352366
private:
353367
QString mName;
354368
QHash<QString, StaticVariable> mVariables;

‎tests/src/core/testqgsexpressioncontext.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class TestQgsExpressionContext : public QObject
5555

5656
void valuesAsMap();
5757
void description();
58+
void readWriteScope();
5859

5960
private:
6061

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

858+
void TestQgsExpressionContext::readWriteScope()
859+
{
860+
QDomImplementation DomImplementation;
861+
QDomDocumentType documentType =
862+
DomImplementation.createDocumentType(
863+
QStringLiteral( "qgis" ), QStringLiteral( "http://mrcc.com/qgis.dtd" ), QStringLiteral( "SYSTEM" ) );
864+
QDomDocument doc( documentType );
865+
QDomElement rootNode = doc.createElement( QStringLiteral( "qgis" ) );
866+
867+
QgsExpressionContextScope s1;
868+
s1.setVariable( QStringLiteral( "v1" ), "t1" );
869+
s1.setVariable( QStringLiteral( "v2" ), 55 );
870+
QDomElement e = doc.createElement( QStringLiteral( "scope_test" ) );
871+
s1.writeXml( e, doc, QgsReadWriteContext() );
872+
doc.appendChild( e );
873+
874+
QgsExpressionContextScope s2;
875+
QCOMPARE( s2.variableCount(), 0 );
876+
877+
// invalid xml element
878+
QDomElement e2 = doc.createElement( QStringLiteral( "empty" ) );
879+
s2.readXml( e2, QgsReadWriteContext() );
880+
QCOMPARE( s2.variableCount(), 0 );
881+
882+
// valid element
883+
s2.readXml( e, QgsReadWriteContext() );
884+
QCOMPARE( s2.variableCount(), 2 );
885+
QCOMPARE( s2.variableNames().toSet(), QSet< QString >() << QStringLiteral( "v1" ) << QStringLiteral( "v2" ) );
886+
QCOMPARE( s2.variable( QStringLiteral( "v1" ) ).toString(), QStringLiteral( "t1" ) );
887+
QCOMPARE( s2.variable( QStringLiteral( "v2" ) ).toInt(), 55 );
888+
}
889+
857890
QGSTEST_MAIN( TestQgsExpressionContext )
858891
#include "testqgsexpressioncontext.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.