Skip to content

Commit

Permalink
Fix merge attributes/features tool resets values to null for int fields
Browse files Browse the repository at this point in the history
Also add a warning if merged attribute value is not compatible with
field type.

Fix #12842
  • Loading branch information
nyalldawson committed Oct 20, 2015
1 parent 1c76b93 commit 099a40b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
37 changes: 35 additions & 2 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6154,15 +6154,33 @@ void QgisApp::mergeAttributesOfSelectedFeatures()
QgsAttributes merged = d.mergedAttributes();
QSet<int> toSkip = d.skippedAttributeIndexes();

bool firstFeature = true;
Q_FOREACH ( QgsFeatureId fid, vl->selectedFeaturesIds() )
{
for ( int i = 0; i < merged.count(); ++i )
{
if ( toSkip.contains( i ) )
continue;

vl->changeAttributeValue( fid, i, merged.at( i ) );
QVariant val = merged.at( i );
// convert to destination data type
if ( ! vl->fields()[i].convertCompatible( val ) )
{
if ( firstFeature )
{
//only warn on first feature
messageBar()->pushMessage(
tr( "Invalid result" ),
tr( "Could not store value '%1' in field of type %2" ).arg( merged.at( i ).toString(), vl->fields()[i].typeName() ),
QgsMessageBar::WARNING );
}
}
else
{
vl->changeAttributeValue( fid, i, val );
}
}
firstFeature = false;
}

vl->endEditCommand();
Expand Down Expand Up @@ -6277,7 +6295,22 @@ void QgisApp::mergeSelectedFeatures()
//create new feature
QgsFeature newFeature;
newFeature.setGeometry( unionGeom );
newFeature.setAttributes( d.mergedAttributes() );

QgsAttributes attrs = d.mergedAttributes();
for ( int i = 0; i < attrs.count(); ++i )
{
QVariant val = attrs.at( i );
// convert to destination data type
if ( ! vl->fields()[i].convertCompatible( val ) )
{
messageBar()->pushMessage(
tr( "Invalid result" ),
tr( "Could not store value '%1' in field of type %2" ).arg( attrs.at( i ).toString(), vl->fields()[i].typeName() ),
QgsMessageBar::WARNING );
}
attrs[i] = val;
}
newFeature.setAttributes( attrs );

QgsFeatureIds::const_iterator feature_it = featureIdsAfter.constBegin();
for ( ; feature_it != featureIdsAfter.constEnd(); ++feature_it )
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgisappinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ class APP_EXPORT QgisAppInterface : public QgisInterface
void removeDockWidget( QDockWidget * dockwidget ) override;

//! return CAD dock widget
QgsAdvancedDigitizingDockWidget *cadDockWidget();
QgsAdvancedDigitizingDockWidget *cadDockWidget() override;

/** Show layer properties dialog for layer
* @param l layer to show properties table for
Expand Down

0 comments on commit 099a40b

Please sign in to comment.