20
20
#include " qgsvectorlayer.h"
21
21
#include < QMessageBox>
22
22
23
- QgsFieldCalculator::QgsFieldCalculator ( QgsVectorLayer* vl ): QDialog(), mVectorLayer( vl )
23
+ QgsFieldCalculator::QgsFieldCalculator ( QgsVectorLayer* vl )
24
+ : QDialog()
25
+ , mVectorLayer( vl )
26
+ , mAttributeId( -1 )
24
27
{
25
28
setupUi ( this );
26
29
@@ -92,15 +95,13 @@ void QgsFieldCalculator::accept()
92
95
93
96
mVectorLayer ->beginEditCommand ( " Field calculator" );
94
97
95
- int attributeId = -1 ; // id of the field (can be existing field or newly created one
96
-
97
98
// update existing field
98
99
if ( mUpdateExistingFieldCheckBox ->checkState () == Qt::Checked )
99
100
{
100
101
QMap<QString, int >::const_iterator fieldIt = mFieldMap .find ( mExistingFieldComboBox ->currentText () );
101
102
if ( fieldIt != mFieldMap .end () )
102
103
{
103
- attributeId = fieldIt.value ();
104
+ mAttributeId = fieldIt.value ();
104
105
}
105
106
}
106
107
// create new field
@@ -128,14 +129,13 @@ void QgsFieldCalculator::accept()
128
129
{
129
130
if ( it.value ().name () == mOutputFieldNameLineEdit ->text () )
130
131
{
131
- attributeId = it.key ();
132
+ mAttributeId = it.key ();
132
133
break ;
133
134
}
134
135
}
135
136
}
136
137
137
-
138
- if ( attributeId == -1 )
138
+ if ( mAttributeId == -1 )
139
139
{
140
140
mVectorLayer ->destroyEditCommand ();
141
141
return ;
@@ -172,9 +172,9 @@ void QgsFieldCalculator::accept()
172
172
if ( value.isError () )
173
173
{
174
174
// insert NULL value for this feature and continue the calculation
175
- if ( searchTree->errorMsg () == QObject::tr ( " Division by zero." ) )
175
+ if ( searchTree->errorMsg () == QObject::tr ( " Division by zero." ) )
176
176
{
177
- mVectorLayer ->changeAttributeValue ( feature.id (), attributeId , QVariant (), false );
177
+ mVectorLayer ->changeAttributeValue ( feature.id (), mAttributeId , QVariant (), false );
178
178
}
179
179
else
180
180
{
@@ -184,11 +184,15 @@ void QgsFieldCalculator::accept()
184
184
}
185
185
else if ( value.isNumeric () )
186
186
{
187
- mVectorLayer ->changeAttributeValue ( feature.id (), attributeId, value.number (), false );
187
+ mVectorLayer ->changeAttributeValue ( feature.id (), mAttributeId , value.number (), false );
188
+ }
189
+ else if ( value.isNull () )
190
+ {
191
+ mVectorLayer ->changeAttributeValue ( feature.id (), mAttributeId , QVariant (), false );
188
192
}
189
193
else
190
194
{
191
- mVectorLayer ->changeAttributeValue ( feature.id (), attributeId , value.string (), false );
195
+ mVectorLayer ->changeAttributeValue ( feature.id (), mAttributeId , value.string (), false );
192
196
}
193
197
194
198
rownum++;
@@ -499,7 +503,7 @@ void QgsFieldCalculator::setOkButtonState()
499
503
}
500
504
501
505
502
- void QgsFieldCalculator::on_mFieldsListWidget_currentItemChanged (QListWidgetItem * current, QListWidgetItem * previous )
506
+ void QgsFieldCalculator::on_mFieldsListWidget_currentItemChanged ( QListWidgetItem * current, QListWidgetItem * previous )
503
507
{
504
508
getFieldValues ( 25 );
505
509
}
0 commit comments