Be much more careful about merging dives

This patch changes the dive merging to be much more careful about
things, because it turns out that we had several small oddities that
caused big merge issues.

The oddities are:

 - the dive "duration" is actually how long we spend under water.

   But that means that when we do "dive->when + dive.duration.seconds"
   to calculate the end of the dive, that is nonsensical if you came up
   to the surface in the middle of a dive.

   Now, normally you don't see profiles like that, but once you start
   merging dives together, it can go from "small detail" to "dominant
   factor".

 - We have two different cases of merging: the automatic "merge new dive
   computer download if it looks like the same dive" (which always has a
   merge offset of 0, since we merge it as a new dive computer) and the
   "merge two different dives into one longer dive.

   The code assumed that it could look at the "downloaded" flag for the
   dive to check one or the other, but that doesn't really work.
   Reading a dive from an XML file isn't any different from downloading
   it.

   So we need to change the logic to determine what kind of merge it is
   to actually check the passed-in time offset.

With this, Stuart Vernon's test-case of eight dives with short surface
intervals in between end up merging correctly into one dive.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Reported-by: Stuart Vernon <stuartv@force2.net>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Linus Torvalds 2015-09-22 12:32:27 -07:00 committed by Dirk Hohndel
parent 000a93fb64
commit 34da4801f4
3 changed files with 99 additions and 22 deletions

View file

@ -22,6 +22,7 @@
#include "divelistview.h"
#include "divepicturemodel.h"
#include "metrics.h"
#include "helpers.h"
// # Date Rtg Dpth Dur Tmp Wght Suit Cyl Gas SAC OTU CNS Loc
static int defaultWidth[] = { 70, 140, 90, 50, 50, 50, 50, 70, 50, 50, 70, 50, 50, 500};
@ -575,12 +576,12 @@ static bool can_merge(const struct dive *a, const struct dive *b, enum asked_use
if (a->when > b->when)
return false;
/* Don't merge dives if there's more than half an hour between them */
if (a->when + a->duration.seconds + 30 * 60 < b->when) {
if (dive_endtime(a) + 30 * 60 < b->when) {
if (*have_asked == NOTYET) {
if (QMessageBox::warning(MainWindow::instance(),
MainWindow::instance()->tr("Warning"),
MainWindow::instance()->tr("Trying to merge dives with %1min interval in between").arg(
(b->when - a->when - a->duration.seconds) / 60),
(b->when - dive_endtime(a)) / 60),
QMessageBox::Ok | QMessageBox::Cancel) == QMessageBox::Cancel) {
*have_asked = DONTMERGE;
return false;