diff options
author | Dirk Hohndel <dirk@hohndel.org> | 2014-06-11 13:56:33 -0700 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2014-06-11 13:56:33 -0700 |
commit | 2b59765da3882f1564e5ae034a35c0ce35441108 (patch) | |
tree | f4d37700f4ae3ab36b4a60e3195a539e79a7344a | |
parent | c4aa1f542ccbbbdc78dd3d44fe08f6410b9a5d2c (diff) | |
download | subsurface-2b59765da3882f1564e5ae034a35c0ce35441108.tar.gz |
Allow the user to delete a dive computer from a dive
This can't be the only dive computer, of course. Goes nicely with the
ability to reprder them.
Fixes #551
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
-rw-r--r-- | dive.c | 80 | ||||
-rw-r--r-- | dive.h | 2 | ||||
-rw-r--r-- | qt-ui/profile/profilewidget2.cpp | 17 | ||||
-rw-r--r-- | qt-ui/profile/profilewidget2.h | 1 |
4 files changed, 78 insertions, 22 deletions
@@ -20,26 +20,6 @@ static const char *default_tags[] = { QT_TRANSLATE_NOOP("gettextFromC", "deco") }; -void make_first_dc() -{ - struct divecomputer *dc = ¤t_dive->dc; - struct divecomputer *newdc = malloc(sizeof(*newdc)); - struct divecomputer *cur_dc = current_dc; /* needs to be in a local variable so the macro isn't re-executed */ - - /* skip the current DC in the linked list */ - while (dc && dc->next != cur_dc) - dc = dc->next; - if (!dc) { - fprintf(stderr, "data inconsistent: can't find the current DC"); - return; - } - dc->next = cur_dc->next; - *newdc = current_dive->dc; - current_dive->dc = *cur_dc; - current_dive->dc.next = newdc; - free(cur_dc); -} - void add_event(struct divecomputer *dc, int time, int type, int flags, int value, const char *name) { struct event *ev, **p; @@ -2326,3 +2306,63 @@ void dive_remove_picture(struct dive *d, struct picture *p) { } + +/* this always acts on the current divecomputer of the current dive */ +void make_first_dc() +{ + struct divecomputer *dc = ¤t_dive->dc; + struct divecomputer *newdc = malloc(sizeof(*newdc)); + struct divecomputer *cur_dc = current_dc; /* needs to be in a local variable so the macro isn't re-executed */ + + /* skip the current DC in the linked list */ + while (dc && dc->next != cur_dc) + dc = dc->next; + if (!dc) { + fprintf(stderr, "data inconsistent: can't find the current DC"); + return; + } + dc->next = cur_dc->next; + *newdc = current_dive->dc; + current_dive->dc = *cur_dc; + current_dive->dc.next = newdc; + free(cur_dc); +} + +/* always acts on the current dive */ +int count_divecomputers(void) +{ + int ret = 1; + struct divecomputer *dc = current_dive->dc.next; + while (dc) { + ret++; + dc = dc->next; + } + return ret; +} + +/* always acts on the current dive */ +void delete_current_divecomputer(void) +{ + struct divecomputer *dc = current_dc; + + if (dc == ¤t_dive->dc) { + /* remove the first one, so copy the second one in place of the first and free the second one + * be careful about freeing the no longer needed structures - since we copy things around we can't use free_dc()*/ + struct divecomputer *fdc = dc->next; + free(dc->sample); + free((void *)dc->model); + free_events(dc->events); + memcpy(dc, fdc, sizeof(struct divecomputer)); + free(fdc); + } else { + struct divecomputer *pdc = ¤t_dive->dc; + while (pdc->next != dc && pdc->next) + pdc = pdc->next; + if (pdc->next == dc) { + pdc->next = dc->next; + free_dc(dc); + } + } + if (dc_number == count_divecomputers()) + dc_number--; +} @@ -456,6 +456,8 @@ static inline struct divecomputer *get_dive_dc(struct dive *dive, int nr) } extern void make_first_dc(void); +extern int count_divecomputers(void); +extern void delete_current_divecomputer(void); /* * Iterate over each dive, with the first parameter being the index diff --git a/qt-ui/profile/profilewidget2.cpp b/qt-ui/profile/profilewidget2.cpp index 681878d0c..0ef645f20 100644 --- a/qt-ui/profile/profilewidget2.cpp +++ b/qt-ui/profile/profilewidget2.cpp @@ -873,10 +873,14 @@ void ProfileWidget2::contextMenuEvent(QContextMenuEvent *event) parentItem = parentItem->parentItem(); } if (isDCName) { - if (dc_number == 0) + if (dc_number == 0 && count_divecomputers() == 1) + // nothing to do, can't delete or reorder return; // create menu to show when right clicking on dive computer name - m.addAction(tr("Make first divecomputer"), this, SLOT(makeFirstDC())); + if (dc_number > 0) + m.addAction(tr("Make first divecomputer"), this, SLOT(makeFirstDC())); + if (count_divecomputers() > 1) + m.addAction(tr("Delete this divecomputer"), this, SLOT(deleteCurrentDC())); m.exec(event->globalPos()); // don't show the regular profile context menu return; @@ -929,6 +933,15 @@ void ProfileWidget2::contextMenuEvent(QContextMenuEvent *event) m.exec(event->globalPos()); } +void ProfileWidget2::deleteCurrentDC() +{ + delete_current_divecomputer(); + mark_divelist_changed(true); + // we need to force it since it's likely the same dive and same dc_number - but that's a different dive computer now + forceReplot = true; + MainWindow::instance()->refreshDisplay(); +} + void ProfileWidget2::makeFirstDC() { make_first_dc(); diff --git a/qt-ui/profile/profilewidget2.h b/qt-ui/profile/profilewidget2.h index f908543d1..47e9a51b3 100644 --- a/qt-ui/profile/profilewidget2.h +++ b/qt-ui/profile/profilewidget2.h @@ -90,6 +90,7 @@ slots: // Necessary to call from QAction's signals. void removeEvent(); void editName(); void makeFirstDC(); + void deleteCurrentDC(); void pointInserted(const QModelIndex &parent, int start, int end); void pointsRemoved(const QModelIndex &, int start, int end); void plotPictures(); |