From ef4705070ff67322386041529b7f10eaf09f6cdf Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Wed, 21 May 2014 12:06:02 -0300 Subject: Create a 'remove_event' function that removes an event. The logic of removing the event was in the UI, and this makes the code harder to test because we need to take into account also the events that the interface is receiving, instead of only relying on the algorithm to test. so, now it lives in dive.h/.c and a unittest is easyer to make. Signed-off-by: Tomaz Canabrava Signed-off-by: Dirk Hohndel --- dive.c | 11 +++++++++++ dive.h | 1 + qt-ui/profile/profilewidget2.cpp | 8 +------- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/dive.c b/dive.c index d9943c23f..7e211f163 100644 --- a/dive.c +++ b/dive.c @@ -65,6 +65,17 @@ void add_event(struct divecomputer *dc, int time, int type, int flags, int value remember_event(name); } +void remove_event(struct event* event) +{ + struct event **ep = ¤t_dc->events; + while (ep && *ep != event) + ep = &(*ep)->next; + if (ep) { + *ep = event->next; + free(event); + } +} + int get_pressure_units(unsigned int mb, const char **units) { int pressure; diff --git a/dive.h b/dive.h index a4bc7f08b..11a724cbe 100644 --- a/dive.h +++ b/dive.h @@ -562,6 +562,7 @@ extern void copy_samples(struct dive *s, struct dive *d); extern void fill_default_cylinder(cylinder_t *cyl); extern void add_gas_switch_event(struct dive *dive, struct divecomputer *dc, int time, int idx); extern void add_event(struct divecomputer *dc, int time, int type, int flags, int value, const char *name); +extern void remove_event(struct event* event); extern void per_cylinder_mean_depth(struct dive *dive, struct divecomputer *dc, int *mean, int *duration); extern int get_cylinder_index(struct dive *dive, struct event *ev); extern int nr_cylinders(struct dive *dive); diff --git a/qt-ui/profile/profilewidget2.cpp b/qt-ui/profile/profilewidget2.cpp index d95a32be8..4a599d711 100644 --- a/qt-ui/profile/profilewidget2.cpp +++ b/qt-ui/profile/profilewidget2.cpp @@ -830,13 +830,7 @@ void ProfileWidget2::removeEvent() tr("Remove the selected event?"), tr("%1 @ %2:%3").arg(event->name).arg(event->time.seconds / 60).arg(event->time.seconds % 60, 2, 10, QChar('0'))), QMessageBox::Ok | QMessageBox::Cancel) == QMessageBox::Ok) { - struct event **ep = ¤t_dc->events; - while (ep && *ep != event) - ep = &(*ep)->next; - if (ep) { - *ep = event->next; - free(event); - } + remove_event(event); mark_divelist_changed(true); replot(); } -- cgit v1.2.3-70-g09d2