Skip to content

Commit c0c94cf

Browse files
committedNov 25, 2014
Fix #10727 - Add update selected button to field calc bar
1 parent d002e5d commit c0c94cf

File tree

3 files changed

+42
-18
lines changed

3 files changed

+42
-18
lines changed
 

‎src/app/qgsattributetabledialog.cpp

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ QgsAttributeTableDialog::QgsAttributeTableDialog( QgsVectorLayer *theLayer, QWid
203203
mFieldModel->setLayer( mLayer );
204204
mFieldCombo->setModel( mFieldModel );
205205
connect( mRunFieldCalc, SIGNAL( clicked() ), this, SLOT( updateFieldFromExpression() ) );
206+
connect( mRunFieldCalcSelected, SIGNAL( clicked() ), this, SLOT( updateFieldFromExpressionSelected() ) );
206207
// NW TODO Fix in 2.6 - Doesn't work with field model for some reason.
207208
// connect( mUpdateExpressionText, SIGNAL( returnPressed() ), this, SLOT( updateFieldFromExpression() ) );
208209
connect( mUpdateExpressionText, SIGNAL( fieldChanged( QString, bool ) ), this, SLOT( updateButtonStatus( QString, bool ) ) );
@@ -230,6 +231,9 @@ void QgsAttributeTableDialog::updateTitle()
230231
mRunFieldCalc->setText( tr( "Update All" ) );
231232
else
232233
mRunFieldCalc->setText( tr( "Update Filtered" ) );
234+
235+
bool enabled = mLayer->selectedFeatureCount() > 0;
236+
mRunFieldCalcSelected->setEnabled( enabled );
233237
}
234238

235239
void QgsAttributeTableDialog::updateButtonStatus( QString fieldName, bool isValid )
@@ -297,49 +301,51 @@ void QgsAttributeTableDialog::columnBoxInit()
297301
}
298302
}
299303

304+
300305
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 )
301320
{
302321
QApplication::setOverrideCursor( Qt::WaitCursor );
303322

304323
mLayer->beginEditCommand( "Field calculator" );
305324

306-
QModelIndex modelindex = mFieldModel->indexFromName( mFieldCombo->currentText() );
325+
QModelIndex modelindex = mFieldModel->indexFromName( fieldName );
307326
int fieldindex = modelindex.data( QgsFieldModel::FieldIndexRole ).toInt();
308327

309328
bool calculationSuccess = true;
310329
QString error;
311330

312331

313-
QgsExpression exp( mUpdateExpressionText->currentField() );
332+
QgsExpression exp( expression );
314333
exp.setGeomCalculator( *myDa );
315334
bool useGeometry = exp.needsGeometry();
316335

317336
QgsFeatureRequest request;
318337
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-
// }
328338

329-
bool filtered = mMainView->filterMode() != QgsAttributeTableFilterModel::ShowAll;
330339
int rownum = 1;
331340

332341
//go through all the features and change the new attributes
333-
QgsFeatureIterator fit = mLayer->getFeatures( request );
342+
QgsFeatureIterator fit = layer->getFeatures( request );
334343
QgsFeature feature;
335344
while ( fit.nextFeature( feature ) )
336345
{
337-
if ( filtered )
346+
if ( !filteredIds.isEmpty() && !filteredIds.contains( feature.id() ) )
338347
{
339-
if ( !filteredIds.contains( feature.id() ) )
340-
{
341348
continue;
342-
}
343349
}
344350

345351
exp.setCurrentRowNumber( rownum );

‎src/app/qgsattributetabledialog.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,9 @@ class APP_EXPORT QgsAttributeTableDialog : public QDialog, private Ui::QgsAttrib
185185
*/
186186
void columnBoxInit();
187187

188+
void runFieldCalculation( QgsVectorLayer* layer, QString fieldName, QString expression, QgsFeatureIds filteredIds = QgsFeatureIds());
188189
void updateFieldFromExpression();
190+
void updateFieldFromExpressionSelected();
189191

190192
private:
191193
QMenu* mMenuActions;

‎src/ui/qgsattributetabledialog.ui

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,16 @@
1414
<string>Attribute Table</string>
1515
</property>
1616
<layout class="QGridLayout" name="gridLayout">
17-
<property name="margin">
17+
<property name="leftMargin">
18+
<number>0</number>
19+
</property>
20+
<property name="topMargin">
21+
<number>0</number>
22+
</property>
23+
<property name="rightMargin">
24+
<number>0</number>
25+
</property>
26+
<property name="bottomMargin">
1827
<number>0</number>
1928
</property>
2029
<property name="spacing">
@@ -643,6 +652,13 @@
643652
</property>
644653
</widget>
645654
</item>
655+
<item>
656+
<widget class="QToolButton" name="mRunFieldCalcSelected">
657+
<property name="text">
658+
<string>Update Selected</string>
659+
</property>
660+
</widget>
661+
</item>
646662
</layout>
647663
</widget>
648664
</item>

0 commit comments

Comments
 (0)
Please sign in to comment.