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

(cherry-picked from 099a40b)
  • Loading branch information
nyalldawson committed Nov 19, 2015
1 parent 45d80f9 commit c576281
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions src/app/qgisapp.cpp
Expand Up @@ -5891,15 +5891,33 @@ void QgisApp::mergeAttributesOfSelectedFeatures()
QgsAttributes merged = d.mergedAttributes(); QgsAttributes merged = d.mergedAttributes();
QSet<int> toSkip = d.skippedAttributeIndexes(); QSet<int> toSkip = d.skippedAttributeIndexes();


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


vl->changeAttributeValue( fid, i, merged.at( i ) ); QVariant val = merged.at( i );
// convert to destination data type
if ( ! vl->pendingFields()[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->pendingFields()[i].typeName() ),
QgsMessageBar::WARNING );
}
}
else
{
vl->changeAttributeValue( fid, i, val );
}
} }
firstFeature = false;
} }


vl->endEditCommand(); vl->endEditCommand();
Expand Down Expand Up @@ -6014,7 +6032,22 @@ void QgisApp::mergeSelectedFeatures()
//create new feature //create new feature
QgsFeature newFeature; QgsFeature newFeature;
newFeature.setGeometry( unionGeom ); 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->pendingFields()[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->pendingFields()[i].typeName() ),
QgsMessageBar::WARNING );
}
attrs[i] = val;
}
newFeature.setAttributes( attrs );


QgsFeatureIds::const_iterator feature_it = featureIdsAfter.constBegin(); QgsFeatureIds::const_iterator feature_it = featureIdsAfter.constBegin();
for ( ; feature_it != featureIdsAfter.constEnd(); ++feature_it ) for ( ; feature_it != featureIdsAfter.constEnd(); ++feature_it )
Expand Down

0 comments on commit c576281

Please sign in to comment.