Undo: implement autogrouping of trips in DiveAdd

If the autogroup flag is set, search for appropriate trips in
DiveAdd() and add the dive to this trip. If no trip exists, add
a new trip.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2018-07-30 10:33:25 +02:00 committed by Dirk Hohndel
parent ec7d85835f
commit 63b65a7e20
7 changed files with 58 additions and 10 deletions

View file

@ -24,6 +24,7 @@
* void add_dive_to_trip(struct dive *dive, dive_trip_t *trip) * void add_dive_to_trip(struct dive *dive, dive_trip_t *trip)
* dive_trip_t *create_and_hookup_trip_from_dive(struct dive *dive) * dive_trip_t *create_and_hookup_trip_from_dive(struct dive *dive)
* dive_trip_t *get_dives_to_autogroup(int start, int *from, int *to, bool *allocated) * dive_trip_t *get_dives_to_autogroup(int start, int *from, int *to, bool *allocated)
* dive_trip_t *get_trip_for_new_dive(struct dive *new_dive, bool *allocated)
* void autogroup_dives(void) * void autogroup_dives(void)
* void combine_trips(struct dive_trip *trip_a, struct dive_trip *trip_b) * void combine_trips(struct dive_trip *trip_a, struct dive_trip *trip_b)
* dive_trip_t *combine_trips_create(struct dive_trip *trip_a, struct dive_trip *trip_b) * dive_trip_t *combine_trips_create(struct dive_trip *trip_a, struct dive_trip *trip_b)
@ -925,6 +926,37 @@ dive_trip_t *create_and_hookup_trip_from_dive(struct dive *dive)
return dive_trip; return dive_trip;
} }
/*
* Find a trip a new dive should be autogrouped with. If no such trips
* exist, allocate a new trip. The bool "*allocated" is set to true
* if a new trip was allocated.
*/
dive_trip_t *get_trip_for_new_dive(struct dive *new_dive, bool *allocated)
{
struct dive *d;
dive_trip_t *trip;
int i;
/* Find dive that is within TRIP_THRESHOLD of current dive */
for_each_dive(i, d) {
/* Check if we're past the range of possible dives */
if (d->when >= new_dive->when + TRIP_THRESHOLD)
break;
if (d->when + TRIP_THRESHOLD >= new_dive->when && d->divetrip) {
/* Found a dive with trip in the range */
*allocated = false;
return d->divetrip;
}
}
/* Didn't find a trip -> allocate a new one */
trip = create_trip_from_dive(new_dive);
trip->autogen = true;
*allocated = true;
return trip;
}
/* /*
* Collect dives for auto-grouping. Pass in first dive which should be checked. * Collect dives for auto-grouping. Pass in first dive which should be checked.
* Returns range of dives that should be autogrouped and trip it should be * Returns range of dives that should be autogrouped and trip it should be

View file

@ -32,6 +32,7 @@ extern dive_trip_t *alloc_trip(void);
extern dive_trip_t *create_trip_from_dive(struct dive *dive); extern dive_trip_t *create_trip_from_dive(struct dive *dive);
extern dive_trip_t *create_and_hookup_trip_from_dive(struct dive *dive); extern dive_trip_t *create_and_hookup_trip_from_dive(struct dive *dive);
extern dive_trip_t *get_dives_to_autogroup(int start, int *from, int *to, bool *allocated); extern dive_trip_t *get_dives_to_autogroup(int start, int *from, int *to, bool *allocated);
extern dive_trip_t *get_trip_for_new_dive(struct dive *new_dive, bool *allocated);
extern void autogroup_dives(void); extern void autogroup_dives(void);
extern struct dive *merge_two_dives(struct dive *a, struct dive *b); extern struct dive *merge_two_dives(struct dive *a, struct dive *b);
extern bool consecutive_selected(); extern bool consecutive_selected();

View file

@ -6,9 +6,9 @@
namespace Command { namespace Command {
// Dive-list related commands // Dive-list related commands
void addDive(dive *d) void addDive(dive *d, bool autogroup)
{ {
execute(new AddDive(d)); execute(new AddDive(d, autogroup));
} }
void deleteDive(const QVector<struct dive*> &divesToDelete) void deleteDive(const QVector<struct dive*> &divesToDelete)

View file

@ -15,7 +15,7 @@ QAction *undoAction(QObject *parent); // Create an undo action.
QAction *redoAction(QObject *parent); // Create an redo action. QAction *redoAction(QObject *parent); // Create an redo action.
// Dive-list related commands // Dive-list related commands
void addDive(dive *d); void addDive(dive *d, bool autogroup);
void deleteDive(const QVector<struct dive*> &divesToDelete); void deleteDive(const QVector<struct dive*> &divesToDelete);
void shiftTime(const QVector<dive *> &changedDives, int amount); void shiftTime(const QVector<dive *> &changedDives, int amount);
void renumberDives(const QVector<QPair<int, int>> &divesToRenumber); void renumberDives(const QVector<QPair<int, int>> &divesToRenumber);

View file

@ -289,16 +289,31 @@ static void moveDivesBetweenTrips(DivesToTrip &dives)
std::reverse(dives.divesToMove.begin(), dives.divesToMove.end()); std::reverse(dives.divesToMove.begin(), dives.divesToMove.end());
} }
AddDive::AddDive(dive *d) AddDive::AddDive(dive *d, bool autogroup)
{ {
setText(tr("add dive")); setText(tr("add dive"));
d->maxdepth.mm = 0; d->maxdepth.mm = 0;
fixup_dive(d); fixup_dive(d);
d->divetrip = nullptr; // TODO: consider autogroup == true d->divetrip = nullptr;
int idx = dive_get_insertion_index(d);
d->number = get_dive_nr_at_idx(idx);
divesToAdd.push_back({ OwningDivePtr(clone_dive(d)), nullptr, d->divetrip, idx }); // Get an owning pointer to a copy of the dive
// Note: this destroys the old dive!
OwningDivePtr divePtr(clone_dive(d));
// If we alloc a new-trip for autogrouping, get an owning pointer to it.
OwningTripPtr allocTrip;
dive_trip *trip = nullptr;
if (autogroup) {
bool alloc;
trip = get_trip_for_new_dive(divePtr.get(), &alloc);
if (alloc)
allocTrip.reset(trip);
}
int idx = dive_get_insertion_index(divePtr.get());
divePtr->number = get_dive_nr_at_idx(idx);
divesToAdd.push_back({ std::move(divePtr), std::move(allocTrip), trip, idx });
} }
bool AddDive::workToBeDone() bool AddDive::workToBeDone()

View file

@ -40,7 +40,7 @@ struct DivesToTrip
class AddDive : public Base { class AddDive : public Base {
public: public:
AddDive(dive *dive); AddDive(dive *dive, bool autogroup);
private: private:
void undo() override; void undo() override;
void redo() override; void redo() override;

View file

@ -799,7 +799,7 @@ void MainTab::acceptChanges()
updateDiveSite(ui.location->currDiveSiteUuid(), &displayed_dive); updateDiveSite(ui.location->currDiveSiteUuid(), &displayed_dive);
copyTagsToDisplayedDive(); copyTagsToDisplayedDive();
Command::addDive(&displayed_dive); Command::addDive(&displayed_dive, autogroup);
editMode = NONE; editMode = NONE;
MainWindow::instance()->exitEditState(); MainWindow::instance()->exitEditState();