mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
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>
This commit is contained in:
parent
c4aa1f542c
commit
2b59765da3
4 changed files with 78 additions and 22 deletions
80
dive.c
80
dive.c
|
@ -20,26 +20,6 @@ static const char *default_tags[] = {
|
||||||
QT_TRANSLATE_NOOP("gettextFromC", "deco")
|
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)
|
void add_event(struct divecomputer *dc, int time, int type, int flags, int value, const char *name)
|
||||||
{
|
{
|
||||||
struct event *ev, **p;
|
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--;
|
||||||
|
}
|
||||||
|
|
2
dive.h
2
dive.h
|
@ -456,6 +456,8 @@ static inline struct divecomputer *get_dive_dc(struct dive *dive, int nr)
|
||||||
}
|
}
|
||||||
|
|
||||||
extern void make_first_dc(void);
|
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
|
* Iterate over each dive, with the first parameter being the index
|
||||||
|
|
|
@ -873,10 +873,14 @@ void ProfileWidget2::contextMenuEvent(QContextMenuEvent *event)
|
||||||
parentItem = parentItem->parentItem();
|
parentItem = parentItem->parentItem();
|
||||||
}
|
}
|
||||||
if (isDCName) {
|
if (isDCName) {
|
||||||
if (dc_number == 0)
|
if (dc_number == 0 && count_divecomputers() == 1)
|
||||||
|
// nothing to do, can't delete or reorder
|
||||||
return;
|
return;
|
||||||
// create menu to show when right clicking on dive computer name
|
// 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());
|
m.exec(event->globalPos());
|
||||||
// don't show the regular profile context menu
|
// don't show the regular profile context menu
|
||||||
return;
|
return;
|
||||||
|
@ -929,6 +933,15 @@ void ProfileWidget2::contextMenuEvent(QContextMenuEvent *event)
|
||||||
m.exec(event->globalPos());
|
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()
|
void ProfileWidget2::makeFirstDC()
|
||||||
{
|
{
|
||||||
make_first_dc();
|
make_first_dc();
|
||||||
|
|
|
@ -90,6 +90,7 @@ slots: // Necessary to call from QAction's signals.
|
||||||
void removeEvent();
|
void removeEvent();
|
||||||
void editName();
|
void editName();
|
||||||
void makeFirstDC();
|
void makeFirstDC();
|
||||||
|
void deleteCurrentDC();
|
||||||
void pointInserted(const QModelIndex &parent, int start, int end);
|
void pointInserted(const QModelIndex &parent, int start, int end);
|
||||||
void pointsRemoved(const QModelIndex &, int start, int end);
|
void pointsRemoved(const QModelIndex &, int start, int end);
|
||||||
void plotPictures();
|
void plotPictures();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue