core: use divelog in importDives() and process_imported_dives()

Instead of a long argument list.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2022-11-12 08:40:04 +01:00 committed by bstoeger
parent 9c253ee6c5
commit 8cd451fc33
17 changed files with 103 additions and 121 deletions

View file

@ -18,11 +18,9 @@ void addDive(dive *d, bool autogroup, bool newNumber)
execute(new AddDive(d, autogroup, newNumber));
}
void importDives(struct dive_table *dives, struct trip_table *trips, struct dive_site_table *sites,
struct device_table *devices, struct filter_preset_table *presets,
int flags, const QString &source)
void importDives(struct divelog *log, int flags, const QString &source)
{
execute(new ImportDives(dives, trips, sites, devices, presets, flags, source));
execute(new ImportDives(log, flags, source));
}
void deleteDive(const QVector<struct dive*> &divesToDelete)

View file

@ -2,17 +2,23 @@
#ifndef COMMAND_H
#define COMMAND_H
#include "core/dive.h"
#include "core/divelog.h"
#include "core/equipment.h"
#include "core/pictureobj.h"
#include "core/taxonomy.h"
#include <QVector>
#include <QAction>
#include <vector>
struct divecomputer;
struct divelog;
struct dive_components;
struct dive_site;
struct dive_trip;
struct event;
struct DiveAndLocation;
struct FilterData;
struct filter_preset_table;
struct device_table;
// We put everything in a namespace, so that we can shorten names without polluting the global namespace
namespace Command {
@ -35,10 +41,7 @@ bool placingCommand(); // Currently executing a new command -> might not have
// If newNumber is true, the dive is assigned a new number, depending on the
// insertion position.
void addDive(dive *d, bool autogroup, bool newNumber);
void importDives(struct dive_table *dives, struct trip_table *trips,
struct dive_site_table *sites, struct device_table *devices,
struct filter_preset_table *filter_presets,
int flags, const QString &source); // The tables are consumed!
void importDives(struct divelog *log, int flags, const QString &source); // The tables are consumed!
void deleteDive(const QVector<struct dive*> &divesToDelete);
void shiftTime(const std::vector<dive *> &changedDives, int amount);
void renumberDives(const QVector<QPair<dive *, int>> &divesToRenumber);

View file

@ -468,11 +468,9 @@ void AddDive::undoit()
setSelection(selection, currentDive);
}
ImportDives::ImportDives(struct dive_table *dives, struct trip_table *trips, struct dive_site_table *sites,
struct device_table *devices, struct filter_preset_table *filter_presets, int flags,
const QString &source)
ImportDives::ImportDives(struct divelog *log, int flags, const QString &source)
{
setText(Command::Base::tr("import %n dive(s) from %1", "", dives->nr).arg(source));
setText(Command::Base::tr("import %n dive(s) from %1", "", log->dives->nr).arg(source));
// this only matters if undoit were called before redoit
currentDive = nullptr;
@ -481,7 +479,7 @@ ImportDives::ImportDives(struct dive_table *dives, struct trip_table *trips, str
struct dive_table dives_to_remove = empty_dive_table;
struct trip_table trips_to_add = empty_trip_table;
struct dive_site_table sites_to_add = empty_dive_site_table;
process_imported_dives(dives, trips, sites, devices, flags,
process_imported_dives(log, flags,
&dives_to_add, &dives_to_remove, &trips_to_add,
&sites_to_add, &devicesToAddAndRemove);
@ -515,8 +513,7 @@ ImportDives::ImportDives(struct dive_table *dives, struct trip_table *trips, str
// When encountering filter presets with equal names, check whether they are
// the same. If they are, ignore them.
if (filter_presets) {
for (const filter_preset &preset: *filter_presets) {
for (const filter_preset &preset: *log->filter_presets) {
QString name = preset.name;
auto it = std::find_if(divelog.filter_presets->begin(), divelog.filter_presets->end(),
[&name](const filter_preset &preset) { return preset.name == name; });
@ -524,10 +521,6 @@ ImportDives::ImportDives(struct dive_table *dives, struct trip_table *trips, str
continue;
filterPresetsToAdd.emplace_back(preset.name, preset.data);
}
// Consume the table for analogy with the other tables.
filter_presets->clear();
}
}
bool ImportDives::workToBeDone()

View file

@ -99,9 +99,7 @@ private:
class ImportDives : public DiveListBase {
public:
// Note: dives and trips are consumed - after the call they will be empty.
ImportDives(struct dive_table *dives, struct trip_table *trips, struct dive_site_table *sites,
struct device_table *devices, struct filter_preset_table *filter_presets, int flags,
const QString &source);
ImportDives(struct divelog *log, int flags, const QString &source);
private:
void undoit() override;
void redoit() override;

View file

@ -6,6 +6,7 @@
#include "command_base.h"
#include "command.h" // for EditCylinderType
#include "core/divecomputer.h"
#include "core/subsurface-qt/divelistnotifier.h"
#include <QVector>

View file

@ -979,8 +979,7 @@ void add_imported_dives(struct divelog *import_log, int flags)
/* Process imported dives and generate lists of dives
* to-be-added and to-be-removed */
process_imported_dives(import_log->dives, import_log->trips, import_log->sites, import_log->devices,
flags, &dives_to_add, &dives_to_remove, &trips_to_add,
process_imported_dives(import_log, flags, &dives_to_add, &dives_to_remove, &trips_to_add,
&dive_sites_to_add, devices_to_add);
/* Add new dives to trip and site to get reference count correct. */
@ -1098,9 +1097,7 @@ bool try_to_merge_trip(struct dive_trip *trip_import, struct dive_table *import_
* - If IMPORT_ADD_TO_NEW_TRIP is true, dives that are not assigned
* to a trip will be added to a newly generated trip.
*/
void process_imported_dives(struct dive_table *import_table, struct trip_table *import_trip_table,
struct dive_site_table *import_sites_table, struct device_table *import_device_table,
int flags,
void process_imported_dives(struct divelog *import_log, int flags,
/* output parameters: */
struct dive_table *dives_to_add, struct dive_table *dives_to_remove,
struct trip_table *trips_to_add, struct dive_site_table *sites_to_add,
@ -1112,13 +1109,6 @@ void process_imported_dives(struct dive_table *import_table, struct trip_table *
bool new_dive_has_number = false;
bool last_old_dive_is_numbered;
/* If the caller didn't pass an import_trip_table because all
* dives are tripless, provide a local table. This may be
* necessary if the trips are autogrouped */
struct trip_table local_trip_table = empty_trip_table;
if (!import_trip_table)
import_trip_table = &local_trip_table;
/* Make sure that output parameters don't contain garbage */
clear_dive_table(dives_to_add);
clear_dive_table(dives_to_remove);
@ -1129,45 +1119,45 @@ void process_imported_dives(struct dive_table *import_table, struct trip_table *
/* Check if any of the new dives has a number. This will be
* important later to decide if we want to renumber the added
* dives */
for (int i = 0; i < import_table->nr; i++) {
if (import_table->dives[i]->number > 0) {
for (int i = 0; i < import_log->dives->nr; i++) {
if (import_log->dives->dives[i]->number > 0) {
new_dive_has_number = true;
break;
}
}
/* If no dives were imported, don't bother doing anything */
if (!import_table->nr)
if (!import_log->dives->nr)
return;
/* Add only the devices that we don't know about yet. */
for (i = 0; i < nr_devices(import_device_table); i++) {
const struct device *dev = get_device(import_device_table, i);
for (i = 0; i < nr_devices(import_log->devices); i++) {
const struct device *dev = get_device(import_log->devices, i);
if (!device_exists(divelog.devices, dev))
add_to_device_table(devices_to_add, dev);
}
/* Sort the table of dives to be imported and combine mergable dives */
sort_dive_table(import_table);
merge_imported_dives(import_table);
sort_dive_table(import_log->dives);
merge_imported_dives(import_log->dives);
/* Autogroup tripless dives if desired by user. But don't autogroup
* if tripless dives should be added to a new trip. */
if (!(flags & IMPORT_ADD_TO_NEW_TRIP))
autogroup_dives(import_table, import_trip_table);
autogroup_dives(import_log->dives, import_log->trips);
/* If dive sites already exist, use the existing versions. */
for (i = 0; i < import_sites_table->nr; i++) {
struct dive_site *new_ds = import_sites_table->dive_sites[i];
for (i = 0; i < import_log->sites->nr; i++) {
struct dive_site *new_ds = import_log->sites->dive_sites[i];
struct dive_site *old_ds = get_same_dive_site(new_ds);
/* Check if it dive site is actually used by new dives. */
for (j = 0; j < import_table->nr; j++) {
if (import_table->dives[j]->dive_site == new_ds)
for (j = 0; j < import_log->dives->nr; j++) {
if (import_log->dives->dives[j]->dive_site == new_ds)
break;
}
if (j == import_table->nr) {
if (j == import_log->dives->nr) {
/* Dive site not even used - free it and go to next. */
free_dive_site(new_ds);
continue;
@ -1180,22 +1170,22 @@ void process_imported_dives(struct dive_table *import_table, struct trip_table *
continue;
}
/* Dive site already exists - use the old and free the new. */
for (j = 0; j < import_table->nr; j++) {
if (import_table->dives[j]->dive_site == new_ds)
import_table->dives[j]->dive_site = old_ds;
for (j = 0; j < import_log->dives->nr; j++) {
if (import_log->dives->dives[j]->dive_site == new_ds)
import_log->dives->dives[j]->dive_site = old_ds;
}
free_dive_site(new_ds);
}
import_sites_table->nr = 0; /* All dive sites were consumed */
import_log->sites->nr = 0; /* All dive sites were consumed */
/* Merge overlapping trips. Since both trip tables are sorted, we
* could be smarter here, but realistically not a whole lot of trips
* will be imported so do a simple n*m loop until someone complains.
*/
for (i = 0; i < import_trip_table->nr; i++) {
trip_import = import_trip_table->trips[i];
for (i = 0; i < import_log->trips->nr; i++) {
trip_import = import_log->trips->trips[i];
if ((flags & IMPORT_MERGE_ALL_TRIPS) || trip_import->autogen) {
if (try_to_merge_trip(trip_import, import_table, flags & IMPORT_PREFER_IMPORTED, dives_to_add, dives_to_remove,
if (try_to_merge_trip(trip_import, import_log->dives, flags & IMPORT_PREFER_IMPORTED, dives_to_add, dives_to_remove,
&sequence_changed, &start_renumbering_at))
continue;
}
@ -1209,34 +1199,34 @@ void process_imported_dives(struct dive_table *import_table, struct trip_table *
insert_dive(dives_to_add, d);
sequence_changed |= !dive_is_after_last(d);
remove_dive(d, import_table);
remove_dive(d, import_log->dives);
}
/* Then, add trip to list of trips to add */
insert_trip(trip_import, trips_to_add);
trip_import->dives.nr = 0; /* Caller is responsible for adding dives to trip */
}
import_trip_table->nr = 0; /* All trips were consumed */
import_log->trips->nr = 0; /* All trips were consumed */
if ((flags & IMPORT_ADD_TO_NEW_TRIP) && import_table->nr > 0) {
if ((flags & IMPORT_ADD_TO_NEW_TRIP) && import_log->dives->nr > 0) {
/* Create a new trip for unassigned dives, if desired. */
new_trip = create_trip_from_dive(import_table->dives[0]);
new_trip = create_trip_from_dive(import_log->dives->dives[0]);
insert_trip(new_trip, trips_to_add);
/* Add all remaining dives to this trip */
for (i = 0; i < import_table->nr; i++) {
struct dive *d = import_table->dives[i];
for (i = 0; i < import_log->dives->nr; i++) {
struct dive *d = import_log->dives->dives[i];
d->divetrip = new_trip;
insert_dive(dives_to_add, d);
sequence_changed |= !dive_is_after_last(d);
}
import_table->nr = 0; /* All dives were consumed */
} else if (import_table->nr > 0) {
/* The remaining dives in import_table are those that don't belong to
import_log->dives->nr = 0; /* All dives were consumed */
} else if (import_log->dives->nr > 0) {
/* The remaining dives in import_log->dives are those that don't belong to
* a trip and the caller does not want them to be associated to a
* new trip. Merge them into the global table. */
sequence_changed |= merge_dive_tables(import_table, NULL, divelog.dives, flags & IMPORT_PREFER_IMPORTED, NULL,
sequence_changed |= merge_dive_tables(import_log->dives, NULL, divelog.dives, flags & IMPORT_PREFER_IMPORTED, NULL,
dives_to_add, dives_to_remove, &start_renumbering_at);
}

View file

@ -36,9 +36,7 @@ extern void process_loaded_dives();
#define IMPORT_MERGE_ALL_TRIPS (1 << 2)
#define IMPORT_ADD_TO_NEW_TRIP (1 << 3)
extern void add_imported_dives(struct divelog *log, int flags);
extern void process_imported_dives(struct dive_table *import_table, struct trip_table *import_trip_table,
struct dive_site_table *import_sites_table, struct device_table *import_devices_table,
int flags,
extern void process_imported_dives(struct divelog *import_log, int flags,
struct dive_table *dives_to_add, struct dive_table *dives_to_remove,
struct trip_table *trips_to_add, struct dive_site_table *sites_to_add,
struct device_table *devices_to_add);

View file

@ -36,6 +36,21 @@ divelog::~divelog()
delete filter_presets;
}
divelog::divelog(divelog &&log) :
dives(new dive_table),
trips(new trip_table),
sites(new dive_site_table),
devices(std::move(log.devices)),
filter_presets(std::move(log.filter_presets))
{
*dives = empty_dive_table;
*trips = empty_trip_table;
*sites = empty_dive_site_table;
move_dive_table(log.dives, dives);
move_trip_table(log.trips, trips);
move_dive_site_table(log.sites, sites);
}
void divelog::clear()
{
while (dives->nr)

View file

@ -22,6 +22,7 @@ struct divelog {
void clear();
divelog();
~divelog();
divelog(divelog &&log); // move constructor (argument is consumed).
#endif
};

View file

@ -53,6 +53,7 @@ static MAKE_REMOVE_FROM(trip_table, trips)
MAKE_SORT(trip_table, struct dive_trip *, trips, comp_trips)
MAKE_REMOVE(trip_table, struct dive_trip *, trip)
MAKE_CLEAR_TABLE(trip_table, trips, trip)
MAKE_MOVE_TABLE(trip_table, trips)
timestamp_t trip_date(const struct dive_trip *trip)
{

View file

@ -57,6 +57,7 @@ extern bool is_trip_before_after(const struct dive *dive, bool before);
extern bool trip_is_single_day(const struct dive_trip *trip);
extern int trip_shown_dives(const struct dive_trip *trip);
void move_trip_table(struct trip_table *src, struct trip_table *dst);
void clear_trip_table(struct trip_table *table);
#ifdef DEBUG_TRIP

View file

@ -965,7 +965,7 @@ void DiveLogImportDialog::on_buttonBox_accepted()
}
QString source = fileNames.size() == 1 ? fileNames[0] : tr("multiple files");
Command::importDives(log.dives, log.trips, log.sites, log.devices, nullptr, IMPORT_MERGE_ALL_TRIPS, source);
Command::importDives(&log, IMPORT_MERGE_ALL_TRIPS, source);
}
TagDragDelegate::TagDragDelegate(QObject *parent) : QStyledItemDelegate(parent)

View file

@ -1284,7 +1284,7 @@ void MainWindow::importFiles(const QStringList fileNames)
parse_file(fileNamePtr.data(), &log);
}
QString source = fileNames.size() == 1 ? fileNames[0] : tr("multiple files");
Command::importDives(log.dives, log.trips, log.sites, log.devices, log.filter_presets, IMPORT_MERGE_ALL_TRIPS, source);
Command::importDives(&log, IMPORT_MERGE_ALL_TRIPS, source);
}
void MainWindow::loadFiles(const QStringList fileNames)

View file

@ -465,7 +465,7 @@ void DivelogsDeWebServices::buttonClicked(QAbstractButton *button)
/* parse file and import dives */
struct divelog log;
parse_file(QFile::encodeName(zipFile.fileName()), &log);
Command::importDives(log.dives, log.trips, log.sites, log.devices, nullptr, IMPORT_MERGE_ALL_TRIPS, QStringLiteral("divelogs.de"));
Command::importDives(&log, IMPORT_MERGE_ALL_TRIPS, QStringLiteral("divelogs.de"));
/* store last entered user/pass in config */
qPrefCloudStorage::set_divelogde_user(ui.userID->text());

View file

@ -1,11 +1,10 @@
#include "diveimportedmodel.h"
#include "core/dive.h"
#include "core/qthelper.h"
#include "core/divelist.h"
#include "commands/command.h"
DiveImportedModel::DiveImportedModel(QObject *o) : QAbstractTableModel(o),
diveTable(empty_dive_table),
sitesTable(empty_dive_site_table)
DiveImportedModel::DiveImportedModel(QObject *o) : QAbstractTableModel(o)
{
connect(&thread, &QThread::finished, this, &DiveImportedModel::downloadThreadFinished);
}
@ -17,7 +16,7 @@ int DiveImportedModel::columnCount(const QModelIndex&) const
int DiveImportedModel::rowCount(const QModelIndex&) const
{
return diveTable.nr;
return log.dives->nr;
}
QVariant DiveImportedModel::headerData(int section, Qt::Orientation orientation, int role) const
@ -50,10 +49,10 @@ QVariant DiveImportedModel::data(const QModelIndex &index, int role) const
if (!index.isValid())
return QVariant();
if (index.row() >= diveTable.nr)
if (index.row() >= log.dives->nr)
return QVariant();
struct dive *d = get_dive_from_table(index.row(), &diveTable);
struct dive *d = get_dive_from_table(index.row(), log.dives);
if (!d)
return QVariant();
@ -92,7 +91,7 @@ void DiveImportedModel::changeSelected(QModelIndex clickedIndex)
void DiveImportedModel::selectAll()
{
std::fill(checkStates.begin(), checkStates.end(), true);
dataChanged(index(0, 0), index(diveTable.nr - 1, 0), QVector<int>() << Qt::CheckStateRole << Selected);
dataChanged(index(0, 0), index(log.dives->nr - 1, 0), QVector<int>() << Qt::CheckStateRole << Selected);
}
void DiveImportedModel::selectRow(int row)
@ -104,7 +103,7 @@ void DiveImportedModel::selectRow(int row)
void DiveImportedModel::selectNone()
{
std::fill(checkStates.begin(), checkStates.end(), false);
dataChanged(index(0, 0), index(diveTable.nr - 1, 0 ), QVector<int>() << Qt::CheckStateRole << Selected);
dataChanged(index(0, 0), index(log.dives->nr - 1, 0 ), QVector<int>() << Qt::CheckStateRole << Selected);
}
Qt::ItemFlags DiveImportedModel::flags(const QModelIndex &index) const
@ -117,8 +116,7 @@ Qt::ItemFlags DiveImportedModel::flags(const QModelIndex &index) const
void DiveImportedModel::clearTable()
{
beginResetModel();
clear_dive_table(&diveTable);
clear_dive_site_table(&sitesTable);
clear_divelog(&log);
endResetModel();
}
@ -127,11 +125,11 @@ void DiveImportedModel::downloadThreadFinished()
beginResetModel();
// Move the table data from thread to model
move_dive_table(&thread.downloadTable, &diveTable);
move_dive_site_table(&thread.diveSiteTable, &sitesTable);
deviceTable = std::move(thread.deviceTable);
move_dive_table(&thread.downloadTable, log.dives);
move_dive_site_table(&thread.diveSiteTable, log.sites);
*log.devices = std::move(thread.deviceTable);
checkStates.resize(diveTable.nr);
checkStates.resize(log.dives->nr);
std::fill(checkStates.begin(), checkStates.end(), true);
endResetModel();
@ -150,46 +148,41 @@ void DiveImportedModel::waitForDownload()
downloadThreadFinished();
}
std::tuple<struct dive_table, struct dive_site_table, struct device_table> DiveImportedModel::consumeTables()
struct divelog DiveImportedModel::consumeTables()
{
beginResetModel();
// Move tables to result
struct dive_table dives = empty_dive_table;
struct dive_site_table sites = empty_dive_site_table;
struct device_table devices;
move_dive_table(&diveTable, &dives);
move_dive_site_table(&sitesTable, &sites);
devices = std::move(deviceTable);
struct divelog res(std::move(log));
// Reset indices
checkStates.clear();
endResetModel();
return std::make_tuple(dives, sites, devices);
return res;
}
int DiveImportedModel::numDives() const
{
return diveTable.nr;
return log.dives->nr;
}
// Delete non-selected dives
void DiveImportedModel::deleteDeselected()
{
int total = diveTable.nr;
int total = log.dives->nr;
int j = 0;
for (int i = 0; i < total; i++) {
if (checkStates[i]) {
j++;
} else {
beginRemoveRows(QModelIndex(), j, j);
delete_dive_from_table(&diveTable, j);
delete_dive_from_table(log.dives, j);
endRemoveRows();
}
}
checkStates.resize(diveTable.nr);
checkStates.resize(log.dives->nr);
std::fill(checkStates.begin(), checkStates.end(), true);
}
@ -199,19 +192,11 @@ void DiveImportedModel::recordDives(int flags)
// delete non-selected dives
deleteDeselected();
// TODO: use structured bindings once we go C++17
std::tuple<struct dive_table, struct dive_site_table, struct device_table> tables = consumeTables();
if (std::get<0>(tables).nr > 0) {
struct divelog log = consumeTables();
if (log.dives->nr > 0) {
auto data = thread.data();
Command::importDives(&std::get<0>(tables), nullptr, &std::get<1>(tables),
&std::get<2>(tables), nullptr, flags, data->devName());
} else {
clear_dive_site_table(&std::get<1>(tables));
Command::importDives(&log, flags, data->devName());
}
// The dives and dive sites have been consumed, but the arrays of the tables
// still exist. Free them.
free(std::get<0>(tables).dives);
free(std::get<1>(tables).dive_sites);
}
QHash<int, QByteArray> DiveImportedModel::roleNames() const {

View file

@ -1,9 +1,8 @@
#ifndef DIVEIMPORTEDMODEL_H
#define DIVEIMPORTEDMODEL_H
#include "core/device.h"
#include "core/divesite.h"
#include "core/divelist.h"
#include "core/divelog.h"
#include "core/downloadfromdcthread.h"
#include <QAbstractTableModel>
@ -23,7 +22,7 @@ public:
Q_INVOKABLE void clearTable();
QHash<int, QByteArray> roleNames() const;
void deleteDeselected();
std::tuple<struct dive_table, struct dive_site_table, struct device_table> consumeTables(); // Returns downloaded tables and resets model.
struct divelog consumeTables(); // Returns downloaded tables and resets model.
int numDives() const;
Q_INVOKABLE void recordDives(int flags = IMPORT_PREFER_IMPORTED | IMPORT_IS_DOWNLOADED);
@ -47,9 +46,7 @@ signals:
private:
std::vector<char> checkStates; // char instead of bool to avoid silly pessimization of std::vector.
struct dive_table diveTable;
struct dive_site_table sitesTable;
struct device_table deviceTable;
struct divelog log;
};
#endif

View file

@ -1,5 +1,6 @@
// SPDX-License-Identifier: GPL-2.0
#include "diveplannermodel.h"
#include "core/dive.h"
#include "core/divelist.h"
#include "core/divelog.h"
#include "core/subsurface-string.h"