Sort the dive table after adding a dive

This commit scares me. Any pointers to dives or indices into the dive
table... anything like that is invalid after we resort the table. Well,
not technically "invalid" (as in bad pointers), but after re-sorting the
table these will possibly not be pointing at what we expected.

This starts with the selection being "wrong" after we add a dive that
isn't the last dive (once we click OK the chronologically last dive will
be selected).

But of course without doing this, our #1 assumption about the dive_table
is broken. The dive_table is supposed to be in chronological order.

Best advice of course would be "don't enter dives out of order" - but of
course that's not realistic.

Fixes #234

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2013-11-14 19:36:41 +09:00
parent f6eb26dbae
commit ed41d5a744

View file

@ -353,6 +353,8 @@ void MainTab::updateDiveInfo(int dive)
ui.diveTimeText->setText(QString::number((int)((d->duration.seconds + 30) / 60))); ui.diveTimeText->setText(QString::number((int)((d->duration.seconds + 30) / 60)));
if (prevd) if (prevd)
ui.surfaceIntervalText->setText(get_time_string(d->when - (prevd->when + prevd->duration.seconds), 4)); ui.surfaceIntervalText->setText(get_time_string(d->when - (prevd->when + prevd->duration.seconds), 4));
else
ui.surfaceIntervalText->clear();
if ((sacVal.mliter = d->sac) > 0) if ((sacVal.mliter = d->sac) > 0)
ui.sacText->setText(get_volume_string(sacVal, TRUE).append(tr("/min"))); ui.sacText->setText(get_volume_string(sacVal, TRUE).append(tr("/min")));
else else
@ -510,13 +512,16 @@ void MainTab::acceptChanges()
mainWindow()->dive_list()->unselectDives(); mainWindow()->dive_list()->unselectDives();
mainWindow()->dive_list()->selectDive(addedDive, true, true); mainWindow()->dive_list()->selectDive(addedDive, true, true);
mainWindow()->showProfile(); mainWindow()->showProfile();
mainWindow()->refreshDisplay();
mark_divelist_changed(TRUE); mark_divelist_changed(TRUE);
DivePlannerPointsModel::instance()->setPlanMode(DivePlannerPointsModel::NOTHING); DivePlannerPointsModel::instance()->setPlanMode(DivePlannerPointsModel::NOTHING);
} }
editMode = NONE; editMode = NONE;
resetPallete(); resetPallete();
// now comes the scary moment... we need to re-sort dive table in case this dive wasn't the last
// so now all pointers become invalid
// fingers crossed that we aren't holding on to anything here
sort_table(&dive_table);
mainWindow()->refreshDisplay(); mainWindow()->refreshDisplay();
} }