Skip to content

Commit b160f10

Browse files
committedAug 10, 2016
QgsExpression::setGeomCalculator now takes a pointer
This allows the calculator to be cleared
1 parent cd5f813 commit b160f10

File tree

8 files changed

+21
-15
lines changed

8 files changed

+21
-15
lines changed
 

‎python/core/qgsexpression.sip

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,11 @@ class QgsExpression
9999
* (used by $length, $area and $perimeter functions only). By default, no geometry
100100
* calculator is set and all distance and area calculations are performed using simple
101101
* cartesian methods (ie no ellipsoidal calculations).
102+
* @param calc geometry calculator. Ownership is not transferred. Set to a nullptr to force
103+
* cartesian calculations.
102104
* @see geomCalculator()
103105
*/
104-
//TODO QGIS 3.0 change calc to a pointer, so that calculator can be cleared by passing nullptr
105-
void setGeomCalculator( const QgsDistanceArea &calc );
106+
void setGeomCalculator( const QgsDistanceArea* calc );
106107

107108
/** Returns the desired distance units for calculations involving geomCalculator(), eg "$length" and "$perimeter".
108109
* @note distances are only converted when a geomCalculator() has been set

‎src/app/qgsattributetabledialog.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ void QgsAttributeTableDialog::runFieldCalculation( QgsVectorLayer* layer, const
443443
QString error;
444444

445445
QgsExpression exp( expression );
446-
exp.setGeomCalculator( *myDa );
446+
exp.setGeomCalculator( myDa );
447447
exp.setDistanceUnits( QgsProject::instance()->distanceUnits() );
448448
exp.setAreaUnits( QgsProject::instance()->areaUnits() );
449449
bool useGeometry = exp.needsGeometry();
@@ -945,7 +945,7 @@ void QgsAttributeTableDialog::setFilterExpression( const QString& filterString,
945945

946946
QApplication::setOverrideCursor( Qt::WaitCursor );
947947

948-
filterExpression.setGeomCalculator( myDa );
948+
filterExpression.setGeomCalculator( &myDa );
949949
filterExpression.setDistanceUnits( QgsProject::instance()->distanceUnits() );
950950
filterExpression.setAreaUnits( QgsProject::instance()->areaUnits() );
951951
QgsFeatureRequest request( mMainView->masterModel()->request() );

‎src/app/qgsfieldcalculator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ void QgsFieldCalculator::accept()
164164

165165
QString calcString = builder->expressionText();
166166
QgsExpression exp( calcString );
167-
exp.setGeomCalculator( myDa );
167+
exp.setGeomCalculator( &myDa );
168168
exp.setDistanceUnits( QgsProject::instance()->distanceUnits() );
169169
exp.setAreaUnits( QgsProject::instance()->areaUnits() );
170170

‎src/core/qgsexpression.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3500,9 +3500,13 @@ void QgsExpression::detach()
35003500
}
35013501
}
35023502

3503-
void QgsExpression::setGeomCalculator( const QgsDistanceArea &calc )
3503+
void QgsExpression::setGeomCalculator( const QgsDistanceArea *calc )
35043504
{
3505-
d->mCalc = QSharedPointer<QgsDistanceArea>( new QgsDistanceArea( calc ) );
3505+
detach();
3506+
if ( calc )
3507+
d->mCalc = QSharedPointer<QgsDistanceArea>( new QgsDistanceArea( *calc ) );
3508+
else
3509+
d->mCalc.clear();
35063510
}
35073511

35083512
bool QgsExpression::prepare( const QgsExpressionContext *context )
@@ -3633,7 +3637,7 @@ QString QgsExpression::replaceExpressionText( const QString &action, const QgsEx
36333637
if ( distanceArea )
36343638
{
36353639
//if QgsDistanceArea specified for area/distance conversion, use it
3636-
exp.setGeomCalculator( *distanceArea );
3640+
exp.setGeomCalculator( distanceArea );
36373641
}
36383642

36393643
QVariant result = exp.evaluate( context );

‎src/core/qgsexpression.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,11 @@ class CORE_EXPORT QgsExpression
219219
* (used by $length, $area and $perimeter functions only). By default, no geometry
220220
* calculator is set and all distance and area calculations are performed using simple
221221
* cartesian methods (ie no ellipsoidal calculations).
222+
* @param calc geometry calculator. Ownership is not transferred. Set to a nullptr to force
223+
* cartesian calculations.
222224
* @see geomCalculator()
223225
*/
224-
//TODO QGIS 3.0 change calc to a pointer, so that calculator can be cleared by passing nullptr
225-
void setGeomCalculator( const QgsDistanceArea &calc );
226+
void setGeomCalculator( const QgsDistanceArea* calc );
226227

227228
/** Returns the desired distance units for calculations involving geomCalculator(), eg "$length" and "$perimeter".
228229
* @note distances are only converted when a geomCalculator() has been set

‎src/core/qgsvectorlayerfeatureiterator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ void QgsVectorLayerFeatureIterator::prepareExpression( int fieldIdx )
529529
da.setSourceCrs( mSource->mCrsId );
530530
da.setEllipsoidalMode( true );
531531
da.setEllipsoid( QgsProject::instance()->readEntry( "Measure", "/Ellipsoid", GEO_NONE ) );
532-
exp->setGeomCalculator( da );
532+
exp->setGeomCalculator( &da );
533533
exp->setDistanceUnits( QgsProject::instance()->distanceUnits() );
534534
exp->setAreaUnits( QgsProject::instance()->areaUnits() );
535535

‎src/gui/qgsexpressionbuilderwidget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ void QgsExpressionBuilderWidget::on_txtExpressionString_textChanged()
540540
if ( mLayer )
541541
{
542542
// Only set calculator if we have layer, else use default.
543-
exp.setGeomCalculator( mDa );
543+
exp.setGeomCalculator( &mDa );
544544

545545
if ( !mFeature.isValid() )
546546
{

‎tests/src/core/testqgsexpression.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,7 +1685,7 @@ class TestQgsExpression: public QObject
16851685

16861686
// test area with geomCalculator
16871687
QgsExpression expArea2( "$area" );
1688-
expArea2.setGeomCalculator( da );
1688+
expArea2.setGeomCalculator( &da );
16891689
vArea = expArea2.evaluate( &context );
16901690
expected = 1009089817.0;
16911691
QVERIFY( qgsDoubleNear( vArea.toDouble(), expected, 1.0 ) );
@@ -1716,7 +1716,7 @@ class TestQgsExpression: public QObject
17161716

17171717
// test perimeter with geomCalculator
17181718
QgsExpression expPerimeter2( "$perimeter" );
1719-
expPerimeter2.setGeomCalculator( da );
1719+
expPerimeter2.setGeomCalculator( &da );
17201720
vPerimeter = expPerimeter2.evaluate( &context );
17211721
expected = 128289.074;
17221722
QVERIFY( qgsDoubleNear( vPerimeter.toDouble(), expected, 0.001 ) );
@@ -1753,7 +1753,7 @@ class TestQgsExpression: public QObject
17531753

17541754
// test length with geomCalculator
17551755
QgsExpression expLength2( "$length" );
1756-
expLength2.setGeomCalculator( da );
1756+
expLength2.setGeomCalculator( &da );
17571757
vLength = expLength2.evaluate( &context );
17581758
expected = 26932.156;
17591759
QVERIFY( qgsDoubleNear( vLength.toDouble(), expected, 0.001 ) );

0 commit comments

Comments
 (0)
Please sign in to comment.