mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
undo: implement renaming of events
There is a slight complexity here owing to the fact that the profile works on a copy of the current dive: We get a copy of the event and have to search for the original event in the current dive. This could be done in the undo command. Nevertheless, here we do it in the profile so that when in the future the profile can work on a non-copied dive we can simply remove this function. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
parent
f9fe6d759f
commit
ab8e317b28
7 changed files with 87 additions and 10 deletions
|
@ -344,4 +344,9 @@ void addEventSetpointChange(struct dive *d, int dcNr, int seconds, pressure_t pO
|
|||
execute(new AddEventSetpointChange(d, dcNr, seconds, pO2));
|
||||
}
|
||||
|
||||
void renameEvent(struct dive *d, int dcNr, struct event *ev, const char *name)
|
||||
{
|
||||
execute(new RenameEvent(d, dcNr, ev, name));
|
||||
}
|
||||
|
||||
} // namespace Command
|
||||
|
|
|
@ -109,6 +109,7 @@ void editTripNotes(dive_trip *trip, const QString &s);
|
|||
void addEventBookmark(struct dive *d, int dcNr, int seconds);
|
||||
void addEventDivemodeSwitch(struct dive *d, int dcNr, int seconds, int divemode);
|
||||
void addEventSetpointChange(struct dive *d, int dcNr, int seconds, pressure_t pO2);
|
||||
void renameEvent(struct dive *d, int dcNr, struct event *ev, const char *name);
|
||||
|
||||
} // namespace Command
|
||||
|
||||
|
|
|
@ -70,4 +70,31 @@ AddEventSetpointChange::AddEventSetpointChange(struct dive *d, int dcNr, int sec
|
|||
setText(tr("Add set point change")); // TODO: format pO2 value in bar or psi.
|
||||
}
|
||||
|
||||
RenameEvent::RenameEvent(struct dive *d, int dcNr, struct event *ev, const char *name) : EventBase(d, dcNr),
|
||||
eventToAdd(clone_event_rename(ev, name)),
|
||||
eventToRemove(ev)
|
||||
{
|
||||
setText(tr("Rename bookmark to %1").arg(name));
|
||||
}
|
||||
|
||||
bool RenameEvent::workToBeDone()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
void RenameEvent::redoit()
|
||||
{
|
||||
struct divecomputer *dc = get_dive_dc(d, dcNr);
|
||||
swap_event(dc, eventToRemove, eventToAdd.get());
|
||||
event *tmp = eventToRemove;
|
||||
eventToRemove = eventToAdd.release();
|
||||
eventToAdd.reset(tmp);
|
||||
}
|
||||
|
||||
void RenameEvent::undoit()
|
||||
{
|
||||
// Undo and redo do the same thing - they simply swap events
|
||||
redoit();
|
||||
}
|
||||
|
||||
} // namespace Command
|
||||
|
|
|
@ -58,6 +58,18 @@ public:
|
|||
AddEventSetpointChange(struct dive *d, int dcNr, int seconds, pressure_t pO2);
|
||||
};
|
||||
|
||||
class RenameEvent : public EventBase {
|
||||
public:
|
||||
RenameEvent(struct dive *d, int dcNr, struct event *ev, const char *name);
|
||||
private:
|
||||
bool workToBeDone() override;
|
||||
void undoit() override;
|
||||
void redoit() override;
|
||||
|
||||
OwningEventPtr eventToAdd; // for undo and redo
|
||||
event *eventToRemove; // for undo and redo
|
||||
};
|
||||
|
||||
} // namespace Command
|
||||
|
||||
#endif // COMMAND_EVENT_H
|
||||
|
|
20
core/dive.c
20
core/dive.c
|
@ -166,6 +166,11 @@ struct event *create_event(unsigned int time, int type, int flags, int value, co
|
|||
return ev;
|
||||
}
|
||||
|
||||
struct event *clone_event_rename(const struct event *ev, const char *name)
|
||||
{
|
||||
return create_event(ev->time.seconds, ev->type, ev->flags, ev->value, name);
|
||||
}
|
||||
|
||||
void add_event_to_dc(struct divecomputer *dc, struct event *ev)
|
||||
{
|
||||
struct event **p;
|
||||
|
@ -192,7 +197,20 @@ struct event *add_event(struct divecomputer *dc, unsigned int time, int type, in
|
|||
return ev;
|
||||
}
|
||||
|
||||
static int same_event(const struct event *a, const struct event *b)
|
||||
/* Substitutes an event in a divecomputer for another. No reordering is performed! */
|
||||
void swap_event(struct divecomputer *dc, struct event *from, struct event *to)
|
||||
{
|
||||
for (struct event **ep = &dc->events; *ep; ep = &(*ep)->next) {
|
||||
if (*ep == from) {
|
||||
to->next = from->next;
|
||||
*ep = to;
|
||||
from->next = NULL; // For good measure.
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool same_event(const struct event *a, const struct event *b)
|
||||
{
|
||||
if (a->time.seconds != b->time.seconds)
|
||||
return 0;
|
||||
|
|
|
@ -378,7 +378,10 @@ extern bool is_cylinder_used(const struct dive *dive, int idx);
|
|||
extern bool is_cylinder_prot(const struct dive *dive, int idx);
|
||||
extern void add_gas_switch_event(struct dive *dive, struct divecomputer *dc, int time, int idx);
|
||||
extern struct event *create_event(unsigned int time, int type, int flags, int value, const char *name);
|
||||
extern struct event *clone_event_rename(const struct event *ev, const char *name);
|
||||
extern void add_event_to_dc(struct divecomputer *dc, struct event *ev);
|
||||
extern void swap_event(struct divecomputer *dc, struct event *from, struct event *to);
|
||||
extern bool same_event(const struct event *a, const struct event *b);
|
||||
extern struct event *add_event(struct divecomputer *dc, unsigned int time, int type, int flags, int value, const char *name);
|
||||
extern void remove_event_from_dc(struct divecomputer *dc, struct event *event);
|
||||
extern void remove_event(const struct event *event);
|
||||
|
|
|
@ -1584,6 +1584,22 @@ void ProfileWidget2::unhideEvents()
|
|||
item->show();
|
||||
}
|
||||
|
||||
// The profile displays a copy of the current_dive, namely displayed_dive.
|
||||
// Therefore, the events we get are likewise copies. This function finds
|
||||
// the original event. TODO: Remove function once the profile can display
|
||||
// arbitrary dives.
|
||||
static event *find_event(const struct event *ev)
|
||||
{
|
||||
struct divecomputer *dc = current_dc;
|
||||
if (!dc)
|
||||
return nullptr;
|
||||
for (struct event *act = current_dc->events; act; act = act->next) {
|
||||
if (same_event(act, ev))
|
||||
return act;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void ProfileWidget2::removeEvent(DiveEventItem *item)
|
||||
{
|
||||
struct event *event = item->getEvent();
|
||||
|
@ -1698,7 +1714,9 @@ double ProfileWidget2::getFontPrintScale()
|
|||
#ifndef SUBSURFACE_MOBILE
|
||||
void ProfileWidget2::editName(DiveEventItem *item)
|
||||
{
|
||||
struct event *event = item->getEvent();
|
||||
struct event *event = find_event(item->getEvent());
|
||||
if (!event)
|
||||
return;
|
||||
bool ok;
|
||||
QString newName = QInputDialog::getText(this, tr("Edit name of bookmark"),
|
||||
tr("Custom name:"), QLineEdit::Normal,
|
||||
|
@ -1710,14 +1728,7 @@ void ProfileWidget2::editName(DiveEventItem *item)
|
|||
lengthWarning.exec();
|
||||
return;
|
||||
}
|
||||
// order is important! first update the current dive (by matching the unchanged event),
|
||||
// then update the displayed dive (as event is part of the events on displayed dive
|
||||
// and will be freed as part of changing the name!
|
||||
update_event_name(current_dive, event, qPrintable(newName));
|
||||
update_event_name(&displayed_dive, event, qPrintable(newName));
|
||||
invalidate_dive_cache(current_dive);
|
||||
mark_divelist_changed(true);
|
||||
replot();
|
||||
Command::renameEvent(current_dive, dc_number, event, qPrintable(newName));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
|
Loading…
Add table
Reference in a new issue