2019-02-17 09:18:00 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
// Helper functions for the undo-commands
|
|
|
|
|
2019-11-24 12:26:29 +00:00
|
|
|
#include "selection.h"
|
|
|
|
#include "divelist.h"
|
core: introduce divelog structure
The parser API was very annoying, as a number of tables
to-be-filled were passed in as pointers. The goal of this
commit is to collect all these tables in a single struct.
This should make it (more or less) clear what is actually
written into the divelog files.
Moreover, it should now be rather easy to search for
instances, where the global logfile is accessed (and it
turns out that there are many!).
The divelog struct does not contain the tables as substructs,
but only collects pointers. The idea is that the "divelog.h"
file can be included without all the other files describing
the numerous tables.
To make it easier to use from C++ parts of the code, the
struct implements a constructor and a destructor. Sadly,
we can't use smart pointers, since the pointers are accessed
from C code. Therfore the constructor and destructor are
quite complex.
The whole commit is large, but was mostly an automatic
conversion.
One oddity of note: the divelog structure also contains
the "autogroup" flag, since that is saved in the divelog.
This actually fixes a bug: Before, when importing dives
from a different log, the autogroup flag was overwritten.
This was probably not intended and does not happen anymore.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2022-11-08 20:31:08 +00:00
|
|
|
#include "divelog.h"
|
2024-03-24 20:03:08 +00:00
|
|
|
#include "errorhelper.h"
|
2020-05-02 12:34:40 +00:00
|
|
|
#include "trip.h"
|
2020-02-03 18:33:06 +00:00
|
|
|
#include "subsurface-qt/divelistnotifier.h"
|
2019-02-17 09:18:00 +00:00
|
|
|
|
2019-11-24 12:26:29 +00:00
|
|
|
#include <QVector>
|
2019-02-17 09:18:00 +00:00
|
|
|
|
2022-04-04 16:57:28 +00:00
|
|
|
struct dive *current_dive = NULL;
|
2019-11-24 14:02:34 +00:00
|
|
|
int amount_selected;
|
2020-05-02 12:34:40 +00:00
|
|
|
static int amount_trips_selected;
|
2019-11-24 14:02:34 +00:00
|
|
|
|
2024-05-04 16:45:55 +00:00
|
|
|
struct dive *first_selected_dive()
|
2019-11-24 14:02:34 +00:00
|
|
|
{
|
|
|
|
int idx;
|
|
|
|
struct dive *d;
|
|
|
|
|
|
|
|
for_each_dive (idx, d) {
|
|
|
|
if (d->selected)
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2024-05-04 16:45:55 +00:00
|
|
|
struct dive *last_selected_dive()
|
2019-11-24 14:02:34 +00:00
|
|
|
{
|
|
|
|
int idx;
|
|
|
|
struct dive *d, *ret = NULL;
|
|
|
|
|
|
|
|
for_each_dive (idx, d) {
|
|
|
|
if (d->selected)
|
|
|
|
ret = d;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-05-04 16:45:55 +00:00
|
|
|
bool consecutive_selected()
|
2019-11-24 14:02:34 +00:00
|
|
|
{
|
|
|
|
struct dive *d;
|
|
|
|
int i;
|
|
|
|
bool consecutive = true;
|
|
|
|
bool firstfound = false;
|
|
|
|
bool lastfound = false;
|
|
|
|
|
|
|
|
if (amount_selected == 0 || amount_selected == 1)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for_each_dive(i, d) {
|
|
|
|
if (d->selected) {
|
|
|
|
if (!firstfound)
|
|
|
|
firstfound = true;
|
|
|
|
else if (lastfound)
|
|
|
|
consecutive = false;
|
|
|
|
} else if (firstfound) {
|
|
|
|
lastfound = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return consecutive;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG_SELECTION_TRACKING
|
2024-05-04 16:53:41 +00:00
|
|
|
void dump_selection()
|
2019-11-24 14:02:34 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct dive *dive;
|
|
|
|
|
|
|
|
printf("currently selected are %u dives:", amount_selected);
|
|
|
|
for_each_dive(i, dive) {
|
|
|
|
if (dive->selected)
|
|
|
|
printf(" %d", i);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-05-21 11:14:47 +00:00
|
|
|
// Get closest dive in selection, if possible a newer dive.
|
|
|
|
// Supposes that selection is sorted
|
|
|
|
static dive *closestInSelection(timestamp_t when, const std::vector<dive *> &selection)
|
2019-02-17 09:18:00 +00:00
|
|
|
{
|
|
|
|
// 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) {
|
2022-05-21 11:14:47 +00:00
|
|
|
if ((*it)->when > when && !(*it)->hidden_by_filter)
|
|
|
|
return *it;
|
2019-02-17 09:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We didn't find a more recent selected dive -> try to
|
|
|
|
// find *any* visible selected dive.
|
|
|
|
for (dive *d: selection) {
|
2022-05-21 11:14:47 +00:00
|
|
|
if (!d->hidden_by_filter)
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 than the given date.
|
|
|
|
// This mimics the old behavior when the current dive changed.
|
|
|
|
// If a current dive outside of the selection was set, add
|
|
|
|
// it to the list of selected dives, so that we never end up
|
|
|
|
// in a situation where we display a non-selected dive.
|
|
|
|
static void setClosestCurrentDive(timestamp_t when, const std::vector<dive *> &selection, QVector<dive *> &divesToSelect)
|
|
|
|
{
|
|
|
|
if (dive *d = closestInSelection(when, selection)) {
|
|
|
|
current_dive = d;
|
|
|
|
return;
|
2019-02-17 09:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
2022-05-21 11:14:47 +00:00
|
|
|
if (current_dive) {
|
|
|
|
current_dive->selected = true;
|
|
|
|
amount_selected++;
|
2020-09-20 12:48:15 +00:00
|
|
|
divesToSelect.push_back(current_dive);
|
2022-05-21 11:14:47 +00:00
|
|
|
}
|
2019-02-17 09:18:00 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 18:38:17 +00:00
|
|
|
|
|
|
|
// Reset the selection to the dives of the "selection" vector.
|
2019-02-17 09:18:00 +00:00
|
|
|
// Set the current dive to "currentDive". "currentDive" must be an element of "selection" (or
|
2022-05-21 18:38:17 +00:00
|
|
|
// null if "selection" is empty).
|
|
|
|
// Does not send signals or clear the trip selection.
|
2022-09-17 17:07:35 +00:00
|
|
|
QVector<dive *> setSelectionCore(const std::vector<dive *> &selection, dive *currentDive)
|
2019-02-17 09:18:00 +00:00
|
|
|
{
|
|
|
|
// 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.
|
2019-06-23 07:22:26 +00:00
|
|
|
QVector<dive *> divesToSelect;
|
2019-06-23 09:13:41 +00:00
|
|
|
divesToSelect.reserve(selection.size());
|
2020-05-02 12:34:40 +00:00
|
|
|
|
2019-02-17 09:18:00 +00:00
|
|
|
// 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();
|
|
|
|
|
2019-06-23 09:13:41 +00:00
|
|
|
if (newState) {
|
|
|
|
++amount_selected;
|
|
|
|
divesToSelect.push_back(d);
|
|
|
|
}
|
|
|
|
d->selected = newState;
|
2019-02-17 09:18:00 +00:00
|
|
|
}
|
|
|
|
|
2019-06-23 10:46:42 +00:00
|
|
|
// We cannot simply change the current dive to the given dive.
|
|
|
|
// It might be hidden by a filter and thus not be selected.
|
|
|
|
current_dive = currentDive;
|
|
|
|
if (current_dive && !currentDive->selected) {
|
|
|
|
// Current not visible -> find a different dive.
|
2020-09-20 12:48:15 +00:00
|
|
|
setClosestCurrentDive(currentDive->when, selection, divesToSelect);
|
2019-02-17 09:18:00 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 18:38:17 +00:00
|
|
|
return divesToSelect;
|
|
|
|
}
|
2022-04-04 16:57:28 +00:00
|
|
|
|
2022-05-21 18:38:17 +00:00
|
|
|
static void clear_trip_selection()
|
|
|
|
{
|
|
|
|
amount_trips_selected = 0;
|
2024-06-01 20:05:57 +00:00
|
|
|
for (auto &trip: *divelog.trips)
|
|
|
|
trip->selected = false;
|
2022-05-21 11:14:47 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 18:38:17 +00:00
|
|
|
// Reset 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 "selection" is empty).
|
|
|
|
// If "currentDc" is negative, an attempt will be made to keep the current computer number.
|
|
|
|
void setSelection(const std::vector<dive *> &selection, dive *currentDive, int currentDc)
|
|
|
|
{
|
|
|
|
// Since we select only dives, there are no selected trips!
|
|
|
|
clear_trip_selection();
|
|
|
|
|
2022-09-17 17:07:35 +00:00
|
|
|
auto selectedDives = setSelectionCore(selection, currentDive);
|
2022-05-21 18:38:17 +00:00
|
|
|
|
|
|
|
// Send the new selection to the UI.
|
2022-09-03 20:45:08 +00:00
|
|
|
emit diveListNotifier.divesSelected(selectedDives, current_dive, currentDc);
|
2022-05-21 18:38:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set selection, but try to keep the current dive. If current dive is not in selection,
|
|
|
|
// find the nearest current dive in the selection
|
|
|
|
// Returns true if the current dive changed.
|
|
|
|
// Do not send a signal.
|
|
|
|
bool setSelectionKeepCurrent(const std::vector<dive *> &selection)
|
2022-05-21 11:14:47 +00:00
|
|
|
{
|
2022-05-21 18:38:17 +00:00
|
|
|
// Since we select only dives, there are no selected trips!
|
|
|
|
clear_trip_selection();
|
|
|
|
|
|
|
|
const dive *oldCurrent = current_dive;
|
|
|
|
|
2022-05-21 11:14:47 +00:00
|
|
|
dive *newCurrent = current_dive;
|
|
|
|
if (current_dive && std::find(selection.begin(), selection.end(), current_dive) == selection.end())
|
|
|
|
newCurrent = closestInSelection(current_dive->when, selection);
|
2022-09-17 17:07:35 +00:00
|
|
|
setSelectionCore(selection, newCurrent);
|
2022-05-21 18:38:17 +00:00
|
|
|
|
|
|
|
return current_dive != oldCurrent;
|
2019-02-17 09:18:00 +00:00
|
|
|
}
|
|
|
|
|
2022-08-27 15:28:34 +00:00
|
|
|
void setTripSelection(dive_trip *trip, dive *currentDive)
|
|
|
|
{
|
|
|
|
if (!trip)
|
|
|
|
return;
|
|
|
|
|
|
|
|
current_dive = currentDive;
|
|
|
|
for (int i = 0; i < divelog.dives->nr; ++i) {
|
|
|
|
dive &d = *divelog.dives->dives[i];
|
|
|
|
d.selected = d.divetrip == trip;
|
|
|
|
}
|
2024-06-01 20:05:57 +00:00
|
|
|
for (auto &t: *divelog.trips)
|
|
|
|
t->selected = t.get() == trip;
|
2022-08-27 15:28:34 +00:00
|
|
|
|
2024-06-02 15:06:18 +00:00
|
|
|
amount_selected = static_cast<int>(trip->dives.size());
|
2022-08-27 15:28:34 +00:00
|
|
|
amount_trips_selected = 1;
|
|
|
|
|
|
|
|
emit diveListNotifier.tripSelected(trip, currentDive);
|
|
|
|
}
|
|
|
|
|
2024-05-04 16:45:55 +00:00
|
|
|
void select_single_dive(dive *d)
|
2019-12-06 08:47:59 +00:00
|
|
|
{
|
|
|
|
if (d)
|
2022-04-04 16:57:28 +00:00
|
|
|
setSelection(std::vector<dive *>{ d }, d, -1);
|
2019-12-06 08:47:59 +00:00
|
|
|
else
|
2022-04-04 16:57:28 +00:00
|
|
|
setSelection(std::vector<dive *>(), nullptr, -1);
|
2019-12-06 08:47:59 +00:00
|
|
|
}
|
|
|
|
|
2019-02-17 09:18:00 +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!
|
|
|
|
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;
|
|
|
|
}
|
2019-11-25 18:00:38 +00:00
|
|
|
|
2022-05-21 11:14:47 +00:00
|
|
|
bool diveInSelection(const std::vector<dive *> &selection, const dive *d)
|
|
|
|
{
|
|
|
|
// Do a binary search using the ordering of the dive list.
|
|
|
|
auto it = std::lower_bound(selection.begin(), selection.end(), d, dive_less_than);
|
|
|
|
return it != selection.end() && *it == d;
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateSelection(std::vector<dive *> &selection, const std::vector<dive *> &add, const std::vector<dive *> &remove)
|
|
|
|
{
|
|
|
|
// We could sort the array and merge the vectors as we do in the undo code. But is it necessary?
|
|
|
|
for (dive *d: add) {
|
|
|
|
auto it = std::lower_bound(selection.begin(), selection.end(), d, dive_less_than);
|
|
|
|
if (it != selection.end() && *it == d)
|
|
|
|
continue; // Ooops. Already there?
|
|
|
|
selection.insert(it, d);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Likewise, we could sort the array and be smarter here. Again, is it necessary?
|
|
|
|
for (dive *d: remove) {
|
|
|
|
auto it = std::lower_bound(selection.begin(), selection.end(), d, dive_less_than);
|
|
|
|
if (it == selection.end() || *it != d)
|
|
|
|
continue; // Ooops. Not there?
|
|
|
|
selection.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 18:00:38 +00:00
|
|
|
// Select the first dive that is visible
|
2024-05-04 16:45:55 +00:00
|
|
|
void select_newest_visible_dive()
|
2019-11-25 18:00:38 +00:00
|
|
|
{
|
core: introduce divelog structure
The parser API was very annoying, as a number of tables
to-be-filled were passed in as pointers. The goal of this
commit is to collect all these tables in a single struct.
This should make it (more or less) clear what is actually
written into the divelog files.
Moreover, it should now be rather easy to search for
instances, where the global logfile is accessed (and it
turns out that there are many!).
The divelog struct does not contain the tables as substructs,
but only collects pointers. The idea is that the "divelog.h"
file can be included without all the other files describing
the numerous tables.
To make it easier to use from C++ parts of the code, the
struct implements a constructor and a destructor. Sadly,
we can't use smart pointers, since the pointers are accessed
from C code. Therfore the constructor and destructor are
quite complex.
The whole commit is large, but was mostly an automatic
conversion.
One oddity of note: the divelog structure also contains
the "autogroup" flag, since that is saved in the divelog.
This actually fixes a bug: Before, when importing dives
from a different log, the autogroup flag was overwritten.
This was probably not intended and does not happen anymore.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2022-11-08 20:31:08 +00:00
|
|
|
for (int i = divelog.dives->nr - 1; i >= 0; --i) {
|
|
|
|
dive *d = divelog.dives->dives[i];
|
2019-11-25 18:00:38 +00:00
|
|
|
if (!d->hidden_by_filter)
|
2019-12-06 08:47:59 +00:00
|
|
|
return select_single_dive(d);
|
2019-11-25 18:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// No visible dive -> deselect all
|
2019-12-06 08:47:59 +00:00
|
|
|
select_single_dive(nullptr);
|
2019-11-25 18:00:38 +00:00
|
|
|
}
|
2020-05-02 12:34:40 +00:00
|
|
|
|
2024-05-04 16:45:55 +00:00
|
|
|
void select_trip(struct dive_trip *trip)
|
2020-05-02 12:34:40 +00:00
|
|
|
{
|
|
|
|
if (trip && !trip->selected) {
|
|
|
|
trip->selected = true;
|
|
|
|
amount_trips_selected++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-04 16:45:55 +00:00
|
|
|
void deselect_trip(struct dive_trip *trip)
|
2020-05-02 12:34:40 +00:00
|
|
|
{
|
|
|
|
if (trip && trip->selected) {
|
|
|
|
trip->selected = false;
|
|
|
|
amount_trips_selected--;
|
|
|
|
}
|
|
|
|
}
|
2020-05-02 12:41:52 +00:00
|
|
|
|
2024-05-04 16:45:55 +00:00
|
|
|
struct dive_trip *single_selected_trip()
|
2020-05-02 16:14:44 +00:00
|
|
|
{
|
|
|
|
if (amount_trips_selected != 1)
|
|
|
|
return NULL;
|
2024-06-01 20:05:57 +00:00
|
|
|
for (auto &trip: *divelog.trips) {
|
|
|
|
if (trip->selected)
|
|
|
|
return trip.get();
|
2020-05-02 16:14:44 +00:00
|
|
|
}
|
2024-03-24 20:03:08 +00:00
|
|
|
report_info("warning: found no selected trip even though one should be selected");
|
2020-05-02 16:14:44 +00:00
|
|
|
return NULL; // shouldn't happen
|
|
|
|
}
|