@@ -203,6 +203,7 @@ QgsAttributeTableDialog::QgsAttributeTableDialog( QgsVectorLayer *theLayer, QWid
203
203
mFieldModel ->setLayer ( mLayer );
204
204
mFieldCombo ->setModel ( mFieldModel );
205
205
connect ( mRunFieldCalc , SIGNAL ( clicked () ), this , SLOT ( updateFieldFromExpression () ) );
206
+ connect ( mRunFieldCalcSelected , SIGNAL ( clicked () ), this , SLOT ( updateFieldFromExpressionSelected () ) );
206
207
// NW TODO Fix in 2.6 - Doesn't work with field model for some reason.
207
208
// connect( mUpdateExpressionText, SIGNAL( returnPressed() ), this, SLOT( updateFieldFromExpression() ) );
208
209
connect ( mUpdateExpressionText , SIGNAL ( fieldChanged ( QString, bool ) ), this , SLOT ( updateButtonStatus ( QString, bool ) ) );
@@ -230,6 +231,9 @@ void QgsAttributeTableDialog::updateTitle()
230
231
mRunFieldCalc ->setText ( tr ( " Update All" ) );
231
232
else
232
233
mRunFieldCalc ->setText ( tr ( " Update Filtered" ) );
234
+
235
+ bool enabled = mLayer ->selectedFeatureCount () > 0 ;
236
+ mRunFieldCalcSelected ->setEnabled ( enabled );
233
237
}
234
238
235
239
void QgsAttributeTableDialog::updateButtonStatus ( QString fieldName, bool isValid )
@@ -297,49 +301,51 @@ void QgsAttributeTableDialog::columnBoxInit()
297
301
}
298
302
}
299
303
304
+
300
305
void QgsAttributeTableDialog::updateFieldFromExpression ()
306
+ {
307
+
308
+ bool filtered = mMainView ->filterMode () != QgsAttributeTableFilterModel::ShowAll;
309
+ QgsFeatureIds filteredIds = filtered ? mMainView ->filteredFeatures () : QgsFeatureIds ();
310
+ this ->runFieldCalculation (mLayer , mFieldCombo ->currentText (), mUpdateExpressionText ->currentField (), filteredIds);
311
+ }
312
+
313
+ void QgsAttributeTableDialog::updateFieldFromExpressionSelected ()
314
+ {
315
+ QgsFeatureIds filteredIds = mLayer ->selectedFeaturesIds ();
316
+ this ->runFieldCalculation (mLayer , mFieldCombo ->currentText (), mUpdateExpressionText ->currentField (), filteredIds);
317
+ }
318
+
319
+ void QgsAttributeTableDialog::runFieldCalculation ( QgsVectorLayer* layer, QString fieldName, QString expression, QgsFeatureIds filteredIds )
301
320
{
302
321
QApplication::setOverrideCursor ( Qt::WaitCursor );
303
322
304
323
mLayer ->beginEditCommand ( " Field calculator" );
305
324
306
- QModelIndex modelindex = mFieldModel ->indexFromName ( mFieldCombo -> currentText () );
325
+ QModelIndex modelindex = mFieldModel ->indexFromName ( fieldName );
307
326
int fieldindex = modelindex.data ( QgsFieldModel::FieldIndexRole ).toInt ();
308
327
309
328
bool calculationSuccess = true ;
310
329
QString error;
311
330
312
331
313
- QgsExpression exp ( mUpdateExpressionText -> currentField () );
332
+ QgsExpression exp ( expression );
314
333
exp.setGeomCalculator ( *myDa );
315
334
bool useGeometry = exp.needsGeometry ();
316
335
317
336
QgsFeatureRequest request;
318
337
request.setFlags ( useGeometry ? QgsFeatureRequest::NoFlags : QgsFeatureRequest::NoGeometry );
319
- QgsFeatureIds filteredIds = mMainView ->filteredFeatures ();
320
- QgsDebugMsg ( QString ( filteredIds.size () ) );
321
-
322
- // This would be nice but doesn't work on all providers
323
- // if ( mMainView->filterMode() != QgsAttributeTableFilterModel::ShowAll )
324
- // {
325
- // QgsDebugMsg( " Updating only selected features " );
326
- // request.setFilterFids( mMainView->filteredFeatures() );
327
- // }
328
338
329
- bool filtered = mMainView ->filterMode () != QgsAttributeTableFilterModel::ShowAll;
330
339
int rownum = 1 ;
331
340
332
341
// go through all the features and change the new attributes
333
- QgsFeatureIterator fit = mLayer ->getFeatures ( request );
342
+ QgsFeatureIterator fit = layer ->getFeatures ( request );
334
343
QgsFeature feature;
335
344
while ( fit.nextFeature ( feature ) )
336
345
{
337
- if ( filtered )
346
+ if ( !filteredIds. isEmpty () && !filteredIds. contains ( feature. id () ) )
338
347
{
339
- if ( !filteredIds.contains ( feature.id () ) )
340
- {
341
348
continue ;
342
- }
343
349
}
344
350
345
351
exp.setCurrentRowNumber ( rownum );
0 commit comments