subsurface/stats/statsselection.cpp
Berthold Stoeger 9f455b1457 selection: move current dive and divecomputer to selection.cpp
This tries to encapsulate the management of the current dive and
divecomputer in the selection code. The current dive is alreay
set by setSelection(). Add a new parameter to also set the
current divecomputer. If -1 is passed, then the current
computer number is remained. This will allow us to audit the code.
Because for now, the whole "current dive computer" thing seems
to be ill-defined.

This fixes a bug: the dive-computer number wasn't validated
when making a new dive the current dive. The new code has some
drawbacks though: when selecting a whole trip, the validation
will be called for all dives in the trip and thus the dive computer
number will depend on the dive with the lowest amount of dive
computers in the trip. This will need to be fixed.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2023-04-16 20:23:59 +02:00

42 lines
1.3 KiB
C++

// SPDX-License-Identifier: GPL-2.0
#include "statsselection.h"
#include "core/dive.h"
#include "core/selection.h"
#include <algorithm>
void processSelection(std::vector<dive *> dives, SelectionModifier modifier)
{
std::vector<dive *> selected;
if (modifier.ctrl) {
// When shift is pressed, add the items under the mouse to the selection
// or, if all items under the mouse are selected, remove them.
selected = getDiveSelection();
bool allSelected = std::all_of(dives.begin(), dives.end(),
[] (const dive *d) { return d->selected; });
if (allSelected) {
// Remove items under cursor from selection. This could be made more efficient.
for (const dive *d: dives) {
auto it = std::find(selected.begin(), selected.end(), d);
if (it != selected.end()) {
// Move last element to deselected element. If this already was
// the last element, this is a no-op. Then, chop off last element.
*it = selected.back();
selected.pop_back();
}
}
} else {
// Add items under cursor to selection
selected.reserve(dives.size() + selected.size());
for (dive *d: dives) {
if (std::find(selected.begin(), selected.end(), d) == selected.end())
selected.push_back(d);
}
}
} else {
selected = std::move(dives);
}
setSelection(selected, selected.empty() ? nullptr : selected.front(), -1);
}