1414 ***************************************************************************/
1515
1616#include " qgsfieldcalculator.h"
17- #include " qgssearchtreenode.h"
18- #include " qgssearchstring.h"
17+ #include " qgsexpression.h"
1918#include " qgsvectordataprovider.h"
2019#include " qgsvectorlayer.h"
2120
@@ -81,19 +80,18 @@ void QgsFieldCalculator::accept()
8180 {
8281 QString calcString = mExpressionTextEdit ->toPlainText ();
8382
84- // create QgsSearchString
85- QgsSearchString searchString ;
86- if ( !searchString. setString ( calcString ) )
83+ // create QgsExpression
84+ QgsExpression exp ( calcString ) ;
85+ if ( exp. hasParserError ( ) )
8786 {
8887 // expression not valid
89- QMessageBox::critical ( 0 , tr ( " Syntax error" ), tr ( QString ( " Invalid expression syntax. The error message of the parser is: '" + searchString. parserErrorMsg () + " '" ).toLocal8Bit ().data () ) );
88+ QMessageBox::critical ( 0 , tr ( " Syntax error" ), tr ( QString ( " Invalid expression syntax. The error message of the parser is: '" + exp. parserErrorString () + " '" ).toLocal8Bit ().data () ) );
9089 return ;
9190 }
9291
93- // get QgsSearchTreeNode
94- QgsSearchTreeNode* searchTree = searchString.tree ();
95- if ( !searchTree )
92+ if ( ! exp.prepare ( mVectorLayer ->pendingFields () ) )
9693 {
94+ QMessageBox::critical ( 0 , tr ( " Evaluation error" ), exp.evalErrorString () );
9795 return ;
9896 }
9997
@@ -156,7 +154,7 @@ void QgsFieldCalculator::accept()
156154 // block layerModified signals (that would trigger table update)
157155 mVectorLayer ->blockSignals ( true );
158156
159- bool useGeometry = searchTree-> needsGeometry ();
157+ bool useGeometry = exp. needsGeometry ();
160158 int rownum = 1 ;
161159
162160 mVectorLayer ->select ( mVectorLayer ->pendingAllAttributesList (), QgsRectangle (), useGeometry, false );
@@ -170,52 +168,18 @@ void QgsFieldCalculator::accept()
170168 }
171169 }
172170
173- searchTree-> setCurrentRowNumber ( rownum );
171+ exp. setCurrentRowNumber ( rownum );
174172
175- QgsSearchTreeValue value;
176- searchTree->getValue ( value, searchTree, mVectorLayer ->pendingFields (), feature );
177- if ( value.isError () )
173+ QVariant value = exp.evaluate ( &feature );
174+ if ( exp.hasEvalError () )
178175 {
179- // insert NULL value for this feature and continue the calculation
180- if ( searchTree->errorMsg () == QObject::tr ( " Division by zero." ) )
181- {
182- mVectorLayer ->changeAttributeValue ( feature.id (), mAttributeId , QVariant (), false );
183- }
184- else
185- {
186- calculationSuccess = false ;
187- error = searchTree->errorMsg ();
188- break ;
189- }
190- }
191- else if ( value.isNumeric () )
192- {
193- const QgsField &f = mVectorLayer ->pendingFields ()[ mAttributeId ];
194- QVariant v;
195-
196- if ( f.type () == QVariant::Double && f.precision () > 0 )
197- {
198- v = QString::number ( value.number (), ' f' , f.precision () );
199- }
200- else if ( f.type () == QVariant::Double && f.precision () == 0 )
201- {
202- v = QString::number ( qRound ( value.number () ) );
203- }
204- else
205- {
206- v = value.number ();
207- }
208-
209- v.convert ( f.type () );
210- mVectorLayer ->changeAttributeValue ( feature.id (), mAttributeId , v, false );
211- }
212- else if ( value.isNull () )
213- {
214- mVectorLayer ->changeAttributeValue ( feature.id (), mAttributeId , QVariant (), false );
176+ calculationSuccess = false ;
177+ error = exp.evalErrorString ();
178+ break ;
215179 }
216180 else
217181 {
218- mVectorLayer ->changeAttributeValue ( feature.id (), mAttributeId , value. string () , false );
182+ mVectorLayer ->changeAttributeValue ( feature.id (), mAttributeId , value, false );
219183 }
220184
221185 rownum++;
@@ -385,17 +349,17 @@ void QgsFieldCalculator::on_mCloseBracketPushButton_clicked()
385349
386350void QgsFieldCalculator::on_mToRealButton_clicked ()
387351{
388- mExpressionTextEdit ->insertPlainText ( " to real ( " );
352+ mExpressionTextEdit ->insertPlainText ( " toreal ( " );
389353}
390354
391355void QgsFieldCalculator::on_mToIntButton_clicked ()
392356{
393- mExpressionTextEdit ->insertPlainText ( " to int ( " );
357+ mExpressionTextEdit ->insertPlainText ( " toint ( " );
394358}
395359
396360void QgsFieldCalculator::on_mToStringButton_clicked ()
397361{
398- mExpressionTextEdit ->insertPlainText ( " to string ( " );
362+ mExpressionTextEdit ->insertPlainText ( " tostring ( " );
399363}
400364
401365void QgsFieldCalculator::on_mLengthButton_clicked ()
0 commit comments