Skip to content

Commit

Permalink
Fix merge attributes tool sets skipped attributes to null (fix #13231)
Browse files Browse the repository at this point in the history
(cherry-picked from 0bda18b)
  • Loading branch information
nyalldawson committed Nov 19, 2015
1 parent ca127f5 commit 45d80f9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/app/qgisapp.cpp
Expand Up @@ -5888,12 +5888,16 @@ void QgisApp::mergeAttributesOfSelectedFeatures()

vl->beginEditCommand( tr( "Merged feature attributes" ) );

const QgsAttributes &merged = d.mergedAttributes();
QgsAttributes merged = d.mergedAttributes();
QSet<int> toSkip = d.skippedAttributeIndexes();

foreach ( QgsFeatureId fid, vl->selectedFeaturesIds() )
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 ) );
}
}
Expand Down
26 changes: 23 additions & 3 deletions src/app/qgsmergeattributesdialog.cpp
Expand Up @@ -585,8 +585,6 @@ QgsAttributes QgsMergeAttributesDialog::mergedAttributes() const
return QgsAttributes();
}

QgsFields fields = mVectorLayer->pendingFields();

QgsAttributes results( mTableWidget->columnCount() );
for ( int i = 0; i < mTableWidget->columnCount(); i++ )
{
Expand All @@ -603,7 +601,7 @@ QgsAttributes QgsMergeAttributesDialog::mergedAttributes() const
if ( idx >= results.count() )
results.resize( idx + 1 ); // make sure the results vector is long enough (maybe not necessary)

if ( comboBox->itemData( comboBox->currentIndex() ) != "skip" )
if ( comboBox->itemData( comboBox->currentIndex() ).toString() != "skip" )
{
results[idx] = currentItem->data( Qt::DisplayRole );
}
Expand All @@ -615,3 +613,25 @@ QgsAttributes QgsMergeAttributesDialog::mergedAttributes() const

return results;
}

QSet<int> QgsMergeAttributesDialog::skippedAttributeIndexes() const
{
QSet<int> skipped;
for ( int i = 0; i < mTableWidget->columnCount(); ++i )
{
QComboBox *comboBox = qobject_cast<QComboBox *>( mTableWidget->cellWidget( 0, i ) );
if ( !comboBox )
{
//something went wrong, better skip this attribute
skipped << i;
continue;
}

if ( comboBox->itemData( comboBox->currentIndex() ).toString() == "skip" )
{
skipped << i;
}
}

return skipped;
}
5 changes: 5 additions & 0 deletions src/app/qgsmergeattributesdialog.h
Expand Up @@ -37,6 +37,11 @@ class APP_EXPORT QgsMergeAttributesDialog: public QDialog, private Ui::QgsMergeA
~QgsMergeAttributesDialog();
QgsAttributes mergedAttributes() const;

/** Returns a list of attribute indexes which should be skipped when merging (eg, attributes
* which have been set to "skip"
*/
QSet<int> skippedAttributeIndexes() const;

private slots:
void comboValueChanged( const QString & text );
void selectedRowChanged();
Expand Down

0 comments on commit 45d80f9

Please sign in to comment.