14
14
***************************************************************************/
15
15
16
16
#include " qgsfieldcalculator.h"
17
- #include " qgssearchtreenode.h"
18
- #include " qgssearchstring.h"
17
+ #include " qgsexpression.h"
19
18
#include " qgsvectordataprovider.h"
20
19
#include " qgsvectorlayer.h"
21
20
@@ -81,19 +80,18 @@ void QgsFieldCalculator::accept()
81
80
{
82
81
QString calcString = mExpressionTextEdit ->toPlainText ();
83
82
84
- // create QgsSearchString
85
- QgsSearchString searchString ;
86
- if ( !searchString. setString ( calcString ) )
83
+ // create QgsExpression
84
+ QgsExpression exp ( calcString ) ;
85
+ if ( exp . hasParserError ( ) )
87
86
{
88
87
// 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 () ) );
90
89
return ;
91
90
}
92
91
93
- // get QgsSearchTreeNode
94
- QgsSearchTreeNode* searchTree = searchString.tree ();
95
- if ( !searchTree )
92
+ if ( ! exp .prepare ( mVectorLayer ->pendingFields () ) )
96
93
{
94
+ QMessageBox::critical ( 0 , tr ( " Evaluation error" ), exp .evalErrorString () );
97
95
return ;
98
96
}
99
97
@@ -156,7 +154,7 @@ void QgsFieldCalculator::accept()
156
154
// block layerModified signals (that would trigger table update)
157
155
mVectorLayer ->blockSignals ( true );
158
156
159
- bool useGeometry = searchTree-> needsGeometry ();
157
+ bool useGeometry = exp . needsGeometry ();
160
158
int rownum = 1 ;
161
159
162
160
mVectorLayer ->select ( mVectorLayer ->pendingAllAttributesList (), QgsRectangle (), useGeometry, false );
@@ -170,52 +168,18 @@ void QgsFieldCalculator::accept()
170
168
}
171
169
}
172
170
173
- searchTree-> setCurrentRowNumber ( rownum );
171
+ exp . setCurrentRowNumber ( rownum );
174
172
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 () )
178
175
{
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 ;
215
179
}
216
180
else
217
181
{
218
- mVectorLayer ->changeAttributeValue ( feature.id (), mAttributeId , value. string () , false );
182
+ mVectorLayer ->changeAttributeValue ( feature.id (), mAttributeId , value, false );
219
183
}
220
184
221
185
rownum++;
@@ -385,17 +349,17 @@ void QgsFieldCalculator::on_mCloseBracketPushButton_clicked()
385
349
386
350
void QgsFieldCalculator::on_mToRealButton_clicked ()
387
351
{
388
- mExpressionTextEdit ->insertPlainText ( " to real ( " );
352
+ mExpressionTextEdit ->insertPlainText ( " toreal ( " );
389
353
}
390
354
391
355
void QgsFieldCalculator::on_mToIntButton_clicked ()
392
356
{
393
- mExpressionTextEdit ->insertPlainText ( " to int ( " );
357
+ mExpressionTextEdit ->insertPlainText ( " toint ( " );
394
358
}
395
359
396
360
void QgsFieldCalculator::on_mToStringButton_clicked ()
397
361
{
398
- mExpressionTextEdit ->insertPlainText ( " to string ( " );
362
+ mExpressionTextEdit ->insertPlainText ( " tostring ( " );
399
363
}
400
364
401
365
void QgsFieldCalculator::on_mLengthButton_clicked ()
0 commit comments