Skip to content

Commit

Permalink
Fix crash on QgsDataDefined copy (fix #12027)
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Apr 21, 2015
1 parent 8cb8755 commit 774e5d3
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 19 deletions.
21 changes: 21 additions & 0 deletions python/core/qgsdatadefined.sip
Expand Up @@ -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
Expand All @@ -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();
Expand Down
65 changes: 49 additions & 16 deletions src/core/qgsdatadefined.cpp
Expand Up @@ -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;
}

Expand All @@ -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() )
{
Expand All @@ -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() )
{
Expand All @@ -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 )
Expand All @@ -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 )
Expand Down Expand Up @@ -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;
}
30 changes: 29 additions & 1 deletion src/core/qgsdatadefined.h
Expand Up @@ -21,6 +21,7 @@

class QgsExpression;
class QgsVectorLayer;
class QgsFields;

/** \ingroup core
* \class QgsDataDefined
Expand Down Expand Up @@ -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
Expand All @@ -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; }
Expand Down Expand Up @@ -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;

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

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

};

#endif // QGSDATADEFINED_H
6 changes: 4 additions & 2 deletions tests/src/core/testqgsdatadefined.cpp
Expand Up @@ -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()
Expand Down

0 comments on commit 774e5d3

Please sign in to comment.