Skip to content
Permalink
Browse files

Fix crash on QgsDataDefined copy (fix #12027)

  • Loading branch information
nyalldawson committed Apr 21, 2015
1 parent 8cb8755 commit 774e5d3012a60c4dadafadd95017f085d72abe80
Showing with 103 additions and 19 deletions.
  1. +21 −0 python/core/qgsdatadefined.sip
  2. +49 −16 src/core/qgsdatadefined.cpp
  3. +29 −1 src/core/qgsdatadefined.h
  4. +4 −2 tests/src/core/testqgsdatadefined.cpp
@@ -30,6 +30,12 @@ class QgsDataDefined
*/
QgsDataDefined( const QgsExpression * expression );

/**
* Copy constructor. Note that copies of data defined objects with expressions
* will not be prepared.
*/
QgsDataDefined( const QgsDataDefined& other );

~QgsDataDefined();

/**Returns whether the data defined container is set to all the default
@@ -54,7 +60,22 @@ class QgsDataDefined
//void setExpressionParams( QMap<QString, QVariant> params );
void insertExpressionParam( QString key, QVariant param );

/** Prepares the expression using a vector layer
* @param layer vector layer
* @returns true if expression was successfully prepared
*/
bool prepareExpression( QgsVectorLayer* layer );

/** Prepares the expression using a fields collection
* @param fields
* @returns true if expression was successfully prepared
* @note added in QGIS 2.9
*/
bool prepareExpression( const QgsFields &fields );

/** Returns whether the data defined object's expression is prepared
* @returns true if expression is prepared
*/
bool expressionIsPrepared() const;

QgsExpression* expression();
@@ -43,9 +43,20 @@ QgsDataDefined::QgsDataDefined( const QgsExpression * expression )
mExpressionPrepared = false;
}

QgsDataDefined::QgsDataDefined( const QgsDataDefined &other )
: mExpression( 0 )
, mActive( other.isActive() )
, mUseExpression( other.useExpression() )
, mExpressionString( other.expressionString() )
, mField( other.field() )
, mExpressionParams( other.expressionParams() )
, mExpressionPrepared( false )
{

}

QgsDataDefined::~QgsDataDefined()
{
mExpressionParams.clear();
delete mExpression;
}

@@ -61,6 +72,20 @@ void QgsDataDefined::setExpressionString( const QString &expr )
}

bool QgsDataDefined::prepareExpression( QgsVectorLayer* layer )
{
if ( layer )
{
return prepareExpression( layer->pendingFields() );
}
else
{
//preparing expression without a layer set, so pass empty field list
QgsFields empty;
return prepareExpression( empty );
}
}

bool QgsDataDefined::prepareExpression( const QgsFields &fields )
{
if ( !mUseExpression || mExpressionString.isEmpty() )
{
@@ -86,16 +111,7 @@ bool QgsDataDefined::prepareExpression( QgsVectorLayer* layer )
}
}

if ( layer )
{
mExpression->prepare( layer->pendingFields() );
}
else
{
//preparing expression without a layer set, so pass empty field list
QgsFields empty;
mExpression->prepare( empty );
}
mExpression->prepare( fields );

if ( mExpression->hasEvalError() )
{
@@ -104,16 +120,16 @@ bool QgsDataDefined::prepareExpression( QgsVectorLayer* layer )
}

mExpressionPrepared = true;
mExprRefColmuns = mExpression->referencedColumns();
mExprRefColumns = mExpression->referencedColumns();

return true;
}

QStringList QgsDataDefined::referencedColumns( QgsVectorLayer* layer )
{
if ( !mExprRefColmuns.isEmpty() )
if ( !mExprRefColumns.isEmpty() )
{
return mExprRefColmuns;
return mExprRefColumns;
}

if ( mUseExpression )
@@ -125,10 +141,10 @@ QStringList QgsDataDefined::referencedColumns( QgsVectorLayer* layer )
}
else if ( !mField.isEmpty() )
{
mExprRefColmuns << mField;
mExprRefColumns << mField;
}

return mExprRefColmuns;
return mExprRefColumns;
}

void QgsDataDefined::insertExpressionParam( QString key, QVariant param )
@@ -181,3 +197,20 @@ bool QgsDataDefined::operator!=( const QgsDataDefined &other ) const
{
return !( *this == other );
}

QgsDataDefined &QgsDataDefined::operator=( const QgsDataDefined & rhs )
{
if ( &rhs == this )
return *this;

delete mExpression;
mExpression = 0;
mActive = rhs.isActive();
mUseExpression = rhs.useExpression();
mExpressionString = rhs.expressionString();
mField = rhs.field();
mExpressionParams = rhs.expressionParams();
mExpressionPrepared = false;
mExprRefColumns.clear();
return *this;
}
@@ -21,6 +21,7 @@

class QgsExpression;
class QgsVectorLayer;
class QgsFields;

/** \ingroup core
* \class QgsDataDefined
@@ -51,6 +52,12 @@ class CORE_EXPORT QgsDataDefined
*/
explicit QgsDataDefined( const QgsExpression * expression );

/**
* Copy constructor. Note that copies of data defined objects with expressions
* will not be prepared.
*/
QgsDataDefined( const QgsDataDefined& other );

~QgsDataDefined();

/**Returns whether the data defined container is set to all the default
@@ -75,7 +82,22 @@ class CORE_EXPORT QgsDataDefined
void setExpressionParams( QMap<QString, QVariant> params ) { mExpressionParams = params; }
void insertExpressionParam( QString key, QVariant param );

/** Prepares the expression using a vector layer
* @param layer vector layer
* @returns true if expression was successfully prepared
*/
bool prepareExpression( QgsVectorLayer* layer );

/** Prepares the expression using a fields collection
* @param fields
* @returns true if expression was successfully prepared
* @note added in QGIS 2.9
*/
bool prepareExpression( const QgsFields &fields );

/** Returns whether the data defined object's expression is prepared
* @returns true if expression is prepared
*/
bool expressionIsPrepared() const { return mExpressionPrepared; }

QgsExpression* expression() { return mExpression; }
@@ -108,6 +130,11 @@ class CORE_EXPORT QgsDataDefined
bool operator==( const QgsDataDefined &other ) const;
bool operator!=( const QgsDataDefined &other ) const;

/** Assignment operator. Note that after assignment the data defined
* object's expression will not be prepared.
*/
QgsDataDefined& operator=( QgsDataDefined const & rhs );

private:
QgsExpression* mExpression;

@@ -118,7 +145,8 @@ class CORE_EXPORT QgsDataDefined

QMap<QString, QVariant> mExpressionParams;
bool mExpressionPrepared;
QStringList mExprRefColmuns;
QStringList mExprRefColumns;

};

#endif // QGSDATADEFINED_H
@@ -75,10 +75,12 @@ void TestQgsDataDefined::create()
void TestQgsDataDefined::copy()
{
QgsDataDefined dd( true, true, QString( "sqrt(2)" ), QString( "field" ) );
dd.prepareExpression(NULL);
dd.prepareExpression( NULL );
QgsDataDefined cpy( dd );
QVERIFY( cpy == dd );
QgsDataDefined assigned;
assigned = cpy;
assigned = dd;
QVERIFY( assigned == dd );
}

void TestQgsDataDefined::gettersSetters()

0 comments on commit 774e5d3

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