Selection: move selection functions from divelist.c to selection.c

Since we now have a selection.c translation unit, put the selection-
related functions there.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2019-11-24 15:02:34 +01:00 committed by bstoeger
parent 7787bfbf9e
commit 4b1a3a1a6e
14 changed files with 142 additions and 127 deletions

View file

@ -29,8 +29,6 @@ extern struct divecomputer *select_dc(struct dive *);
extern unsigned int dc_number; extern unsigned int dc_number;
extern unsigned int amount_selected;
extern int is_default_dive_computer_device(const char *); extern int is_default_dive_computer_device(const char *);
extern int is_default_dive_computer(const char *, const char *); extern int is_default_dive_computer(const char *, const char *);

View file

@ -19,6 +19,7 @@
#include "planner.h" #include "planner.h"
#include "qthelper.h" #include "qthelper.h"
#include "git-access.h" #include "git-access.h"
#include "selection.h"
#include "table.h" #include "table.h"
#include "trip.h" #include "trip.h"
@ -28,23 +29,6 @@ static bool dive_list_changed = false;
bool autogroup = false; bool autogroup = false;
unsigned int amount_selected;
#if DEBUG_SELECTION_TRACKING
void dump_selection(void)
{
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
void set_autogroup(bool value) void set_autogroup(bool value)
{ {
/* if we keep the UI paradigm, this needs to toggle /* if we keep the UI paradigm, this needs to toggle
@ -64,7 +48,6 @@ void get_dive_gas(const struct dive *dive, int *o2_p, int *he_p, int *o2max_p)
int i; int i;
int maxo2 = -1, maxhe = -1, mino2 = 1000; int maxo2 = -1, maxhe = -1, mino2 = 1000;
for (i = 0; i < dive->cylinders.nr; i++) { for (i = 0; i < dive->cylinders.nr; i++) {
const cylinder_t *cyl = get_cylinder(dive, i); const cylinder_t *cyl = get_cylinder(dive, i);
int o2 = get_o2(cyl->gasmix); int o2 = get_o2(cyl->gasmix);
@ -159,7 +142,6 @@ static int calculate_otu(const struct dive *dive)
return lrint(otu); return lrint(otu);
} }
/* Calculate the CNS for a single dive - this only takes the first divecomputer into account. /* Calculate the CNS for a single dive - this only takes the first divecomputer into account.
The CNS contributions are summed for dive segments defined by samples. The maximum O2 exposure duration for each The CNS contributions are summed for dive segments defined by samples. The maximum O2 exposure duration for each
segment is calculated based on the mean depth of the two samples (start & end) that define each segment. The CNS segment is calculated based on the mean depth of the two samples (start & end) that define each segment. The CNS
@ -648,30 +630,6 @@ char *get_dive_gas_string(const struct dive *dive)
return buffer; return buffer;
} }
struct dive *first_selected_dive()
{
int idx;
struct dive *d;
for_each_dive (idx, d) {
if (d->selected)
return d;
}
return NULL;
}
struct dive *last_selected_dive()
{
int idx;
struct dive *d, *ret = NULL;
for_each_dive (idx, d) {
if (d->selected)
ret = d;
}
return ret;
}
/* Like strcmp(), but don't crash on null-pointers */ /* Like strcmp(), but don't crash on null-pointers */
static int safe_strcmp(const char *s1, const char *s2) static int safe_strcmp(const char *s1, const char *s2)
{ {
@ -834,71 +792,6 @@ void append_dive(struct dive *dive)
amount_selected++; amount_selected++;
} }
bool consecutive_selected()
{
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;
}
void select_dive(struct dive *dive)
{
if (!dive)
return;
if (!dive->selected) {
dive->selected = 1;
amount_selected++;
}
current_dive = dive;
}
void deselect_dive(struct dive *dive)
{
int idx;
if (dive && dive->selected) {
dive->selected = 0;
if (amount_selected)
amount_selected--;
if (current_dive == dive && amount_selected > 0) {
/* pick a different dive as selected */
int selected_dive = idx = get_divenr(dive);
while (--selected_dive >= 0) {
dive = get_dive(selected_dive);
if (dive && dive->selected) {
current_dive = dive;
return;
}
}
selected_dive = idx;
while (++selected_dive < dive_table.nr) {
dive = get_dive(selected_dive);
if (dive && dive->selected) {
current_dive = dive;
return;
}
}
}
current_dive = NULL;
}
}
int shown_dives = 0; int shown_dives = 0;
bool filter_dive(struct dive *d, bool shown) bool filter_dive(struct dive *d, bool shown)
{ {

View file

@ -41,12 +41,7 @@ extern void insert_dive(struct dive_table *table, struct dive *d);
extern void get_dive_gas(const struct dive *dive, int *o2_p, int *he_p, int *o2low_p); extern void get_dive_gas(const struct dive *dive, int *o2_p, int *he_p, int *o2low_p);
extern int get_divenr(const struct dive *dive); extern int get_divenr(const struct dive *dive);
extern int remove_dive(const struct dive *dive, struct dive_table *table); extern int remove_dive(const struct dive *dive, struct dive_table *table);
extern bool consecutive_selected();
extern void select_dive(struct dive *dive);
extern void deselect_dive(struct dive *dive);
extern bool filter_dive(struct dive *d, bool shown); /* returns true if status changed */ extern bool filter_dive(struct dive *d, bool shown); /* returns true if status changed */
extern struct dive *first_selected_dive();
extern struct dive *last_selected_dive();
extern int get_dive_nr_at_idx(int idx); extern int get_dive_nr_at_idx(int idx);
extern void set_dive_nr_for_current_dive(); extern void set_dive_nr_for_current_dive();
extern timestamp_t get_surface_interval(timestamp_t when); extern timestamp_t get_surface_interval(timestamp_t when);
@ -63,10 +58,6 @@ void clear_dive_file_data();
void clear_dive_table(struct dive_table *table); void clear_dive_table(struct dive_table *table);
void move_dive_table(struct dive_table *src, struct dive_table *dst); void move_dive_table(struct dive_table *src, struct dive_table *dst);
#ifdef DEBUG_TRIP
extern void dump_selection(void);
#endif
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View file

@ -8,6 +8,112 @@
#include <QVector> #include <QVector>
int amount_selected;
extern "C" void select_dive(struct dive *dive)
{
if (!dive)
return;
if (!dive->selected) {
dive->selected = 1;
amount_selected++;
}
current_dive = dive;
}
extern "C" void deselect_dive(struct dive *dive)
{
int idx;
if (dive && dive->selected) {
dive->selected = 0;
if (amount_selected)
amount_selected--;
if (current_dive == dive && amount_selected > 0) {
/* pick a different dive as selected */
int selected_dive = idx = get_divenr(dive);
while (--selected_dive >= 0) {
dive = get_dive(selected_dive);
if (dive && dive->selected) {
current_dive = dive;
return;
}
}
selected_dive = idx;
while (++selected_dive < dive_table.nr) {
dive = get_dive(selected_dive);
if (dive && dive->selected) {
current_dive = dive;
return;
}
}
}
current_dive = NULL;
}
}
extern "C" struct dive *first_selected_dive()
{
int idx;
struct dive *d;
for_each_dive (idx, d) {
if (d->selected)
return d;
}
return NULL;
}
extern "C" struct dive *last_selected_dive()
{
int idx;
struct dive *d, *ret = NULL;
for_each_dive (idx, d) {
if (d->selected)
ret = d;
}
return ret;
}
extern "C" bool consecutive_selected()
{
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
extern "C" void dump_selection(void)
{
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
// Set the current dive either from a list of selected dives, // Set the current dive either from a list of selected dives,
// or a newly selected dive. In both cases, try to select the // or a newly selected dive. In both cases, try to select the
// dive that is newer that is newer than the given date. // dive that is newer that is newer than the given date.

View file

@ -4,13 +4,35 @@
#ifndef SELECTION_H #ifndef SELECTION_H
#define SELECTION_H #define SELECTION_H
struct dive;
extern int amount_selected;
/*** C and C++ functions ***/
#ifdef __cplusplus
extern "C" {
#endif
extern void select_dive(struct dive *dive);
extern void deselect_dive(struct dive *dive);
extern struct dive *first_selected_dive(void);
extern struct dive *last_selected_dive(void);
extern bool consecutive_selected(void);
#if DEBUG_SELECTION_TRACKING
extern void dump_selection(void);
#endif
#ifdef __cplusplus
}
#endif
/*** C++-only functions ***/ /*** C++-only functions ***/
#ifdef __cplusplus #ifdef __cplusplus
#include <vector> #include <vector>
struct dive;
// Reset the selection to the dives of the "selection" vector and send the appropriate signals. // 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 // Set the current dive to "currentDive". "currentDive" must be an element of "selection" (or
// null if "seletion" is empty). // null if "seletion" is empty).

View file

@ -2,6 +2,7 @@
#include "trip.h" #include "trip.h"
#include "subsurface-string.h" #include "subsurface-string.h"
#include "selection.h"
#include "table.h" #include "table.h"
struct trip_table trip_table; struct trip_table trip_table;

View file

@ -9,7 +9,7 @@
#include "desktop-widgets/modeldelegates.h" #include "desktop-widgets/modeldelegates.h"
#include "desktop-widgets/mainwindow.h" #include "desktop-widgets/mainwindow.h"
#include "desktop-widgets/divepicturewidget.h" #include "desktop-widgets/divepicturewidget.h"
#include "core/display.h" #include "core/selection.h"
#include "core/divefilter.h" #include "core/divefilter.h"
#include <unistd.h> #include <unistd.h>
#include <QSettings> #include <QSettings>

View file

@ -18,7 +18,7 @@
#include "core/qthelper.h" #include "core/qthelper.h"
#include "libdivecomputer/parser.h" #include "libdivecomputer/parser.h"
#include "desktop-widgets/divelistview.h" #include "desktop-widgets/divelistview.h"
#include "core/display.h" #include "core/selection.h"
#include "profile-widget/profilewidget2.h" #include "profile-widget/profilewidget2.h"
#include "commands/command.h" #include "commands/command.h"
#include "core/metadata.h" #include "core/metadata.h"

View file

@ -11,7 +11,7 @@
#include "core/file.h" #include "core/file.h"
#include "desktop-widgets/mapwidget.h" #include "desktop-widgets/mapwidget.h"
#include "desktop-widgets/tab-widgets/maintab.h" #include "desktop-widgets/tab-widgets/maintab.h"
#include "core/display.h" #include "core/selection.h"
#include "core/membuffer.h" #include "core/membuffer.h"
#include "core/cloudstorage.h" #include "core/cloudstorage.h"
#include "core/subsurface-string.h" #include "core/subsurface-string.h"

View file

@ -3,7 +3,7 @@
#include "ui_TabDiveStatistics.h" #include "ui_TabDiveStatistics.h"
#include <core/qthelper.h> #include <core/qthelper.h>
#include <core/display.h> #include <core/selection.h>
#include <core/statistics.h> #include <core/statistics.h>
TabDiveStatistics::TabDiveStatistics(QWidget *parent) : TabBase(parent), ui(new Ui::TabDiveStatistics()) TabDiveStatistics::TabDiveStatistics(QWidget *parent) : TabBase(parent), ui(new Ui::TabDiveStatistics())

View file

@ -12,7 +12,7 @@
#include "core/trip.h" #include "core/trip.h"
#include "qt-models/diveplannermodel.h" #include "qt-models/diveplannermodel.h"
#include "desktop-widgets/divelistview.h" #include "desktop-widgets/divelistview.h"
#include "core/display.h" #include "core/selection.h"
#include "profile-widget/profilewidget2.h" #include "profile-widget/profilewidget2.h"
#include "desktop-widgets/diveplanner.h" #include "desktop-widgets/diveplanner.h"
#include "core/divesitehelpers.h" #include "core/divesitehelpers.h"

View file

@ -4,7 +4,7 @@
#include <list> #include <list>
#include "templatelayout.h" #include "templatelayout.h"
#include "core/display.h" #include "core/selection.h"
QList<QString> grantlee_templates, grantlee_statistics_templates; QList<QString> grantlee_templates, grantlee_statistics_templates;

View file

@ -36,6 +36,7 @@
#include "core/downloadfromdcthread.h" #include "core/downloadfromdcthread.h"
#include "core/subsurface-string.h" #include "core/subsurface-string.h"
#include "core/pref.h" #include "core/pref.h"
#include "core/selection.h"
#include "core/ssrf.h" #include "core/ssrf.h"
#include "core/save-profiledata.h" #include "core/save-profiledata.h"
#include "core/settings/qPrefGeneral.h" #include "core/settings/qPrefGeneral.h"

View file

@ -70,6 +70,7 @@ SOURCES += ../../subsurface-mobile-main.cpp \
../../core/equipment.c \ ../../core/equipment.c \
../../core/gas.c \ ../../core/gas.c \
../../core/membuffer.c \ ../../core/membuffer.c \
../../core/selection.cpp \
../../core/sha1.c \ ../../core/sha1.c \
../../core/strtod.c \ ../../core/strtod.c \
../../core/tag.c \ ../../core/tag.c \
@ -187,6 +188,8 @@ HEADERS += \
../../core/membuffer.h \ ../../core/membuffer.h \
../../core/metrics.h \ ../../core/metrics.h \
../../core/qt-gui.h \ ../../core/qt-gui.h \
../../core/selection.h \
../../core/divecomputer.h \
../../core/sha1.h \ ../../core/sha1.h \
../../core/strndup.h \ ../../core/strndup.h \
../../core/subsurfacestartup.h \ ../../core/subsurfacestartup.h \