Skip to content

Commit 7d38eac

Browse files
committedJun 19, 2013
"Merge selected features" leads to data loss (Fix #6902)
1 parent 23b5afb commit 7d38eac

File tree

2 files changed

+37
-33
lines changed

2 files changed

+37
-33
lines changed
 

‎src/app/qgsmergeattributesdialog.cpp

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -243,48 +243,49 @@ void QgsMergeAttributesDialog::refreshMergedValue( int col )
243243

244244
//evaluate behaviour (feature value or min / max / mean )
245245
QString mergeBehaviourString = comboBox->currentText();
246-
QString evalText; //text that has to be inserted into merge result field
246+
QVariant mergeResult; // result to show in the merge result field
247247
if ( mergeBehaviourString == tr( "Minimum" ) )
248248
{
249-
evalText = minimumAttributeString( col );
249+
mergeResult = minimumAttribute( col );
250250
}
251251
else if ( mergeBehaviourString == tr( "Maximum" ) )
252252
{
253-
evalText = maximumAttributeString( col );
253+
mergeResult = maximumAttribute( col );
254254
}
255255
else if ( mergeBehaviourString == tr( "Mean" ) )
256256
{
257-
evalText = meanAttributeString( col );
257+
mergeResult = meanAttribute( col );
258258
}
259259
else if ( mergeBehaviourString == tr( "Median" ) )
260260
{
261-
evalText = medianAttributeString( col );
261+
mergeResult = medianAttribute( col );
262262
}
263263
else if ( mergeBehaviourString == tr( "Sum" ) )
264264
{
265-
evalText = sumAttributeString( col );
265+
mergeResult = sumAttribute( col );
266266
}
267267
else if ( mergeBehaviourString == tr( "Concatenation" ) )
268268
{
269-
evalText = concatenationAttributeString( col );
269+
mergeResult = concatenationAttribute( col );
270270
}
271271
else if ( mergeBehaviourString == tr( "Skip attribute" ) )
272272
{
273-
evalText = tr( "Skipped" );
273+
mergeResult = tr( "Skipped" );
274274
}
275275
else //an existing feature value
276276
{
277277
int featureId = mergeBehaviourString.split( " " ).at( 1 ).toInt(); //probably not very robust for translations...
278-
evalText = featureAttributeString( featureId, col );
278+
mergeResult = featureAttribute( featureId, col );
279279
}
280280

281281
//insert string into table widget
282-
QTableWidgetItem* newTotalItem = new QTableWidgetItem( evalText );
282+
QTableWidgetItem* newTotalItem = new QTableWidgetItem();
283+
newTotalItem->setData( Qt::DisplayRole, mergeResult );
283284
newTotalItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable );
284285
mTableWidget->setItem( mTableWidget->rowCount() - 1, col, newTotalItem );
285286
}
286287

287-
QString QgsMergeAttributesDialog::featureAttributeString( int featureId, int col )
288+
QVariant QgsMergeAttributesDialog::featureAttribute( int featureId, int col )
288289
{
289290
int idx = mTableWidget->horizontalHeaderItem( col )->data( Qt::UserRole ).toInt();
290291

@@ -296,15 +297,15 @@ QString QgsMergeAttributesDialog::featureAttributeString( int featureId, int col
296297
if ( i < mFeatureList.size() &&
297298
QgsAttributeEditor::retrieveValue( mTableWidget->cellWidget( i + 1, col ), mVectorLayer, idx, value ) )
298299
{
299-
return value.toString();
300+
return value;
300301
}
301302
else
302303
{
303-
return "";
304+
return QVariant( mVectorLayer->pendingFields()[col].type() );
304305
}
305306
}
306307

307-
QString QgsMergeAttributesDialog::minimumAttributeString( int col )
308+
QVariant QgsMergeAttributesDialog::minimumAttribute( int col )
308309
{
309310
double minimumValue = std::numeric_limits<double>::max();
310311
double currentValue;
@@ -329,10 +330,10 @@ QString QgsMergeAttributesDialog::minimumAttributeString( int col )
329330
return QString();
330331
}
331332

332-
return QString::number( minimumValue, 'f' );
333+
return QVariant( minimumValue );
333334
}
334335

335-
QString QgsMergeAttributesDialog::maximumAttributeString( int col )
336+
QVariant QgsMergeAttributesDialog::maximumAttribute( int col )
336337
{
337338
double maximumValue = -std::numeric_limits<double>::max();
338339
double currentValue;
@@ -354,13 +355,13 @@ QString QgsMergeAttributesDialog::maximumAttributeString( int col )
354355

355356
if ( numberOfConsideredFeatures < 1 )
356357
{
357-
return QString();
358+
return QVariant( mVectorLayer->pendingFields()[col].type() );
358359
}
359360

360-
return QString::number( maximumValue, 'f' );
361+
return QVariant( maximumValue );
361362
}
362363

363-
QString QgsMergeAttributesDialog::meanAttributeString( int col )
364+
QVariant QgsMergeAttributesDialog::meanAttribute( int col )
364365
{
365366
int numberOfConsideredFeatures = 0;
366367
double currentValue;
@@ -377,10 +378,10 @@ QString QgsMergeAttributesDialog::meanAttributeString( int col )
377378
}
378379
}
379380
double mean = sum / numberOfConsideredFeatures;
380-
return QString::number( mean, 'f' );
381+
return QVariant( mean );
381382
}
382383

383-
QString QgsMergeAttributesDialog::medianAttributeString( int col )
384+
QVariant QgsMergeAttributesDialog::medianAttribute( int col )
384385
{
385386
//bring all values into a list and sort
386387
QList<double> valueList;
@@ -409,10 +410,10 @@ QString QgsMergeAttributesDialog::medianAttributeString( int col )
409410
{
410411
medianValue = valueList[( size + 1 ) / 2 - 1];
411412
}
412-
return QString::number( medianValue, 'f' );
413+
return QVariant( medianValue );
413414
}
414415

415-
QString QgsMergeAttributesDialog::sumAttributeString( int col )
416+
QVariant QgsMergeAttributesDialog::sumAttribute( int col )
416417
{
417418
double sum = 0.0;
418419
bool conversion = false;
@@ -425,10 +426,10 @@ QString QgsMergeAttributesDialog::sumAttributeString( int col )
425426
sum += currentValue;
426427
}
427428
}
428-
return QString::number( sum, 'f' );
429+
return QVariant( sum );
429430
}
430431

431-
QString QgsMergeAttributesDialog::concatenationAttributeString( int col )
432+
QVariant QgsMergeAttributesDialog::concatenationAttribute( int col )
432433
{
433434
QStringList concatString;
434435
for ( int i = 0; i < mFeatureList.size(); ++i )
@@ -562,6 +563,8 @@ QgsAttributes QgsMergeAttributesDialog::mergedAttributes() const
562563
return QgsAttributes();
563564
}
564565

566+
QgsFields fields = mVectorLayer->pendingFields();
567+
565568
QgsAttributes results( mTableWidget->columnCount() );
566569
for ( int i = 0; i < mTableWidget->columnCount(); i++ )
567570
{
@@ -577,7 +580,8 @@ QgsAttributes QgsMergeAttributesDialog::mergedAttributes() const
577580

578581
if ( idx >= results.count() )
579582
results.resize( idx + 1 ); // make sure the results vector is long enough (maybe not necessary)
580-
results[idx] = currentItem->text();
583+
584+
results[idx] = currentItem->data( Qt::DisplayRole );
581585
}
582586

583587
return results;

‎src/app/qgsmergeattributesdialog.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,19 @@ class QgsMergeAttributesDialog: public QDialog, private Ui::QgsMergeAttributesDi
5454
/**Calculates the merged value of a column (depending on the selected merge behaviour) and inserts the value in the corresponding cell*/
5555
void refreshMergedValue( int col );
5656
/**Inserts the attribute value of a specific feature into the row of merged attributes*/
57-
QString featureAttributeString( int featureId, int col );
57+
QVariant featureAttribute( int featureId, int col );
5858
/**Calculates and inserts the minimum attribute value of a column*/
59-
QString minimumAttributeString( int col );
59+
QVariant minimumAttribute( int col );
6060
/**Calculates and inserts the maximum value of a column*/
61-
QString maximumAttributeString( int col );
61+
QVariant maximumAttribute( int col );
6262
/**Calculates and inserts the mean value of a column*/
63-
QString meanAttributeString( int col );
63+
QVariant meanAttribute( int col );
6464
/**Calculates and inserts the median value of a column*/
65-
QString medianAttributeString( int col );
65+
QVariant medianAttribute( int col );
6666
/**Calculates and inserts the sum of a column*/
67-
QString sumAttributeString( int col );
67+
QVariant sumAttribute( int col );
6868
/**Appends the values of the features for the final value*/
69-
QString concatenationAttributeString( int col );
69+
QVariant concatenationAttribute( int col );
7070
/**Sets mSelectionRubberBand to a new feature*/
7171
void createRubberBandForFeature( int featureId );
7272

0 commit comments

Comments
 (0)
Please sign in to comment.