2018-07-19 20:35:25 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
|
|
|
#include "command_divelist.h"
|
2024-09-01 12:12:05 +00:00
|
|
|
#include "core/divefilter.h"
|
2018-07-19 20:35:25 +00:00
|
|
|
#include "core/divelist.h"
|
core: introduce divelog structure
The parser API was very annoying, as a number of tables
to-be-filled were passed in as pointers. The goal of this
commit is to collect all these tables in a single struct.
This should make it (more or less) clear what is actually
written into the divelog files.
Moreover, it should now be rather easy to search for
instances, where the global logfile is accessed (and it
turns out that there are many!).
The divelog struct does not contain the tables as substructs,
but only collects pointers. The idea is that the "divelog.h"
file can be included without all the other files describing
the numerous tables.
To make it easier to use from C++ parts of the code, the
struct implements a constructor and a destructor. Sadly,
we can't use smart pointers, since the pointers are accessed
from C code. Therfore the constructor and destructor are
quite complex.
The whole commit is large, but was mostly an automatic
conversion.
One oddity of note: the divelog structure also contains
the "autogroup" flag, since that is saved in the divelog.
This actually fixes a bug: Before, when importing dives
from a different log, the autogroup flag was overwritten.
This was probably not intended and does not happen anymore.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2022-11-08 20:31:08 +00:00
|
|
|
#include "core/divelog.h"
|
2019-03-03 16:10:09 +00:00
|
|
|
#include "core/qthelper.h"
|
2019-11-24 12:26:29 +00:00
|
|
|
#include "core/selection.h"
|
2020-02-03 18:33:06 +00:00
|
|
|
#include "core/subsurface-qt/divelistnotifier.h"
|
2018-08-27 06:55:09 +00:00
|
|
|
#include "qt-models/filtermodels.h"
|
2024-09-01 12:12:05 +00:00
|
|
|
#include "qt-models/divelocationmodel.h"
|
2018-07-19 20:35:25 +00:00
|
|
|
|
|
|
|
namespace Command {
|
|
|
|
|
2020-05-02 12:34:40 +00:00
|
|
|
// Helper function that takes care to unselect trips that are removed from the backend
|
2024-06-01 20:05:57 +00:00
|
|
|
static std::unique_ptr<dive_trip> remove_trip_from_backend(dive_trip *trip)
|
2020-05-02 12:34:40 +00:00
|
|
|
{
|
|
|
|
if (trip->selected)
|
|
|
|
deselect_trip(trip);
|
2024-06-08 14:53:55 +00:00
|
|
|
auto [t, idx] = divelog.trips.pull(trip);
|
2024-06-01 20:05:57 +00:00
|
|
|
return std::move(t);
|
2020-05-02 12:34:40 +00:00
|
|
|
}
|
|
|
|
|
2018-07-19 20:35:25 +00:00
|
|
|
// This helper function removes a dive, takes ownership of the dive and adds it to a DiveToAdd structure.
|
2018-11-26 21:06:17 +00:00
|
|
|
// If the trip the dive belongs to becomes empty, it is removed and added to the tripsToAdd vector.
|
2018-12-23 22:45:12 +00:00
|
|
|
// It is crucial that dives are added in reverse order of deletion, so that the indices are correctly
|
2018-07-19 20:35:25 +00:00
|
|
|
// set and that the trips are added before they are used!
|
2024-05-31 15:15:47 +00:00
|
|
|
DiveToAdd DiveListBase::removeDive(struct dive *d, std::vector<std::unique_ptr<dive_trip>> &tripsToAdd)
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
{
|
|
|
|
// If the dive was the current dive, reset the current dive. The calling
|
|
|
|
// command is responsible of finding a new dive.
|
2019-06-23 10:13:25 +00:00
|
|
|
if (d == current_dive)
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
current_dive = nullptr;
|
|
|
|
|
2019-03-05 21:58:47 +00:00
|
|
|
// remove dive from trip and site - if this is the last dive in the trip
|
2018-07-19 20:35:25 +00:00
|
|
|
// remove the whole trip.
|
2019-04-14 13:21:00 +00:00
|
|
|
DiveToAdd res;
|
2018-12-23 09:08:44 +00:00
|
|
|
res.trip = unregister_dive_from_trip(d);
|
2019-03-10 15:03:39 +00:00
|
|
|
if (d->dive_site)
|
|
|
|
diveSiteCountChanged(d->dive_site);
|
2019-03-05 21:58:47 +00:00
|
|
|
res.site = unregister_dive_from_dive_site(d);
|
2024-06-02 15:06:18 +00:00
|
|
|
if (res.trip && res.trip->dives.empty()) {
|
2024-06-08 14:53:55 +00:00
|
|
|
divelog.trips.sort(); // Removal of dives has changed order of trips! (TODO: remove this)
|
2024-06-01 20:05:57 +00:00
|
|
|
auto trip = remove_trip_from_backend(res.trip); // Remove trip from backend
|
|
|
|
tripsToAdd.push_back(std::move(trip)); // Take ownership of trip
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
size_t idx = divelog.dives.get_idx(d);
|
|
|
|
if (idx == std::string::npos)
|
2019-11-13 17:50:48 +00:00
|
|
|
qWarning("Deletion of unknown dive!");
|
2019-04-14 13:21:00 +00:00
|
|
|
|
2020-10-30 20:46:01 +00:00
|
|
|
DiveFilter::instance()->diveRemoved(d);
|
2019-11-17 16:41:23 +00:00
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
res.dive = divelog.dives.unregister_dive(idx); // Remove dive from backend
|
Dive list: implement proper Qt-model semantics for DiveTripModel
Previously, each dive-list modifying function would lead to a
full model reset. Instead, implement proper Qt-model semantics
using beginInsertRows()/endInsertRows(), beginRemoveRows()/
endRemoveRows(), dataChange().
To do so, a DiveListNotifer singleton is generatated, which
broadcasts all changes to the dive-list. Signals are sent by
the commands and received by the DiveTripModel. Signals are
batched by dive-trip. This seems to be an adequate compromise
for the two kinds of list-views (tree and list). In the common
usecase mostly dives of a single trip are affected.
Thus, batching of dives is performed in two positions:
- At command-level to batch by trip
- In DiveTripModel to feed batches of contiguous elements
to Qt's begin*/end*-functions.
This is conceptually simple, but rather complex code. To avoid
repetition of complex loops, the batching is implemented in
templated-functions, which are passed lambda-functions, which
are called for each batch.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-30 07:20:25 +00:00
|
|
|
|
2018-07-19 20:35:25 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-03-10 15:03:39 +00:00
|
|
|
void DiveListBase::diveSiteCountChanged(struct dive_site *ds)
|
|
|
|
{
|
|
|
|
if (std::find(sitesCountChanged.begin(), sitesCountChanged.end(), ds) == sitesCountChanged.end())
|
|
|
|
sitesCountChanged.push_back(ds);
|
|
|
|
}
|
|
|
|
|
2018-07-19 20:35:25 +00:00
|
|
|
// This helper function adds a dive and returns ownership to the backend. It may also add a dive trip.
|
|
|
|
// It is crucial that dives are added in reverse order of deletion (see comment above)!
|
|
|
|
// Returns pointer to added dive (which is owned by the backend!)
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
dive *DiveListBase::addDive(DiveToAdd &d)
|
2018-07-19 20:35:25 +00:00
|
|
|
{
|
|
|
|
if (d.trip)
|
2024-06-08 13:28:16 +00:00
|
|
|
d.trip->add_dive(d.dive.get());
|
2019-03-10 15:03:39 +00:00
|
|
|
if (d.site) {
|
2024-05-11 13:01:37 +00:00
|
|
|
d.site->add_dive(d.dive.get());
|
2019-03-10 15:03:39 +00:00
|
|
|
diveSiteCountChanged(d.site);
|
|
|
|
}
|
2024-06-18 19:19:14 +00:00
|
|
|
return divelog.dives.register_dive(std::move(d.dive)); // Transfer ownership to core and update fulltext index
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
2019-06-23 07:22:26 +00:00
|
|
|
// Some signals are sent in batches per trip. To avoid writing the same loop
|
|
|
|
// twice, this template takes a vector of trip / dive pairs, sorts it
|
|
|
|
// by trip and then calls a function-object with trip and a QVector of dives in that trip.
|
|
|
|
// The dives are sorted by the dive_less_than() function defined in the core.
|
|
|
|
// Input parameters:
|
|
|
|
// - dives: a vector of trip,dive pairs, which will be sorted and processed in batches by trip.
|
|
|
|
// - action: a function object, taking a trip-pointer and a QVector of dives, which will be called for each batch.
|
|
|
|
template<typename Function>
|
|
|
|
void processByTrip(std::vector<std::pair<dive_trip *, dive *>> &dives, Function action)
|
|
|
|
{
|
|
|
|
// Sort lexicographically by trip then according to the dive_less_than() function.
|
|
|
|
std::sort(dives.begin(), dives.end(),
|
|
|
|
[](const std::pair<dive_trip *, dive *> &e1, const std::pair<dive_trip *, dive *> &e2)
|
2024-06-07 08:25:09 +00:00
|
|
|
{ return e1.first == e2.first ? dive_less_than(*e1.second, *e2.second) : e1.first < e2.first; });
|
2019-06-23 07:22:26 +00:00
|
|
|
|
|
|
|
// Then, process the dives in batches by trip
|
|
|
|
size_t i, j; // Begin and end of batch
|
|
|
|
for (i = 0; i < dives.size(); i = j) {
|
|
|
|
dive_trip *trip = dives[i].first;
|
|
|
|
for (j = i + 1; j < dives.size() && dives[j].first == trip; ++j)
|
|
|
|
; // pass
|
|
|
|
// Copy dives into a QVector. Some sort of "range_view" would be ideal, but Qt doesn't work this way.
|
|
|
|
QVector<dive *> divesInTrip(j - i);
|
|
|
|
for (size_t k = i; k < j; ++k)
|
|
|
|
divesInTrip[k - i] = dives[k].second;
|
|
|
|
|
|
|
|
// Finally, emit the signal
|
|
|
|
action(trip, divesInTrip);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-19 20:35:25 +00:00
|
|
|
// This helper function calls removeDive() on a list of dives to be removed and
|
|
|
|
// returns a vector of corresponding DiveToAdd objects, which can later be readded.
|
2018-11-26 21:06:17 +00:00
|
|
|
// Moreover, a vector of deleted trips is returned, if trips became empty.
|
2018-07-19 20:35:25 +00:00
|
|
|
// The passed in vector is cleared.
|
2019-03-03 14:12:22 +00:00
|
|
|
DivesAndTripsToAdd DiveListBase::removeDives(DivesAndSitesToRemove &divesAndSitesToDelete)
|
2018-07-19 20:35:25 +00:00
|
|
|
{
|
2018-11-26 21:06:17 +00:00
|
|
|
std::vector<DiveToAdd> divesToAdd;
|
2024-05-31 15:15:47 +00:00
|
|
|
std::vector<std::unique_ptr<dive_trip>> tripsToAdd;
|
2024-05-04 11:39:04 +00:00
|
|
|
std::vector<std::unique_ptr<dive_site>> sitesToAdd;
|
2019-03-03 14:12:22 +00:00
|
|
|
divesToAdd.reserve(divesAndSitesToDelete.dives.size());
|
|
|
|
sitesToAdd.reserve(divesAndSitesToDelete.sites.size());
|
2018-07-19 20:35:25 +00:00
|
|
|
|
2019-11-17 16:41:23 +00:00
|
|
|
// Remember old number of shown dives
|
2020-10-30 21:10:31 +00:00
|
|
|
int oldShown = DiveFilter::instance()->shownDives();
|
2019-11-17 16:41:23 +00:00
|
|
|
|
2019-05-15 09:40:39 +00:00
|
|
|
// Make sure that the dive list is sorted. The added dives will be sent in a signal
|
|
|
|
// and the recipients assume that the dives are sorted the same way as they are
|
|
|
|
// in the core list.
|
2024-06-07 08:25:09 +00:00
|
|
|
std::sort(divesAndSitesToDelete.dives.begin(), divesAndSitesToDelete.dives.end(),
|
|
|
|
[](const dive *d1, const dive *d2) { return dive_less_than(*d1, *d2); });
|
2019-05-15 09:40:39 +00:00
|
|
|
|
2019-03-03 14:12:22 +00:00
|
|
|
for (dive *d: divesAndSitesToDelete.dives)
|
2018-11-26 21:06:17 +00:00
|
|
|
divesToAdd.push_back(removeDive(d, tripsToAdd));
|
2019-03-03 14:12:22 +00:00
|
|
|
divesAndSitesToDelete.dives.clear();
|
|
|
|
|
|
|
|
for (dive_site *ds: divesAndSitesToDelete.sites) {
|
2024-06-08 14:30:24 +00:00
|
|
|
auto res = divelog.sites.pull(ds);
|
2024-05-11 09:47:45 +00:00
|
|
|
sitesToAdd.push_back(std::move(res.ptr));
|
|
|
|
emit diveListNotifier.diveSiteDeleted(ds, res.idx);
|
2019-03-03 14:12:22 +00:00
|
|
|
}
|
|
|
|
divesAndSitesToDelete.sites.clear();
|
2018-07-19 20:35:25 +00:00
|
|
|
|
2020-02-03 18:33:06 +00:00
|
|
|
// We send one dives-deleted signal per trip (see comments in divelistnotifier.h).
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
// Therefore, collect all dives in an array and sort by trip.
|
Dive list: implement proper Qt-model semantics for DiveTripModel
Previously, each dive-list modifying function would lead to a
full model reset. Instead, implement proper Qt-model semantics
using beginInsertRows()/endInsertRows(), beginRemoveRows()/
endRemoveRows(), dataChange().
To do so, a DiveListNotifer singleton is generatated, which
broadcasts all changes to the dive-list. Signals are sent by
the commands and received by the DiveTripModel. Signals are
batched by dive-trip. This seems to be an adequate compromise
for the two kinds of list-views (tree and list). In the common
usecase mostly dives of a single trip are affected.
Thus, batching of dives is performed in two positions:
- At command-level to batch by trip
- In DiveTripModel to feed batches of contiguous elements
to Qt's begin*/end*-functions.
This is conceptually simple, but rather complex code. To avoid
repetition of complex loops, the batching is implemented in
templated-functions, which are passed lambda-functions, which
are called for each batch.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-30 07:20:25 +00:00
|
|
|
std::vector<std::pair<dive_trip *, dive *>> dives;
|
2018-11-26 21:06:17 +00:00
|
|
|
dives.reserve(divesToAdd.size());
|
|
|
|
for (const DiveToAdd &entry: divesToAdd)
|
Dive list: implement proper Qt-model semantics for DiveTripModel
Previously, each dive-list modifying function would lead to a
full model reset. Instead, implement proper Qt-model semantics
using beginInsertRows()/endInsertRows(), beginRemoveRows()/
endRemoveRows(), dataChange().
To do so, a DiveListNotifer singleton is generatated, which
broadcasts all changes to the dive-list. Signals are sent by
the commands and received by the DiveTripModel. Signals are
batched by dive-trip. This seems to be an adequate compromise
for the two kinds of list-views (tree and list). In the common
usecase mostly dives of a single trip are affected.
Thus, batching of dives is performed in two positions:
- At command-level to batch by trip
- In DiveTripModel to feed batches of contiguous elements
to Qt's begin*/end*-functions.
This is conceptually simple, but rather complex code. To avoid
repetition of complex loops, the batching is implemented in
templated-functions, which are passed lambda-functions, which
are called for each batch.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-30 07:20:25 +00:00
|
|
|
dives.push_back({ entry.trip, entry.dive.get() });
|
|
|
|
|
|
|
|
// Send signals.
|
|
|
|
processByTrip(dives, [&](dive_trip *trip, const QVector<dive *> &divesInTrip) {
|
2018-11-26 21:06:17 +00:00
|
|
|
// Check if this trip is supposed to be deleted, by checking if it was marked as "add it".
|
Dive list: implement proper Qt-model semantics for DiveTripModel
Previously, each dive-list modifying function would lead to a
full model reset. Instead, implement proper Qt-model semantics
using beginInsertRows()/endInsertRows(), beginRemoveRows()/
endRemoveRows(), dataChange().
To do so, a DiveListNotifer singleton is generatated, which
broadcasts all changes to the dive-list. Signals are sent by
the commands and received by the DiveTripModel. Signals are
batched by dive-trip. This seems to be an adequate compromise
for the two kinds of list-views (tree and list). In the common
usecase mostly dives of a single trip are affected.
Thus, batching of dives is performed in two positions:
- At command-level to batch by trip
- In DiveTripModel to feed batches of contiguous elements
to Qt's begin*/end*-functions.
This is conceptually simple, but rather complex code. To avoid
repetition of complex loops, the batching is implemented in
templated-functions, which are passed lambda-functions, which
are called for each batch.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-30 07:20:25 +00:00
|
|
|
bool deleteTrip = trip &&
|
2024-05-31 15:15:47 +00:00
|
|
|
std::find_if(tripsToAdd.begin(), tripsToAdd.end(), [trip](const std::unique_ptr<dive_trip> &ptr)
|
2018-11-26 21:06:17 +00:00
|
|
|
{ return ptr.get() == trip; }) != tripsToAdd.end();
|
Dive list: implement proper Qt-model semantics for DiveTripModel
Previously, each dive-list modifying function would lead to a
full model reset. Instead, implement proper Qt-model semantics
using beginInsertRows()/endInsertRows(), beginRemoveRows()/
endRemoveRows(), dataChange().
To do so, a DiveListNotifer singleton is generatated, which
broadcasts all changes to the dive-list. Signals are sent by
the commands and received by the DiveTripModel. Signals are
batched by dive-trip. This seems to be an adequate compromise
for the two kinds of list-views (tree and list). In the common
usecase mostly dives of a single trip are affected.
Thus, batching of dives is performed in two positions:
- At command-level to batch by trip
- In DiveTripModel to feed batches of contiguous elements
to Qt's begin*/end*-functions.
This is conceptually simple, but rather complex code. To avoid
repetition of complex loops, the batching is implemented in
templated-functions, which are passed lambda-functions, which
are called for each batch.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-30 07:20:25 +00:00
|
|
|
emit diveListNotifier.divesDeleted(trip, deleteTrip, divesInTrip);
|
|
|
|
});
|
2019-11-17 16:41:23 +00:00
|
|
|
|
2020-10-30 21:10:31 +00:00
|
|
|
if (oldShown != DiveFilter::instance()->shownDives())
|
2019-11-17 16:41:23 +00:00
|
|
|
emit diveListNotifier.numShownChanged();
|
|
|
|
|
2019-03-03 14:12:22 +00:00
|
|
|
return { std::move(divesToAdd), std::move(tripsToAdd), std::move(sitesToAdd) };
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This helper function is the counterpart fo removeDives(): it calls addDive() on a list
|
|
|
|
// of dives to be (re)added and returns a vector of the added dives. It does this in reverse
|
|
|
|
// order, so that trips are created appropriately and indexing is correct.
|
|
|
|
// The passed in vector is cleared.
|
2019-03-03 14:12:22 +00:00
|
|
|
DivesAndSitesToRemove DiveListBase::addDives(DivesAndTripsToAdd &toAdd)
|
2018-07-19 20:35:25 +00:00
|
|
|
{
|
|
|
|
std::vector<dive *> res;
|
2019-03-03 14:12:22 +00:00
|
|
|
std::vector<dive_site *> sites;
|
2019-06-23 07:22:26 +00:00
|
|
|
std::vector<std::pair<dive_trip *, dive *>> dives;
|
2018-11-26 21:06:17 +00:00
|
|
|
res.resize(toAdd.dives.size());
|
2019-03-03 14:12:22 +00:00
|
|
|
sites.reserve(toAdd.sites.size());
|
2019-06-23 07:22:26 +00:00
|
|
|
dives.reserve(toAdd.sites.size());
|
2018-07-19 20:35:25 +00:00
|
|
|
|
2019-05-15 09:40:39 +00:00
|
|
|
// Make sure that the dive list is sorted. The added dives will be sent in a signal
|
|
|
|
// and the recipients assume that the dives are sorted the same way as they are
|
|
|
|
// in the core list.
|
|
|
|
std::sort(toAdd.dives.begin(), toAdd.dives.end(),
|
|
|
|
[](const DiveToAdd &d, const DiveToAdd &d2)
|
2024-06-07 08:25:09 +00:00
|
|
|
{ return dive_less_than(*d.dive, *d2.dive); });
|
2019-05-15 09:40:39 +00:00
|
|
|
|
Dive list: implement proper Qt-model semantics for DiveTripModel
Previously, each dive-list modifying function would lead to a
full model reset. Instead, implement proper Qt-model semantics
using beginInsertRows()/endInsertRows(), beginRemoveRows()/
endRemoveRows(), dataChange().
To do so, a DiveListNotifer singleton is generatated, which
broadcasts all changes to the dive-list. Signals are sent by
the commands and received by the DiveTripModel. Signals are
batched by dive-trip. This seems to be an adequate compromise
for the two kinds of list-views (tree and list). In the common
usecase mostly dives of a single trip are affected.
Thus, batching of dives is performed in two positions:
- At command-level to batch by trip
- In DiveTripModel to feed batches of contiguous elements
to Qt's begin*/end*-functions.
This is conceptually simple, but rather complex code. To avoid
repetition of complex loops, the batching is implemented in
templated-functions, which are passed lambda-functions, which
are called for each batch.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-30 07:20:25 +00:00
|
|
|
// Now, add the dives
|
2018-08-13 03:17:23 +00:00
|
|
|
// Note: the idiomatic STL-way would be std::transform, but let's use a loop since
|
|
|
|
// that is closer to classical C-style.
|
|
|
|
auto it2 = res.rbegin();
|
2020-02-13 22:39:44 +00:00
|
|
|
QVector<dive *> divesToFilter;
|
|
|
|
divesToFilter.reserve(toAdd.dives.size());
|
2019-06-23 07:22:26 +00:00
|
|
|
for (auto it = toAdd.dives.rbegin(); it != toAdd.dives.rend(); ++it, ++it2) {
|
2018-08-13 03:17:23 +00:00
|
|
|
*it2 = addDive(*it);
|
2019-06-23 07:22:26 +00:00
|
|
|
dives.push_back({ (*it2)->divetrip, *it2 });
|
2020-02-13 22:39:44 +00:00
|
|
|
divesToFilter.push_back(*it2);
|
2019-06-23 07:22:26 +00:00
|
|
|
}
|
2018-11-26 21:06:17 +00:00
|
|
|
toAdd.dives.clear();
|
|
|
|
|
2020-02-13 22:39:44 +00:00
|
|
|
ShownChange change = DiveFilter::instance()->update(divesToFilter);
|
|
|
|
|
2018-11-26 21:06:17 +00:00
|
|
|
// If the dives belong to new trips, add these as well.
|
|
|
|
// Remember the pointers so that we can later check if a trip was newly added
|
|
|
|
std::vector<dive_trip *> addedTrips;
|
|
|
|
addedTrips.reserve(toAdd.trips.size());
|
2024-05-31 15:15:47 +00:00
|
|
|
for (std::unique_ptr<dive_trip> &trip: toAdd.trips) {
|
2024-06-08 14:53:55 +00:00
|
|
|
auto [t, idx] = divelog.trips.put(std::move(trip)); // Return ownership to backend
|
2024-06-01 20:05:57 +00:00
|
|
|
addedTrips.push_back(t);
|
2018-11-26 21:06:17 +00:00
|
|
|
}
|
|
|
|
toAdd.trips.clear();
|
2018-07-19 20:35:25 +00:00
|
|
|
|
2019-03-03 14:12:22 +00:00
|
|
|
// Finally, add any necessary dive sites
|
2024-05-04 11:39:04 +00:00
|
|
|
for (std::unique_ptr<dive_site> &ds: toAdd.sites) {
|
2024-06-08 14:30:24 +00:00
|
|
|
auto res = divelog.sites.register_site(std::move(ds));
|
2024-05-11 09:47:45 +00:00
|
|
|
sites.push_back(res.ptr);
|
|
|
|
emit diveListNotifier.diveSiteAdded(sites.back(), res.idx);
|
2019-03-03 14:12:22 +00:00
|
|
|
}
|
|
|
|
toAdd.sites.clear();
|
|
|
|
|
2019-02-11 14:34:43 +00:00
|
|
|
// Send signals by trip.
|
2019-06-23 07:22:26 +00:00
|
|
|
processByTrip(dives, [&](dive_trip *trip, const QVector<dive *> &divesInTrip) {
|
2018-11-26 21:06:17 +00:00
|
|
|
// Now, let's check if this trip is supposed to be created, by checking if it was marked as "add it".
|
Dive list: implement proper Qt-model semantics for DiveTripModel
Previously, each dive-list modifying function would lead to a
full model reset. Instead, implement proper Qt-model semantics
using beginInsertRows()/endInsertRows(), beginRemoveRows()/
endRemoveRows(), dataChange().
To do so, a DiveListNotifer singleton is generatated, which
broadcasts all changes to the dive-list. Signals are sent by
the commands and received by the DiveTripModel. Signals are
batched by dive-trip. This seems to be an adequate compromise
for the two kinds of list-views (tree and list). In the common
usecase mostly dives of a single trip are affected.
Thus, batching of dives is performed in two positions:
- At command-level to batch by trip
- In DiveTripModel to feed batches of contiguous elements
to Qt's begin*/end*-functions.
This is conceptually simple, but rather complex code. To avoid
repetition of complex loops, the batching is implemented in
templated-functions, which are passed lambda-functions, which
are called for each batch.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-30 07:20:25 +00:00
|
|
|
bool createTrip = trip && std::find(addedTrips.begin(), addedTrips.end(), trip) != addedTrips.end();
|
|
|
|
// Finally, emit the signal
|
|
|
|
emit diveListNotifier.divesAdded(trip, createTrip, divesInTrip);
|
|
|
|
});
|
2019-11-17 16:41:23 +00:00
|
|
|
|
2020-02-13 22:39:44 +00:00
|
|
|
if (!change.newShown.empty() || !change.newHidden.empty())
|
2019-11-17 16:41:23 +00:00
|
|
|
emit diveListNotifier.numShownChanged();
|
|
|
|
|
2024-01-15 20:22:20 +00:00
|
|
|
return { std::move(res), std::move(sites) };
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This helper function renumbers dives according to an array of id/number pairs.
|
|
|
|
// The old numbers are stored in the array, thus calling this function twice has no effect.
|
2018-07-30 13:55:29 +00:00
|
|
|
static void renumberDives(QVector<QPair<dive *, int>> &divesToRenumber)
|
2018-07-19 20:35:25 +00:00
|
|
|
{
|
2019-06-23 07:22:26 +00:00
|
|
|
QVector<dive *> dives;
|
|
|
|
dives.reserve(divesToRenumber.size());
|
2018-07-19 20:35:25 +00:00
|
|
|
for (auto &pair: divesToRenumber) {
|
2018-07-30 13:55:29 +00:00
|
|
|
dive *d = pair.first;
|
2018-07-19 20:35:25 +00:00
|
|
|
if (!d)
|
|
|
|
continue;
|
|
|
|
std::swap(d->number, pair.second);
|
2019-06-23 07:22:26 +00:00
|
|
|
dives.push_back(d);
|
2024-06-25 12:04:01 +00:00
|
|
|
d->invalidate_cache();
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
Dive list: implement proper Qt-model semantics for DiveTripModel
Previously, each dive-list modifying function would lead to a
full model reset. Instead, implement proper Qt-model semantics
using beginInsertRows()/endInsertRows(), beginRemoveRows()/
endRemoveRows(), dataChange().
To do so, a DiveListNotifer singleton is generatated, which
broadcasts all changes to the dive-list. Signals are sent by
the commands and received by the DiveTripModel. Signals are
batched by dive-trip. This seems to be an adequate compromise
for the two kinds of list-views (tree and list). In the common
usecase mostly dives of a single trip are affected.
Thus, batching of dives is performed in two positions:
- At command-level to batch by trip
- In DiveTripModel to feed batches of contiguous elements
to Qt's begin*/end*-functions.
This is conceptually simple, but rather complex code. To avoid
repetition of complex loops, the batching is implemented in
templated-functions, which are passed lambda-functions, which
are called for each batch.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-30 07:20:25 +00:00
|
|
|
|
|
|
|
// Send signals.
|
2019-06-23 07:22:26 +00:00
|
|
|
emit diveListNotifier.divesChanged(dives, DiveField::NR);
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This helper function moves a dive to a trip. The old trip is recorded in the
|
|
|
|
// passed-in structure. This means that calling the function twice on the same
|
|
|
|
// object is a no-op concerning the dive. If the old trip was deleted from the
|
|
|
|
// core, an owning pointer to the removed trip is returned, otherwise a null pointer.
|
2024-05-31 15:15:47 +00:00
|
|
|
static std::unique_ptr<dive_trip> moveDiveToTrip(DiveToTrip &diveToTrip)
|
2018-07-19 20:35:25 +00:00
|
|
|
{
|
|
|
|
// Firstly, check if we move to the same trip and bail if this is a no-op.
|
|
|
|
if (diveToTrip.trip == diveToTrip.dive->divetrip)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
// Remove from old trip
|
2024-05-31 15:15:47 +00:00
|
|
|
std::unique_ptr<dive_trip> res;
|
2018-07-19 20:35:25 +00:00
|
|
|
|
|
|
|
// Remove dive from trip - if this is the last dive in the trip, remove the whole trip.
|
2018-12-23 09:08:44 +00:00
|
|
|
dive_trip *trip = unregister_dive_from_trip(diveToTrip.dive);
|
2024-06-02 15:06:18 +00:00
|
|
|
if (trip && trip->dives.empty())
|
2024-06-01 20:05:57 +00:00
|
|
|
res = remove_trip_from_backend(trip); // Remove trip from backend
|
2018-07-19 20:35:25 +00:00
|
|
|
|
|
|
|
// Store old trip and get new trip we should associate this dive with
|
|
|
|
std::swap(trip, diveToTrip.trip);
|
2020-02-19 22:59:18 +00:00
|
|
|
if (trip)
|
2024-06-08 13:28:16 +00:00
|
|
|
trip->add_dive(diveToTrip.dive);
|
2024-06-25 12:04:01 +00:00
|
|
|
diveToTrip.dive->invalidate_cache(); // Ensure that dive is written in git_save()
|
2018-07-19 20:35:25 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This helper function moves a set of dives between trips using the
|
|
|
|
// moveDiveToTrip function. Before doing so, it adds the necessary trips to
|
|
|
|
// the core. Trips that are removed from the core because they are empty
|
|
|
|
// are recorded in the passed in struct. The vectors of trips and dives
|
|
|
|
// are reversed. Thus, calling the function twice on the same object is
|
|
|
|
// a no-op.
|
|
|
|
static void moveDivesBetweenTrips(DivesToTrip &dives)
|
|
|
|
{
|
Dive list: implement proper Qt-model semantics for DiveTripModel
Previously, each dive-list modifying function would lead to a
full model reset. Instead, implement proper Qt-model semantics
using beginInsertRows()/endInsertRows(), beginRemoveRows()/
endRemoveRows(), dataChange().
To do so, a DiveListNotifer singleton is generatated, which
broadcasts all changes to the dive-list. Signals are sent by
the commands and received by the DiveTripModel. Signals are
batched by dive-trip. This seems to be an adequate compromise
for the two kinds of list-views (tree and list). In the common
usecase mostly dives of a single trip are affected.
Thus, batching of dives is performed in two positions:
- At command-level to batch by trip
- In DiveTripModel to feed batches of contiguous elements
to Qt's begin*/end*-functions.
This is conceptually simple, but rather complex code. To avoid
repetition of complex loops, the batching is implemented in
templated-functions, which are passed lambda-functions, which
are called for each batch.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-30 07:20:25 +00:00
|
|
|
// We collect an array of created trips so that we can instruct
|
|
|
|
// the model to create a new entry
|
|
|
|
std::vector<dive_trip *> createdTrips;
|
|
|
|
createdTrips.reserve(dives.tripsToAdd.size());
|
|
|
|
|
|
|
|
// First, bring back the trip(s)
|
2024-05-31 15:15:47 +00:00
|
|
|
for (std::unique_ptr<dive_trip> &trip: dives.tripsToAdd) {
|
2024-06-08 14:53:55 +00:00
|
|
|
auto [t, idx] = divelog.trips.put(std::move(trip)); // Return ownership to backend
|
Dive list: implement proper Qt-model semantics for DiveTripModel
Previously, each dive-list modifying function would lead to a
full model reset. Instead, implement proper Qt-model semantics
using beginInsertRows()/endInsertRows(), beginRemoveRows()/
endRemoveRows(), dataChange().
To do so, a DiveListNotifer singleton is generatated, which
broadcasts all changes to the dive-list. Signals are sent by
the commands and received by the DiveTripModel. Signals are
batched by dive-trip. This seems to be an adequate compromise
for the two kinds of list-views (tree and list). In the common
usecase mostly dives of a single trip are affected.
Thus, batching of dives is performed in two positions:
- At command-level to batch by trip
- In DiveTripModel to feed batches of contiguous elements
to Qt's begin*/end*-functions.
This is conceptually simple, but rather complex code. To avoid
repetition of complex loops, the batching is implemented in
templated-functions, which are passed lambda-functions, which
are called for each batch.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-30 07:20:25 +00:00
|
|
|
createdTrips.push_back(t);
|
|
|
|
}
|
2018-07-19 20:35:25 +00:00
|
|
|
dives.tripsToAdd.clear();
|
|
|
|
|
|
|
|
for (DiveToTrip &dive: dives.divesToMove) {
|
2024-05-31 15:15:47 +00:00
|
|
|
std::unique_ptr<dive_trip> tripToAdd = moveDiveToTrip(dive);
|
2018-07-19 20:35:25 +00:00
|
|
|
// register trips that we'll have to readd
|
|
|
|
if (tripToAdd)
|
|
|
|
dives.tripsToAdd.push_back(std::move(tripToAdd));
|
|
|
|
}
|
|
|
|
|
Dive list: implement proper Qt-model semantics for DiveTripModel
Previously, each dive-list modifying function would lead to a
full model reset. Instead, implement proper Qt-model semantics
using beginInsertRows()/endInsertRows(), beginRemoveRows()/
endRemoveRows(), dataChange().
To do so, a DiveListNotifer singleton is generatated, which
broadcasts all changes to the dive-list. Signals are sent by
the commands and received by the DiveTripModel. Signals are
batched by dive-trip. This seems to be an adequate compromise
for the two kinds of list-views (tree and list). In the common
usecase mostly dives of a single trip are affected.
Thus, batching of dives is performed in two positions:
- At command-level to batch by trip
- In DiveTripModel to feed batches of contiguous elements
to Qt's begin*/end*-functions.
This is conceptually simple, but rather complex code. To avoid
repetition of complex loops, the batching is implemented in
templated-functions, which are passed lambda-functions, which
are called for each batch.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-30 07:20:25 +00:00
|
|
|
// We send one signal per from-trip/to-trip pair.
|
|
|
|
// First, collect all dives in a struct and sort by from-trip/to-trip.
|
|
|
|
struct DiveMoved {
|
|
|
|
dive_trip *from;
|
|
|
|
dive_trip *to;
|
|
|
|
dive *d;
|
|
|
|
};
|
|
|
|
std::vector<DiveMoved> divesMoved;
|
|
|
|
divesMoved.reserve(dives.divesToMove.size());
|
|
|
|
for (const DiveToTrip &entry: dives.divesToMove)
|
|
|
|
divesMoved.push_back({ entry.trip, entry.dive->divetrip, entry.dive });
|
|
|
|
|
|
|
|
// Sort lexicographically by from-trip, to-trip and by start-time.
|
|
|
|
// Use std::tie() for lexicographical sorting.
|
|
|
|
std::sort(divesMoved.begin(), divesMoved.end(), [] ( const DiveMoved &d1, const DiveMoved &d2)
|
|
|
|
{ return std::tie(d1.from, d1.to, d1.d->when) < std::tie(d2.from, d2.to, d2.d->when); });
|
|
|
|
|
|
|
|
// Now, process the dives in batches by trip
|
|
|
|
// TODO: this is a bit different from the cases above, so we don't use the processByTrip template,
|
|
|
|
// but repeat the loop here. We might think about generalizing the template, if more of such
|
|
|
|
// "special cases" appear.
|
|
|
|
size_t i, j; // Begin and end of batch
|
|
|
|
for (i = 0; i < divesMoved.size(); i = j) {
|
|
|
|
dive_trip *from = divesMoved[i].from;
|
|
|
|
dive_trip *to = divesMoved[i].to;
|
|
|
|
for (j = i + 1; j < divesMoved.size() && divesMoved[j].from == from && divesMoved[j].to == to; ++j)
|
|
|
|
; // pass
|
|
|
|
// Copy dives into a QVector. Some sort of "range_view" would be ideal, but Qt doesn't work this way.
|
|
|
|
QVector<dive *> divesInTrip(j - i);
|
|
|
|
for (size_t k = i; k < j; ++k)
|
|
|
|
divesInTrip[k - i] = divesMoved[k].d;
|
|
|
|
|
2018-12-08 20:15:59 +00:00
|
|
|
// Check if the from-trip was deleted: If yes, it was recorded in the tripsToAdd structure.
|
|
|
|
// Only set the flag if this is that last time this trip is featured.
|
Dive list: implement proper Qt-model semantics for DiveTripModel
Previously, each dive-list modifying function would lead to a
full model reset. Instead, implement proper Qt-model semantics
using beginInsertRows()/endInsertRows(), beginRemoveRows()/
endRemoveRows(), dataChange().
To do so, a DiveListNotifer singleton is generatated, which
broadcasts all changes to the dive-list. Signals are sent by
the commands and received by the DiveTripModel. Signals are
batched by dive-trip. This seems to be an adequate compromise
for the two kinds of list-views (tree and list). In the common
usecase mostly dives of a single trip are affected.
Thus, batching of dives is performed in two positions:
- At command-level to batch by trip
- In DiveTripModel to feed batches of contiguous elements
to Qt's begin*/end*-functions.
This is conceptually simple, but rather complex code. To avoid
repetition of complex loops, the batching is implemented in
templated-functions, which are passed lambda-functions, which
are called for each batch.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-30 07:20:25 +00:00
|
|
|
bool deleteFrom = from &&
|
2018-12-08 20:15:59 +00:00
|
|
|
std::find_if(divesMoved.begin() + j, divesMoved.end(), // Is this the last occurence of "from"?
|
|
|
|
[from](const DiveMoved &entry) { return entry.from == from; }) == divesMoved.end() &&
|
|
|
|
std::find_if(dives.tripsToAdd.begin(), dives.tripsToAdd.end(), // Is "from" in tripsToAdd?
|
2024-05-31 15:15:47 +00:00
|
|
|
[from](const std::unique_ptr<dive_trip> &trip) { return trip.get() == from; }) != dives.tripsToAdd.end();
|
Dive list: implement proper Qt-model semantics for DiveTripModel
Previously, each dive-list modifying function would lead to a
full model reset. Instead, implement proper Qt-model semantics
using beginInsertRows()/endInsertRows(), beginRemoveRows()/
endRemoveRows(), dataChange().
To do so, a DiveListNotifer singleton is generatated, which
broadcasts all changes to the dive-list. Signals are sent by
the commands and received by the DiveTripModel. Signals are
batched by dive-trip. This seems to be an adequate compromise
for the two kinds of list-views (tree and list). In the common
usecase mostly dives of a single trip are affected.
Thus, batching of dives is performed in two positions:
- At command-level to batch by trip
- In DiveTripModel to feed batches of contiguous elements
to Qt's begin*/end*-functions.
This is conceptually simple, but rather complex code. To avoid
repetition of complex loops, the batching is implemented in
templated-functions, which are passed lambda-functions, which
are called for each batch.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-30 07:20:25 +00:00
|
|
|
// Check if the to-trip has to be created. For this purpose, we saved an array of trips to be created.
|
|
|
|
bool createTo = false;
|
|
|
|
if (to) {
|
|
|
|
// Check if the element is there...
|
|
|
|
auto it = std::find(createdTrips.begin(), createdTrips.end(), to);
|
|
|
|
|
|
|
|
// ...if it is - remove it as we don't want the model to create the trip twice!
|
|
|
|
if (it != createdTrips.end()) {
|
|
|
|
createTo = true;
|
|
|
|
// erase/remove would be more performant, but this is irrelevant in the big scheme of things.
|
|
|
|
createdTrips.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, emit the signal
|
|
|
|
emit diveListNotifier.divesMovedBetweenTrips(from, to, deleteFrom, createTo, divesInTrip);
|
|
|
|
}
|
|
|
|
|
2018-07-19 20:35:25 +00:00
|
|
|
// Reverse the tripsToAdd and the divesToAdd, so that on undo/redo the operations
|
|
|
|
// will be performed in reverse order.
|
|
|
|
std::reverse(dives.tripsToAdd.begin(), dives.tripsToAdd.end());
|
|
|
|
std::reverse(dives.divesToMove.begin(), dives.divesToMove.end());
|
|
|
|
}
|
|
|
|
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
void DiveListBase::initWork()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveListBase::finishWork()
|
|
|
|
{
|
2019-03-10 15:03:39 +00:00
|
|
|
for (dive_site *ds: sitesCountChanged)
|
|
|
|
emit diveListNotifier.diveSiteDiveCountChanged(ds);
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DiveListBase::undo()
|
|
|
|
{
|
|
|
|
initWork();
|
|
|
|
undoit();
|
|
|
|
finishWork();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveListBase::redo()
|
|
|
|
{
|
|
|
|
initWork();
|
|
|
|
redoit();
|
|
|
|
finishWork();
|
|
|
|
}
|
|
|
|
|
2024-06-21 14:43:27 +00:00
|
|
|
AddDive::AddDive(std::unique_ptr<dive> d, bool autogroup, bool newNumber)
|
2018-07-19 20:35:25 +00:00
|
|
|
{
|
2020-03-21 23:46:36 +00:00
|
|
|
setText(Command::Base::tr("add dive"));
|
2024-09-03 15:04:48 +00:00
|
|
|
d->maxdepth = 0_m;
|
|
|
|
d->dcs[0].maxdepth = 0_m;
|
2024-06-23 12:20:59 +00:00
|
|
|
divelog.dives.fixup_dive(*d);
|
2018-07-30 08:33:25 +00:00
|
|
|
|
2019-10-27 11:22:56 +00:00
|
|
|
// this only matters if undoit were called before redoit
|
|
|
|
currentDive = nullptr;
|
|
|
|
|
2024-06-21 14:43:27 +00:00
|
|
|
d->selected = false; // If we clone a planned dive, it might have been selected.
|
|
|
|
// We have to clear the flag, as selections will be managed
|
|
|
|
// on dive-addition.
|
2018-07-30 08:33:25 +00:00
|
|
|
|
|
|
|
// If we alloc a new-trip for autogrouping, get an owning pointer to it.
|
2024-05-31 15:15:47 +00:00
|
|
|
std::unique_ptr<dive_trip> allocTrip;
|
2024-06-21 14:43:27 +00:00
|
|
|
dive_trip *trip = d->divetrip;
|
|
|
|
dive_site *site = d->dive_site;
|
2019-03-05 21:58:47 +00:00
|
|
|
// We have to delete the pointers to trip and site, because this would prevent the core from adding to the
|
|
|
|
// trip or site and we would get the count-of-dives in the trip or site wrong. Yes, that's all horribly subtle!
|
2024-06-21 14:43:27 +00:00
|
|
|
d->divetrip = nullptr;
|
|
|
|
d->dive_site = nullptr;
|
Undo: make adding of planned dive undo-able
Planned dives were still added by directly calling core code.
This could confuse the undo-machinery, leading to crashes.
Instead, use the proper undo-command. The problem is that as
opposed to the other AddDive-commands, planned dives may
belong to a trip. Thus, the interface to the AddDive command
was changed to respect the divetrip field. Make sure that
the other callers reset that field (actually, it should never
be set). Add a comment describing the perhaps surprising
interface (the passed-in dive, usually displayed dive, is
reset).
Moreover, a dive cloned in the planner is not assigned a
new number. Thus, add an argument to the AddDive-command,
which expresses whether a new number should be generated
for the to-be-added dive.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-09-08 17:58:11 +00:00
|
|
|
if (!trip && autogroup) {
|
2024-06-21 14:43:27 +00:00
|
|
|
auto [t, allocated] = get_trip_for_new_dive(divelog, d.get());
|
2024-06-01 20:05:57 +00:00
|
|
|
trip = t;
|
|
|
|
allocTrip = std::move(allocated);
|
2018-07-30 08:33:25 +00:00
|
|
|
}
|
|
|
|
|
2024-06-21 14:43:27 +00:00
|
|
|
int idx = divelog.dives.get_insertion_index(d.get());
|
Undo: make adding of planned dive undo-able
Planned dives were still added by directly calling core code.
This could confuse the undo-machinery, leading to crashes.
Instead, use the proper undo-command. The problem is that as
opposed to the other AddDive-commands, planned dives may
belong to a trip. Thus, the interface to the AddDive command
was changed to respect the divetrip field. Make sure that
the other callers reset that field (actually, it should never
be set). Add a comment describing the perhaps surprising
interface (the passed-in dive, usually displayed dive, is
reset).
Moreover, a dive cloned in the planner is not assigned a
new number. Thus, add an argument to the AddDive-command,
which expresses whether a new number should be generated
for the to-be-added dive.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-09-08 17:58:11 +00:00
|
|
|
if (newNumber)
|
2024-06-21 14:43:27 +00:00
|
|
|
d->number = divelog.dives.get_dive_nr_at_idx(idx);
|
Dive list: implement proper Qt-model semantics for DiveTripModel
Previously, each dive-list modifying function would lead to a
full model reset. Instead, implement proper Qt-model semantics
using beginInsertRows()/endInsertRows(), beginRemoveRows()/
endRemoveRows(), dataChange().
To do so, a DiveListNotifer singleton is generatated, which
broadcasts all changes to the dive-list. Signals are sent by
the commands and received by the DiveTripModel. Signals are
batched by dive-trip. This seems to be an adequate compromise
for the two kinds of list-views (tree and list). In the common
usecase mostly dives of a single trip are affected.
Thus, batching of dives is performed in two positions:
- At command-level to batch by trip
- In DiveTripModel to feed batches of contiguous elements
to Qt's begin*/end*-functions.
This is conceptually simple, but rather complex code. To avoid
repetition of complex loops, the batching is implemented in
templated-functions, which are passed lambda-functions, which
are called for each batch.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-30 07:20:25 +00:00
|
|
|
|
2024-06-21 14:43:27 +00:00
|
|
|
divesToAdd.dives.push_back({ std::move(d), trip, site });
|
2018-11-26 21:06:17 +00:00
|
|
|
if (allocTrip)
|
|
|
|
divesToAdd.trips.push_back(std::move(allocTrip));
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AddDive::workToBeDone()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
void AddDive::redoit()
|
2018-07-19 20:35:25 +00:00
|
|
|
{
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
// Remember selection so that we can undo it
|
|
|
|
selection = getDiveSelection();
|
|
|
|
currentDive = current_dive;
|
Dive list view: replace signal-magic by flag
In DiveListView, we have a very fundamental problem: When
On the one hand, we get informed of user-selection in the
DiveListView::selectionChanged() slot. This has to set the
correct flags in the C-backend.
On the other hand, sometimes we have to set the selection
programatically, e.g. when selecting a trip. This is done
by calling QItemSelectionModel::select().
But: this will *also* call into the above slot, in which
we can't tell whether it was a user interaction or an
internal call. This can lead to either infinite loops or
very inefficient behavior, because the current dive
is set numerous times.
The current code is aware of that and disconnects the
corresponding signal. This is scary, as these signals are
set internally by the model and view. Replace this
by a global "command executing" flag in DiveListNotifier.
The flag is set using a "marker" class, which resets the flag
once it goes out of scope (cf. RAII pattern).
In DiveListView, only process a selection if the flag is not
set. Otherwise simply call the QTreeView base class, to reflect
the new selection in the UI.
To have a common point for notifications of selection changes,
add such a signal to DiveListNotifier. This signal will be
used by the DiveListView as well as the Command-objects.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-01 17:23:43 +00:00
|
|
|
|
2019-03-03 14:12:22 +00:00
|
|
|
divesAndSitesToRemove = addDives(divesToAdd);
|
2024-06-08 14:53:55 +00:00
|
|
|
divelog.trips.sort(); // Though unlikely, adding a dive may reorder trips
|
2018-07-19 20:35:25 +00:00
|
|
|
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
// Select the newly added dive
|
2022-04-04 16:57:28 +00:00
|
|
|
setSelection(divesAndSitesToRemove.dives, divesAndSitesToRemove.dives[0], -1);
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
void AddDive::undoit()
|
2018-07-19 20:35:25 +00:00
|
|
|
{
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
// Simply remove the dive that was previously added...
|
2019-03-03 14:12:22 +00:00
|
|
|
divesToAdd = removeDives(divesAndSitesToRemove);
|
2024-06-08 14:53:55 +00:00
|
|
|
divelog.trips.sort(); // Though unlikely, removing a dive may reorder trips
|
2018-07-19 20:35:25 +00:00
|
|
|
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
// ...and restore the selection
|
2022-04-04 16:57:28 +00:00
|
|
|
setSelection(selection, currentDive, -1);
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
2022-11-12 07:40:04 +00:00
|
|
|
ImportDives::ImportDives(struct divelog *log, int flags, const QString &source)
|
2018-12-23 22:45:12 +00:00
|
|
|
{
|
2024-06-07 08:25:09 +00:00
|
|
|
setText(Command::Base::tr("import %n dive(s) from %1", "", log->dives.size()).arg(source));
|
2018-12-23 22:45:12 +00:00
|
|
|
|
2019-10-27 11:22:56 +00:00
|
|
|
// this only matters if undoit were called before redoit
|
|
|
|
currentDive = nullptr;
|
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
auto [dives_to_add, dives_to_remove, trips_to_add, sites_to_add, devices_to_add] =
|
2024-06-19 20:45:25 +00:00
|
|
|
divelog.process_imported_dives(*log, flags);
|
2024-06-07 08:25:09 +00:00
|
|
|
|
|
|
|
// Add devices to devicesToAddAndRemove structure
|
|
|
|
devicesToAddAndRemove = std::move(devices_to_add);
|
2018-12-23 22:45:12 +00:00
|
|
|
|
|
|
|
// Add trips to the divesToAdd.trips structure
|
2024-06-01 20:05:57 +00:00
|
|
|
divesToAdd.trips.reserve(trips_to_add.size());
|
|
|
|
for (auto &trip: trips_to_add)
|
|
|
|
divesToAdd.trips.push_back(std::move(trip));
|
2018-12-23 22:45:12 +00:00
|
|
|
|
2019-03-03 14:12:22 +00:00
|
|
|
// Add sites to the divesToAdd.sites structure
|
2024-05-11 09:47:45 +00:00
|
|
|
divesToAdd.sites = std::move(sites_to_add);
|
2019-03-03 14:12:22 +00:00
|
|
|
|
2018-12-23 22:45:12 +00:00
|
|
|
// Add dives to the divesToAdd.dives structure
|
2024-06-07 08:25:09 +00:00
|
|
|
divesToAdd.dives.reserve(dives_to_add.size());
|
|
|
|
for (auto &divePtr: dives_to_add) {
|
2018-12-23 22:45:12 +00:00
|
|
|
divePtr->selected = false; // See above in AddDive::AddDive()
|
|
|
|
dive_trip *trip = divePtr->divetrip;
|
|
|
|
divePtr->divetrip = nullptr; // See above in AddDive::AddDive()
|
2019-03-05 21:58:47 +00:00
|
|
|
dive_site *site = divePtr->dive_site;
|
|
|
|
divePtr->dive_site = nullptr; // See above in AddDive::AddDive()
|
2018-12-23 22:45:12 +00:00
|
|
|
|
2019-04-14 13:21:00 +00:00
|
|
|
divesToAdd.dives.push_back({ std::move(divePtr), trip, site });
|
2018-12-23 22:45:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add dive to be deleted to the divesToRemove structure
|
2024-06-07 08:25:09 +00:00
|
|
|
divesAndSitesToRemove.dives = std::move(dives_to_remove);
|
2020-06-28 13:24:19 +00:00
|
|
|
|
|
|
|
// When encountering filter presets with equal names, check whether they are
|
|
|
|
// the same. If they are, ignore them.
|
2024-06-08 16:54:23 +00:00
|
|
|
for (const filter_preset &preset: log->filter_presets) {
|
2024-02-29 12:39:17 +00:00
|
|
|
std::string name = preset.name;
|
2024-06-08 16:54:23 +00:00
|
|
|
auto it = std::find_if(divelog.filter_presets.begin(), divelog.filter_presets.end(),
|
2022-11-12 07:40:04 +00:00
|
|
|
[&name](const filter_preset &preset) { return preset.name == name; });
|
2024-06-08 16:54:23 +00:00
|
|
|
if (it != divelog.filter_presets.end() && it->data == preset.data)
|
2022-11-12 07:40:04 +00:00
|
|
|
continue;
|
|
|
|
filterPresetsToAdd.emplace_back(preset.name, preset.data);
|
2020-06-28 13:24:19 +00:00
|
|
|
}
|
2018-12-23 22:45:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ImportDives::workToBeDone()
|
|
|
|
{
|
|
|
|
return !divesToAdd.dives.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImportDives::redoit()
|
|
|
|
{
|
|
|
|
// Remember selection so that we can undo it
|
|
|
|
currentDive = current_dive;
|
|
|
|
|
2019-03-03 14:12:22 +00:00
|
|
|
// Add new dives and sites
|
|
|
|
DivesAndSitesToRemove divesAndSitesToRemoveNew = addDives(divesToAdd);
|
2018-12-23 22:45:12 +00:00
|
|
|
|
2019-03-03 14:12:22 +00:00
|
|
|
// Remove old dives and sites
|
|
|
|
divesToAdd = removeDives(divesAndSitesToRemove);
|
2018-12-23 22:45:12 +00:00
|
|
|
|
|
|
|
// Select the newly added dives
|
2022-04-04 16:57:28 +00:00
|
|
|
setSelection(divesAndSitesToRemoveNew.dives, divesAndSitesToRemoveNew.dives.back(), -1);
|
2018-12-23 22:45:12 +00:00
|
|
|
|
2019-03-03 14:12:22 +00:00
|
|
|
// Remember dives and sites to remove
|
|
|
|
divesAndSitesToRemove = std::move(divesAndSitesToRemoveNew);
|
2020-06-28 13:24:19 +00:00
|
|
|
|
2020-10-17 14:07:39 +00:00
|
|
|
// Add devices
|
2024-05-31 05:08:54 +00:00
|
|
|
for (const device &dev: devicesToAddAndRemove)
|
|
|
|
add_to_device_table(divelog.devices, dev);
|
2020-10-17 14:07:39 +00:00
|
|
|
|
2020-06-28 13:24:19 +00:00
|
|
|
// Add new filter presets
|
|
|
|
for (auto &it: filterPresetsToAdd) {
|
2024-06-08 16:54:23 +00:00
|
|
|
filterPresetsToRemove.push_back(divelog.filter_presets.add(it.first, it.second));
|
2020-06-28 13:24:19 +00:00
|
|
|
emit diveListNotifier.filterPresetAdded(filterPresetsToRemove.back());
|
|
|
|
}
|
|
|
|
filterPresetsToAdd.clear();
|
2020-11-14 15:59:35 +00:00
|
|
|
|
|
|
|
emit diveListNotifier.divesImported();
|
2018-12-23 22:45:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ImportDives::undoit()
|
|
|
|
{
|
2019-03-03 14:12:22 +00:00
|
|
|
// Add new dives and sites
|
|
|
|
DivesAndSitesToRemove divesAndSitesToRemoveNew = addDives(divesToAdd);
|
2018-12-23 22:45:12 +00:00
|
|
|
|
2019-03-03 14:12:22 +00:00
|
|
|
// Remove old dives and sites
|
|
|
|
divesToAdd = removeDives(divesAndSitesToRemove);
|
2018-12-23 22:45:12 +00:00
|
|
|
|
2019-03-03 14:12:22 +00:00
|
|
|
// Remember dives and sites to remove
|
|
|
|
divesAndSitesToRemove = std::move(divesAndSitesToRemoveNew);
|
2018-12-23 22:45:12 +00:00
|
|
|
|
|
|
|
// ...and restore the selection
|
2022-04-04 16:57:28 +00:00
|
|
|
setSelection(selection, currentDive, -1);
|
2020-06-28 13:24:19 +00:00
|
|
|
|
2020-10-17 14:07:39 +00:00
|
|
|
// Remove devices
|
2024-05-31 05:08:54 +00:00
|
|
|
for (const device &dev: devicesToAddAndRemove)
|
|
|
|
remove_device(divelog.devices, dev);
|
2020-10-17 14:07:39 +00:00
|
|
|
|
2020-06-28 13:24:19 +00:00
|
|
|
// Remove filter presets. Do this in reverse order.
|
|
|
|
for (auto it = filterPresetsToRemove.rbegin(); it != filterPresetsToRemove.rend(); ++it) {
|
|
|
|
int index = *it;
|
2024-06-08 16:54:23 +00:00
|
|
|
const filter_preset &preset = divelog.filter_presets[index];
|
|
|
|
std::string oldName = preset.name;
|
|
|
|
FilterData oldData = preset.data;
|
|
|
|
divelog.filter_presets.remove(index);
|
2020-06-28 13:24:19 +00:00
|
|
|
emit diveListNotifier.filterPresetRemoved(index);
|
|
|
|
filterPresetsToAdd.emplace_back(oldName, oldData);
|
|
|
|
}
|
|
|
|
filterPresetsToRemove.clear();
|
|
|
|
std::reverse(filterPresetsToAdd.begin(), filterPresetsToAdd.end());
|
2018-12-23 22:45:12 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 14:12:22 +00:00
|
|
|
DeleteDive::DeleteDive(const QVector<struct dive*> &divesToDeleteIn)
|
2018-07-19 20:35:25 +00:00
|
|
|
{
|
2020-01-06 20:47:53 +00:00
|
|
|
divesToDelete.dives = std::vector<dive *>(divesToDeleteIn.begin(), divesToDeleteIn.end());
|
2020-03-21 23:46:36 +00:00
|
|
|
setText(QStringLiteral("%1 [%2]").arg(Command::Base::tr("delete %n dive(s)", "", divesToDelete.dives.size())).arg(getListOfDives(divesToDelete.dives)));
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DeleteDive::workToBeDone()
|
|
|
|
{
|
2019-03-03 14:12:22 +00:00
|
|
|
return !divesToDelete.dives.empty();
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
void DeleteDive::undoit()
|
2018-07-19 20:35:25 +00:00
|
|
|
{
|
|
|
|
divesToDelete = addDives(divesToAdd);
|
2024-06-08 14:53:55 +00:00
|
|
|
divelog.trips.sort(); // Though unlikely, removing a dive may reorder trips
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
|
|
|
|
// Select all re-added dives and make the first one current
|
2019-03-03 14:12:22 +00:00
|
|
|
dive *currentDive = !divesToDelete.dives.empty() ? divesToDelete.dives[0] : nullptr;
|
2022-04-04 16:57:28 +00:00
|
|
|
setSelection(divesToDelete.dives, currentDive, -1);
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
void DeleteDive::redoit()
|
2018-07-19 20:35:25 +00:00
|
|
|
{
|
|
|
|
divesToAdd = removeDives(divesToDelete);
|
2024-06-08 14:53:55 +00:00
|
|
|
divelog.trips.sort(); // Though unlikely, adding a dive may reorder trips
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
|
|
|
|
// Deselect all dives and select dive that was close to the first deleted dive
|
|
|
|
dive *newCurrent = nullptr;
|
2018-11-26 21:06:17 +00:00
|
|
|
if (!divesToAdd.dives.empty()) {
|
|
|
|
timestamp_t when = divesToAdd.dives[0].dive->when;
|
2024-06-18 18:57:10 +00:00
|
|
|
newCurrent = divelog.dives.find_next_visible_dive(when);
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
}
|
2019-12-06 08:47:59 +00:00
|
|
|
select_single_dive(newCurrent);
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-03 15:04:54 +00:00
|
|
|
ShiftTime::ShiftTime(std::vector<dive *> changedDives, int amount)
|
2018-07-19 20:35:25 +00:00
|
|
|
: diveList(changedDives), timeChanged(amount)
|
|
|
|
{
|
2020-03-21 23:46:36 +00:00
|
|
|
setText(QStringLiteral("%1 [%2]").arg(Command::Base::tr("shift time of %n dives", "", changedDives.size())).arg(getListOfDives(changedDives)));
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
void ShiftTime::redoit()
|
2018-07-19 20:35:25 +00:00
|
|
|
{
|
2019-06-23 07:22:26 +00:00
|
|
|
std::vector<dive_trip *> trips;
|
|
|
|
for (dive *d: diveList) {
|
2018-12-20 18:03:44 +00:00
|
|
|
d->when += timeChanged;
|
2019-06-23 07:22:26 +00:00
|
|
|
if (d->divetrip && std::find(trips.begin(), trips.end(), d->divetrip) == trips.end())
|
|
|
|
trips.push_back(d->divetrip);
|
|
|
|
}
|
2018-07-19 20:35:25 +00:00
|
|
|
|
2019-06-23 07:22:26 +00:00
|
|
|
// Changing times may have unsorted the dive and trip tables
|
2024-06-07 08:25:09 +00:00
|
|
|
divelog.dives.sort();
|
2024-06-08 14:53:55 +00:00
|
|
|
divelog.trips.sort();
|
2019-06-23 07:22:26 +00:00
|
|
|
for (dive_trip *trip: trips)
|
2024-06-02 15:06:18 +00:00
|
|
|
trip->sort_dives();
|
Dive list: implement proper Qt-model semantics for DiveTripModel
Previously, each dive-list modifying function would lead to a
full model reset. Instead, implement proper Qt-model semantics
using beginInsertRows()/endInsertRows(), beginRemoveRows()/
endRemoveRows(), dataChange().
To do so, a DiveListNotifer singleton is generatated, which
broadcasts all changes to the dive-list. Signals are sent by
the commands and received by the DiveTripModel. Signals are
batched by dive-trip. This seems to be an adequate compromise
for the two kinds of list-views (tree and list). In the common
usecase mostly dives of a single trip are affected.
Thus, batching of dives is performed in two positions:
- At command-level to batch by trip
- In DiveTripModel to feed batches of contiguous elements
to Qt's begin*/end*-functions.
This is conceptually simple, but rather complex code. To avoid
repetition of complex loops, the batching is implemented in
templated-functions, which are passed lambda-functions, which
are called for each batch.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-07-30 07:20:25 +00:00
|
|
|
|
2019-06-23 07:22:26 +00:00
|
|
|
// Send signals
|
2020-10-25 21:42:40 +00:00
|
|
|
QVector<dive *> dives = stdToQt<dive *>(diveList);
|
2020-01-03 15:04:54 +00:00
|
|
|
emit diveListNotifier.divesTimeChanged(timeChanged, dives);
|
|
|
|
emit diveListNotifier.divesChanged(dives, DiveField::DATETIME);
|
2018-07-19 20:35:25 +00:00
|
|
|
|
2019-06-23 09:13:41 +00:00
|
|
|
// Select the changed dives
|
2022-04-04 16:57:28 +00:00
|
|
|
setSelection(diveList, diveList[0], -1);
|
2019-06-23 09:13:41 +00:00
|
|
|
|
2018-07-19 20:35:25 +00:00
|
|
|
// Negate the time-shift so that the next call does the reverse
|
|
|
|
timeChanged = -timeChanged;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShiftTime::workToBeDone()
|
|
|
|
{
|
2020-01-03 15:04:54 +00:00
|
|
|
return !diveList.empty();
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
void ShiftTime::undoit()
|
2018-07-19 20:35:25 +00:00
|
|
|
{
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
// Same as redoit(), since after redoit() we reversed the timeOffset
|
|
|
|
redoit();
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-30 13:55:29 +00:00
|
|
|
RenumberDives::RenumberDives(const QVector<QPair<dive *, int>> &divesToRenumberIn) : divesToRenumber(divesToRenumberIn)
|
2018-07-19 20:35:25 +00:00
|
|
|
{
|
2020-03-05 17:00:00 +00:00
|
|
|
std::vector<struct dive *> dives;
|
|
|
|
dives.reserve(divesToRenumber.length());
|
|
|
|
for (QPair<dive *, int>divePlusNumber: divesToRenumber)
|
|
|
|
dives.push_back(divePlusNumber.first);
|
2020-03-21 23:46:36 +00:00
|
|
|
setText(QStringLiteral("%1 [%2]").arg(Command::Base::tr("renumber %n dive(s)", "", divesToRenumber.count())).arg(getListOfDives(dives)));
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
void RenumberDives::undoit()
|
2018-07-19 20:35:25 +00:00
|
|
|
{
|
|
|
|
renumberDives(divesToRenumber);
|
2019-06-23 09:13:41 +00:00
|
|
|
|
|
|
|
// Select the changed dives
|
|
|
|
std::vector<dive *> dives;
|
|
|
|
dives.reserve(divesToRenumber.size());
|
|
|
|
for (const QPair<dive *, int> &item: divesToRenumber)
|
|
|
|
dives.push_back(item.first);
|
2022-04-04 16:57:28 +00:00
|
|
|
setSelection(dives, dives[0], -1);
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RenumberDives::workToBeDone()
|
|
|
|
{
|
|
|
|
return !divesToRenumber.isEmpty();
|
|
|
|
}
|
|
|
|
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
void RenumberDives::redoit()
|
2018-07-19 20:35:25 +00:00
|
|
|
{
|
|
|
|
// Redo and undo do the same thing!
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
undoit();
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TripBase::workToBeDone()
|
|
|
|
{
|
|
|
|
return !divesToMove.divesToMove.empty();
|
|
|
|
}
|
|
|
|
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
void TripBase::redoit()
|
2018-07-19 20:35:25 +00:00
|
|
|
{
|
|
|
|
moveDivesBetweenTrips(divesToMove);
|
2024-06-08 14:53:55 +00:00
|
|
|
divelog.trips.sort(); // Though unlikely, moving dives may reorder trips
|
2019-06-23 09:13:41 +00:00
|
|
|
|
|
|
|
// Select the moved dives
|
|
|
|
std::vector<dive *> dives;
|
|
|
|
dives.reserve(divesToMove.divesToMove.size());
|
|
|
|
for (const DiveToTrip &item: divesToMove.divesToMove)
|
|
|
|
dives.push_back(item.dive);
|
2022-04-04 16:57:28 +00:00
|
|
|
setSelection(dives, dives[0], -1);
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
void TripBase::undoit()
|
2018-07-19 20:35:25 +00:00
|
|
|
{
|
|
|
|
// Redo and undo do the same thing!
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
redoit();
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
2020-11-24 19:17:27 +00:00
|
|
|
RemoveDivesFromTrip::RemoveDivesFromTrip(const QVector<dive *> &divesToRemoveIn)
|
2018-07-19 20:35:25 +00:00
|
|
|
{
|
2020-11-24 19:17:27 +00:00
|
|
|
// Filter out dives outside of trip. Note: This is in a separate loop to get the command-description right.
|
|
|
|
QVector<dive *> divesToRemove;
|
|
|
|
for (dive *d: divesToRemoveIn) {
|
|
|
|
if (d->divetrip)
|
|
|
|
divesToRemove.push_back(d);
|
|
|
|
}
|
2020-03-21 23:46:36 +00:00
|
|
|
setText(QStringLiteral("%1 [%2]").arg(Command::Base::tr("remove %n dive(s) from trip", "", divesToRemove.size())).arg(getListOfDives(divesToRemove)));
|
2018-07-19 20:35:25 +00:00
|
|
|
divesToMove.divesToMove.reserve(divesToRemove.size());
|
2019-02-27 22:10:24 +00:00
|
|
|
for (dive *d: divesToRemove) {
|
|
|
|
// If a user manually removes a dive from a trip, don't autogroup this dive.
|
|
|
|
// The flag will not be reset on undo, but that should be acceptable.
|
|
|
|
d->notrip = true;
|
2018-07-19 20:35:25 +00:00
|
|
|
divesToMove.divesToMove.push_back( {d, nullptr} );
|
2019-02-27 22:10:24 +00:00
|
|
|
}
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RemoveAutogenTrips::RemoveAutogenTrips()
|
|
|
|
{
|
2020-03-21 23:46:36 +00:00
|
|
|
setText(Command::Base::tr("remove autogenerated trips"));
|
2018-07-19 20:35:25 +00:00
|
|
|
// TODO: don't touch core-innards directly
|
2024-06-07 08:25:09 +00:00
|
|
|
for (auto &d: divelog.dives) {
|
|
|
|
if (d->divetrip && d->divetrip->autogen)
|
|
|
|
divesToMove.divesToMove.push_back( {d.get(), nullptr} );
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AddDivesToTrip::AddDivesToTrip(const QVector<dive *> &divesToAddIn, dive_trip *trip)
|
|
|
|
{
|
2020-03-21 23:46:36 +00:00
|
|
|
setText(QStringLiteral("%1 [%2]").arg(Command::Base::tr("add %n dives to trip", "", divesToAddIn.size())).arg(getListOfDives(divesToAddIn)));
|
2018-07-19 20:35:25 +00:00
|
|
|
for (dive *d: divesToAddIn)
|
|
|
|
divesToMove.divesToMove.push_back( {d, trip} );
|
|
|
|
}
|
|
|
|
|
|
|
|
CreateTrip::CreateTrip(const QVector<dive *> &divesToAddIn)
|
|
|
|
{
|
2020-03-21 23:46:36 +00:00
|
|
|
setText(Command::Base::tr("create trip"));
|
2018-07-19 20:35:25 +00:00
|
|
|
|
|
|
|
if (divesToAddIn.isEmpty())
|
|
|
|
return;
|
|
|
|
|
2024-06-01 20:05:57 +00:00
|
|
|
auto trip = create_trip_from_dive(divesToAddIn[0]);
|
2018-07-19 20:35:25 +00:00
|
|
|
for (dive *d: divesToAddIn)
|
2024-06-01 20:05:57 +00:00
|
|
|
divesToMove.divesToMove.push_back( { d, trip.get() });
|
|
|
|
divesToMove.tripsToAdd.push_back(std::move(trip));
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AutogroupDives::AutogroupDives()
|
|
|
|
{
|
2020-03-21 23:46:36 +00:00
|
|
|
setText(Command::Base::tr("autogroup dives"));
|
2018-07-19 20:35:25 +00:00
|
|
|
|
2024-06-07 08:25:09 +00:00
|
|
|
for (auto &entry: get_dives_to_autogroup(divelog.dives)) {
|
2018-07-19 20:35:25 +00:00
|
|
|
// If this is an allocated trip, take ownership
|
2024-06-01 20:05:57 +00:00
|
|
|
if (entry.created_trip)
|
|
|
|
divesToMove.tripsToAdd.push_back(std::move(entry.created_trip));
|
2024-06-07 08:25:09 +00:00
|
|
|
for (auto it = divelog.dives.begin() + entry.from; it != divelog.dives.begin() + entry.to; ++it)
|
|
|
|
divesToMove.divesToMove.push_back( { it->get(), entry.trip } );
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MergeTrips::MergeTrips(dive_trip *trip1, dive_trip *trip2)
|
|
|
|
{
|
|
|
|
if (trip1 == trip2)
|
|
|
|
return;
|
2024-06-01 20:05:57 +00:00
|
|
|
std::unique_ptr<dive_trip> newTrip = combine_trips(trip1, trip2);
|
2024-06-02 15:06:18 +00:00
|
|
|
for (dive *d: trip1->dives)
|
|
|
|
divesToMove.divesToMove.push_back( { d, newTrip.get() } );
|
|
|
|
for (dive *d: trip2->dives)
|
|
|
|
divesToMove.divesToMove.push_back( { d, newTrip.get() } );
|
2024-06-01 20:05:57 +00:00
|
|
|
divesToMove.tripsToAdd.push_back(std::move(newTrip));
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
2024-06-03 19:50:08 +00:00
|
|
|
SplitDivesBase::SplitDivesBase(dive *d, std::array<std::unique_ptr<dive>, 2> newDives)
|
2018-07-19 20:35:25 +00:00
|
|
|
{
|
2019-03-31 08:20:13 +00:00
|
|
|
// If either of the new dives is null, simply return. Empty arrays indicate that nothing is to be done.
|
|
|
|
if (!newDives[0] || !newDives[1])
|
2018-07-19 20:35:25 +00:00
|
|
|
return;
|
|
|
|
|
2018-08-10 12:02:02 +00:00
|
|
|
// Currently, the core code selects the dive -> this is not what we want, as
|
|
|
|
// we manually manage the selection post-command.
|
|
|
|
// TODO: Reset selection in core.
|
2019-03-31 08:20:13 +00:00
|
|
|
newDives[0]->selected = false;
|
|
|
|
newDives[1]->selected = false;
|
|
|
|
|
2019-05-17 16:43:25 +00:00
|
|
|
// The new dives will be registered to the dive site using the site member
|
|
|
|
// of the DiveToAdd structure. For this to work, we must set the dive's
|
|
|
|
// dive_site member to null. Yes, that's subtle!
|
|
|
|
newDives[0]->dive_site = nullptr;
|
|
|
|
newDives[1]->dive_site = nullptr;
|
|
|
|
|
2019-03-03 14:12:22 +00:00
|
|
|
diveToSplit.dives.push_back(d);
|
2018-11-26 21:06:17 +00:00
|
|
|
splitDives.dives.resize(2);
|
2024-06-03 19:50:08 +00:00
|
|
|
splitDives.dives[0].dive = std::move(newDives[0]);
|
2018-11-26 21:06:17 +00:00
|
|
|
splitDives.dives[0].trip = d->divetrip;
|
2019-05-17 16:43:25 +00:00
|
|
|
splitDives.dives[0].site = d->dive_site;
|
2024-06-03 19:50:08 +00:00
|
|
|
splitDives.dives[1].dive = std::move(newDives[1]);
|
2018-11-26 21:06:17 +00:00
|
|
|
splitDives.dives[1].trip = d->divetrip;
|
2019-05-17 16:43:25 +00:00
|
|
|
splitDives.dives[1].site = d->dive_site;
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
2019-03-31 08:20:13 +00:00
|
|
|
bool SplitDivesBase::workToBeDone()
|
2018-07-19 20:35:25 +00:00
|
|
|
{
|
2019-03-03 14:12:22 +00:00
|
|
|
return !diveToSplit.dives.empty();
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
2019-03-31 08:20:13 +00:00
|
|
|
void SplitDivesBase::redoit()
|
2018-07-19 20:35:25 +00:00
|
|
|
{
|
2018-08-10 12:02:02 +00:00
|
|
|
divesToUnsplit = addDives(splitDives);
|
|
|
|
unsplitDive = removeDives(diveToSplit);
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
|
|
|
|
// Select split dives and make first dive current
|
2022-04-04 16:57:28 +00:00
|
|
|
setSelection(divesToUnsplit.dives, divesToUnsplit.dives[0], -1);
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
2019-03-31 08:20:13 +00:00
|
|
|
void SplitDivesBase::undoit()
|
2018-07-19 20:35:25 +00:00
|
|
|
{
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
// Note: reverse order with respect to redoit()
|
2018-08-10 12:02:02 +00:00
|
|
|
diveToSplit = addDives(unsplitDive);
|
|
|
|
splitDives = removeDives(divesToUnsplit);
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
|
|
|
|
// Select unsplit dive and make it current
|
2022-04-04 16:57:28 +00:00
|
|
|
setSelection(diveToSplit.dives, diveToSplit.dives[0], -1);
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
2024-06-03 19:50:08 +00:00
|
|
|
static std::array<std::unique_ptr<dive>, 2> doSplitDives(const dive *d, duration_t time)
|
2019-03-31 08:20:13 +00:00
|
|
|
{
|
|
|
|
// Split the dive
|
|
|
|
if (time.seconds < 0)
|
2024-06-24 17:17:43 +00:00
|
|
|
return divelog.dives.split_dive(*d);
|
2019-03-31 08:20:13 +00:00
|
|
|
else
|
2024-06-24 17:17:43 +00:00
|
|
|
return divelog.dives.split_dive_at_time(*d, time);
|
2019-03-31 08:20:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SplitDives::SplitDives(dive *d, duration_t time) : SplitDivesBase(d, doSplitDives(d, time))
|
|
|
|
{
|
2020-03-21 23:46:36 +00:00
|
|
|
setText(Command::Base::tr("split dive"));
|
2019-03-31 08:20:13 +00:00
|
|
|
}
|
|
|
|
|
2024-06-03 19:50:08 +00:00
|
|
|
SplitDiveComputer::SplitDiveComputer(dive *d, int dc_num) :
|
2024-06-24 17:17:43 +00:00
|
|
|
SplitDivesBase(d, divelog.dives.split_divecomputer(*d, dc_num))
|
2019-03-31 08:20:13 +00:00
|
|
|
{
|
2020-03-21 23:46:36 +00:00
|
|
|
setText(Command::Base::tr("split dive computer"));
|
2019-03-31 08:20:13 +00:00
|
|
|
}
|
|
|
|
|
2024-06-04 05:10:22 +00:00
|
|
|
DiveComputerBase::DiveComputerBase(dive *old_dive, std::unique_ptr<dive> new_dive, int dc_nr_before, int dc_nr_after) :
|
2022-04-04 16:57:28 +00:00
|
|
|
dc_nr_before(dc_nr_before),
|
|
|
|
dc_nr_after(dc_nr_after)
|
2019-05-17 20:22:55 +00:00
|
|
|
{
|
|
|
|
if (!new_dive)
|
|
|
|
return;
|
|
|
|
|
2019-05-19 12:27:10 +00:00
|
|
|
diveToRemove.dives.push_back(old_dive);
|
2019-05-17 20:22:55 +00:00
|
|
|
|
|
|
|
// Currently, the core code selects the dive -> this is not what we want, as
|
|
|
|
// we manually manage the selection post-command.
|
|
|
|
// TODO: Reset selection in core.
|
|
|
|
new_dive->selected = false;
|
|
|
|
|
|
|
|
// Reset references to trip and site in the new dive, as the undo command
|
|
|
|
// will add the dive to the trip and site.
|
|
|
|
new_dive->divetrip = nullptr;
|
|
|
|
new_dive->dive_site = nullptr;
|
|
|
|
|
|
|
|
diveToAdd.dives.resize(1);
|
2024-06-04 05:10:22 +00:00
|
|
|
diveToAdd.dives[0].dive = std::move(new_dive);
|
2019-05-19 12:27:10 +00:00
|
|
|
diveToAdd.dives[0].trip = old_dive->divetrip;
|
|
|
|
diveToAdd.dives[0].site = old_dive->dive_site;
|
2019-05-17 20:22:55 +00:00
|
|
|
}
|
|
|
|
|
2019-05-19 12:27:10 +00:00
|
|
|
bool DiveComputerBase::workToBeDone()
|
2019-05-17 20:22:55 +00:00
|
|
|
{
|
|
|
|
return !diveToRemove.dives.empty() || !diveToAdd.dives.empty();
|
|
|
|
}
|
|
|
|
|
2019-05-19 12:27:10 +00:00
|
|
|
void DiveComputerBase::redoit()
|
2019-05-17 20:22:55 +00:00
|
|
|
{
|
|
|
|
DivesAndSitesToRemove addedDive = addDives(diveToAdd);
|
|
|
|
diveToAdd = removeDives(diveToRemove);
|
|
|
|
diveToRemove = std::move(addedDive);
|
|
|
|
|
2019-11-13 17:46:12 +00:00
|
|
|
// Select added dive and make it current.
|
|
|
|
// This automatically replots the profile.
|
2022-04-04 16:57:28 +00:00
|
|
|
setSelection(diveToRemove.dives, diveToRemove.dives[0], dc_nr_after);
|
2019-05-17 20:22:55 +00:00
|
|
|
|
2019-05-19 16:43:21 +00:00
|
|
|
std::swap(dc_nr_before, dc_nr_after);
|
2019-05-17 20:22:55 +00:00
|
|
|
}
|
|
|
|
|
2019-05-19 12:27:10 +00:00
|
|
|
void DiveComputerBase::undoit()
|
2019-05-17 20:22:55 +00:00
|
|
|
{
|
|
|
|
// Undo and redo do the same
|
|
|
|
redoit();
|
|
|
|
}
|
|
|
|
|
2019-05-19 12:27:10 +00:00
|
|
|
MoveDiveComputerToFront::MoveDiveComputerToFront(dive *d, int dc_num)
|
2024-06-04 05:10:22 +00:00
|
|
|
: DiveComputerBase(d, clone_make_first_dc(*d, dc_num), dc_num, 0)
|
2019-05-19 12:27:10 +00:00
|
|
|
{
|
2020-03-21 23:46:36 +00:00
|
|
|
setText(Command::Base::tr("move dive computer to front"));
|
2019-05-19 12:27:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DeleteDiveComputer::DeleteDiveComputer(dive *d, int dc_num)
|
2024-06-25 12:45:33 +00:00
|
|
|
: DiveComputerBase(d, divelog.dives.clone_delete_divecomputer(*d, dc_num),
|
2024-06-30 15:16:14 +00:00
|
|
|
dc_num, std::min(d->number_of_computers() - 1, dc_num))
|
2019-05-19 12:27:10 +00:00
|
|
|
{
|
2020-03-21 23:46:36 +00:00
|
|
|
setText(Command::Base::tr("delete dive computer"));
|
2019-05-19 12:27:10 +00:00
|
|
|
}
|
|
|
|
|
2024-09-01 12:12:05 +00:00
|
|
|
MergeDives::MergeDives(const QVector <dive *> &dives) : site(nullptr)
|
2018-07-19 20:35:25 +00:00
|
|
|
{
|
2020-03-21 23:46:36 +00:00
|
|
|
setText(Command::Base::tr("merge dive"));
|
2018-07-19 20:35:25 +00:00
|
|
|
|
2024-05-28 19:31:11 +00:00
|
|
|
// Just a safety check - if there's not two or more dives - do nothing.
|
2018-07-19 20:35:25 +00:00
|
|
|
// The caller should have made sure that this doesn't happen.
|
|
|
|
if (dives.count() < 2) {
|
2019-11-13 17:50:48 +00:00
|
|
|
qWarning("Merging less than two dives");
|
2018-07-19 20:35:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-09-01 12:12:05 +00:00
|
|
|
auto [d, set_location] = divelog.dives.merge_dives(qtToStd(dives));
|
2018-07-19 20:35:25 +00:00
|
|
|
|
2019-06-23 19:36:54 +00:00
|
|
|
// The merged dive gets the number of the first dive with a non-zero number
|
|
|
|
for (const dive *dive: dives) {
|
|
|
|
if (dive->number) {
|
|
|
|
d->number = dive->number;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-07-19 20:35:25 +00:00
|
|
|
|
|
|
|
// We will only renumber the remaining dives if the joined dives are consecutive.
|
|
|
|
// Otherwise all bets are off concerning what the user wanted and doing nothing seems
|
|
|
|
// like the best option.
|
2024-06-07 08:25:09 +00:00
|
|
|
size_t idx = divelog.dives.get_idx(dives[0]);
|
|
|
|
size_t num = dives.count();
|
|
|
|
if (idx == std::string::npos) {
|
2018-07-19 20:35:25 +00:00
|
|
|
// It was the callers responsibility to pass only known dives.
|
|
|
|
// Something is seriously wrong - give up.
|
2019-11-13 17:50:48 +00:00
|
|
|
qWarning("Merging unknown dives");
|
2018-07-19 20:35:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// std::equal compares two ranges. The parameters are (begin_range1, end_range1, begin_range2).
|
|
|
|
// Here, we can compare C-arrays, because QVector guarantees contiguous storage.
|
2024-06-07 08:25:09 +00:00
|
|
|
if (std::equal(&dives[0], &dives[0] + num, divelog.dives.begin() + idx, [](dive *d1,
|
|
|
|
const std::unique_ptr<dive> &d2) { return d1 == d2.get(); }) &&
|
2018-07-19 20:35:25 +00:00
|
|
|
dives[0]->number && dives.last()->number && dives[0]->number < dives.last()->number) {
|
|
|
|
// We have a consecutive set of dives. Rename all following dives according to the
|
|
|
|
// number of erased dives. This considers that there might be missing numbers.
|
|
|
|
// Comment copied from core/divelist.c:
|
|
|
|
// So if you had a dive list 1 3 6 7 8, and you
|
|
|
|
// merge 1 and 3, the resulting numbered list will
|
|
|
|
// be 1 4 5 6, because we assume that there were
|
|
|
|
// some missing dives (originally dives 4 and 5),
|
|
|
|
// that now will still be missing (dives 2 and 3
|
|
|
|
// in the renumbered world).
|
|
|
|
//
|
|
|
|
// Obviously the normal case is that everything is
|
|
|
|
// consecutive, and the difference will be 1, so the
|
|
|
|
// above example is not supposed to be normal.
|
|
|
|
int diff = dives.last()->number - dives[0]->number;
|
|
|
|
int previousnr = dives[0]->number;
|
2024-06-07 08:25:09 +00:00
|
|
|
for (size_t i = idx + num; i < divelog.dives.size(); ++i) {
|
|
|
|
int newnr = divelog.dives[i]->number - diff;
|
2018-07-19 20:35:25 +00:00
|
|
|
|
|
|
|
// Stop renumbering if stuff isn't in order (see also core/divelist.c)
|
|
|
|
if (newnr <= previousnr)
|
|
|
|
break;
|
2024-06-07 08:25:09 +00:00
|
|
|
divesToRenumber.append(QPair<dive *,int>(divelog.dives[i].get(), newnr));
|
2018-07-19 20:35:25 +00:00
|
|
|
previousnr = newnr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-26 21:06:17 +00:00
|
|
|
mergedDive.dives.resize(1);
|
2024-06-04 11:22:25 +00:00
|
|
|
mergedDive.dives[0].trip = d->divetrip;
|
|
|
|
mergedDive.dives[0].site = d->dive_site;
|
2020-01-06 20:47:53 +00:00
|
|
|
divesToMerge.dives = std::vector<dive *>(dives.begin(), dives.end());
|
2024-06-04 11:22:25 +00:00
|
|
|
|
2024-09-01 12:12:05 +00:00
|
|
|
if (set_location.has_value() && d->dive_site) {
|
|
|
|
location = *set_location;
|
|
|
|
site = d->dive_site;
|
|
|
|
}
|
|
|
|
|
2024-06-04 11:22:25 +00:00
|
|
|
// We got our preferred trip and site, so now the references can be deleted from the newly generated dive
|
|
|
|
d->divetrip = nullptr;
|
|
|
|
d->dive_site = nullptr;
|
|
|
|
|
|
|
|
mergedDive.dives[0].dive = std::move(d);
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MergeDives::workToBeDone()
|
|
|
|
{
|
2018-11-26 21:06:17 +00:00
|
|
|
return !mergedDive.dives.empty();
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
2024-09-01 12:12:05 +00:00
|
|
|
void MergeDives::swapDivesite()
|
|
|
|
{
|
|
|
|
if (!site)
|
|
|
|
return;
|
|
|
|
std::swap(location, site->location);
|
|
|
|
emit diveListNotifier.diveSiteChanged(site, LocationInformationModel::LOCATION); // Inform frontend of changed dive site.
|
|
|
|
}
|
|
|
|
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
void MergeDives::redoit()
|
2018-07-19 20:35:25 +00:00
|
|
|
{
|
|
|
|
renumberDives(divesToRenumber);
|
2018-08-10 12:02:02 +00:00
|
|
|
diveToUnmerge = addDives(mergedDive);
|
2018-07-19 20:35:25 +00:00
|
|
|
unmergedDives = removeDives(divesToMerge);
|
2024-09-01 12:12:05 +00:00
|
|
|
swapDivesite();
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
|
|
|
|
// Select merged dive and make it current
|
2022-04-04 16:57:28 +00:00
|
|
|
setSelection(diveToUnmerge.dives, diveToUnmerge.dives[0], -1);
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
void MergeDives::undoit()
|
2018-07-19 20:35:25 +00:00
|
|
|
{
|
|
|
|
divesToMerge = addDives(unmergedDives);
|
2018-08-10 12:02:02 +00:00
|
|
|
mergedDive = removeDives(diveToUnmerge);
|
2018-07-19 20:35:25 +00:00
|
|
|
renumberDives(divesToRenumber);
|
2024-09-01 12:12:05 +00:00
|
|
|
swapDivesite();
|
Undo: select dives after add, remove, merge, split dive commands
Select the proper dives after the add, remove, split and merge
dives commands on undo *and* redo. Generally, select the added
dives. For undo of add, remember the pre-addition selection.
For redo of remove, select the closest dive to the first removed
dive.
The biggest part of the commit is the signal-interface between
the dive commands and the dive-list model and dive-list view.
This is done in two steps:
1) To the DiveTripModel in batches of trips. The dive trip model
transforms the dives into indices.
2) To the DiveListView. The DiveListView has to translate the
DiveTripModel indexes to actual indexes via its QSortFilterProxy-
model.
For code-reuse, derive all divelist-changing commands from a new base-class,
which has a flag that describes whether the divelist changed. The helper
functions which add and remove dives are made members of the base class and
set the flag is a selected dive is added or removed.
To properly detect when the current dive was deleted it
became necessary to turn the current dive from an index
to a pointer, because indices are not stable.
Unfortunately, in some cases an index was expected and these
places now have to transform the dive into an index. These
should be converted in due course.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2018-08-03 09:35:43 +00:00
|
|
|
|
|
|
|
// Select unmerged dives and make first one current
|
2022-04-04 16:57:28 +00:00
|
|
|
setSelection(divesToMerge.dives, divesToMerge.dives[0], -1);
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Command
|