Skip to content
Permalink
Browse files

Add methods to QgsDataDefined for reading and writing to XML

  • Loading branch information
nyalldawson committed Dec 10, 2014
1 parent a1e0f01 commit bcc29ff5d00f1e4d0f69ebd85710453ff8e59e87
Showing with 141 additions and 2 deletions.
  1. +21 −0 python/core/qgsdatadefined.sip
  2. +37 −2 src/core/qgsdatadefined.cpp
  3. +21 −0 src/core/qgsdatadefined.h
  4. +62 −0 tests/src/core/testqgsdatadefined.cpp
@@ -57,5 +57,26 @@ class QgsDataDefined

// @note not available in python bindings
//QMap< QString, QString > toMap();

/**Returns a DOM element containing the properties of the data defined container.
* @param DOM document
* @param elementName name for DOM element
* @returns DOM element corresponding to data defined container
* @note added in QGIS 2.7
* @see setFromXmlElement
*/
QDomElement toXmlElement( QDomDocument &document, const QString &elementName ) const;

/**Sets the properties of the data defined container from an XML element. Calling
* this will overwrite all the current properties of the container.
* @param element DOM element
* @returns true if properties were successfully read from element
* @note added in QGIS 2.7
* @see toXmlElement
*/
bool setFromXmlElement( const QDomElement& element );

bool operator==( const QgsDataDefined &other );
bool operator!=( const QgsDataDefined &other );

};
@@ -23,8 +23,8 @@
QgsDataDefined::QgsDataDefined( bool active,
bool useexpr,
const QString& expr,
const QString& field ) :
mActive( active )
const QString& field )
: mActive( active )
, mUseExpression( useexpr )
, mExpressionString( expr )
, mField( field )
@@ -136,3 +136,38 @@ QMap< QString, QString > QgsDataDefined::toMap()

return map;
}

QDomElement QgsDataDefined::toXmlElement( QDomDocument &document, const QString& elementName ) const
{
QDomElement element = document.createElement( elementName );
element.setAttribute( "active", mActive ? "true" : "false" );
element.setAttribute( "useExpr", mUseExpression ? "true" : "false" );
element.setAttribute( "expr", mExpressionString );
element.setAttribute( "field", mField );
return element;
}

bool QgsDataDefined::setFromXmlElement( const QDomElement &element )
{
if ( element.isNull() )
{
return false;
}

mActive = element.attribute( "active" ).compare( "true", Qt::CaseInsensitive ) == 0;
mUseExpression = element.attribute( "useExpr" ).compare( "true", Qt::CaseInsensitive ) == 0;
mField = element.attribute( "field" );
setExpressionString( element.attribute( "expr" ) );
return true;
}

bool QgsDataDefined::operator==( const QgsDataDefined &other )
{
return other.isActive() == mActive && other.useExpression() == mUseExpression &&
other.field() == mField && other.expressionString() == mExpressionString;
}

bool QgsDataDefined::operator!=( const QgsDataDefined &other )
{
return !( *this == other );
}
@@ -80,6 +80,27 @@ class CORE_EXPORT QgsDataDefined
// @note not available in python bindings
QMap< QString, QString > toMap();

/**Returns a DOM element containing the properties of the data defined container.
* @param DOM document
* @param elementName name for DOM element
* @returns DOM element corresponding to data defined container
* @note added in QGIS 2.7
* @see setFromXmlElement
*/
QDomElement toXmlElement( QDomDocument &document, const QString &elementName ) const;

/**Sets the properties of the data defined container from an XML element. Calling
* this will overwrite all the current properties of the container.
* @param element DOM element
* @returns true if properties were successfully read from element
* @note added in QGIS 2.7
* @see toXmlElement
*/
bool setFromXmlElement( const QDomElement& element );

bool operator==( const QgsDataDefined &other );
bool operator!=( const QgsDataDefined &other );

private:
QgsExpression* mExpression;

@@ -35,6 +35,8 @@ class TestQgsDataDefined: public QObject
void create();//test creating a data defined container
void gettersSetters(); //test getters and setters
void defaultValues(); //test hasDefaultValues method
void equality(); //test equality operators
void xmlMethods(); //test saving and reading from xml

private:
};
@@ -110,5 +112,65 @@ void TestQgsDataDefined::defaultValues()
delete dd;
}

void TestQgsDataDefined::equality()
{
QgsDataDefined dd1;
dd1.setActive( true );
dd1.setField( QString( "field" ) );
dd1.setExpressionString( QString( "expression" ) );
dd1.setUseExpression( true );
QgsDataDefined dd2;
dd2.setActive( true );
dd2.setField( QString( "field" ) );
dd2.setExpressionString( QString( "expression" ) );
dd2.setUseExpression( true );
QVERIFY( dd1 == dd2 );
QVERIFY( !( dd1 != dd2 ) );

//test that all applicable components contribute to equality
dd2.setActive( false );
QVERIFY( !( dd1 == dd2 ) );
QVERIFY( dd1 != dd2 );
dd2.setActive( true );
dd2.setField( QString( "a" ) );
QVERIFY( !( dd1 == dd2 ) );
QVERIFY( dd1 != dd2 );
dd2.setField( QString( "field" ) );
dd2.setExpressionString( QString( "b" ) );
QVERIFY( !( dd1 == dd2 ) );
QVERIFY( dd1 != dd2 );
dd2.setExpressionString( QString( "expression" ) );
dd2.setUseExpression( false );
QVERIFY( !( dd1 == dd2 ) );
QVERIFY( dd1 != dd2 );
}

void TestQgsDataDefined::xmlMethods()
{
//create a test dom element
QDomImplementation DomImplementation;
QDomDocumentType documentType =
DomImplementation.createDocumentType(
"qgis", "http://mrcc.com/qgis.dtd", "SYSTEM" );
QDomDocument doc( documentType );

QgsDataDefined dd1;
dd1.setActive( true );
dd1.setField( QString( "field" ) );
dd1.setExpressionString( QString( "expression" ) );
dd1.setUseExpression( true );
QDomElement ddElem = dd1.toXmlElement( doc, "test" );

//test reading
QgsDataDefined dd2;
QVERIFY( dd2 != dd1 );
QVERIFY( dd2.setFromXmlElement( ddElem ) );
QVERIFY( dd2 == dd1 );

//test reading from null element
QDomElement badElem;
QVERIFY( !dd2.setFromXmlElement( badElem ) );
}

QTEST_MAIN( TestQgsDataDefined )
#include "testqgsdatadefined.moc"

0 comments on commit bcc29ff

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