Skip to content

Commit 0105afc

Browse files
author
mhugent
committedOct 2, 2009
Update of existing fields in field calculator and possibility to apply only to selection
git-svn-id: http://svn.osgeo.org/qgis/trunk@11741 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 77c03ad commit 0105afc

File tree

3 files changed

+220
-150
lines changed

3 files changed

+220
-150
lines changed
 

‎src/app/qgsfieldcalculator.cpp

Lines changed: 77 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ QgsFieldCalculator::QgsFieldCalculator( QgsVectorLayer* vl ): QDialog(), mVector
3232
mOuputFieldWidthSpinBox->setValue( 10 );
3333
mOutputFieldPrecisionSpinBox->setValue( 3 );
3434

35+
mUpdateExistingFieldCheckBox->setCheckState( Qt::Checked );
36+
3537
//disable ok button until there is text for output field and expression
3638
mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
3739
}
@@ -65,43 +67,60 @@ void QgsFieldCalculator::accept()
6567

6668
mVectorLayer->beginEditCommand( "Field calculator" );
6769

68-
//create new field
69-
QgsField newField( mOutputFieldNameLineEdit->text() );
70-
if ( mOutputFieldTypeComboBox->currentText() == tr( "Double" ) )
71-
{
72-
newField.setType( QVariant::Double );
73-
}
74-
else if ( mOutputFieldTypeComboBox->currentText() == tr( "Integer" ) )
70+
int attributeId = -1; //id of the field (can be existing field or newly created one
71+
72+
//update existing field
73+
if ( mUpdateExistingFieldCheckBox->checkState() == Qt::Checked )
7574
{
76-
newField.setType( QVariant::Int );
75+
QMap<QString, int>::const_iterator fieldIt = mFieldMap.find( mExistingFieldComboBox->currentText() );
76+
if ( fieldIt != mFieldMap.end() )
77+
{
78+
attributeId = fieldIt.value();
79+
}
7780
}
78-
else if ( mOutputFieldTypeComboBox->currentText() == tr( "String" ) )
81+
//create new field
82+
else
7983
{
80-
newField.setType( QVariant::String );
81-
}
84+
//create new field
85+
QgsField newField( mOutputFieldNameLineEdit->text() );
86+
if ( mOutputFieldTypeComboBox->currentText() == tr( "Double" ) )
87+
{
88+
newField.setType( QVariant::Double );
89+
}
90+
else if ( mOutputFieldTypeComboBox->currentText() == tr( "Integer" ) )
91+
{
92+
newField.setType( QVariant::Int );
93+
}
94+
else if ( mOutputFieldTypeComboBox->currentText() == tr( "String" ) )
95+
{
96+
newField.setType( QVariant::String );
97+
}
8298

83-
newField.setLength( mOuputFieldWidthSpinBox->value() );
84-
newField.setPrecision( mOutputFieldPrecisionSpinBox->value() );
99+
newField.setLength( mOuputFieldWidthSpinBox->value() );
100+
newField.setPrecision( mOutputFieldPrecisionSpinBox->value() );
85101

102+
if ( !mVectorLayer->addAttribute( newField ) )
103+
{
104+
QMessageBox::critical( 0, tr( "Provider error" ), tr( "Could not add the new field to the provider." ) );
105+
mVectorLayer->destroyEditCommand();
106+
return;
107+
}
86108

87-
if ( !mVectorLayer->addAttribute( newField ) )
88-
{
89-
mVectorLayer->destroyEditCommand();
90-
return;
91-
}
109+
//get index of the new field
110+
const QgsFieldMap fieldList = mVectorLayer->pendingFields();
92111

93-
//get index of the new field
94-
const QgsFieldMap fieldList = mVectorLayer->pendingFields();
95-
int attributeId = -1;
96-
QgsFieldMap::const_iterator it = fieldList.constBegin();
97-
for ( ; it != fieldList.constEnd(); ++it )
98-
{
99-
if ( it.value().name() == mOutputFieldNameLineEdit->text() )
112+
QgsFieldMap::const_iterator it = fieldList.constBegin();
113+
for ( ; it != fieldList.constEnd(); ++it )
100114
{
101-
attributeId = it.key();
102-
break;
115+
if ( it.value().name() == mOutputFieldNameLineEdit->text() )
116+
{
117+
attributeId = it.key();
118+
break;
119+
}
103120
}
104121
}
122+
123+
105124
if ( attributeId == -1 )
106125
{
107126
mVectorLayer->destroyEditCommand();
@@ -112,9 +131,21 @@ void QgsFieldCalculator::accept()
112131
QgsFeature feature;
113132
bool calculationSuccess = true;
114133

134+
bool onlySelected = ( mOnlyUpdateSelectedCheckBox->checkState() == Qt::Checked );
135+
QgsFeatureIds selectedIds = mVectorLayer->selectedFeaturesIds();
136+
137+
115138
mVectorLayer->select( mVectorLayer->pendingAllAttributesList(), QgsRectangle(), false, false );
116139
while ( mVectorLayer->nextFeature( feature ) )
117140
{
141+
if ( onlySelected )
142+
{
143+
if ( !selectedIds.contains( feature.id() ) )
144+
{
145+
continue;
146+
}
147+
}
148+
118149
QgsSearchTreeValue value = searchTree->valueAgainst( mVectorLayer->pendingFields(), feature.attributeMap() );
119150
if ( value.isError() )
120151
{
@@ -158,10 +189,27 @@ void QgsFieldCalculator::populateFields()
158189
QgsFieldMap::const_iterator fieldIt = fieldMap.constBegin();
159190
for ( ; fieldIt != fieldMap.constEnd(); ++fieldIt )
160191
{
192+
161193
QString fieldName = fieldIt.value().name();
194+
195+
//insert into field list and field combo box
162196
mFieldMap.insert( fieldName, fieldIt.key() );
163197
mFieldsListWidget->addItem( fieldName );
198+
mExistingFieldComboBox->addItem( fieldName );
199+
}
200+
}
201+
202+
void QgsFieldCalculator::on_mUpdateExistingFieldCheckBox_stateChanged( int state )
203+
{
204+
if ( state == Qt::Checked )
205+
{
206+
mNewFieldGroupBox->setEnabled( false );
164207
}
208+
else
209+
{
210+
mNewFieldGroupBox->setEnabled( true );
211+
}
212+
setOkButtonState();
165213
}
166214

167215
void QgsFieldCalculator::on_mFieldsListWidget_itemDoubleClicked( QListWidgetItem * item )
@@ -343,7 +391,8 @@ void QgsFieldCalculator::getFieldValues( int limit )
343391
void QgsFieldCalculator::setOkButtonState()
344392
{
345393
bool okEnabled = true;
346-
if ( mOutputFieldNameLineEdit->text().isEmpty() || mExpressionTextEdit->toPlainText().isEmpty() )
394+
if (( mOutputFieldNameLineEdit->text().isEmpty() && mUpdateExistingFieldCheckBox->checkState() == Qt::Unchecked )\
395+
|| mExpressionTextEdit->toPlainText().isEmpty() )
347396
{
348397
okEnabled = false;
349398
}

‎src/app/qgsfieldcalculator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class QgsFieldCalculator: public QDialog, private Ui::QgsFieldCalculatorBase
3131
public slots:
3232
void accept();
3333

34+
void on_mUpdateExistingFieldCheckBox_stateChanged( int state );
3435
void on_mFieldsListWidget_itemDoubleClicked( QListWidgetItem * item );
3536
void on_mValueListWidget_itemDoubleClicked( QListWidgetItem * item );
3637
void on_mPlusPushButton_clicked();

‎src/ui/qgsfieldcalculatorbase.ui

Lines changed: 142 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,225 +1,245 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<ui version="4.0">
1+
<ui version="4.0" >
32
<class>QgsFieldCalculatorBase</class>
4-
<widget class="QDialog" name="QgsFieldCalculatorBase">
5-
<property name="geometry">
3+
<widget class="QDialog" name="QgsFieldCalculatorBase" >
4+
<property name="geometry" >
65
<rect>
76
<x>0</x>
87
<y>0</y>
9-
<width>383</width>
10-
<height>496</height>
8+
<width>615</width>
9+
<height>628</height>
1110
</rect>
1211
</property>
13-
<property name="windowTitle">
12+
<property name="windowTitle" >
1413
<string>Field calculator</string>
1514
</property>
16-
<layout class="QGridLayout" name="gridLayout_3">
17-
<item row="0" column="0">
18-
<widget class="QLabel" name="mFieldNameLabel">
19-
<property name="text">
20-
<string>Output field name:</string>
15+
<layout class="QGridLayout" name="gridLayout_5" >
16+
<item row="0" column="0" >
17+
<widget class="QCheckBox" name="mUpdateExistingFieldCheckBox" >
18+
<property name="text" >
19+
<string>Update existing field</string>
2120
</property>
2221
</widget>
2322
</item>
24-
<item row="0" column="2">
25-
<widget class="QLineEdit" name="mOutputFieldNameLineEdit"/>
23+
<item row="0" column="1" colspan="2" >
24+
<widget class="QComboBox" name="mExistingFieldComboBox" />
2625
</item>
27-
<item row="1" column="0">
28-
<widget class="QLabel" name="mOutputFieldTypeLabel">
29-
<property name="text">
30-
<string>Output field type:</string>
26+
<item row="1" column="0" colspan="3" >
27+
<widget class="QCheckBox" name="mOnlyUpdateSelectedCheckBox" >
28+
<property name="text" >
29+
<string>Only update selected features</string>
3130
</property>
3231
</widget>
3332
</item>
34-
<item row="1" column="2">
35-
<widget class="QComboBox" name="mOutputFieldTypeComboBox"/>
36-
</item>
37-
<item row="2" column="0">
38-
<widget class="QLabel" name="mOutputFieldWidthLabel">
39-
<property name="text">
40-
<string>Output field width:</string>
41-
</property>
42-
</widget>
43-
</item>
44-
<item row="2" column="1">
45-
<widget class="QSpinBox" name="mOuputFieldWidthSpinBox"/>
46-
</item>
47-
<item row="2" column="2">
48-
<widget class="QLabel" name="mOutputFieldPrecisionLabel">
49-
<property name="text">
50-
<string>Output field precision:</string>
33+
<item row="2" column="0" colspan="3" >
34+
<widget class="QGroupBox" name="mNewFieldGroupBox" >
35+
<property name="title" >
36+
<string>New field</string>
5137
</property>
38+
<layout class="QGridLayout" name="gridLayout_3" >
39+
<item row="0" column="0" >
40+
<widget class="QLabel" name="mFieldNameLabel" >
41+
<property name="text" >
42+
<string>Output field name:</string>
43+
</property>
44+
</widget>
45+
</item>
46+
<item row="0" column="1" colspan="3" >
47+
<widget class="QLineEdit" name="mOutputFieldNameLineEdit" />
48+
</item>
49+
<item row="1" column="0" >
50+
<widget class="QLabel" name="mOutputFieldTypeLabel" >
51+
<property name="text" >
52+
<string>Output field type:</string>
53+
</property>
54+
</widget>
55+
</item>
56+
<item row="1" column="1" colspan="3" >
57+
<widget class="QComboBox" name="mOutputFieldTypeComboBox" />
58+
</item>
59+
<item row="2" column="0" >
60+
<widget class="QLabel" name="mOutputFieldWidthLabel" >
61+
<property name="text" >
62+
<string>Output field width:</string>
63+
</property>
64+
</widget>
65+
</item>
66+
<item row="2" column="1" >
67+
<widget class="QSpinBox" name="mOuputFieldWidthSpinBox" />
68+
</item>
69+
<item row="2" column="2" >
70+
<widget class="QLabel" name="mOutputFieldPrecisionLabel" >
71+
<property name="text" >
72+
<string>Output field precision:</string>
73+
</property>
74+
</widget>
75+
</item>
76+
<item row="2" column="3" >
77+
<widget class="QSpinBox" name="mOutputFieldPrecisionSpinBox" />
78+
</item>
79+
</layout>
5280
</widget>
5381
</item>
54-
<item row="2" column="3">
55-
<widget class="QSpinBox" name="mOutputFieldPrecisionSpinBox"/>
56-
</item>
57-
<item row="3" column="0" colspan="2">
58-
<widget class="QGroupBox" name="mFieldsGroupBox">
59-
<property name="title">
82+
<item row="3" column="0" colspan="2" >
83+
<widget class="QGroupBox" name="mFieldsGroupBox" >
84+
<property name="title" >
6085
<string>Fields</string>
6186
</property>
62-
<widget class="QListWidget" name="mFieldsListWidget">
63-
<property name="geometry">
64-
<rect>
65-
<x>10</x>
66-
<y>20</y>
67-
<width>221</width>
68-
<height>151</height>
69-
</rect>
70-
</property>
71-
</widget>
87+
<layout class="QGridLayout" name="gridLayout_4" >
88+
<item row="0" column="0" >
89+
<widget class="QListWidget" name="mFieldsListWidget" />
90+
</item>
91+
</layout>
7292
</widget>
7393
</item>
74-
<item row="3" column="2" colspan="2">
75-
<widget class="QGroupBox" name="mValuesGroupBox">
76-
<property name="title">
94+
<item row="3" column="2" >
95+
<widget class="QGroupBox" name="mValuesGroupBox" >
96+
<property name="title" >
7797
<string>Values</string>
7898
</property>
79-
<layout class="QGridLayout" name="gridLayout_2">
80-
<item row="0" column="0" colspan="2">
81-
<widget class="QListWidget" name="mValueListWidget"/>
99+
<layout class="QGridLayout" name="gridLayout_2" >
100+
<item row="0" column="0" colspan="2" >
101+
<widget class="QListWidget" name="mValueListWidget" />
82102
</item>
83-
<item row="1" column="0">
84-
<widget class="QPushButton" name="mSamplePushButton">
85-
<property name="text">
103+
<item row="1" column="0" >
104+
<widget class="QPushButton" name="mSamplePushButton" >
105+
<property name="text" >
86106
<string>Sample</string>
87107
</property>
88108
</widget>
89109
</item>
90-
<item row="1" column="1">
91-
<widget class="QPushButton" name="mAllPushButton">
92-
<property name="text">
110+
<item row="1" column="1" >
111+
<widget class="QPushButton" name="mAllPushButton" >
112+
<property name="text" >
93113
<string>All</string>
94114
</property>
95115
</widget>
96116
</item>
97117
</layout>
98118
</widget>
99119
</item>
100-
<item row="4" column="0" colspan="4">
101-
<widget class="QGroupBox" name="mOperatorsGroupBox">
102-
<property name="title">
120+
<item row="4" column="0" colspan="3" >
121+
<widget class="QGroupBox" name="mOperatorsGroupBox" >
122+
<property name="title" >
103123
<string>Operators</string>
104124
</property>
105-
<layout class="QGridLayout" name="gridLayout">
106-
<item row="0" column="0">
107-
<widget class="QPushButton" name="mPlusPushButton">
108-
<property name="text">
125+
<layout class="QGridLayout" name="gridLayout" >
126+
<item row="0" column="0" >
127+
<widget class="QPushButton" name="mPlusPushButton" >
128+
<property name="text" >
109129
<string>+</string>
110130
</property>
111131
</widget>
112132
</item>
113-
<item row="0" column="1">
114-
<widget class="QPushButton" name="mMultiplyPushButton">
115-
<property name="text">
133+
<item row="0" column="1" >
134+
<widget class="QPushButton" name="mMultiplyPushButton" >
135+
<property name="text" >
116136
<string>*</string>
117137
</property>
118138
</widget>
119139
</item>
120-
<item row="0" column="2">
121-
<widget class="QPushButton" name="mSqrtButton">
122-
<property name="text">
140+
<item row="0" column="2" >
141+
<widget class="QPushButton" name="mSqrtButton" >
142+
<property name="text" >
123143
<string>sqrt</string>
124144
</property>
125145
</widget>
126146
</item>
127-
<item row="0" column="3">
128-
<widget class="QPushButton" name="mSinButton">
129-
<property name="text">
147+
<item row="0" column="3" >
148+
<widget class="QPushButton" name="mSinButton" >
149+
<property name="text" >
130150
<string>sin</string>
131151
</property>
132152
</widget>
133153
</item>
134-
<item row="0" column="4">
135-
<widget class="QPushButton" name="mTanButton">
136-
<property name="text">
154+
<item row="0" column="4" >
155+
<widget class="QPushButton" name="mTanButton" >
156+
<property name="text" >
137157
<string>tan</string>
138158
</property>
139159
</widget>
140160
</item>
141-
<item row="0" column="5">
142-
<widget class="QPushButton" name="mACosButton">
143-
<property name="text">
161+
<item row="0" column="5" >
162+
<widget class="QPushButton" name="mACosButton" >
163+
<property name="text" >
144164
<string>acos</string>
145165
</property>
146166
</widget>
147167
</item>
148-
<item row="0" column="6">
149-
<widget class="QPushButton" name="mOpenBracketPushButton">
150-
<property name="text">
168+
<item row="0" column="6" >
169+
<widget class="QPushButton" name="mOpenBracketPushButton" >
170+
<property name="text" >
151171
<string>(</string>
152172
</property>
153173
</widget>
154174
</item>
155-
<item row="1" column="0">
156-
<widget class="QPushButton" name="mMinusPushButton">
157-
<property name="text">
175+
<item row="1" column="0" >
176+
<widget class="QPushButton" name="mMinusPushButton" >
177+
<property name="text" >
158178
<string>-</string>
159179
</property>
160180
</widget>
161181
</item>
162-
<item row="1" column="1">
163-
<widget class="QPushButton" name="mDividePushButton">
164-
<property name="text">
182+
<item row="1" column="1" >
183+
<widget class="QPushButton" name="mDividePushButton" >
184+
<property name="text" >
165185
<string>/</string>
166186
</property>
167187
</widget>
168188
</item>
169-
<item row="1" column="2">
170-
<widget class="QPushButton" name="mExpButton">
171-
<property name="text">
189+
<item row="1" column="2" >
190+
<widget class="QPushButton" name="mExpButton" >
191+
<property name="text" >
172192
<string>^</string>
173193
</property>
174194
</widget>
175195
</item>
176-
<item row="1" column="3">
177-
<widget class="QPushButton" name="mCosButton">
178-
<property name="text">
196+
<item row="1" column="3" >
197+
<widget class="QPushButton" name="mCosButton" >
198+
<property name="text" >
179199
<string>cos</string>
180200
</property>
181201
</widget>
182202
</item>
183-
<item row="1" column="4">
184-
<widget class="QPushButton" name="mASinButton">
185-
<property name="text">
203+
<item row="1" column="4" >
204+
<widget class="QPushButton" name="mASinButton" >
205+
<property name="text" >
186206
<string>asin</string>
187207
</property>
188208
</widget>
189209
</item>
190-
<item row="1" column="5">
191-
<widget class="QPushButton" name="mATanButton">
192-
<property name="text">
210+
<item row="1" column="5" >
211+
<widget class="QPushButton" name="mATanButton" >
212+
<property name="text" >
193213
<string>atan</string>
194214
</property>
195215
</widget>
196216
</item>
197-
<item row="1" column="6">
198-
<widget class="QPushButton" name="mCloseBracketPushButton">
199-
<property name="text">
217+
<item row="1" column="6" >
218+
<widget class="QPushButton" name="mCloseBracketPushButton" >
219+
<property name="text" >
200220
<string>)</string>
201221
</property>
202222
</widget>
203223
</item>
204224
</layout>
205225
</widget>
206226
</item>
207-
<item row="5" column="0" colspan="2">
208-
<widget class="QLabel" name="mFieldCalculatorExpressionLabel">
209-
<property name="text">
227+
<item row="5" column="0" colspan="2" >
228+
<widget class="QLabel" name="mFieldCalculatorExpressionLabel" >
229+
<property name="text" >
210230
<string>Field calculator expression:</string>
211231
</property>
212232
</widget>
213233
</item>
214-
<item row="6" column="0" colspan="4">
215-
<widget class="QTextEdit" name="mExpressionTextEdit"/>
234+
<item row="6" column="0" colspan="3" >
235+
<widget class="QTextEdit" name="mExpressionTextEdit" />
216236
</item>
217-
<item row="7" column="0" colspan="2">
218-
<widget class="QDialogButtonBox" name="mButtonBox">
219-
<property name="orientation">
237+
<item row="7" column="0" colspan="2" >
238+
<widget class="QDialogButtonBox" name="mButtonBox" >
239+
<property name="orientation" >
220240
<enum>Qt::Horizontal</enum>
221241
</property>
222-
<property name="standardButtons">
242+
<property name="standardButtons" >
223243
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
224244
</property>
225245
</widget>
@@ -234,11 +254,11 @@
234254
<receiver>QgsFieldCalculatorBase</receiver>
235255
<slot>accept()</slot>
236256
<hints>
237-
<hint type="sourcelabel">
257+
<hint type="sourcelabel" >
238258
<x>248</x>
239259
<y>254</y>
240260
</hint>
241-
<hint type="destinationlabel">
261+
<hint type="destinationlabel" >
242262
<x>157</x>
243263
<y>274</y>
244264
</hint>
@@ -250,11 +270,11 @@
250270
<receiver>QgsFieldCalculatorBase</receiver>
251271
<slot>reject()</slot>
252272
<hints>
253-
<hint type="sourcelabel">
273+
<hint type="sourcelabel" >
254274
<x>316</x>
255275
<y>260</y>
256276
</hint>
257-
<hint type="destinationlabel">
277+
<hint type="destinationlabel" >
258278
<x>286</x>
259279
<y>274</y>
260280
</hint>

0 commit comments

Comments
 (0)
Please sign in to comment.