Skip to content

Commit

Permalink
Fix merge feature attrs: use the correct formatter for data
Browse files Browse the repository at this point in the history
and use actual data for calculations and to return values

Fixes #32208
  • Loading branch information
elpaso authored and nyalldawson committed Oct 25, 2019
1 parent e3e9d37 commit 221cfc3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
43 changes: 31 additions & 12 deletions src/app/qgsmergeattributesdialog.cpp
Expand Up @@ -114,7 +114,7 @@ void QgsMergeAttributesDialog::createTableWidgetContents()
for ( int idx = 0; idx < mFields.count(); ++idx )
{
const QgsEditorWidgetSetup setup = QgsGui::editorWidgetRegistry()->findBest( mVectorLayer, mFields.at( idx ).name() );
if ( setup.type() == QLatin1String( "Hidden" ) || setup.type() == QLatin1String( "Immutable" ) )
if ( setup.type() == QStringLiteral( "Hidden" ) || setup.type() == QStringLiteral( "Immutable" ) )
{
mHiddenAttributes.insert( idx );
continue;
Expand Down Expand Up @@ -153,6 +153,7 @@ void QgsMergeAttributesDialog::createTableWidgetContents()
QString stringVal = formatter->representValue( mVectorLayer, idx, setup.config(), QVariant(), attrs.at( idx ) );

QTableWidgetItem *attributeValItem = new QTableWidgetItem( stringVal );
attributeValItem->setData( Qt::UserRole, attrs.at( idx ) );
attributeValItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
mTableWidget->setItem( i + 1, j, attributeValItem );
}
Expand Down Expand Up @@ -319,20 +320,20 @@ void QgsMergeAttributesDialog::refreshMergedValue( int col )
return;
}

int fieldIdx = mTableWidget->horizontalHeaderItem( col )->data( FieldIndex ).toInt();
const int fieldIdx = mTableWidget->horizontalHeaderItem( col )->data( FieldIndex ).toInt();

//evaluate behavior (feature value or min / max / mean )
QString mergeBehaviorString = comboBox->currentData().toString();
const QString mergeBehaviorString = comboBox->currentData().toString();
QVariant mergeResult; // result to show in the merge result field
if ( mergeBehaviorString == QLatin1String( "concat" ) )
if ( mergeBehaviorString == QStringLiteral( "concat" ) )
{
mergeResult = concatenationAttribute( col );
}
else if ( mergeBehaviorString == QLatin1String( "skip" ) )
else if ( mergeBehaviorString == QStringLiteral( "skip" ) )
{
mergeResult = tr( "Skipped" );
}
else if ( mergeBehaviorString == QLatin1String( "manual" ) )
else if ( mergeBehaviorString == QStringLiteral( "manual" ) )
{
return; //nothing to do
}
Expand All @@ -345,14 +346,30 @@ void QgsMergeAttributesDialog::refreshMergedValue( int col )
else
{
//numerical statistic
QgsStatisticalSummary::Statistic stat = ( QgsStatisticalSummary::Statistic )( comboBox->currentData().toInt() );
QgsStatisticalSummary::Statistic stat = static_cast< QgsStatisticalSummary::Statistic >( comboBox->currentData().toInt() );
mergeResult = calcStatistic( fieldIdx, stat );
}

//insert string into table widget
mUpdating = true; // prevent combobox changing to "manual" value
QTableWidgetItem *item = mTableWidget->item( mTableWidget->rowCount() - 1, col );
item->setData( Qt::DisplayRole, mergeResult );

// Result formatting
QString stringVal;
if ( mergeBehaviorString != QStringLiteral( "skip" ) && mergeBehaviorString != QStringLiteral( "manual" ) )
{
const QgsEditorWidgetSetup setup = mFields.at( fieldIdx ).editorWidgetSetup();
const QgsFieldFormatter *formatter = QgsApplication::fieldFormatterRegistry()->fieldFormatter( setup.type() );
stringVal = formatter->representValue( mVectorLayer, fieldIdx, setup.config(), QVariant(), mergeResult );
}
else
{
stringVal = mergeResult.toString();
}

item->setData( Qt::DisplayRole, stringVal );
item->setData( Qt::UserRole, mergeResult );

mUpdating = false;
}

Expand Down Expand Up @@ -380,7 +397,9 @@ QVariant QgsMergeAttributesDialog::calcStatistic( int col, QgsStatisticalSummary
QList<double> values;
for ( int i = 0; i < mFeatureList.size(); ++i )
{
double currentValue = mTableWidget->item( i + 1, col )->text().toDouble( &conversion );
QTableWidgetItem *currentItem = mTableWidget->item( i + 1, col );
const QVariant currentData = currentItem->data( Qt::UserRole );
const double currentValue = currentData.toDouble( &conversion );
if ( conversion )
{
values << currentValue;
Expand Down Expand Up @@ -582,9 +601,9 @@ QgsAttributes QgsMergeAttributesDialog::mergedAttributes() const
if ( fieldIdx >= results.count() )
results.resize( fieldIdx + 1 ); // make sure the results vector is long enough (maybe not necessary)

if ( comboBox->currentData().toString() != QLatin1String( "skip" ) )
if ( comboBox->currentData().toString() != QStringLiteral( "skip" ) )
{
results[fieldIdx] = currentItem->data( Qt::DisplayRole );
results[fieldIdx] = currentItem->data( Qt::UserRole );
}
widgetIndex++;
}
Expand Down Expand Up @@ -612,7 +631,7 @@ QSet<int> QgsMergeAttributesDialog::skippedAttributeIndexes() const
continue;
}

if ( comboBox->currentData().toString() == QLatin1String( "skip" ) )
if ( comboBox->currentData().toString() == QStringLiteral( "skip" ) )
{
skipped << i;
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsmergeattributesdialog.h
Expand Up @@ -39,7 +39,7 @@ class APP_EXPORT QgsMergeAttributesDialog: public QDialog, private Ui::QgsMergeA

enum ItemDataRole
{
FieldIndex = Qt::UserRole //!< Index of corresponding field in source table
FieldIndex = Qt::UserRole //!< Index of corresponding field in source table for table header
};


Expand Down

0 comments on commit 221cfc3

Please sign in to comment.