2018-07-19 20:35:25 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
|
|
|
#include "command_divelist.h"
|
|
|
|
#include "desktop-widgets/mainwindow.h"
|
|
|
|
#include "desktop-widgets/divelistview.h"
|
|
|
|
#include "core/divelist.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
|
|
|
#include "core/display.h" // for amount_selected
|
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
|
|
|
#include "core/subsurface-qt/DiveListNotifier.h"
|
2018-08-27 06:55:09 +00:00
|
|
|
#include "qt-models/filtermodels.h"
|
2018-07-19 20:35:25 +00:00
|
|
|
|
|
|
|
namespace Command {
|
|
|
|
|
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
|
|
|
// Generally, signals are sent in batches per trip. To avoid writing the same loop
|
|
|
|
// again and again, 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.
|
|
|
|
// 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)
|
|
|
|
{
|
|
|
|
// Use std::tie for lexicographical sorting of trip, then start-time
|
|
|
|
std::sort(dives.begin(), dives.end(),
|
|
|
|
[](const std::pair<dive_trip *, dive *> &e1, const std::pair<dive_trip *, dive *> &e2)
|
|
|
|
{ return std::tie(e1.first, e1.second->when) < std::tie(e2.first, e2.second->when); });
|
|
|
|
|
|
|
|
// 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 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!
|
2018-11-26 21:06:17 +00:00
|
|
|
DiveToAdd DiveListBase::removeDive(struct dive *d, std::vector<OwningTripPtr> &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 to be removed is selected, we will inform the frontend
|
|
|
|
// later via a signal that the dive changed.
|
|
|
|
if (d->selected)
|
|
|
|
selectionChanged = true;
|
|
|
|
|
|
|
|
// If the dive was the current dive, reset the current dive. The calling
|
|
|
|
// command is responsible of finding a new dive.
|
|
|
|
if (d == current_dive) {
|
|
|
|
selectionChanged = true; // Should have been set above, as current dive is always selected.
|
|
|
|
current_dive = nullptr;
|
|
|
|
}
|
|
|
|
|
2018-07-19 20:35:25 +00:00
|
|
|
DiveToAdd res;
|
|
|
|
res.idx = get_divenr(d);
|
|
|
|
if (res.idx < 0)
|
|
|
|
qWarning() << "Deletion of unknown dive!";
|
|
|
|
|
|
|
|
// 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
|
|
|
res.trip = unregister_dive_from_trip(d);
|
2018-11-08 15:58:33 +00:00
|
|
|
if (res.trip && res.trip->dives.nr == 0) {
|
2018-11-26 22:39:37 +00:00
|
|
|
unregister_trip(res.trip, &trip_table); // Remove trip from backend
|
2018-11-26 21:06:17 +00:00
|
|
|
tripsToAdd.emplace_back(res.trip); // Take ownership of trip
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res.dive.reset(unregister_dive(res.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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
add_dive_to_trip(d.dive.get(), d.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
|
|
|
dive *res = d.dive.release(); // Give up ownership of dive
|
2018-08-27 06:55:09 +00:00
|
|
|
|
|
|
|
// Set the filter flag according to current filter settings
|
|
|
|
bool show = MultiFilterSortModel::instance()->showDive(res);
|
|
|
|
res->hidden_by_filter = !show;
|
|
|
|
|
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
|
|
|
add_single_dive(d.idx, res); // Return ownership to backend
|
2018-12-03 10:36:55 +00:00
|
|
|
invalidate_dive_cache(res); // Ensure that dive is written in git_save()
|
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
|
|
|
|
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 to be removed is selected, we will inform the frontend
|
|
|
|
// later via a signal that the dive changed.
|
|
|
|
if (res->selected)
|
|
|
|
selectionChanged = true;
|
|
|
|
|
2018-07-19 20:35:25 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2018-11-26 21:06:17 +00:00
|
|
|
DivesAndTripsToAdd DiveListBase::removeDives(std::vector<dive *> &divesToDelete)
|
2018-07-19 20:35:25 +00:00
|
|
|
{
|
2018-11-26 21:06:17 +00:00
|
|
|
std::vector<DiveToAdd> divesToAdd;
|
|
|
|
std::vector<OwningTripPtr> tripsToAdd;
|
|
|
|
divesToAdd.reserve(divesToDelete.size());
|
2018-07-19 20:35:25 +00:00
|
|
|
|
|
|
|
for (dive *d: divesToDelete)
|
2018-11-26 21:06:17 +00:00
|
|
|
divesToAdd.push_back(removeDive(d, tripsToAdd));
|
2018-07-19 20:35:25 +00:00
|
|
|
divesToDelete.clear();
|
|
|
|
|
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 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 &&
|
2018-11-26 21:06:17 +00:00
|
|
|
std::find_if(tripsToAdd.begin(), tripsToAdd.end(), [trip](const OwningTripPtr &ptr)
|
|
|
|
{ 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);
|
|
|
|
});
|
2018-11-26 21:06:17 +00:00
|
|
|
return { std::move(divesToAdd), std::move(tripsToAdd) };
|
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.
|
2018-11-26 21:06:17 +00:00
|
|
|
std::vector<dive *> DiveListBase::addDives(DivesAndTripsToAdd &toAdd)
|
2018-07-19 20:35:25 +00:00
|
|
|
{
|
|
|
|
std::vector<dive *> res;
|
2018-11-26 21:06:17 +00:00
|
|
|
res.resize(toAdd.dives.size());
|
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
|
|
|
// 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();
|
2018-11-26 21:06:17 +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);
|
2018-11-26 21:06:17 +00:00
|
|
|
toAdd.dives.clear();
|
|
|
|
|
|
|
|
// 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());
|
|
|
|
for (OwningTripPtr &trip: toAdd.trips) {
|
|
|
|
addedTrips.push_back(trip.get());
|
2018-11-26 22:39:37 +00:00
|
|
|
insert_trip(trip.release(), &trip_table); // Return ownership to backend
|
2018-11-26 21:06:17 +00:00
|
|
|
}
|
|
|
|
toAdd.trips.clear();
|
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
|
|
|
// We send one dives-deleted signal per trip (see comments in DiveListNotifier.h).
|
|
|
|
// Therefore, collect all dives in a array and sort by trip.
|
|
|
|
std::vector<std::pair<dive_trip *, dive *>> dives;
|
|
|
|
dives.reserve(res.size());
|
|
|
|
for (dive *d: res)
|
|
|
|
dives.push_back({ d->divetrip, d });
|
|
|
|
|
|
|
|
// Send signals.
|
|
|
|
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);
|
|
|
|
});
|
2018-07-19 20:35:25 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-12-03 10:36:55 +00:00
|
|
|
// TODO: switch from uniq-id to indices once all divelist-actions are controlled by undo-able commands
|
2018-07-30 13:55:29 +00:00
|
|
|
static void renumberDives(QVector<QPair<dive *, int>> &divesToRenumber)
|
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);
|
2018-12-03 10:36:55 +00:00
|
|
|
invalidate_dive_cache(d);
|
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
|
|
|
|
|
|
|
// Emit changed signals per trip.
|
|
|
|
// First, collect all dives and sort by trip
|
|
|
|
std::vector<std::pair<dive_trip *, dive *>> dives;
|
|
|
|
dives.reserve(divesToRenumber.size());
|
|
|
|
for (const auto &pair: divesToRenumber) {
|
2018-07-30 13:55:29 +00:00
|
|
|
dive *d = pair.first;
|
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({ d->divetrip, d });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send signals.
|
|
|
|
processByTrip(dives, [&](dive_trip *trip, const QVector<dive *> &divesInTrip) {
|
|
|
|
emit diveListNotifier.divesChanged(trip, divesInTrip);
|
|
|
|
});
|
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.
|
|
|
|
static OwningTripPtr moveDiveToTrip(DiveToTrip &diveToTrip)
|
|
|
|
{
|
|
|
|
// 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
|
|
|
|
OwningTripPtr res;
|
|
|
|
|
|
|
|
// 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);
|
2018-11-08 15:58:33 +00:00
|
|
|
if (trip && trip->dives.nr == 0) {
|
2018-11-26 22:39:37 +00:00
|
|
|
unregister_trip(trip, &trip_table); // Remove trip from backend
|
2018-07-19 20:35:25 +00:00
|
|
|
res.reset(trip);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store old trip and get new trip we should associate this dive with
|
|
|
|
std::swap(trip, diveToTrip.trip);
|
|
|
|
add_dive_to_trip(diveToTrip.dive, trip);
|
2018-12-03 10:36:55 +00:00
|
|
|
invalidate_dive_cache(diveToTrip.dive); // 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)
|
|
|
|
for (OwningTripPtr &trip: dives.tripsToAdd) {
|
|
|
|
dive_trip *t = trip.release(); // Give up ownership
|
|
|
|
createdTrips.push_back(t);
|
2018-11-26 22:39:37 +00:00
|
|
|
insert_trip(t, &trip_table); // 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
|
|
|
}
|
2018-07-19 20:35:25 +00:00
|
|
|
dives.tripsToAdd.clear();
|
|
|
|
|
|
|
|
for (DiveToTrip &dive: dives.divesToMove) {
|
|
|
|
OwningTripPtr tripToAdd = moveDiveToTrip(dive);
|
|
|
|
// 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?
|
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
|
|
|
[from](const OwningTripPtr &trip) { return trip.get() == from; }) != dives.tripsToAdd.end();
|
|
|
|
// 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
|
|
|
// Turn current selection into a vector.
|
|
|
|
// TODO: This could be made much more efficient if we kept a sorted list of selected dives!
|
|
|
|
static std::vector<dive *> getDiveSelection()
|
|
|
|
{
|
|
|
|
std::vector<dive *> res;
|
|
|
|
res.reserve(amount_selected);
|
|
|
|
|
|
|
|
int i;
|
|
|
|
dive *d;
|
|
|
|
for_each_dive(i, d) {
|
|
|
|
if (d->selected)
|
|
|
|
res.push_back(d);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveListBase::initWork()
|
|
|
|
{
|
|
|
|
selectionChanged = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveListBase::finishWork()
|
|
|
|
{
|
|
|
|
if (selectionChanged) // If the selection changed -> tell the frontend
|
|
|
|
emit diveListNotifier.selectionChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the current dive either from a list of selected dives,
|
|
|
|
// or a newly selected dive. In both cases, try to select the
|
|
|
|
// dive that is newer that is newer than the given date.
|
|
|
|
// This mimics the old behavior when the current dive changed.
|
|
|
|
static void setClosestCurrentDive(timestamp_t when, const std::vector<dive *> &selection)
|
|
|
|
{
|
|
|
|
// Start from back until we get the first dive that is before
|
|
|
|
// the supposed-to-be selected dive. (Note: this mimics the
|
|
|
|
// old behavior when the current dive changed).
|
|
|
|
for (auto it = selection.rbegin(); it < selection.rend(); ++it) {
|
|
|
|
if ((*it)->when > when && !(*it)->hidden_by_filter) {
|
|
|
|
current_dive = *it;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We didn't find a more recent selected dive -> try to
|
|
|
|
// find *any* visible selected dive.
|
|
|
|
for (dive *d: selection) {
|
|
|
|
if (!d->hidden_by_filter) {
|
|
|
|
current_dive = d;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No selected dive is visible! Take the closest dive. Note, this might
|
|
|
|
// return null, but that just means unsetting the current dive (as no
|
|
|
|
// dive is visible anyway).
|
|
|
|
current_dive = find_next_visible_dive(when);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rese the selection to the dives of the "selection" vector and send the appropriate signals.
|
|
|
|
// Set the current dive to "currentDive". "currentDive" must be an element of "selection" (or
|
|
|
|
// null if "seletion" is empty).
|
|
|
|
void DiveListBase::restoreSelection(const std::vector<dive *> &selection, dive *currentDive)
|
|
|
|
{
|
|
|
|
// To do so, generate vectors of dives to be selected and deselected.
|
|
|
|
// We send signals batched by trip, so keep track of trip/dive pairs.
|
|
|
|
std::vector<std::pair<dive_trip *, dive *>> divesToSelect;
|
|
|
|
std::vector<std::pair<dive_trip *, dive *>> divesToDeselect;
|
|
|
|
|
|
|
|
// TODO: We might want to keep track of selected dives in a more efficient way!
|
|
|
|
int i;
|
|
|
|
dive *d;
|
|
|
|
amount_selected = 0; // We recalculate amount_selected
|
|
|
|
for_each_dive(i, d) {
|
|
|
|
// We only modify dives that are currently visible.
|
|
|
|
if (d->hidden_by_filter) {
|
|
|
|
d->selected = false; // Note, not necessary, just to be sure
|
|
|
|
// that we get amount_selected right
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search the dive in the list of selected dives.
|
|
|
|
// TODO: By sorting the list in the same way as the backend, this could be made more efficient.
|
|
|
|
bool newState = std::find(selection.begin(), selection.end(), d) != selection.end();
|
|
|
|
|
|
|
|
// TODO: Instead of using select_dive() and deselect_dive(), we set selected directly.
|
|
|
|
// The reason is that deselect() automatically sets a new current dive, which we
|
|
|
|
// don't want, as we set it later anyway.
|
|
|
|
// There is other parts of the C++ code that touches the innards directly, but
|
|
|
|
// ultimately this should be pushed down to C.
|
|
|
|
if (newState && !d->selected) {
|
|
|
|
d->selected = true;
|
|
|
|
++amount_selected;
|
|
|
|
divesToSelect.push_back({ d->divetrip, d });
|
|
|
|
} else if (!newState && d->selected) {
|
|
|
|
d->selected = false;
|
|
|
|
divesToDeselect.push_back({ d->divetrip, d });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the select and deselect signals
|
|
|
|
processByTrip(divesToSelect, [&](dive_trip *trip, const QVector<dive *> &divesInTrip) {
|
|
|
|
emit diveListNotifier.divesSelected(trip, divesInTrip);
|
|
|
|
});
|
|
|
|
processByTrip(divesToDeselect, [&](dive_trip *trip, const QVector<dive *> &divesInTrip) {
|
|
|
|
emit diveListNotifier.divesDeselected(trip, divesInTrip);
|
|
|
|
});
|
|
|
|
|
|
|
|
bool currentDiveChanged = false;
|
2018-08-14 13:12:21 +00:00
|
|
|
// If currentDive is null, we have no current dive. In such a case always
|
|
|
|
// signal the frontend.
|
|
|
|
if (!currentDive) {
|
|
|
|
currentDiveChanged = true;
|
|
|
|
emit diveListNotifier.currentDiveChanged();
|
|
|
|
} else if (current_dive != currentDive) {
|
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
|
|
|
currentDiveChanged = true;
|
|
|
|
|
|
|
|
// We cannot simply change the currentd dive to the given dive.
|
|
|
|
// It might be hidden by a filter and thus not be selected.
|
|
|
|
if (currentDive->selected)
|
|
|
|
// Current dive is visible and selected. Excellent.
|
|
|
|
current_dive = currentDive;
|
|
|
|
else
|
|
|
|
// Current not visible -> find a different dive.
|
|
|
|
setClosestCurrentDive(currentDive->when, selection);
|
|
|
|
emit diveListNotifier.currentDiveChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If anything changed (selection or current dive), send a final signal.
|
|
|
|
if (!divesToSelect.empty() || !divesToDeselect.empty() || currentDiveChanged)
|
|
|
|
selectionChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveListBase::undo()
|
|
|
|
{
|
|
|
|
auto marker = diveListNotifier.enterCommand();
|
|
|
|
initWork();
|
|
|
|
undoit();
|
|
|
|
finishWork();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveListBase::redo()
|
|
|
|
{
|
|
|
|
auto marker = diveListNotifier.enterCommand();
|
|
|
|
initWork();
|
|
|
|
redoit();
|
|
|
|
finishWork();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
AddDive::AddDive(dive *d, bool autogroup, bool newNumber)
|
2018-07-19 20:35:25 +00:00
|
|
|
{
|
|
|
|
setText(tr("add dive"));
|
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
|
|
|
// By convention, d is "displayed dive" and can be overwritten.
|
2018-07-19 20:35:25 +00:00
|
|
|
d->maxdepth.mm = 0;
|
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
|
|
|
d->dc.maxdepth.mm = 0;
|
2018-07-19 20:35:25 +00:00
|
|
|
fixup_dive(d);
|
2018-07-30 08:33:25 +00:00
|
|
|
|
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
|
|
|
// Get an owning pointer to a copied or moved dive
|
|
|
|
// Note: if move is true, this destroys the old dive!
|
2018-07-30 08:33:25 +00:00
|
|
|
OwningDivePtr divePtr(clone_dive(d));
|
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
|
|
|
divePtr->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.
|
|
|
|
OwningTripPtr allocTrip;
|
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
|
|
|
dive_trip *trip = divePtr->divetrip;
|
|
|
|
// We have to delete the pointer-to-trip, because this would prevent the core from adding to the trip
|
|
|
|
// and we would get the count-of-dives in the trip wrong. Yes, that's all horribly subtle!
|
|
|
|
divePtr->divetrip = nullptr;
|
|
|
|
if (!trip && autogroup) {
|
2018-07-30 08:33:25 +00:00
|
|
|
bool alloc;
|
|
|
|
trip = get_trip_for_new_dive(divePtr.get(), &alloc);
|
|
|
|
if (alloc)
|
|
|
|
allocTrip.reset(trip);
|
|
|
|
}
|
|
|
|
|
2018-11-24 09:11:07 +00:00
|
|
|
int idx = dive_table_get_insertion_index(&dive_table, divePtr.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)
|
|
|
|
divePtr->number = 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
|
|
|
|
2018-11-26 21:06:17 +00:00
|
|
|
divesToAdd.dives.push_back({ std::move(divePtr), trip, idx });
|
|
|
|
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
|
|
|
|
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
|
|
|
divesToRemove = addDives(divesToAdd);
|
2018-11-24 11:31:35 +00:00
|
|
|
sort_trip_table(&trip_table); // Though unlikely, adding a dive may reorder trips
|
2018-07-19 20:35:25 +00:00
|
|
|
mark_divelist_changed(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
|
|
|
// Select the newly added dive
|
|
|
|
restoreSelection(divesToRemove, divesToRemove[0]);
|
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
|
|
|
|
|
|
|
// Exit from edit mode, but don't recalculate dive list
|
|
|
|
// TODO: Remove edit mode
|
|
|
|
MainWindow::instance()->refreshDisplay(false);
|
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...
|
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
|
|
|
divesToAdd = removeDives(divesToRemove);
|
2018-11-24 11:31:35 +00:00
|
|
|
sort_trip_table(&trip_table); // 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
|
|
|
|
restoreSelection(selection, currentDive);
|
|
|
|
|
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
|
|
|
// Exit from edit mode, but don't recalculate dive list
|
|
|
|
// TODO: Remove edit mode
|
|
|
|
MainWindow::instance()->refreshDisplay(false);
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
2019-01-13 20:53:57 +00:00
|
|
|
ImportDives::ImportDives(struct dive_table *dives, struct trip_table *trips, int flags, const QString &source)
|
2018-12-23 22:45:12 +00:00
|
|
|
{
|
|
|
|
setText(tr("import %n dive(s) from %1", "", dives->nr).arg(source));
|
|
|
|
|
|
|
|
struct dive_table dives_to_add = { 0 };
|
|
|
|
struct dive_table dives_to_remove = { 0 };
|
|
|
|
struct trip_table trips_to_add = { 0 };
|
2019-01-13 20:53:57 +00:00
|
|
|
process_imported_dives(dives, trips, flags, &dives_to_add, &dives_to_remove, &trips_to_add);
|
2018-12-23 22:45:12 +00:00
|
|
|
|
|
|
|
// Add trips to the divesToAdd.trips structure
|
|
|
|
divesToAdd.trips.reserve(trips_to_add.nr);
|
|
|
|
for (int i = 0; i < trips_to_add.nr; ++i)
|
|
|
|
divesToAdd.trips.emplace_back(trips_to_add.trips[i]);
|
|
|
|
|
|
|
|
// Add dives to the divesToAdd.dives structure
|
|
|
|
divesToAdd.dives.reserve(dives_to_add.nr);
|
|
|
|
for (int i = 0; i < dives_to_add.nr; ++i) {
|
|
|
|
OwningDivePtr divePtr(dives_to_add.dives[i]);
|
|
|
|
divePtr->selected = false; // See above in AddDive::AddDive()
|
|
|
|
dive_trip *trip = divePtr->divetrip;
|
|
|
|
divePtr->divetrip = nullptr; // See above in AddDive::AddDive()
|
|
|
|
int idx = dive_table_get_insertion_index(&dive_table, divePtr.get());
|
|
|
|
|
|
|
|
// Note: The dives are added in reverse order of the divesToAdd array.
|
|
|
|
// This, and the fact that we populate the array in chronological order
|
|
|
|
// means that wo do *not* have to manipulated the indices.
|
|
|
|
// Yes, that's all horribly subtle.
|
|
|
|
divesToAdd.dives.push_back({ std::move(divePtr), trip, idx });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add dive to be deleted to the divesToRemove structure
|
|
|
|
divesToRemove.reserve(dives_to_remove.nr);
|
|
|
|
for (int i = 0; i < dives_to_remove.nr; ++i)
|
|
|
|
divesToRemove.push_back(dives_to_remove.dives[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ImportDives::workToBeDone()
|
|
|
|
{
|
|
|
|
return !divesToAdd.dives.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImportDives::redoit()
|
|
|
|
{
|
|
|
|
// Remember selection so that we can undo it
|
|
|
|
currentDive = current_dive;
|
|
|
|
|
|
|
|
// Add new dives
|
|
|
|
std::vector<dive *> divesToRemoveNew = addDives(divesToAdd);
|
|
|
|
|
|
|
|
// Remove old dives
|
|
|
|
divesToAdd = removeDives(divesToRemove);
|
|
|
|
|
|
|
|
// Select the newly added dives
|
|
|
|
restoreSelection(divesToRemoveNew, divesToRemoveNew.back());
|
|
|
|
|
|
|
|
// Remember dives to remove
|
|
|
|
divesToRemove = std::move(divesToRemoveNew);
|
|
|
|
|
|
|
|
mark_divelist_changed(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImportDives::undoit()
|
|
|
|
{
|
|
|
|
// Add new dives
|
|
|
|
std::vector<dive *> divesToRemoveNew = addDives(divesToAdd);
|
|
|
|
|
|
|
|
// Remove old dives
|
|
|
|
divesToAdd = removeDives(divesToRemove);
|
|
|
|
|
|
|
|
// Remember dives to remove
|
|
|
|
divesToRemove = std::move(divesToRemoveNew);
|
|
|
|
|
|
|
|
// ...and restore the selection
|
|
|
|
restoreSelection(selection, currentDive);
|
|
|
|
|
|
|
|
mark_divelist_changed(true);
|
|
|
|
}
|
|
|
|
|
2018-07-19 20:35:25 +00:00
|
|
|
DeleteDive::DeleteDive(const QVector<struct dive*> &divesToDeleteIn) : divesToDelete(divesToDeleteIn.toStdVector())
|
|
|
|
{
|
|
|
|
setText(tr("delete %n dive(s)", "", divesToDelete.size()));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DeleteDive::workToBeDone()
|
|
|
|
{
|
|
|
|
return !divesToDelete.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 DeleteDive::undoit()
|
2018-07-19 20:35:25 +00:00
|
|
|
{
|
|
|
|
divesToDelete = addDives(divesToAdd);
|
2018-11-24 11:31:35 +00:00
|
|
|
sort_trip_table(&trip_table); // Though unlikely, removing a dive may reorder trips
|
2018-07-19 20:35:25 +00:00
|
|
|
mark_divelist_changed(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
|
|
|
|
|
|
|
// Select all re-added dives and make the first one current
|
|
|
|
dive *currentDive = !divesToDelete.empty() ? divesToDelete[0] : nullptr;
|
|
|
|
restoreSelection(divesToDelete, currentDive);
|
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);
|
2018-11-24 11:31:35 +00:00
|
|
|
sort_trip_table(&trip_table); // Though unlikely, adding a dive may reorder trips
|
2018-07-19 20:35:25 +00:00
|
|
|
mark_divelist_changed(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
|
|
|
|
|
|
|
// 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;
|
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
|
|
|
newCurrent = find_next_visible_dive(when);
|
|
|
|
}
|
|
|
|
if (newCurrent)
|
|
|
|
restoreSelection(std::vector<dive *>{ newCurrent }, newCurrent);
|
|
|
|
else
|
|
|
|
restoreSelection(std::vector<dive *>(), nullptr);
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ShiftTime::ShiftTime(const QVector<dive *> &changedDives, int amount)
|
|
|
|
: diveList(changedDives), timeChanged(amount)
|
|
|
|
{
|
|
|
|
setText(tr("shift time of %n dives", "", changedDives.count()));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
for (dive *d: diveList)
|
2018-12-20 18:03:44 +00:00
|
|
|
d->when += timeChanged;
|
2018-07-19 20:35:25 +00:00
|
|
|
|
|
|
|
// Changing times may have unsorted the dive table
|
2018-11-24 09:11:07 +00:00
|
|
|
sort_dive_table(&dive_table);
|
2018-11-24 11:31:35 +00:00
|
|
|
sort_trip_table(&trip_table);
|
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-11-09 15:41:45 +00:00
|
|
|
// We send one time changed signal per trip (see comments in DiveListNotifier.h).
|
|
|
|
// 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;
|
|
|
|
dives.reserve(diveList.size());
|
|
|
|
for (dive *d: diveList)
|
|
|
|
dives.push_back({ d->divetrip, d });
|
|
|
|
|
2018-11-09 15:41:45 +00:00
|
|
|
// Send signals and sort tables.
|
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
|
|
|
processByTrip(dives, [&](dive_trip *trip, const QVector<dive *> &divesInTrip) {
|
2018-11-09 15:41:45 +00:00
|
|
|
if (trip)
|
2018-11-24 09:11:07 +00:00
|
|
|
sort_dive_table(&trip->dives); // Keep the trip-table in order
|
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.divesTimeChanged(trip, timeChanged, divesInTrip);
|
|
|
|
});
|
2018-07-19 20:35:25 +00:00
|
|
|
|
|
|
|
// Negate the time-shift so that the next call does the reverse
|
|
|
|
timeChanged = -timeChanged;
|
|
|
|
|
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
|
|
|
mark_divelist_changed(true);
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ShiftTime::workToBeDone()
|
|
|
|
{
|
|
|
|
return !diveList.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 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
|
|
|
{
|
|
|
|
setText(tr("renumber %n dive(s)", "", divesToRenumber.count()));
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
mark_divelist_changed(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2018-11-24 11:31:35 +00:00
|
|
|
sort_trip_table(&trip_table); // Though unlikely, moving dives may reorder trips
|
2018-07-19 20:35:25 +00:00
|
|
|
|
|
|
|
mark_divelist_changed(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 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
|
|
|
}
|
|
|
|
|
|
|
|
RemoveDivesFromTrip::RemoveDivesFromTrip(const QVector<dive *> &divesToRemove)
|
|
|
|
{
|
|
|
|
setText(tr("remove %n dive(s) from trip", "", divesToRemove.size()));
|
|
|
|
divesToMove.divesToMove.reserve(divesToRemove.size());
|
|
|
|
for (dive *d: divesToRemove)
|
|
|
|
divesToMove.divesToMove.push_back( {d, nullptr} );
|
|
|
|
}
|
|
|
|
|
|
|
|
RemoveAutogenTrips::RemoveAutogenTrips()
|
|
|
|
{
|
|
|
|
setText(tr("remove autogenerated trips"));
|
|
|
|
// TODO: don't touch core-innards directly
|
|
|
|
int i;
|
|
|
|
struct dive *dive;
|
|
|
|
for_each_dive(i, dive) {
|
|
|
|
if (dive->divetrip && dive->divetrip->autogen)
|
|
|
|
divesToMove.divesToMove.push_back( {dive, nullptr} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AddDivesToTrip::AddDivesToTrip(const QVector<dive *> &divesToAddIn, dive_trip *trip)
|
|
|
|
{
|
|
|
|
setText(tr("add %n dives to trip", "", divesToAddIn.size()));
|
|
|
|
for (dive *d: divesToAddIn)
|
|
|
|
divesToMove.divesToMove.push_back( {d, trip} );
|
|
|
|
}
|
|
|
|
|
|
|
|
CreateTrip::CreateTrip(const QVector<dive *> &divesToAddIn)
|
|
|
|
{
|
|
|
|
setText(tr("create trip"));
|
|
|
|
|
|
|
|
if (divesToAddIn.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
dive_trip *trip = create_trip_from_dive(divesToAddIn[0]);
|
|
|
|
divesToMove.tripsToAdd.emplace_back(trip);
|
|
|
|
for (dive *d: divesToAddIn)
|
|
|
|
divesToMove.divesToMove.push_back( {d, trip} );
|
|
|
|
}
|
|
|
|
|
|
|
|
AutogroupDives::AutogroupDives()
|
|
|
|
{
|
|
|
|
setText(tr("autogroup dives"));
|
|
|
|
|
|
|
|
dive_trip *trip;
|
|
|
|
bool alloc;
|
|
|
|
int from, to;
|
2018-11-24 06:50:13 +00:00
|
|
|
for(int i = 0; (trip = get_dives_to_autogroup(&dive_table, i, &from, &to, &alloc)) != NULL; i = to) {
|
2018-07-19 20:35:25 +00:00
|
|
|
// If this is an allocated trip, take ownership
|
|
|
|
if (alloc)
|
|
|
|
divesToMove.tripsToAdd.emplace_back(trip);
|
|
|
|
for (int j = from; j < to; ++j)
|
|
|
|
divesToMove.divesToMove.push_back( { get_dive(j), trip } );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MergeTrips::MergeTrips(dive_trip *trip1, dive_trip *trip2)
|
|
|
|
{
|
|
|
|
if (trip1 == trip2)
|
|
|
|
return;
|
2018-12-08 18:47:28 +00:00
|
|
|
dive_trip *newTrip = combine_trips(trip1, trip2);
|
2018-07-19 20:35:25 +00:00
|
|
|
divesToMove.tripsToAdd.emplace_back(newTrip);
|
2018-11-08 15:58:33 +00:00
|
|
|
for (int i = 0; i < trip1->dives.nr; ++i)
|
|
|
|
divesToMove.divesToMove.push_back( { trip1->dives.dives[i], newTrip } );
|
|
|
|
for (int i = 0; i < trip2->dives.nr; ++i)
|
|
|
|
divesToMove.divesToMove.push_back( { trip2->dives.dives[i], newTrip } );
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SplitDives::SplitDives(dive *d, duration_t time)
|
|
|
|
{
|
|
|
|
setText(tr("split dive"));
|
|
|
|
|
|
|
|
// Split the dive
|
|
|
|
dive *new1, *new2;
|
|
|
|
int idx = time.seconds < 0 ?
|
2018-12-09 12:17:45 +00:00
|
|
|
split_dive(d, &new1, &new2) :
|
|
|
|
split_dive_at_time(d, time, &new1, &new2);
|
2018-07-19 20:35:25 +00:00
|
|
|
|
2018-08-10 12:02:02 +00:00
|
|
|
// If this didn't work, simply return. Empty arrays indicate that nothing is to be done.
|
|
|
|
if (idx < 0)
|
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.
|
|
|
|
new1->selected = false;
|
|
|
|
new2->selected = false;
|
|
|
|
|
|
|
|
diveToSplit.push_back(d);
|
2018-11-26 21:06:17 +00:00
|
|
|
splitDives.dives.resize(2);
|
|
|
|
splitDives.dives[0].dive.reset(new1);
|
|
|
|
splitDives.dives[0].trip = d->divetrip;
|
|
|
|
splitDives.dives[0].idx = idx;
|
|
|
|
splitDives.dives[1].dive.reset(new2);
|
|
|
|
splitDives.dives[1].trip = d->divetrip;
|
|
|
|
splitDives.dives[1].idx = idx + 1;
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SplitDives::workToBeDone()
|
|
|
|
{
|
2018-08-10 12:02:02 +00:00
|
|
|
return !diveToSplit.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 SplitDives::redoit()
|
2018-07-19 20:35:25 +00:00
|
|
|
{
|
2018-08-10 12:02:02 +00:00
|
|
|
divesToUnsplit = addDives(splitDives);
|
|
|
|
unsplitDive = removeDives(diveToSplit);
|
2018-07-19 20:35:25 +00:00
|
|
|
mark_divelist_changed(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
|
|
|
|
|
|
|
// Select split dives and make first dive current
|
2018-08-10 12:02:02 +00:00
|
|
|
restoreSelection(divesToUnsplit, divesToUnsplit[0]);
|
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 SplitDives::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);
|
2018-07-19 20:35:25 +00:00
|
|
|
mark_divelist_changed(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
|
|
|
|
|
|
|
// Select unsplit dive and make it current
|
2018-08-10 12:02:02 +00:00
|
|
|
restoreSelection(diveToSplit, diveToSplit[0] );
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MergeDives::MergeDives(const QVector <dive *> &dives)
|
|
|
|
{
|
|
|
|
setText(tr("merge dive"));
|
|
|
|
|
|
|
|
// Just a safety check - if there's not two or more dives - do nothing
|
|
|
|
// The caller should have made sure that this doesn't happen.
|
|
|
|
if (dives.count() < 2) {
|
|
|
|
qWarning() << "Merging less than two dives";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dive_trip *preferred_trip;
|
|
|
|
OwningDivePtr d(merge_dives(dives[0], dives[1], dives[1]->when - dives[0]->when, false, &preferred_trip));
|
|
|
|
|
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: Remove selection code from core.
|
|
|
|
d->selected = false;
|
|
|
|
|
2018-07-19 20:35:25 +00:00
|
|
|
// Set the preferred dive trip, so that for subsequent merges the better trip can be selected
|
|
|
|
d->divetrip = preferred_trip;
|
|
|
|
for (int i = 2; i < dives.count(); ++i) {
|
|
|
|
d.reset(merge_dives(d.get(), dives[i], dives[i]->when - d->when, false, &preferred_trip));
|
|
|
|
// Set the preferred dive trip, so that for subsequent merges the better trip can be selected
|
|
|
|
d->divetrip = preferred_trip;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We got our preferred trip, so now the reference can be deleted from the newly generated dive
|
|
|
|
d->divetrip = nullptr;
|
|
|
|
|
|
|
|
// The merged dive gets the number of the first dive
|
|
|
|
d->number = dives[0]->number;
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
int idx = get_divenr(dives[0]);
|
|
|
|
int num = dives.count();
|
|
|
|
if (idx < 0 || idx + num > dive_table.nr) {
|
|
|
|
// It was the callers responsibility to pass only known dives.
|
|
|
|
// Something is seriously wrong - give up.
|
|
|
|
qWarning() << "Merging unknown dives";
|
|
|
|
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.
|
|
|
|
if (std::equal(&dives[0], &dives[0] + num, &dive_table.dives[idx]) &&
|
|
|
|
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;
|
|
|
|
divesToRenumber.reserve(dive_table.nr - idx - num);
|
|
|
|
int previousnr = dives[0]->number;
|
|
|
|
for (int i = idx + num; i < dive_table.nr; ++i) {
|
|
|
|
int newnr = dive_table.dives[i]->number - diff;
|
|
|
|
|
|
|
|
// Stop renumbering if stuff isn't in order (see also core/divelist.c)
|
|
|
|
if (newnr <= previousnr)
|
|
|
|
break;
|
2018-07-30 13:55:29 +00:00
|
|
|
divesToRenumber.append(QPair<dive *,int>(dive_table.dives[i], newnr));
|
2018-07-19 20:35:25 +00:00
|
|
|
previousnr = newnr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-26 21:06:17 +00:00
|
|
|
mergedDive.dives.resize(1);
|
|
|
|
mergedDive.dives[0].dive = std::move(d);
|
|
|
|
mergedDive.dives[0].idx = get_divenr(dives[0]);
|
|
|
|
mergedDive.dives[0].trip = preferred_trip;
|
2018-07-19 20:35:25 +00:00
|
|
|
divesToMerge = dives.toStdVector();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MergeDives::workToBeDone()
|
|
|
|
{
|
2018-11-26 21:06:17 +00:00
|
|
|
return !mergedDive.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 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);
|
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
|
2018-08-10 12:02:02 +00:00
|
|
|
restoreSelection(diveToUnmerge, diveToUnmerge[0]);
|
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);
|
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
|
|
|
|
restoreSelection(divesToMerge, divesToMerge[0]);
|
2018-07-19 20:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Command
|