2017-04-27 18:25:32 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "qt-models/divetripmodel.h"
|
2019-11-17 17:13:55 +00:00
|
|
|
#include "core/divefilter.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 "core/divelog.h"
|
2020-03-05 19:00:31 +00:00
|
|
|
#ifdef SUBSURFACE_MOBILE
|
|
|
|
#include "qt-models/mobilelistmodel.h"
|
|
|
|
#endif
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "core/gettextfromc.h"
|
|
|
|
#include "core/metrics.h"
|
2019-11-25 23:08:35 +00:00
|
|
|
#include "core/selection.h"
|
2021-12-16 22:09:39 +00:00
|
|
|
#include "core/string-format.h"
|
2019-05-31 14:09:14 +00:00
|
|
|
#include "core/trip.h"
|
2018-06-03 20:15:19 +00:00
|
|
|
#include "core/qthelper.h"
|
2022-09-24 12:06:56 +00:00
|
|
|
#include "core/range.h"
|
2019-08-31 13:05:11 +00:00
|
|
|
#include "core/divesite.h"
|
2020-04-10 07:42:14 +00:00
|
|
|
#include "core/picture.h"
|
2018-05-11 15:25:41 +00:00
|
|
|
#include "core/subsurface-string.h"
|
2019-05-30 16:29:36 +00:00
|
|
|
#include "core/tag.h"
|
2019-08-31 13:05:11 +00:00
|
|
|
#include "qt-models/divelocationmodel.h" // For the dive-site field ids
|
2019-11-13 14:08:40 +00:00
|
|
|
#include "commands/command.h"
|
2015-06-29 17:39:12 +00:00
|
|
|
#include <QIcon>
|
2017-10-03 20:16:15 +00:00
|
|
|
#include <QDebug>
|
2020-03-05 19:00:31 +00:00
|
|
|
#include <QDateTime>
|
2018-12-27 09:06:11 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
// 1) Base functions
|
2015-05-28 21:33:51 +00:00
|
|
|
|
2018-10-14 06:56:53 +00:00
|
|
|
static int nitrox_sort_value(const struct dive *dive)
|
2015-05-28 21:33:51 +00:00
|
|
|
{
|
2024-06-24 19:04:31 +00:00
|
|
|
auto [o2, he, o2max ] = dive->get_maximal_gas();
|
2015-05-28 21:33:51 +00:00
|
|
|
return he * 1000 + o2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static QVariant dive_table_alignment(int column)
|
|
|
|
{
|
|
|
|
switch (column) {
|
2018-12-27 09:06:11 +00:00
|
|
|
case DiveTripModelBase::DEPTH:
|
|
|
|
case DiveTripModelBase::DURATION:
|
|
|
|
case DiveTripModelBase::TEMPERATURE:
|
|
|
|
case DiveTripModelBase::TOTALWEIGHT:
|
|
|
|
case DiveTripModelBase::SAC:
|
|
|
|
case DiveTripModelBase::OTU:
|
|
|
|
case DiveTripModelBase::MAXCNS:
|
2015-05-28 21:33:51 +00:00
|
|
|
// Right align numeric columns
|
2018-10-14 06:56:53 +00:00
|
|
|
return int(Qt::AlignRight | Qt::AlignVCenter);
|
|
|
|
// NR needs to be left aligned because its the indent marker for trips too
|
2018-12-27 09:06:11 +00:00
|
|
|
case DiveTripModelBase::NR:
|
|
|
|
case DiveTripModelBase::DATE:
|
|
|
|
case DiveTripModelBase::RATING:
|
|
|
|
case DiveTripModelBase::SUIT:
|
|
|
|
case DiveTripModelBase::CYLINDER:
|
|
|
|
case DiveTripModelBase::GAS:
|
|
|
|
case DiveTripModelBase::TAGS:
|
|
|
|
case DiveTripModelBase::PHOTOS:
|
|
|
|
case DiveTripModelBase::COUNTRY:
|
|
|
|
case DiveTripModelBase::BUDDIES:
|
2022-02-12 13:35:22 +00:00
|
|
|
case DiveTripModelBase::DIVEGUIDE:
|
2018-12-27 09:06:11 +00:00
|
|
|
case DiveTripModelBase::LOCATION:
|
2023-03-16 00:29:43 +00:00
|
|
|
case DiveTripModelBase::NOTES:
|
2023-09-22 21:35:17 +00:00
|
|
|
case DiveTripModelBase::DIVEMODE:
|
2018-10-14 06:56:53 +00:00
|
|
|
return int(Qt::AlignLeft | Qt::AlignVCenter);
|
2015-05-28 21:33:51 +00:00
|
|
|
}
|
2018-10-14 06:56:53 +00:00
|
|
|
return QVariant();
|
2015-05-28 21:33:51 +00:00
|
|
|
}
|
|
|
|
|
2019-11-12 05:31:56 +00:00
|
|
|
QString DiveTripModelBase::tripShortDate(const dive_trip *trip)
|
|
|
|
{
|
|
|
|
if (!trip)
|
|
|
|
return QString();
|
2024-06-08 13:28:16 +00:00
|
|
|
QDateTime firstTime = timestampToDateTime(trip->date());
|
2019-11-12 05:31:56 +00:00
|
|
|
QString firstMonth = firstTime.toString("MMM");
|
|
|
|
return QStringLiteral("%1\n'%2").arg(firstMonth,firstTime.toString("yy"));
|
|
|
|
}
|
|
|
|
|
|
|
|
QString DiveTripModelBase::tripTitle(const dive_trip *trip)
|
|
|
|
{
|
|
|
|
if (!trip)
|
|
|
|
return QString();
|
2024-06-02 15:06:18 +00:00
|
|
|
QString numDives = tr("(%n dive(s))", "", static_cast<int>(trip->dives.size()));
|
2024-06-08 13:28:16 +00:00
|
|
|
int shown = trip->shown_dives();
|
2024-06-02 15:06:18 +00:00
|
|
|
QString shownDives = shown != !trip->dives.empty() ? QStringLiteral(" ") + tr("(%L1 shown)").arg(shown) : QString();
|
2024-05-31 15:15:47 +00:00
|
|
|
QString title = QString::fromStdString(trip->location);
|
2019-11-12 05:31:56 +00:00
|
|
|
|
|
|
|
if (title.isEmpty()) {
|
|
|
|
// so use the date range
|
2024-06-08 13:28:16 +00:00
|
|
|
QDateTime firstTime = timestampToDateTime(trip->date());
|
2019-11-12 05:31:56 +00:00
|
|
|
QString firstMonth = firstTime.toString("MMM");
|
|
|
|
QString firstYear = firstTime.toString("yyyy");
|
2024-06-02 15:06:18 +00:00
|
|
|
QDateTime lastTime = timestampToDateTime(trip->dives[0]->when);
|
2019-11-12 05:31:56 +00:00
|
|
|
QString lastMonth = lastTime.toString("MMM");
|
|
|
|
QString lastYear = lastTime.toString("yyyy");
|
|
|
|
if (lastMonth == firstMonth && lastYear == firstYear)
|
|
|
|
title = firstMonth + " " + firstYear;
|
|
|
|
else if (lastMonth != firstMonth && lastYear == firstYear)
|
|
|
|
title = firstMonth + "-" + lastMonth + " " + firstYear;
|
|
|
|
else
|
|
|
|
title = firstMonth + " " + firstYear + " - " + lastMonth + " " + lastYear;
|
|
|
|
}
|
|
|
|
return QStringLiteral("%1 %2%3").arg(title, numDives, shownDives);
|
|
|
|
}
|
|
|
|
|
2018-12-27 09:06:11 +00:00
|
|
|
QVariant DiveTripModelBase::tripData(const dive_trip *trip, int column, int role)
|
2015-05-28 21:33:51 +00:00
|
|
|
{
|
2020-03-05 19:00:31 +00:00
|
|
|
#ifdef SUBSURFACE_MOBILE
|
|
|
|
// Special roles for mobile
|
|
|
|
switch(role) {
|
|
|
|
case MobileListModel::TripIdRole: return QString::number(trip->id);
|
2024-06-02 15:06:18 +00:00
|
|
|
case MobileListModel::TripNrDivesRole: return static_cast<int>(trip->dives.size());
|
2019-11-12 05:31:56 +00:00
|
|
|
case MobileListModel::TripShortDateRole: return tripShortDate(trip);
|
|
|
|
case MobileListModel::TripTitleRole: return tripTitle(trip);
|
2024-05-31 15:15:47 +00:00
|
|
|
case MobileListModel::TripLocationRole: return QString::fromStdString(trip->location);
|
|
|
|
case MobileListModel::TripNotesRole: return QString::fromStdString(trip->notes);
|
2020-03-05 19:00:31 +00:00
|
|
|
}
|
|
|
|
#endif
|
2019-12-12 22:40:16 +00:00
|
|
|
// Set the font for all trips alike
|
|
|
|
if (role == Qt::FontRole)
|
|
|
|
return defaultModelFont();
|
2015-05-28 21:33:51 +00:00
|
|
|
|
2018-10-14 06:56:53 +00:00
|
|
|
if (role == TRIP_ROLE)
|
2018-11-22 07:06:07 +00:00
|
|
|
return QVariant::fromValue(const_cast<dive_trip *>(trip)); // Not nice: casting away a const
|
2015-05-28 21:33:51 +00:00
|
|
|
|
|
|
|
if (role == Qt::DisplayRole) {
|
|
|
|
switch (column) {
|
2018-12-27 09:06:11 +00:00
|
|
|
case DiveTripModelBase::NR:
|
2015-05-28 21:33:51 +00:00
|
|
|
QString shownText;
|
2024-06-08 13:28:16 +00:00
|
|
|
int countShown = trip->shown_dives();
|
2024-06-02 15:06:18 +00:00
|
|
|
if (countShown < static_cast<int>(trip->dives.size()))
|
2015-10-08 09:02:42 +00:00
|
|
|
shownText = tr("(%1 shown)").arg(countShown);
|
2024-06-01 20:05:57 +00:00
|
|
|
return formatTripTitleWithDives(*trip) + " " + shownText;
|
2015-05-28 21:33:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-14 06:56:53 +00:00
|
|
|
return QVariant();
|
2015-05-28 21:33:51 +00:00
|
|
|
}
|
|
|
|
|
2018-07-22 10:21:58 +00:00
|
|
|
static const QString icon_names[4] = {
|
|
|
|
QStringLiteral(":zero"),
|
|
|
|
QStringLiteral(":photo-in-icon"),
|
|
|
|
QStringLiteral(":photo-out-icon"),
|
|
|
|
QStringLiteral(":photo-in-out-icon")
|
|
|
|
};
|
2015-05-28 21:33:51 +00:00
|
|
|
|
2018-10-14 06:56:53 +00:00
|
|
|
static int countPhotos(const struct dive *d)
|
|
|
|
{ // Determine whether dive has pictures, and whether they were taken during or before/after dive.
|
|
|
|
const int bufperiod = 120; // A 2-min buffer period. Photos within 2 min of dive are assumed as
|
2024-06-05 15:02:40 +00:00
|
|
|
int diveTotaltime = d->totaltime().seconds; // taken during the dive, not before/after.
|
2018-10-14 06:56:53 +00:00
|
|
|
int pic_offset, icon_index = 0;
|
2024-05-30 13:00:28 +00:00
|
|
|
for (auto &picture: d->pictures) { // Step through each of the pictures for this dive:
|
|
|
|
pic_offset = picture.offset.seconds;
|
2018-10-14 06:56:53 +00:00
|
|
|
if ((pic_offset < -bufperiod) | (pic_offset > diveTotaltime+bufperiod)) {
|
|
|
|
icon_index |= 0x02; // If picture is before/after the dive
|
2019-05-15 14:42:14 +00:00
|
|
|
// then set the appropriate bit ...
|
|
|
|
} else {
|
2018-10-14 06:56:53 +00:00
|
|
|
icon_index |= 0x01; // else set the bit for picture during the dive
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return icon_index; // return value: 0=no pictures; 1=pictures during dive;
|
|
|
|
} // 2=pictures before/after; 3=pictures during as well as before/after
|
|
|
|
|
|
|
|
static QString displayDuration(const struct dive *d)
|
2015-05-28 21:33:51 +00:00
|
|
|
{
|
2018-10-14 06:56:53 +00:00
|
|
|
if (prefs.units.show_units_table)
|
2024-05-27 15:09:48 +00:00
|
|
|
return get_dive_duration_string(d->duration.seconds, gettextFromC::tr("h"), gettextFromC::tr("min"), "", ":", d->dcs[0].divemode == FREEDIVE);
|
2018-10-14 06:56:53 +00:00
|
|
|
else
|
2024-05-27 15:09:48 +00:00
|
|
|
return get_dive_duration_string(d->duration.seconds, "", "", "", ":", d->dcs[0].divemode == FREEDIVE);
|
2018-10-14 06:56:53 +00:00
|
|
|
}
|
2015-05-28 21:33:51 +00:00
|
|
|
|
2018-10-14 06:56:53 +00:00
|
|
|
static QString displayTemperature(const struct dive *d, bool units)
|
|
|
|
{
|
|
|
|
if (!d->watertemp.mkelvin)
|
|
|
|
return QString();
|
|
|
|
return get_temperature_string(d->watertemp, units);
|
|
|
|
}
|
|
|
|
|
|
|
|
static QString displaySac(const struct dive *d, bool units)
|
|
|
|
{
|
|
|
|
if (!d->sac)
|
|
|
|
return QString();
|
|
|
|
QString s = get_volume_string(d->sac, units);
|
2024-01-15 20:22:20 +00:00
|
|
|
return units ? s + gettextFromC::tr("/min") : std::move(s);
|
2018-10-14 06:56:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static QString displayWeight(const struct dive *d, bool units)
|
|
|
|
{
|
2024-06-30 09:13:39 +00:00
|
|
|
QString s = get_weight_string(d->total_weight(), false);
|
2018-10-14 06:56:53 +00:00
|
|
|
if (!units)
|
|
|
|
return s;
|
|
|
|
else if (get_units()->weight == units::KG)
|
|
|
|
return s + gettextFromC::tr("kg");
|
|
|
|
else
|
|
|
|
return s + gettextFromC::tr("lbs");
|
|
|
|
}
|
|
|
|
|
2021-02-10 21:16:25 +00:00
|
|
|
static QPixmap &getGlobeIcon()
|
|
|
|
{
|
|
|
|
static std::unique_ptr<QPixmap> icon;
|
|
|
|
if (!icon) {
|
|
|
|
const IconMetrics &im = defaultIconMetrics();
|
|
|
|
icon = std::make_unique<QPixmap>(QIcon(":globe-icon").pixmap(im.sz_small, im.sz_small));
|
|
|
|
}
|
|
|
|
return *icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
static QPixmap &getPhotoIcon(int idx)
|
|
|
|
{
|
|
|
|
static std::unique_ptr<QPixmap[]> icons;
|
|
|
|
if (!icons) {
|
|
|
|
const IconMetrics &im = defaultIconMetrics();
|
|
|
|
icons = std::make_unique<QPixmap[]>(std::size(icon_names));
|
2022-10-30 20:24:13 +00:00
|
|
|
for (auto [i, name]: enumerated_range(icon_names))
|
|
|
|
icons[i] = QIcon(name).pixmap(im.sz_small, im.sz_small);
|
2021-02-10 21:16:25 +00:00
|
|
|
}
|
|
|
|
return icons[idx];
|
|
|
|
}
|
|
|
|
|
2022-02-12 13:43:04 +00:00
|
|
|
// textual description of the meaning of a column for use in tooltips
|
|
|
|
QString DiveTripModelBase::getDescription(int column)
|
|
|
|
{
|
|
|
|
switch (column) {
|
|
|
|
case NR:
|
|
|
|
return tr("#");
|
|
|
|
case DATE:
|
|
|
|
return tr("Date");
|
|
|
|
case RATING:
|
|
|
|
return tr("Rating");
|
|
|
|
case DEPTH:
|
|
|
|
return tr("Depth(%1)").arg((get_units()->length == units::METERS) ? tr("m") : tr("ft"));
|
|
|
|
case DURATION:
|
|
|
|
return tr("Duration");
|
|
|
|
case TEMPERATURE:
|
|
|
|
return tr("Temp.(°%1)").arg((get_units()->temperature == units::CELSIUS) ? "C" : "F");
|
|
|
|
case TOTALWEIGHT:
|
|
|
|
return tr("Weight(%1)").arg((get_units()->weight == units::KG) ? tr("kg") : tr("lbs"));
|
|
|
|
case SUIT:
|
|
|
|
return tr("Suit");
|
|
|
|
case CYLINDER:
|
|
|
|
return tr("Cylinder");
|
|
|
|
case GAS:
|
|
|
|
return tr("Gas");
|
|
|
|
case SAC:
|
|
|
|
const char *unit;
|
|
|
|
get_volume_units(0, NULL, &unit);
|
|
|
|
return tr("SAC(%1)").arg(QString(unit).append(tr("/min")));
|
|
|
|
case OTU:
|
|
|
|
return tr("OTU");
|
|
|
|
case MAXCNS:
|
|
|
|
return tr("Max. CNS");
|
|
|
|
case TAGS:
|
|
|
|
return tr("Tags");
|
|
|
|
case PHOTOS:
|
|
|
|
return tr("Media before/during/after dive");
|
|
|
|
case COUNTRY:
|
|
|
|
return tr("Country");
|
|
|
|
case BUDDIES:
|
|
|
|
return tr("Buddy");
|
|
|
|
case DIVEGUIDE:
|
|
|
|
return tr("Dive guide");
|
|
|
|
case LOCATION:
|
|
|
|
return tr("Location");
|
2023-03-16 00:29:43 +00:00
|
|
|
case NOTES:
|
|
|
|
return tr("Notes");
|
2023-09-22 21:35:17 +00:00
|
|
|
case DIVEMODE:
|
|
|
|
return tr("Divemode");
|
2022-02-12 13:43:04 +00:00
|
|
|
default:
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-24 08:01:24 +00:00
|
|
|
QVariant DiveTripModelBase::diveData(const struct dive *d, int column, int role) const
|
2018-10-14 06:56:53 +00:00
|
|
|
{
|
2020-03-05 19:00:31 +00:00
|
|
|
#ifdef SUBSURFACE_MOBILE
|
|
|
|
// Special roles for mobile
|
|
|
|
switch (role) {
|
|
|
|
case MobileListModel::DiveDateRole: return (qlonglong)d->when;
|
|
|
|
// We have to return a QString as trip-id, because that will be used as section
|
|
|
|
// variable in the QtQuick list view. That has to be a string because it will try
|
|
|
|
// to do locale-aware sorting. And amazingly this can't be changed.
|
2020-12-15 15:10:08 +00:00
|
|
|
case MobileListModel::DateTimeRole: return formatDiveDateTime(d);
|
2020-03-05 19:00:31 +00:00
|
|
|
case MobileListModel::IdRole: return d->id;
|
|
|
|
case MobileListModel::NumberRole: return d->number;
|
2024-06-30 18:55:34 +00:00
|
|
|
case MobileListModel::LocationRole: return QString::fromStdString(d->get_location());
|
2024-05-27 15:09:48 +00:00
|
|
|
case MobileListModel::DepthRole: return get_depth_string(d->dcs[0].maxdepth.mm, true, true);
|
2020-12-15 14:33:39 +00:00
|
|
|
case MobileListModel::DurationRole: return formatDiveDuration(d);
|
2024-05-27 15:09:48 +00:00
|
|
|
case MobileListModel::DepthDurationRole: return QStringLiteral("%1 / %2").arg(get_depth_string(d->dcs[0].maxdepth.mm, true, true),
|
2020-12-15 14:33:39 +00:00
|
|
|
formatDiveDuration(d));
|
2020-03-05 19:00:31 +00:00
|
|
|
case MobileListModel::RatingRole: return d->rating;
|
|
|
|
case MobileListModel::VizRole: return d->visibility;
|
2024-05-29 18:40:18 +00:00
|
|
|
case MobileListModel::SuitRole: return QString::fromStdString(d->suit);
|
2020-03-05 19:00:31 +00:00
|
|
|
case MobileListModel::AirTempRole: return get_temperature_string(d->airtemp, true);
|
|
|
|
case MobileListModel::WaterTempRole: return get_temperature_string(d->watertemp, true);
|
|
|
|
case MobileListModel::SacRole: return formatSac(d);
|
2020-12-15 14:33:39 +00:00
|
|
|
case MobileListModel::SumWeightRole: return formatSumWeight(d);
|
2024-05-29 18:40:18 +00:00
|
|
|
case MobileListModel::DiveGuideRole: return QString::fromStdString(d->diveguide);
|
|
|
|
case MobileListModel::BuddyRole: return QString::fromStdString(d->buddy);
|
2024-05-29 15:57:48 +00:00
|
|
|
case MobileListModel::TagsRole: return QString::fromStdString(taglist_get_tagstring(d->tags));
|
2020-03-05 19:00:31 +00:00
|
|
|
case MobileListModel::NotesRole: return formatNotes(d);
|
2020-12-15 14:33:39 +00:00
|
|
|
case MobileListModel::GpsRole: return formatDiveGPS(d);
|
2020-03-05 19:00:31 +00:00
|
|
|
case MobileListModel::GpsDecimalRole: return format_gps_decimal(d);
|
2024-05-27 15:09:48 +00:00
|
|
|
case MobileListModel::NoDiveRole: return d->duration.seconds == 0 && d->dcs[0].duration.seconds == 0;
|
2020-03-05 19:00:31 +00:00
|
|
|
case MobileListModel::DiveSiteRole: return QVariant::fromValue(d->dive_site);
|
|
|
|
case MobileListModel::CylinderRole: return formatGetCylinder(d).join(", ");
|
|
|
|
case MobileListModel::GetCylinderRole: return formatGetCylinder(d);
|
2020-12-14 21:42:07 +00:00
|
|
|
case MobileListModel::CylinderListRole: return formatFullCylinderList();
|
2024-05-29 05:03:03 +00:00
|
|
|
case MobileListModel::SingleWeightRole: return d->weightsystems.size() <= 1;
|
2020-12-14 21:42:07 +00:00
|
|
|
case MobileListModel::StartPressureRole: return formatStartPressure(d);
|
|
|
|
case MobileListModel::EndPressureRole: return formatEndPressure(d);
|
|
|
|
case MobileListModel::FirstGasRole: return formatFirstGas(d);
|
2020-03-05 19:00:31 +00:00
|
|
|
case MobileListModel::SelectedRole: return d->selected;
|
2020-02-19 22:05:09 +00:00
|
|
|
case MobileListModel::DiveInTripRole: return d->divetrip != NULL;
|
2020-03-20 22:31:46 +00:00
|
|
|
case MobileListModel::IsInvalidRole: return d->invalid;
|
2020-03-05 19:00:31 +00:00
|
|
|
}
|
|
|
|
#endif
|
2015-05-28 21:33:51 +00:00
|
|
|
switch (role) {
|
2019-12-12 22:40:16 +00:00
|
|
|
case Qt::FontRole:
|
|
|
|
return d->invalid ? invalidFont : defaultModelFont();
|
|
|
|
case Qt::ForegroundRole:
|
|
|
|
return d->invalid ? invalidForeground : QVariant();
|
2015-05-28 21:33:51 +00:00
|
|
|
case Qt::TextAlignmentRole:
|
2018-10-14 06:56:53 +00:00
|
|
|
return dive_table_alignment(column);
|
2015-05-28 21:33:51 +00:00
|
|
|
case Qt::DisplayRole:
|
|
|
|
switch (column) {
|
|
|
|
case NR:
|
2018-10-14 06:56:53 +00:00
|
|
|
return d->number;
|
2015-05-28 21:33:51 +00:00
|
|
|
case DATE:
|
2018-10-14 06:56:53 +00:00
|
|
|
return get_dive_date_string(d->when);
|
2015-05-28 21:33:51 +00:00
|
|
|
case DEPTH:
|
2018-10-14 06:56:53 +00:00
|
|
|
return get_depth_string(d->maxdepth, prefs.units.show_units_table);
|
2015-05-28 21:33:51 +00:00
|
|
|
case DURATION:
|
2018-10-14 06:56:53 +00:00
|
|
|
return displayDuration(d);
|
2015-05-28 21:33:51 +00:00
|
|
|
case TEMPERATURE:
|
2018-10-14 06:56:53 +00:00
|
|
|
return displayTemperature(d, prefs.units.show_units_table);
|
2015-05-28 21:33:51 +00:00
|
|
|
case TOTALWEIGHT:
|
2018-10-14 06:56:53 +00:00
|
|
|
return displayWeight(d, prefs.units.show_units_table);
|
2015-05-28 21:33:51 +00:00
|
|
|
case SUIT:
|
2024-05-29 18:40:18 +00:00
|
|
|
return QString::fromStdString(d->suit);
|
2015-05-28 21:33:51 +00:00
|
|
|
case CYLINDER:
|
2024-05-28 19:31:11 +00:00
|
|
|
return !d->cylinders.empty() ? QString::fromStdString(d->cylinders[0].type.description) : QString();
|
2015-05-28 21:33:51 +00:00
|
|
|
case SAC:
|
2018-10-14 06:56:53 +00:00
|
|
|
return displaySac(d, prefs.units.show_units_table);
|
2015-05-28 21:33:51 +00:00
|
|
|
case OTU:
|
2018-10-14 06:56:53 +00:00
|
|
|
return d->otu;
|
2015-05-28 21:33:51 +00:00
|
|
|
case MAXCNS:
|
2017-10-09 06:46:18 +00:00
|
|
|
if (prefs.units.show_units_table)
|
2018-10-14 06:56:53 +00:00
|
|
|
return QString("%1%").arg(d->maxcns);
|
2017-10-09 06:46:18 +00:00
|
|
|
else
|
2018-10-14 06:56:53 +00:00
|
|
|
return d->maxcns;
|
2018-04-04 06:18:29 +00:00
|
|
|
case TAGS:
|
2024-05-29 15:57:48 +00:00
|
|
|
return QString::fromStdString(taglist_get_tagstring(d->tags));
|
2016-11-19 12:23:54 +00:00
|
|
|
case PHOTOS:
|
|
|
|
break;
|
2017-10-02 15:52:07 +00:00
|
|
|
case COUNTRY:
|
2024-06-30 18:55:34 +00:00
|
|
|
return QString::fromStdString(d->get_country());
|
2018-09-04 15:10:11 +00:00
|
|
|
case BUDDIES:
|
2024-05-29 18:40:18 +00:00
|
|
|
return QString::fromStdString(d->buddy);
|
2022-02-12 13:35:22 +00:00
|
|
|
case DIVEGUIDE:
|
2024-05-29 18:40:18 +00:00
|
|
|
return QString::fromStdString(d->diveguide);
|
2015-05-28 21:33:51 +00:00
|
|
|
case LOCATION:
|
2024-06-30 18:55:34 +00:00
|
|
|
return QString::fromStdString(d->get_location());
|
2015-05-28 21:33:51 +00:00
|
|
|
case GAS:
|
2023-03-17 07:21:47 +00:00
|
|
|
return formatDiveGasString(d);
|
2023-03-16 00:29:43 +00:00
|
|
|
case NOTES:
|
2024-05-29 18:40:18 +00:00
|
|
|
return QString::fromStdString(d->notes);
|
2023-09-22 21:35:17 +00:00
|
|
|
case DIVEMODE:
|
2024-05-27 15:09:48 +00:00
|
|
|
return QString(divemode_text_ui[(int)d->dcs[0].divemode]);
|
2015-05-28 21:33:51 +00:00
|
|
|
}
|
|
|
|
break;
|
2015-06-29 17:39:12 +00:00
|
|
|
case Qt::DecorationRole:
|
2016-11-19 12:23:54 +00:00
|
|
|
switch (column) {
|
2017-10-02 15:52:07 +00:00
|
|
|
//TODO: ADD A FLAG
|
|
|
|
case COUNTRY:
|
2018-10-14 06:56:53 +00:00
|
|
|
return QVariant();
|
2016-11-19 12:23:54 +00:00
|
|
|
case LOCATION:
|
2024-06-30 14:33:52 +00:00
|
|
|
if (d->dive_has_gps_location())
|
2021-02-10 21:16:25 +00:00
|
|
|
return getGlobeIcon();
|
2016-11-19 12:23:54 +00:00
|
|
|
break;
|
2016-11-21 09:26:58 +00:00
|
|
|
case PHOTOS:
|
2021-02-10 21:16:25 +00:00
|
|
|
// If there are photos, show one of the three photo icons: fish= photos during dive;
|
|
|
|
// sun=photos before/after dive; sun+fish=photos during dive as well as before/after
|
2024-05-30 13:00:28 +00:00
|
|
|
if (!d->pictures.empty())
|
2021-02-10 21:16:25 +00:00
|
|
|
return getPhotoIcon(countPhotos(d));
|
|
|
|
break;
|
2016-11-19 12:23:54 +00:00
|
|
|
}
|
2015-06-29 17:39:12 +00:00
|
|
|
break;
|
2015-05-28 21:33:51 +00:00
|
|
|
case Qt::ToolTipRole:
|
2022-02-12 13:43:04 +00:00
|
|
|
return getDescription(column);
|
2018-10-14 06:56:53 +00:00
|
|
|
case STAR_ROLE:
|
|
|
|
return d->rating;
|
|
|
|
case DIVE_ROLE:
|
2018-11-22 07:06:07 +00:00
|
|
|
return QVariant::fromValue(const_cast<dive *>(d)); // Not nice: casting away a const
|
2020-01-12 00:40:20 +00:00
|
|
|
case CURRENT_ROLE:
|
|
|
|
return d == current_dive;
|
2016-11-21 09:26:58 +00:00
|
|
|
}
|
2018-10-14 06:56:53 +00:00
|
|
|
return QVariant();
|
2015-05-28 21:33:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 09:06:11 +00:00
|
|
|
QVariant DiveTripModelBase::headerData(int section, Qt::Orientation orientation, int role) const
|
2015-05-28 21:33:51 +00:00
|
|
|
{
|
|
|
|
if (orientation == Qt::Vertical)
|
2018-10-14 06:56:53 +00:00
|
|
|
return QVariant();
|
2015-05-28 21:33:51 +00:00
|
|
|
|
|
|
|
switch (role) {
|
|
|
|
case Qt::TextAlignmentRole:
|
2018-10-14 06:56:53 +00:00
|
|
|
return dive_table_alignment(section);
|
2015-05-28 21:33:51 +00:00
|
|
|
case Qt::FontRole:
|
2018-10-14 06:56:53 +00:00
|
|
|
return defaultModelFont();
|
2018-11-04 19:24:16 +00:00
|
|
|
case Qt::InitialSortOrderRole:
|
|
|
|
// By default, sort NR and DATE descending, everything else ascending.
|
|
|
|
return section == NR || section == DATE ? Qt::DescendingOrder : Qt::AscendingOrder;
|
2015-05-28 21:33:51 +00:00
|
|
|
case Qt::DisplayRole:
|
|
|
|
switch (section) {
|
|
|
|
case NR:
|
2018-10-14 06:56:53 +00:00
|
|
|
return tr("#");
|
2015-05-28 21:33:51 +00:00
|
|
|
case DATE:
|
2018-10-14 06:56:53 +00:00
|
|
|
return tr("Date");
|
2015-05-28 21:33:51 +00:00
|
|
|
case RATING:
|
2018-10-14 06:56:53 +00:00
|
|
|
return tr("Rating");
|
2015-05-28 21:33:51 +00:00
|
|
|
case DEPTH:
|
2018-10-14 06:56:53 +00:00
|
|
|
return tr("Depth");
|
2015-05-28 21:33:51 +00:00
|
|
|
case DURATION:
|
2018-10-14 06:56:53 +00:00
|
|
|
return tr("Duration");
|
2015-05-28 21:33:51 +00:00
|
|
|
case TEMPERATURE:
|
2018-10-14 06:56:53 +00:00
|
|
|
return tr("Temp.");
|
2015-05-28 21:33:51 +00:00
|
|
|
case TOTALWEIGHT:
|
2018-10-14 06:56:53 +00:00
|
|
|
return tr("Weight");
|
2015-05-28 21:33:51 +00:00
|
|
|
case SUIT:
|
2018-10-14 06:56:53 +00:00
|
|
|
return tr("Suit");
|
2015-05-28 21:33:51 +00:00
|
|
|
case CYLINDER:
|
2018-10-14 06:56:53 +00:00
|
|
|
return tr("Cylinder");
|
2015-05-28 21:33:51 +00:00
|
|
|
case GAS:
|
2018-10-14 06:56:53 +00:00
|
|
|
return tr("Gas");
|
2015-05-28 21:33:51 +00:00
|
|
|
case SAC:
|
2018-10-14 06:56:53 +00:00
|
|
|
return tr("SAC");
|
2015-05-28 21:33:51 +00:00
|
|
|
case OTU:
|
2018-10-14 06:56:53 +00:00
|
|
|
return tr("OTU");
|
2015-05-28 21:33:51 +00:00
|
|
|
case MAXCNS:
|
2018-10-14 06:56:53 +00:00
|
|
|
return tr("Max CNS");
|
2018-04-04 06:18:29 +00:00
|
|
|
case TAGS:
|
2018-10-14 06:56:53 +00:00
|
|
|
return tr("Tags");
|
2016-11-19 12:23:54 +00:00
|
|
|
case PHOTOS:
|
2018-10-14 06:56:53 +00:00
|
|
|
return tr("Media");
|
2017-10-02 15:52:07 +00:00
|
|
|
case COUNTRY:
|
2018-10-14 06:56:53 +00:00
|
|
|
return tr("Country");
|
2018-09-04 15:10:11 +00:00
|
|
|
case BUDDIES:
|
2018-10-14 06:56:53 +00:00
|
|
|
return tr("Buddy");
|
2022-02-12 13:35:22 +00:00
|
|
|
case DIVEGUIDE:
|
|
|
|
return tr("Dive guide");
|
2015-05-28 21:33:51 +00:00
|
|
|
case LOCATION:
|
2018-10-14 06:56:53 +00:00
|
|
|
return tr("Location");
|
2023-03-16 00:29:43 +00:00
|
|
|
case NOTES:
|
|
|
|
return tr("Notes");
|
2023-09-22 21:35:17 +00:00
|
|
|
case DIVEMODE:
|
|
|
|
return tr("Divemode");
|
2015-05-28 21:33:51 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Qt::ToolTipRole:
|
2022-02-12 13:43:04 +00:00
|
|
|
return getDescription(section);
|
2015-05-28 21:33:51 +00:00
|
|
|
}
|
|
|
|
|
2018-10-14 06:56:53 +00:00
|
|
|
return QVariant();
|
2015-05-28 21:33:51 +00:00
|
|
|
}
|
|
|
|
|
2019-11-25 23:08:35 +00:00
|
|
|
// After resetting the model, the higher up model or view may call this
|
|
|
|
// function to get informed on the current selection.
|
|
|
|
// TODO: Currently, this reads and resets the selection. Make this more
|
|
|
|
// efficient by maintaining a list of selected dives.
|
|
|
|
void DiveTripModelBase::initSelection()
|
|
|
|
{
|
|
|
|
std::vector<dive *> dives = getDiveSelection();
|
|
|
|
if (!dives.empty())
|
2022-04-04 16:57:28 +00:00
|
|
|
setSelection(dives, current_dive, -1);
|
2019-11-25 23:08:35 +00:00
|
|
|
else
|
|
|
|
select_newest_visible_dive();
|
|
|
|
}
|
|
|
|
|
2020-04-13 21:04:30 +00:00
|
|
|
// Currently only used by the mobile models
|
2019-12-11 08:50:38 +00:00
|
|
|
void DiveTripModelBase::reset()
|
|
|
|
{
|
|
|
|
beginResetModel();
|
cleanup: invert control-flow when resetting the core structures
To reset the core data structures, the mobile and desktop UIs
were calling into the dive-list models, which then reset the
core data structures, themselves and the unrelated
locationinformation model. The UI code then reset various other
things, such as the TankInformation model or the map. . This was
unsatisfying from a control-flow perspective, as the models should
display the core data, not act on it. Moreover, this meant lots
of intricate intermodule-dependencies.
Thus, straighten up the control flow: give the C core the
possibility to send a "all data reset" event. And do that
in those functions that reset the core data structures.
Let each module react to this event by itself. This removes
inter-module dependencies. For example, the MainWindow now
doesn't have to reset the TankInfoModel or the MapWidget.
Then, to reset the core data structures, let the UI code
simply directly call the respective core functions.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-05-04 22:12:36 +00:00
|
|
|
oldCurrent = nullptr;
|
2019-12-11 08:50:38 +00:00
|
|
|
clearData();
|
|
|
|
populate();
|
2020-04-01 14:37:35 +00:00
|
|
|
uiNotification(tr("finish populating data store"));
|
2019-12-11 08:50:38 +00:00
|
|
|
endResetModel();
|
2020-04-01 14:37:35 +00:00
|
|
|
uiNotification(tr("setting up internal data structures"));
|
2020-03-07 18:12:03 +00:00
|
|
|
emit diveListNotifier.numShownChanged();
|
2020-04-01 14:37:35 +00:00
|
|
|
uiNotification(tr("done setting up internal data structures"));
|
2019-12-11 08:50:38 +00:00
|
|
|
}
|
|
|
|
|
2020-03-24 08:01:24 +00:00
|
|
|
DiveTripModelBase::DiveTripModelBase(QObject *parent) : QAbstractItemModel(parent),
|
|
|
|
invalidForeground(Qt::gray)
|
2018-09-30 14:06:17 +00:00
|
|
|
{
|
2020-03-24 08:01:24 +00:00
|
|
|
invalidFont.setStrikeOut(true);
|
2018-09-30 14:06:17 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 09:06:11 +00:00
|
|
|
int DiveTripModelBase::columnCount(const QModelIndex&) const
|
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-12-27 09:06:11 +00:00
|
|
|
return COLUMNS;
|
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-12-27 09:06:11 +00:00
|
|
|
Qt::ItemFlags DiveTripModelBase::flags(const QModelIndex &index) const
|
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-12-27 09:06:11 +00:00
|
|
|
dive *d = diveOrNull(index);
|
|
|
|
Qt::ItemFlags base = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
|
|
|
|
|
|
|
|
// Only dives have editable fields and only the number is editable
|
|
|
|
return d && index.column() == NR ? base | Qt::ItemIsEditable : base;
|
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
|
|
|
}
|
|
|
|
|
2020-02-09 21:47:47 +00:00
|
|
|
// Update visibility status of dive and return dives whose visibility changed.
|
|
|
|
// Attention: the changed dives are removed from the original vector!
|
|
|
|
static ShownChange updateShown(QVector<dive *> &dives)
|
|
|
|
{
|
|
|
|
DiveFilter *filter = DiveFilter::instance();
|
2020-02-13 22:39:44 +00:00
|
|
|
ShownChange res = filter->update(dives);
|
2020-02-09 21:47:47 +00:00
|
|
|
if (!res.newShown.empty() || !res.newHidden.empty())
|
|
|
|
emit diveListNotifier.numShownChanged();
|
|
|
|
for (dive *d: res.newHidden)
|
|
|
|
dives.removeAll(d);
|
|
|
|
for (dive *d: res.newShown)
|
|
|
|
dives.removeAll(d);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update shown status of *all* dives, i.e. reset the filter
|
|
|
|
static ShownChange updateShownAll()
|
|
|
|
{
|
|
|
|
DiveFilter *filter = DiveFilter::instance();
|
2020-02-13 22:39:44 +00:00
|
|
|
ShownChange res = filter->updateAll();
|
2020-02-09 21:47:47 +00:00
|
|
|
if (!res.newShown.empty() || !res.newHidden.empty())
|
|
|
|
emit diveListNotifier.numShownChanged();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2022-08-21 09:22:34 +00:00
|
|
|
void DiveTripModelBase::currentChanged(dive *currentDive)
|
2020-03-06 15:57:58 +00:00
|
|
|
{
|
|
|
|
// On Desktop we use a signal to forward current-dive changed, on mobile we use ROLE_CURRENT.
|
|
|
|
// TODO: Unify - use the role for both.
|
|
|
|
#if defined(SUBSURFACE_MOBILE)
|
|
|
|
static QVector<int> roles = { CURRENT_ROLE };
|
|
|
|
if (oldCurrent) {
|
|
|
|
QModelIndex oldIdx = diveToIdx(oldCurrent);
|
|
|
|
dataChanged(oldIdx, oldIdx, roles);
|
|
|
|
}
|
2022-08-21 09:22:34 +00:00
|
|
|
if (currentDive && oldCurrent != currentDive) {
|
|
|
|
QModelIndex newIdx = diveToIdx(currentDive);
|
2020-03-06 15:57:58 +00:00
|
|
|
dataChanged(newIdx, newIdx, roles);
|
|
|
|
}
|
|
|
|
#endif
|
2022-08-21 09:22:34 +00:00
|
|
|
oldCurrent = currentDive;
|
2020-03-06 15:57:58 +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
|
|
|
// Find a range of matching elements in a vector.
|
|
|
|
// Input parameters:
|
|
|
|
// v: vector to be searched
|
|
|
|
// first: first element to search
|
|
|
|
// cond: a function that is fed elements and returns an integer:
|
|
|
|
// - >0: matches
|
|
|
|
// - 0: doesn't match
|
|
|
|
// - <0: stop searching, no more elements will be found
|
|
|
|
// cond is called exactly once per element and from the beginning of the range.
|
|
|
|
// Returns a pair [first, last) with usual C++ semantics: last-first is the size of the found range.
|
|
|
|
// If no items were found, first and last are set to the size of the vector.
|
|
|
|
template <typename Vector, typename Predicate>
|
|
|
|
std::pair<int, int> findRangeIf(const Vector &v, int first, Predicate cond)
|
|
|
|
{
|
|
|
|
int size = (int)v.size();
|
|
|
|
for (int i = first; i < size; ++i) {
|
|
|
|
int res = cond(v[i]);
|
|
|
|
if (res > 0) {
|
|
|
|
for (int j = i + 1; j < size; ++j) {
|
|
|
|
if (cond(v[j]) <= 0)
|
|
|
|
return { i, j };
|
|
|
|
}
|
|
|
|
return { i, size };
|
|
|
|
} else if (res < 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return { size, size };
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ideally, Qt's model/view functions are processed in batches of contiguous
|
|
|
|
// items. Therefore, this template is used to process actions on ranges of
|
|
|
|
// contiguous elements of a vector.
|
|
|
|
// Input paremeters:
|
|
|
|
// - items: vector to process, wich must allow random access via [] and the size() function
|
|
|
|
// - cond: a predicate that is tested for each element. contiguous ranges of elements which
|
|
|
|
// test for true are collected. cond is fed an element and should return:
|
|
|
|
// - >0: matches
|
|
|
|
// - 0: doesn't match
|
|
|
|
// - <0: stop searching, no more elements will be found
|
|
|
|
// - action: action that is called with the vector, first and last element of the range.
|
|
|
|
template<typename Vector, typename Predicate, typename Action>
|
|
|
|
void processRanges(Vector &items, Predicate cond, Action action)
|
|
|
|
{
|
|
|
|
// Note: the "i++" is correct: We know that the last element tested
|
|
|
|
// negatively -> we can skip it. Thus we avoid checking any element
|
|
|
|
// twice.
|
|
|
|
for(int i = 0;; i++) {
|
|
|
|
std::pair<int,int> range = findRangeIf(items, i, cond);
|
|
|
|
if (range.first >= (int)items.size())
|
|
|
|
break;
|
|
|
|
int delta = action(items, range.first, range.second);
|
|
|
|
i = range.second + delta;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// processRangesZip() is a refined version of processRanges(), which operates on two vectors.
|
|
|
|
// The vectors are supposed to be sorted equivalently. That is, the first matching
|
|
|
|
// item will of the first vector will match to the first item of the second vector.
|
|
|
|
// It is supposed that all elements of the second vector will match to an element of
|
|
|
|
// the first vector.
|
|
|
|
// Input parameters:
|
|
|
|
// - items1: vector to process, wich must allow random access via [] and the size() function
|
|
|
|
// - items2: second vector to process. every item in items2 must match to an item in items1
|
|
|
|
// in ascending order.
|
|
|
|
// - cond1: a predicate that is tested for each element of items1 with the next unmatched element
|
|
|
|
// of items2. returns a boolean
|
|
|
|
// - action: action that is called with the vectors, first and last element of the first range
|
|
|
|
// and first element of the last range.
|
|
|
|
template<typename Vector1, typename Vector2, typename Predicate, typename Action>
|
|
|
|
void processRangesZip(Vector1 &items1, Vector2 &items2, Predicate cond, Action action)
|
|
|
|
{
|
|
|
|
int actItem = 0;
|
|
|
|
processRanges(items1,
|
|
|
|
[&](typename Vector1::const_reference &e) mutable -> int { // Condition. Marked mutable so that it can change actItem
|
|
|
|
if (actItem >= items2.size())
|
|
|
|
return -1; // No more items -> bail
|
|
|
|
if (!cond(e, items2[actItem]))
|
|
|
|
return 0;
|
|
|
|
++actItem;
|
|
|
|
return 1;
|
|
|
|
},
|
|
|
|
[&](Vector1 &v1, int from, int to) { // Action
|
2020-01-07 03:00:20 +00:00
|
|
|
return action(v1, items2, from, to, actItem);
|
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-12-27 09:06:11 +00:00
|
|
|
// Add items from vector "v2" to vector "v1" in batches of contiguous objects.
|
|
|
|
// The items are inserted at places according to a sort order determined by "comp".
|
|
|
|
// "v1" and "v2" are supposed to be ordered accordingly.
|
|
|
|
// TODO: We might use binary search with std::lower_bound(), but not sure if it's worth it.
|
|
|
|
// Input parameters:
|
|
|
|
// - v1: destination vector
|
|
|
|
// - v2: source vector
|
|
|
|
// - comp: compare-function, which is fed elements from v2 and v1. returns true for "insert here".
|
|
|
|
// - adder: performs the insertion. Perameters: v1, v2, insertion index, from, to range in v2.
|
|
|
|
template <typename Vector1, typename Vector2, typename Comparator, typename Inserter>
|
|
|
|
void addInBatches(Vector1 &v1, const Vector2 &v2, Comparator comp, Inserter insert)
|
2015-05-28 21:33:51 +00:00
|
|
|
{
|
2018-12-27 09:06:11 +00:00
|
|
|
int idx = 0; // Index where dives will be inserted
|
|
|
|
int i, j; // Begin and end of range to insert
|
|
|
|
for (i = 0; i < (int)v2.size(); i = j) {
|
|
|
|
for (; idx < (int)v1.size() && !comp(v2[i], v1[idx]); ++idx)
|
|
|
|
; // Pass
|
|
|
|
|
|
|
|
// We found the index of the first item to add.
|
|
|
|
// Now search how many items we should insert there.
|
|
|
|
if (idx == (int)v1.size()) {
|
|
|
|
// We were at end -> insert the remaining items
|
|
|
|
j = v2.size();
|
|
|
|
} else {
|
|
|
|
for (j = i + 1; j < (int)v2.size() && comp(v2[j], v1[idx]); ++j)
|
|
|
|
; // Pass
|
|
|
|
}
|
2015-05-28 21:33:51 +00:00
|
|
|
|
2018-12-27 09:06:11 +00:00
|
|
|
// Now add the batch
|
|
|
|
insert(v1, v2, idx, i, j);
|
|
|
|
|
|
|
|
// Skip over inserted dives for searching the new insertion position plus one.
|
|
|
|
// If we added at the end, the loop will end anyway.
|
|
|
|
idx += j - i + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 2) TreeModel functions
|
|
|
|
|
|
|
|
DiveTripModelTree::DiveTripModelTree(QObject *parent) : DiveTripModelBase(parent)
|
|
|
|
{
|
|
|
|
// Stay informed of changes to the divelist
|
|
|
|
connect(&diveListNotifier, &DiveListNotifier::divesAdded, this, &DiveTripModelTree::divesAdded);
|
|
|
|
connect(&diveListNotifier, &DiveListNotifier::divesDeleted, this, &DiveTripModelTree::divesDeleted);
|
|
|
|
connect(&diveListNotifier, &DiveListNotifier::divesChanged, this, &DiveTripModelTree::divesChanged);
|
2019-08-31 13:05:11 +00:00
|
|
|
connect(&diveListNotifier, &DiveListNotifier::diveSiteChanged, this, &DiveTripModelTree::diveSiteChanged);
|
2018-12-27 09:06:11 +00:00
|
|
|
connect(&diveListNotifier, &DiveListNotifier::divesMovedBetweenTrips, this, &DiveTripModelTree::divesMovedBetweenTrips);
|
|
|
|
connect(&diveListNotifier, &DiveListNotifier::divesTimeChanged, this, &DiveTripModelTree::divesTimeChanged);
|
2022-09-04 06:21:48 +00:00
|
|
|
connect(&diveListNotifier, &DiveListNotifier::divesSelected, this, &DiveTripModelTree::divesSelectedSlot);
|
2022-08-27 15:28:34 +00:00
|
|
|
connect(&diveListNotifier, &DiveListNotifier::tripSelected, this, &DiveTripModelTree::tripSelected);
|
2019-02-24 22:02:22 +00:00
|
|
|
connect(&diveListNotifier, &DiveListNotifier::tripChanged, this, &DiveTripModelTree::tripChanged);
|
2019-12-09 14:13:53 +00:00
|
|
|
connect(&diveListNotifier, &DiveListNotifier::filterReset, this, &DiveTripModelTree::filterReset);
|
2020-04-19 17:04:56 +00:00
|
|
|
connect(&diveListNotifier, &DiveListNotifier::cylinderAdded, this, &DiveTripModelTree::diveChanged);
|
|
|
|
connect(&diveListNotifier, &DiveListNotifier::cylinderEdited, this, &DiveTripModelTree::diveChanged);
|
|
|
|
connect(&diveListNotifier, &DiveListNotifier::cylinderRemoved, this, &DiveTripModelTree::diveChanged);
|
|
|
|
connect(&diveListNotifier, &DiveListNotifier::pictureOffsetChanged, this, &DiveTripModelTree::diveChanged);
|
|
|
|
connect(&diveListNotifier, &DiveListNotifier::picturesRemoved, this, &DiveTripModelTree::diveChanged);
|
|
|
|
connect(&diveListNotifier, &DiveListNotifier::picturesAdded, this, &DiveTripModelTree::diveChanged);
|
cleanup: invert control-flow when resetting the core structures
To reset the core data structures, the mobile and desktop UIs
were calling into the dive-list models, which then reset the
core data structures, themselves and the unrelated
locationinformation model. The UI code then reset various other
things, such as the TankInformation model or the map. . This was
unsatisfying from a control-flow perspective, as the models should
display the core data, not act on it. Moreover, this meant lots
of intricate intermodule-dependencies.
Thus, straighten up the control flow: give the C core the
possibility to send a "all data reset" event. And do that
in those functions that reset the core data structures.
Let each module react to this event by itself. This removes
inter-module dependencies. For example, the MainWindow now
doesn't have to reset the TankInfoModel or the MapWidget.
Then, to reset the core data structures, let the UI code
simply directly call the respective core functions.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-05-04 22:12:36 +00:00
|
|
|
connect(&diveListNotifier, &DiveListNotifier::dataReset, this, &DiveTripModelTree::reset);
|
2018-12-27 09:06:11 +00:00
|
|
|
|
2019-12-11 08:50:38 +00:00
|
|
|
populate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveTripModelTree::populate()
|
|
|
|
{
|
2020-10-30 20:28:11 +00:00
|
|
|
DiveFilter::instance()->reset(); // The data was reset - update filter status. TODO: should this really be done here?
|
2020-09-20 11:40:21 +00:00
|
|
|
|
2020-04-01 14:37:35 +00:00
|
|
|
// we want this to be two calls as the second text is overwritten below by the lines starting with "\r"
|
|
|
|
uiNotification(QObject::tr("populate data model"));
|
|
|
|
uiNotification(QObject::tr("start processing"));
|
2024-06-07 08:25:09 +00:00
|
|
|
for (auto &d: divelog.dives) {
|
2024-06-23 12:20:59 +00:00
|
|
|
divelog.dives.update_cylinder_related_info(*d);
|
2020-02-09 21:47:47 +00:00
|
|
|
if (d->hidden_by_filter)
|
|
|
|
continue;
|
2024-05-31 15:15:47 +00:00
|
|
|
dive_trip *trip = d->divetrip;
|
2015-05-28 21:33:51 +00:00
|
|
|
|
2018-12-27 09:06:11 +00:00
|
|
|
// If this dive doesn't have a trip, add as top-level item.
|
|
|
|
if (!trip) {
|
2024-06-07 08:25:09 +00:00
|
|
|
items.emplace_back(d.get());
|
2015-05-28 21:33:51 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-09-30 14:06:17 +00:00
|
|
|
// Check if that trip is already known to us: search for the first item
|
2018-11-10 08:07:42 +00:00
|
|
|
// that corresponds to that trip
|
2018-09-30 14:06:17 +00:00
|
|
|
auto it = std::find_if(items.begin(), items.end(), [trip](const Item &item)
|
2018-11-10 08:07:42 +00:00
|
|
|
{ return item.d_or_t.trip == trip; });
|
2018-09-30 14:06:17 +00:00
|
|
|
if (it == items.end()) {
|
|
|
|
// We didn't find an entry for this trip -> add one
|
2024-06-07 08:25:09 +00:00
|
|
|
items.emplace_back(trip, d.get());
|
2018-09-30 14:06:17 +00:00
|
|
|
} else {
|
|
|
|
// We found the trip -> simply add the dive
|
2024-06-07 08:25:09 +00:00
|
|
|
it->dives.push_back(d.get());
|
2015-05-28 21:33:51 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-06 15:57:58 +00:00
|
|
|
|
|
|
|
// Remember the index of the current dive
|
|
|
|
oldCurrent = current_dive;
|
2024-06-07 08:25:09 +00:00
|
|
|
uiNotification(QObject::tr("%1 dives processed").arg(divelog.dives.size()));
|
2018-12-27 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int DiveTripModelTree::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
// No parent means top level - return the number of top-level items
|
|
|
|
if (!parent.isValid())
|
|
|
|
return items.size();
|
|
|
|
|
|
|
|
// If the parent has a parent, this is a dive -> no entries
|
|
|
|
if (parent.parent().isValid())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// If this is outside of our top-level list -> no entries
|
|
|
|
int row = parent.row();
|
|
|
|
if (row < 0 || row >= (int)items.size())
|
|
|
|
return 0;
|
2015-05-28 21:33:51 +00:00
|
|
|
|
2018-12-27 09:06:11 +00:00
|
|
|
// Only trips have items
|
|
|
|
const Item &entry = items[parent.row()];
|
|
|
|
return entry.d_or_t.trip ? entry.dives.size() : 0;
|
|
|
|
}
|
|
|
|
|
2019-11-18 17:44:17 +00:00
|
|
|
void DiveTripModelList::clearData()
|
|
|
|
{
|
|
|
|
items.clear();
|
|
|
|
}
|
|
|
|
|
2018-12-27 09:06:11 +00:00
|
|
|
static const quintptr noParent = ~(quintptr)0; // This is the "internalId" marker for top-level item
|
|
|
|
|
|
|
|
QModelIndex DiveTripModelTree::index(int row, int column, const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
if (!hasIndex(row, column, parent))
|
|
|
|
return QModelIndex();
|
|
|
|
|
|
|
|
// In the "internalId", we store either ~0 for top-level items or the
|
|
|
|
// index of the parent item. A top-level item has an invalid parent.
|
|
|
|
return createIndex(row, column, parent.isValid() ? parent.row() : noParent);
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex DiveTripModelTree::parent(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
return QModelIndex();
|
|
|
|
|
|
|
|
// In the "internalId", we store either ~0 for top-level items
|
|
|
|
// or the index of the parent item.
|
|
|
|
quintptr id = index.internalId();
|
|
|
|
if (id == noParent)
|
|
|
|
return QModelIndex();
|
|
|
|
|
|
|
|
// Parent must be top-level item
|
|
|
|
return createIndex(id, 0, noParent);
|
|
|
|
}
|
|
|
|
|
|
|
|
DiveTripModelTree::Item::Item(dive_trip *t, const QVector<dive *> &divesIn) : d_or_t{nullptr, t},
|
2020-02-10 20:00:26 +00:00
|
|
|
dives(std::vector<dive *>(divesIn.begin(), divesIn.end()))
|
2018-12-27 09:06:11 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-10 20:00:26 +00:00
|
|
|
DiveTripModelTree::Item::Item(dive_trip *t, dive *d) : d_or_t{nullptr, t}, dives({ d })
|
2018-12-27 09:06:11 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-10 20:00:26 +00:00
|
|
|
DiveTripModelTree::Item::Item(dive *d) : d_or_t{d, nullptr}
|
2018-12-27 09:06:11 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DiveTripModelTree::Item::isDive(const dive *d) const
|
|
|
|
{
|
|
|
|
return d_or_t.dive == d;
|
2015-05-28 21:33:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 09:06:11 +00:00
|
|
|
dive *DiveTripModelTree::Item::getDive() const
|
2015-05-28 21:33:51 +00:00
|
|
|
{
|
2018-12-27 09:06:11 +00:00
|
|
|
return d_or_t.dive;
|
2015-05-28 21:33:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 09:06:11 +00:00
|
|
|
timestamp_t DiveTripModelTree::Item::when() const
|
|
|
|
{
|
2024-06-08 13:28:16 +00:00
|
|
|
return d_or_t.trip ? d_or_t.trip->date() : d_or_t.dive->when;
|
2018-12-27 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dive_or_trip DiveTripModelTree::tripOrDive(const QModelIndex &index) const
|
2018-09-30 14:06:17 +00:00
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
return { nullptr, nullptr };
|
|
|
|
|
|
|
|
QModelIndex parent = index.parent();
|
|
|
|
// An invalid parent means that we're at the top-level
|
2018-11-10 08:07:42 +00:00
|
|
|
if (!parent.isValid())
|
|
|
|
return items[index.row()].d_or_t;
|
2018-09-30 14:06:17 +00:00
|
|
|
|
|
|
|
// Otherwise, we're at a leaf -> thats a dive
|
2018-11-10 08:07:42 +00:00
|
|
|
return { items[parent.row()].dives[index.row()], nullptr };
|
2018-09-30 14:06:17 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 09:06:11 +00:00
|
|
|
dive *DiveTripModelTree::diveOrNull(const QModelIndex &index) const
|
2018-09-30 14:06:17 +00:00
|
|
|
{
|
2018-11-10 08:07:42 +00:00
|
|
|
return tripOrDive(index).dive;
|
2018-09-30 14:06:17 +00:00
|
|
|
}
|
|
|
|
|
2020-02-09 21:47:47 +00:00
|
|
|
// The tree-version of the model wants to process the dives per trip.
|
|
|
|
// This template takes a vector of dives and calls a function batchwise for each trip.
|
|
|
|
template<typename Function>
|
|
|
|
void processByTrip(QVector<dive *> dives, Function action)
|
|
|
|
{
|
|
|
|
// Sort lexicographically by trip then according to the dive_less_than() function.
|
|
|
|
std::sort(dives.begin(), dives.end(), [](const dive *d1, const dive *d2)
|
2024-06-07 08:25:09 +00:00
|
|
|
{ return d1->divetrip == d2->divetrip ? dive_less_than_ptr(d1, d2) : d1->divetrip < d2->divetrip; });
|
2020-02-09 21:47:47 +00:00
|
|
|
|
|
|
|
// Then, process the dives in batches by trip
|
|
|
|
int i, j; // Begin and end of batch
|
|
|
|
for (i = 0; i < dives.size(); i = j) {
|
|
|
|
dive_trip *trip = dives[i]->divetrip;
|
|
|
|
for (j = i + 1; j < dives.size() && dives[j]->divetrip == trip; ++j)
|
|
|
|
; // pass
|
|
|
|
// Copy dives into a QVector. Some sort of "range_view" would be ideal.
|
|
|
|
QVector<dive *> divesInTrip(j - i);
|
|
|
|
for (int k = i; k < j; ++k)
|
|
|
|
divesInTrip[k - i] = dives[k];
|
|
|
|
|
|
|
|
// Finally, emit the signal
|
|
|
|
action(trip, divesInTrip);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This recalculates the filters and add / removes the newly shown / hidden dives
|
2019-12-09 14:13:53 +00:00
|
|
|
// Attention: Since this uses / modifies the hidden_by_filter flag of the
|
|
|
|
// core dive structure, only one DiveTripModel[Tree|List] must exist at
|
|
|
|
// a given time!
|
|
|
|
void DiveTripModelTree::filterReset()
|
2019-11-17 18:53:18 +00:00
|
|
|
{
|
2020-02-09 21:47:47 +00:00
|
|
|
ShownChange change = updateShownAll();
|
|
|
|
processByTrip(change.newHidden, [this] (dive_trip *trip, const QVector<dive *> &divesInTrip)
|
|
|
|
{ divesHidden(trip, divesInTrip); });
|
|
|
|
processByTrip(change.newShown, [this] (dive_trip *trip, const QVector<dive *> &divesInTrip)
|
|
|
|
{ divesShown(trip, divesInTrip); });
|
2019-12-10 08:42:05 +00:00
|
|
|
|
|
|
|
// If the current dive changed, instruct the UI of the changed selection
|
|
|
|
// TODO: This is way to heavy, as it reloads the whole selection!
|
2020-02-13 21:56:16 +00:00
|
|
|
if (change.currentChanged)
|
2019-12-10 08:42:05 +00:00
|
|
|
initSelection();
|
Dive list: cache shown flag in model (quick-fix for undo-crash)
We have a very fundamental problem with data-duplication in
core and qt-models. In a particular case, this led to an easily
reproducible crash:
1) An undo command moved the last dive of a trip to another.
2) When an undo-command removed the last dive of
a trip to a different trip, the dive was removed from the
trip in the core. Then, the model was updated.
3) That lead at first to a rearrangement of the trips, because
the trip with the added dive is moved before the trip with
the removed dive.
4) In such a case, the filter-model checks the visibility of
the trip.
5) Since the trip with the removed dive has no dives in the core,
visibility was determined as false.
6) From this point on the mappings of the QSortFilterProxyModel
were messed up. Accesses led to crashes. It is unclear
whether this is a Qt bug or only a QOI issue.
As a quick-fix, cache the visibility flag of trips directly
in the Qt-models. Don't set the visibility directly in the
core, but go via the Qt-models. Thus, a more clear layering
is achieved.
In the long run, we can hopefully get rid of the data-duplication
in the models.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-06-22 18:48:59 +00:00
|
|
|
}
|
|
|
|
|
2020-02-09 21:47:47 +00:00
|
|
|
void DiveTripModelTree::divesShown(dive_trip *trip, const QVector<dive *> &dives)
|
|
|
|
{
|
|
|
|
if (dives.empty())
|
|
|
|
return;
|
|
|
|
if (trip) {
|
|
|
|
// Find the trip
|
|
|
|
int idx = findTripIdx(trip);
|
|
|
|
if (idx < 0) {
|
|
|
|
addTrip(trip, dives); // Trip had no visible dives.
|
|
|
|
} else {
|
|
|
|
addDivesToTrip(idx, dives);
|
|
|
|
// Update the shown-count of the trip.
|
|
|
|
dataChanged(createIndex(idx, 0, noParent), createIndex(idx, 0, noParent));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
addDivesTopLevel(dives);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveTripModelTree::divesHidden(dive_trip *trip, const QVector<dive *> &dives)
|
|
|
|
{
|
|
|
|
if (dives.empty())
|
|
|
|
return;
|
|
|
|
if (trip) {
|
|
|
|
// Find the trip
|
|
|
|
int idx = findTripIdx(trip);
|
|
|
|
if (idx < 0) {
|
|
|
|
qWarning("DiveTripModelTree::divesHidden(): unknown trip");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (dives.size() == (int)items[idx].dives.size()) {
|
|
|
|
removeTrip(idx); // If all dives are hidden, remove the whole trip!
|
|
|
|
} else {
|
|
|
|
removeDivesFromTrip(idx, dives);
|
2022-08-20 18:56:09 +00:00
|
|
|
// Note: if dives are shown and hidden from a trip, we send two signals. Shrug.
|
2020-02-09 21:47:47 +00:00
|
|
|
dataChanged(createIndex(idx, 0, noParent), createIndex(idx, 0, noParent));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
removeDivesTopLevel(dives);
|
|
|
|
}
|
|
|
|
}
|
2019-11-17 18:53:18 +00:00
|
|
|
|
2018-12-27 09:06:11 +00:00
|
|
|
QVariant DiveTripModelTree::data(const QModelIndex &index, int role) const
|
2018-09-30 14:06:17 +00:00
|
|
|
{
|
2018-11-10 08:07:42 +00:00
|
|
|
dive_or_trip entry = tripOrDive(index);
|
2020-03-06 15:33:34 +00:00
|
|
|
if (!entry.trip && !entry.dive)
|
|
|
|
return QVariant(); // That's an invalid index!
|
|
|
|
if (role == IS_TRIP_ROLE) {
|
2019-11-11 20:36:51 +00:00
|
|
|
return !!entry.trip;
|
2020-03-06 15:33:34 +00:00
|
|
|
} else if (role == TRIP_HAS_CURRENT_ROLE) {
|
|
|
|
if (!entry.trip)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const Item &item = items[index.row()];
|
|
|
|
return std::find(item.dives.begin(), item.dives.end(), current_dive) != item.dives.end();
|
|
|
|
}
|
2020-02-20 21:22:57 +00:00
|
|
|
if (entry.trip) {
|
2018-11-10 08:07:42 +00:00
|
|
|
return tripData(entry.trip, index.column(), role);
|
2020-02-20 21:22:57 +00:00
|
|
|
} else if (entry.dive) {
|
|
|
|
#if defined(SUBSURFACE_MOBILE)
|
|
|
|
if (role == MobileListModel::TripAbove)
|
|
|
|
return tripInDirection(entry.dive, +1);
|
|
|
|
if (role == MobileListModel::TripBelow)
|
|
|
|
return tripInDirection(entry.dive, -1);
|
|
|
|
#endif
|
2020-03-06 15:33:34 +00:00
|
|
|
return diveData(entry.dive, index.column(), role);
|
2020-02-20 21:22:57 +00:00
|
|
|
} else {
|
|
|
|
return QVariant();
|
|
|
|
}
|
2018-09-30 14:06:17 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 09:06:11 +00:00
|
|
|
// After a trip changed, the top level might need to be reordered.
|
|
|
|
// Move the item and send a "data-changed" signal.
|
|
|
|
void DiveTripModelTree::topLevelChanged(int idx)
|
2018-11-13 08:26:53 +00:00
|
|
|
{
|
|
|
|
if (idx < 0 || idx >= (int)items.size())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// First, try to move backwards
|
|
|
|
int newIdx = idx;
|
|
|
|
while (newIdx > 0 && dive_or_trip_less_than(items[idx].d_or_t, items[newIdx - 1].d_or_t))
|
|
|
|
--newIdx;
|
|
|
|
|
|
|
|
// If that didn't change, try to move forward
|
|
|
|
if (newIdx == idx) {
|
2018-11-26 21:08:29 +00:00
|
|
|
++newIdx;
|
|
|
|
while (newIdx < (int)items.size() && !dive_or_trip_less_than(items[idx].d_or_t, items[newIdx].d_or_t))
|
2018-11-13 08:26:53 +00:00
|
|
|
++newIdx;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If index changed, move items
|
2018-11-26 21:08:29 +00:00
|
|
|
if (newIdx != idx && newIdx != idx + 1) {
|
2018-11-13 08:26:53 +00:00
|
|
|
beginMoveRows(QModelIndex(), idx, idx, QModelIndex(), newIdx);
|
2022-09-24 12:06:56 +00:00
|
|
|
move_in_range(items, idx, idx + 1, newIdx);
|
2018-11-13 08:26:53 +00:00
|
|
|
endMoveRows();
|
|
|
|
}
|
|
|
|
|
2019-11-15 09:21:57 +00:00
|
|
|
// If we moved the object backwards in the array, we have to
|
|
|
|
// subtract one from the index to account for the removed object.
|
|
|
|
if (newIdx > idx)
|
|
|
|
--newIdx;
|
|
|
|
|
2018-11-13 08:26:53 +00:00
|
|
|
// Finally, inform UI of changed trip header
|
|
|
|
QModelIndex tripIdx = createIndex(newIdx, 0, noParent);
|
|
|
|
dataChanged(tripIdx, tripIdx);
|
|
|
|
}
|
|
|
|
|
2018-12-27 09:06:11 +00:00
|
|
|
void DiveTripModelTree::addDivesToTrip(int trip, const QVector<dive *> &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
|
|
|
{
|
2018-11-10 12:02:46 +00:00
|
|
|
// Construct the parent index, ie. the index of the trip.
|
|
|
|
QModelIndex parent = createIndex(trip, 0, noParent);
|
|
|
|
|
|
|
|
addInBatches(items[trip].dives, dives,
|
2024-06-07 08:25:09 +00:00
|
|
|
[](dive *d, dive *d2) { return dive_less_than_ptr(d, d2); }, // comp
|
2018-11-10 12:02:46 +00:00
|
|
|
[&](std::vector<dive *> &items, const QVector<dive *> &dives, int idx, int from, int to) { // inserter
|
|
|
|
beginInsertRows(parent, idx, idx + to - from - 1);
|
|
|
|
items.insert(items.begin() + idx, dives.begin() + from, dives.begin() + to);
|
|
|
|
endInsertRows();
|
|
|
|
});
|
2018-11-13 08:26:53 +00:00
|
|
|
|
|
|
|
// If necessary, move the trip
|
|
|
|
topLevelChanged(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
|
|
|
}
|
|
|
|
|
2018-12-27 09:06:11 +00:00
|
|
|
int DiveTripModelTree::findTripIdx(const dive_trip *trip) const
|
|
|
|
{
|
2022-10-31 18:35:15 +00:00
|
|
|
return index_of_if(items, [trip] (const Item &item)
|
|
|
|
{ return item.d_or_t.trip == trip; });
|
2018-12-27 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int DiveTripModelTree::findDiveIdx(const dive *d) const
|
|
|
|
{
|
2022-10-31 18:35:15 +00:00
|
|
|
return index_of_if(items, [d] (const Item &item)
|
|
|
|
{ return item.isDive(d); });
|
2018-12-27 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int DiveTripModelTree::findDiveInTrip(int tripIdx, const dive *d) const
|
|
|
|
{
|
2022-10-31 18:35:15 +00:00
|
|
|
return index_of(items[tripIdx].dives, d);
|
2018-12-27 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int DiveTripModelTree::findInsertionIndex(const dive_trip *trip) const
|
|
|
|
{
|
|
|
|
dive_or_trip d_or_t{ nullptr, (dive_trip *)trip };
|
2022-10-30 20:24:13 +00:00
|
|
|
for (auto [i, item]: enumerated_range(items)) {
|
|
|
|
if (dive_or_trip_less_than(d_or_t, item.d_or_t))
|
2018-12-27 09:06:11 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return items.size();
|
|
|
|
}
|
|
|
|
|
2018-08-26 12:42:38 +00:00
|
|
|
// This function is used to compare a dive to an arbitrary entry (dive or trip).
|
|
|
|
// For comparing two dives, use the core function dive_less_than_entry, which
|
|
|
|
// effectively sorts by timestamp.
|
|
|
|
// If comparing to a trip, the policy for equal-times is to place the dives
|
|
|
|
// before the trip in the case of equal timestamps.
|
2018-12-27 09:06:11 +00:00
|
|
|
bool DiveTripModelTree::dive_before_entry(const dive *d, const Item &entry)
|
2018-08-26 12:42:38 +00:00
|
|
|
{
|
2018-11-10 08:07:42 +00:00
|
|
|
dive_or_trip d_or_t { (dive *)d, nullptr };
|
|
|
|
return dive_or_trip_less_than(d_or_t, entry.d_or_t);
|
2018-08-26 12:42:38 +00:00
|
|
|
}
|
|
|
|
|
2020-02-09 21:47:47 +00:00
|
|
|
void DiveTripModelTree::addDivesTopLevel(const QVector<dive *> &dives)
|
|
|
|
{
|
|
|
|
addInBatches(items, dives,
|
|
|
|
&dive_before_entry, // comp
|
|
|
|
[&](std::vector<Item> &items, const QVector<dive *> &dives, int idx, int from, int to) { // inserter
|
|
|
|
beginInsertRows(QModelIndex(), idx, idx + to - from - 1);
|
|
|
|
items.insert(items.begin() + idx, dives.begin() + from, dives.begin() + to);
|
|
|
|
endInsertRows();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveTripModelTree::removeDivesTopLevel(const QVector<dive *> &dives)
|
|
|
|
{
|
|
|
|
processRangesZip(items, dives,
|
|
|
|
[](const Item &e, dive *d) { return e.getDive() == d; }, // Condition
|
|
|
|
[&](std::vector<Item> &items, const QVector<dive *> &, int from, int to, int) -> int { // Action
|
|
|
|
beginRemoveRows(QModelIndex(), from, to - 1);
|
|
|
|
items.erase(items.begin() + from, items.begin() + to);
|
|
|
|
endRemoveRows();
|
|
|
|
return from - to; // Delta: negate the number of items deleted
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveTripModelTree::addTrip(dive_trip *trip, const QVector<dive *> &dives)
|
|
|
|
{
|
|
|
|
int idx = findInsertionIndex(trip); // Find the place where to insert the trip
|
|
|
|
beginInsertRows(QModelIndex(), idx, idx);
|
|
|
|
items.insert(items.begin() + idx, { trip, dives });
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveTripModelTree::removeTrip(int idx)
|
|
|
|
{
|
|
|
|
beginRemoveRows(QModelIndex(), idx, idx);
|
|
|
|
items.erase(items.begin() + idx);
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveTripModelTree::removeDivesFromTrip(int idx, const QVector<dive *> &dives)
|
|
|
|
{
|
|
|
|
// Construct the parent index, ie. the index of the trip.
|
|
|
|
QModelIndex parent = createIndex(idx, 0, noParent);
|
|
|
|
|
|
|
|
// Delete a number of dives in a trip. We do this range-wise.
|
|
|
|
processRangesZip(items[idx].dives, dives,
|
|
|
|
[](dive *d1, dive *d2) { return d1 == d2; }, // Condition
|
|
|
|
[&](std::vector<dive *> &diveList, const QVector<dive *> &, int from, int to, int) -> int { // Action
|
|
|
|
beginRemoveRows(parent, from, to - 1);
|
|
|
|
diveList.erase(diveList.begin() + from, diveList.begin() + to);
|
|
|
|
endRemoveRows();
|
|
|
|
return from - to; // Delta: negate the number of items deleted
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter out hidden dives
|
|
|
|
static QVector<dive *> visibleDives(const QVector<dive *> &dives)
|
|
|
|
{
|
|
|
|
QVector<dive *> res;
|
|
|
|
res.reserve(dives.size());
|
|
|
|
// std::copy_if with a lambda might be more compact and elegant, but a simple loop is probably more readable.
|
|
|
|
for (dive *d: dives) {
|
|
|
|
if (!d->hidden_by_filter)
|
|
|
|
res.push_back(d);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-02-20 21:22:57 +00:00
|
|
|
#ifdef SUBSURFACE_MOBILE
|
|
|
|
int DiveTripModelTree::tripInDirection(const struct dive *d, int direction) const
|
|
|
|
{
|
2022-10-30 20:24:13 +00:00
|
|
|
for (auto [i, item]: enumerated_range(items)) {
|
|
|
|
if (item.d_or_t.dive == d || (item.d_or_t.trip && findDiveInTrip(i, d) != -1)) {
|
2020-02-20 21:22:57 +00:00
|
|
|
// now walk in the direction given to find a trip
|
|
|
|
int offset = direction;
|
|
|
|
while (i + offset >= 0 && i + offset < (int)items.size()) {
|
|
|
|
if (items[i + offset].d_or_t.trip && (!d->divetrip || items[i + offset].d_or_t.trip->id != d->divetrip->id))
|
|
|
|
// we found a trip (and if the dive is already in a trip, we make sure this is a different trip)
|
|
|
|
return items[i + offset].d_or_t.trip->id;
|
|
|
|
offset += direction;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-02-09 21:47:47 +00:00
|
|
|
void DiveTripModelTree::divesAdded(dive_trip *trip, bool newTrip, const QVector<dive *> &divesIn)
|
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
|
|
|
{
|
2020-02-09 21:47:47 +00:00
|
|
|
QVector <dive *> dives = visibleDives(divesIn);
|
|
|
|
if (dives.empty())
|
|
|
|
return;
|
|
|
|
|
2018-12-27 09:06:11 +00:00
|
|
|
if (!trip) {
|
|
|
|
// This is outside of a trip. Add dives at the top-level in batches.
|
2020-02-09 21:47:47 +00:00
|
|
|
addDivesTopLevel(dives);
|
|
|
|
} else if (newTrip) {
|
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're supposed to add the whole trip. Just insert the trip.
|
2020-02-09 21:47:47 +00:00
|
|
|
addTrip(trip, 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
|
|
|
} else {
|
|
|
|
// Ok, we have to add dives to an existing trip
|
|
|
|
// Find the trip...
|
|
|
|
int idx = findTripIdx(trip);
|
|
|
|
if (idx < 0) {
|
2020-02-09 21:47:47 +00:00
|
|
|
// We don't know the trip - this may happen if all the old
|
|
|
|
// dives are hidden by the filter.
|
|
|
|
addTrip(trip, dives);
|
|
|
|
} else {
|
|
|
|
// ...and add dives.
|
|
|
|
addDivesToTrip(idx, 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-09 21:47:47 +00:00
|
|
|
void DiveTripModelTree::divesDeleted(dive_trip *trip, bool deleteTrip, const QVector<dive *> &divesIn)
|
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
|
|
|
{
|
2020-02-09 21:47:47 +00:00
|
|
|
QVector <dive *> dives = visibleDives(divesIn);
|
|
|
|
if (dives.empty())
|
|
|
|
return;
|
|
|
|
|
2020-03-06 15:57:58 +00:00
|
|
|
if (oldCurrent && std::find(dives.begin(), dives.end(), oldCurrent) != dives.end())
|
|
|
|
oldCurrent = nullptr;
|
|
|
|
divesDeletedInternal(trip, deleteTrip, dives); // Tail call
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveTripModelTree::divesDeletedInternal(dive_trip *trip, bool deleteTrip, const QVector<dive *> &dives)
|
|
|
|
{
|
2018-12-27 09:06:11 +00:00
|
|
|
if (!trip) {
|
|
|
|
// This is outside of a trip. Delete top-level dives in batches.
|
2020-02-09 21:47:47 +00:00
|
|
|
removeDivesTopLevel(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
|
|
|
} else {
|
|
|
|
// Find the trip
|
|
|
|
int idx = findTripIdx(trip);
|
|
|
|
if (idx < 0) {
|
|
|
|
// We don't know the trip - this shouldn't happen. We seem to have
|
|
|
|
// missed some signals!
|
2018-12-27 09:06:11 +00:00
|
|
|
qWarning() << "DiveTripModelTree::divesDeleted(): unknown 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
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-09 21:47:47 +00:00
|
|
|
if (deleteTrip || dives.size() >= (int)items[idx].dives.size()) {
|
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're supposed to delete the whole trip. Nice, we don't have to
|
|
|
|
// care about individual dives. Just remove the row.
|
2020-02-09 21:47:47 +00:00
|
|
|
removeTrip(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
|
|
|
} else {
|
2020-02-09 21:47:47 +00:00
|
|
|
removeDivesFromTrip(idx, dives);
|
|
|
|
topLevelChanged(idx); // If necessary, move the 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-31 13:05:11 +00:00
|
|
|
// Helper function: collect the dives that are at the given dive site
|
|
|
|
static QVector<dive *> getDivesForSite(struct dive_site *ds)
|
|
|
|
{
|
|
|
|
QVector<dive *> diveSiteDives;
|
2024-05-04 12:41:04 +00:00
|
|
|
diveSiteDives.reserve(ds->dives.size());
|
2019-08-31 13:05:11 +00:00
|
|
|
|
2024-05-04 12:41:04 +00:00
|
|
|
for (dive *d: ds->dives)
|
|
|
|
diveSiteDives.push_back(d);
|
2019-08-31 13:05:11 +00:00
|
|
|
|
|
|
|
return diveSiteDives;
|
|
|
|
}
|
|
|
|
|
|
|
|
// On the change of which dive site field should we update the
|
|
|
|
// dive in the list?
|
|
|
|
static bool isInterestingDiveSiteField(int field)
|
|
|
|
{
|
|
|
|
return field == LocationInformationModel::NAME // dive site name can is shown in the dive list
|
|
|
|
|| field == LocationInformationModel::TAXONOMY // country is shown in the dive list
|
|
|
|
|| field == LocationInformationModel::LOCATION; // the globe icon in the dive list shows whether we have coordinates
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveTripModelTree::diveSiteChanged(dive_site *ds, int field)
|
|
|
|
{
|
|
|
|
if (!isInterestingDiveSiteField(field))
|
|
|
|
return;
|
|
|
|
divesChanged(getDivesForSite(ds));
|
|
|
|
}
|
|
|
|
|
2019-06-23 07:22:26 +00:00
|
|
|
void DiveTripModelTree::divesChanged(const QVector<dive *> &dives)
|
|
|
|
{
|
|
|
|
processByTrip(dives, [this] (dive_trip *trip, const QVector<dive *> &divesInTrip)
|
|
|
|
{ divesChangedTrip(trip, divesInTrip); });
|
|
|
|
}
|
|
|
|
|
2020-04-19 17:04:56 +00:00
|
|
|
void DiveTripModelTree::diveChanged(dive *d)
|
2020-05-05 09:42:01 +00:00
|
|
|
{
|
|
|
|
divesChanged(QVector<dive *> { d });
|
|
|
|
}
|
|
|
|
|
2020-02-09 21:47:47 +00:00
|
|
|
void DiveTripModelTree::divesChangedTrip(dive_trip *trip, const QVector<dive *> &divesIn)
|
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
|
|
|
{
|
2020-02-09 21:47:47 +00:00
|
|
|
QVector<dive *> dives = divesIn;
|
|
|
|
ShownChange shownChange = updateShown(dives);
|
|
|
|
divesShown(trip, shownChange.newShown);
|
|
|
|
divesHidden(trip, shownChange.newHidden);
|
2019-03-22 19:55:05 +00:00
|
|
|
|
2018-12-27 09:06:11 +00:00
|
|
|
if (!trip) {
|
|
|
|
// This is outside of a trip. Process top-level items range-wise.
|
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
|
|
|
|
|
|
|
// Since we know that the dive list is sorted, we will only ever search for the first element
|
|
|
|
// in dives as this must be the first that we encounter. Once we find a range, increase the
|
|
|
|
// index accordingly.
|
|
|
|
processRangesZip(items, dives,
|
|
|
|
[](const Item &e, dive *d) { return e.getDive() == d; }, // Condition
|
|
|
|
[&](const std::vector<Item> &, const QVector<dive *> &, int from, int to, int) -> int { // Action
|
|
|
|
// TODO: We might be smarter about which columns changed!
|
|
|
|
dataChanged(createIndex(from, 0, noParent), createIndex(to - 1, COLUMNS - 1, noParent));
|
|
|
|
return 0; // No items added or deleted
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Find the trip.
|
|
|
|
int idx = findTripIdx(trip);
|
|
|
|
if (idx < 0) {
|
2020-02-09 21:47:47 +00:00
|
|
|
// We don't know the trip! We seem to have missed some signals!
|
2018-12-27 09:06:11 +00:00
|
|
|
qWarning() << "DiveTripModelTree::divesChanged(): unknown 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
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change the dives in the trip. We do this range-wise.
|
|
|
|
processRangesZip(items[idx].dives, dives,
|
2019-12-13 06:31:16 +00:00
|
|
|
std::equal_to<const dive *>(), // Condition: dive-pointers are equal
|
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
|
|
|
[&](const std::vector<dive *> &, const QVector<dive *> &, int from, int to, int) -> int { // Action
|
|
|
|
// TODO: We might be smarter about which columns changed!
|
|
|
|
dataChanged(createIndex(from, 0, idx), createIndex(to - 1, COLUMNS - 1, idx));
|
|
|
|
return 0; // No items added or deleted
|
|
|
|
});
|
2018-11-13 08:26:53 +00:00
|
|
|
|
|
|
|
// If necessary, move the trip
|
|
|
|
topLevelChanged(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
|
|
|
}
|
2020-02-13 21:56:16 +00:00
|
|
|
|
|
|
|
// If the current dive changed (because the change caused it to become hidden
|
|
|
|
// by the filter), instruct the UI of the changed selection.
|
|
|
|
// TODO: This is way to heavy, as it reloads the whole selection!
|
|
|
|
if (shownChange.currentChanged)
|
|
|
|
initSelection();
|
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
|
|
|
}
|
|
|
|
|
2019-02-24 22:02:22 +00:00
|
|
|
void DiveTripModelTree::tripChanged(dive_trip *trip, TripField)
|
|
|
|
{
|
|
|
|
int idx = findTripIdx(trip);
|
2020-02-09 21:47:47 +00:00
|
|
|
if (idx < 0)
|
|
|
|
return; // We don't know the trip - this happens if all dives are hidden
|
2019-02-24 22:02:22 +00:00
|
|
|
|
|
|
|
dataChanged(createIndex(idx, 0, noParent), createIndex(idx, COLUMNS - 1, noParent));
|
|
|
|
}
|
|
|
|
|
2020-02-09 21:47:47 +00:00
|
|
|
void DiveTripModelTree::divesMovedBetweenTrips(dive_trip *from, dive_trip *to, bool deleteFrom, bool createTo, const QVector<dive *> &divesIn)
|
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
|
|
|
{
|
2020-02-09 21:47:47 +00:00
|
|
|
QVector <dive *> dives = visibleDives(divesIn);
|
|
|
|
if (dives.empty())
|
|
|
|
return;
|
|
|
|
|
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
|
|
|
// Move dives between trips. This is an "interesting" problem, as we might
|
|
|
|
// move from trip to trip, from trip to top-level or from top-level to trip.
|
|
|
|
// Moreover, we might have to add a trip first or delete an old trip.
|
2019-06-23 07:22:26 +00:00
|
|
|
// For simplicity, we will simply use the already existing divesAdded() / divesDeleted()
|
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
|
|
|
// functions. This *is* cheating. But let's just try this and see how graceful
|
|
|
|
// this is handled by Qt and if it gives some ugly UI behavior!
|
|
|
|
|
2018-12-27 09:06:11 +00:00
|
|
|
// But first let's just rule out the trivial case: same-to-same trip move.
|
|
|
|
if (from == to)
|
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
|
|
|
return;
|
|
|
|
|
|
|
|
// Cheating!
|
2018-08-25 14:42:35 +00:00
|
|
|
// Unfortunately, removing the dives means that their selection is lost.
|
|
|
|
// Thus, remember the selection and re-add it later.
|
2020-03-06 15:57:58 +00:00
|
|
|
divesDeletedInternal(from, deleteFrom, dives); // Use internal version to keep current dive
|
2020-10-03 08:37:37 +00:00
|
|
|
divesAdded(to, createTo, dives);
|
2019-06-23 07:22:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DiveTripModelTree::divesTimeChanged(timestamp_t delta, const QVector<dive *> &dives)
|
|
|
|
{
|
|
|
|
processByTrip(dives, [this, delta] (dive_trip *trip, const QVector<dive *> &divesInTrip)
|
|
|
|
{ divesTimeChangedTrip(trip, delta, divesInTrip); });
|
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
|
|
|
}
|
|
|
|
|
2020-02-09 21:47:47 +00:00
|
|
|
void DiveTripModelTree::divesTimeChangedTrip(dive_trip *trip, timestamp_t delta, const QVector<dive *> &divesIn)
|
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
|
|
|
{
|
2020-02-09 21:47:47 +00:00
|
|
|
QVector <dive *> dives = visibleDives(divesIn);
|
|
|
|
if (dives.empty())
|
|
|
|
return;
|
|
|
|
|
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
|
|
|
// As in the case of divesMovedBetweenTrips(), this is a tricky, but solvable, problem.
|
|
|
|
// We have to consider the direction (delta < 0 or delta >0) and that dives at their destination
|
|
|
|
// position have different contiguous batches than at their original position. For now,
|
|
|
|
// cheat and simply do a remove/add pair. Note that for this to work it is crucial the the
|
2018-12-27 09:06:11 +00:00
|
|
|
// order of the dives don't change. This is indeed the case, as all starting-times were
|
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
|
|
|
// moved by the same delta.
|
|
|
|
|
|
|
|
// Cheating!
|
2018-08-25 14:42:35 +00:00
|
|
|
// Unfortunately, removing the dives means that their selection is lost.
|
|
|
|
// Thus, remember the selection and re-add it later.
|
2020-03-06 15:57:58 +00:00
|
|
|
divesDeletedInternal(trip, false, dives); // Use internal version to keep current dive
|
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
|
|
|
divesAdded(trip, false, dives);
|
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
|
|
|
}
|
|
|
|
|
2020-03-06 15:57:58 +00:00
|
|
|
QModelIndex DiveTripModelTree::diveToIdx(const dive *d) const
|
|
|
|
{
|
|
|
|
if (!d)
|
|
|
|
return QModelIndex();
|
|
|
|
dive_trip *trip = d->divetrip;
|
|
|
|
if (!trip) {
|
|
|
|
// Outside of a trip - search top-level.
|
|
|
|
int idx = findDiveIdx(d);
|
|
|
|
if (idx < 0) {
|
|
|
|
// We don't know this dive. Something is wrong. Warn and bail.
|
|
|
|
qWarning() << "DiveTripModelTree::diveToIdx(): unknown top-level dive";
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
return createIndex(idx, 0, noParent);
|
|
|
|
} else {
|
|
|
|
int idx = findTripIdx(trip);
|
|
|
|
if (idx < 0) {
|
|
|
|
// We don't know the trip - this shouldn't happen. Warn and bail.
|
|
|
|
qWarning() << "DiveTripModelTree::diveToIdx(): unknown trip";
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
int diveIdx = findDiveInTrip(idx, d);
|
|
|
|
if (diveIdx < 0) {
|
|
|
|
// We don't know this dive. Something is wrong. Warn and bail.
|
|
|
|
qWarning() << "DiveTripModelTree::diveToIdx(): unknown dive";
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
return createIndex(diveIdx, 0, idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-04 06:21:48 +00:00
|
|
|
void DiveTripModelTree::divesSelectedSlot(const QVector<dive *> &divesIn, dive *currentDive, int currentDC)
|
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
|
|
|
{
|
2020-02-09 21:47:47 +00:00
|
|
|
QVector <dive *> dives = visibleDives(divesIn);
|
|
|
|
|
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
|
|
|
// We got a number of dives that have been selected. Turn this into QModelIndexes and
|
|
|
|
// emit a signal, so that views can change the selection.
|
2020-03-11 10:30:51 +00:00
|
|
|
QVector<QModelIndex> indices;
|
|
|
|
indices.reserve(dives.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
|
|
|
|
2024-01-16 16:39:19 +00:00
|
|
|
processByTrip(std::move(dives), [this, &indices] (dive_trip *trip, const QVector<dive *> &divesInTrip)
|
2020-03-11 10:30:51 +00:00
|
|
|
{ divesSelectedTrip(trip, divesInTrip, indices); });
|
2019-06-23 09:13:41 +00:00
|
|
|
|
2022-09-04 06:21:48 +00:00
|
|
|
emit divesSelected(indices, diveToIdx(currentDive), currentDC);
|
2019-06-23 10:46:42 +00:00
|
|
|
|
|
|
|
// The current dive has changed. Transform the current dive into an index and pass it on to the view.
|
2022-08-21 09:22:34 +00:00
|
|
|
currentChanged(currentDive);
|
2019-06-23 09:13:41 +00:00
|
|
|
}
|
|
|
|
|
2020-03-11 10:30:51 +00:00
|
|
|
void DiveTripModelTree::divesSelectedTrip(dive_trip *trip, const QVector<dive *> &dives, QVector<QModelIndex> &indices)
|
2019-06-23 09:13:41 +00:00
|
|
|
{
|
2018-12-27 09:06:11 +00:00
|
|
|
if (!trip) {
|
|
|
|
// This is at the top level.
|
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
|
|
|
// Since both lists are sorted, we can do this linearly. Perhaps a binary search
|
|
|
|
// would be better?
|
|
|
|
int j = 0; // Index in items array
|
2022-10-30 20:24:13 +00:00
|
|
|
for (struct dive *dive: dives) {
|
|
|
|
while (j < (int)items.size() && !items[j].isDive(dive))
|
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
|
|
|
++j;
|
|
|
|
if (j >= (int)items.size())
|
|
|
|
break;
|
2020-03-11 10:30:51 +00:00
|
|
|
indices.append(createIndex(j, 0, noParent));
|
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
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Find the trip.
|
|
|
|
int idx = findTripIdx(trip);
|
|
|
|
if (idx < 0) {
|
|
|
|
// We don't know the trip - this shouldn't happen. We seem to have
|
|
|
|
// missed some signals!
|
2019-06-23 09:13:41 +00:00
|
|
|
qWarning() << "DiveTripModelTree::diveSelectedTrip(): unknown trip";
|
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
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Locate the indices inside the trip.
|
|
|
|
// Since both lists are sorted, we can do this linearly. Perhaps a binary search
|
|
|
|
// would be better?
|
|
|
|
int j = 0; // Index in items array
|
|
|
|
const Item &entry = items[idx];
|
2022-10-30 20:24:13 +00:00
|
|
|
for (struct dive *dive: dives) {
|
|
|
|
while (j < (int)entry.dives.size() && entry.dives[j] != dive)
|
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
|
|
|
++j;
|
|
|
|
if (j >= (int)entry.dives.size())
|
|
|
|
break;
|
2020-03-11 10:30:51 +00:00
|
|
|
indices.append(createIndex(j, 0, idx));
|
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 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-08-14 13:12:21 +00:00
|
|
|
|
2022-08-27 15:28:34 +00:00
|
|
|
void DiveTripModelTree::tripSelected(dive_trip *trip, dive *currentDive)
|
|
|
|
{
|
|
|
|
if (!trip)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Find the trip.
|
|
|
|
int idx = findTripIdx(trip);
|
|
|
|
if (idx < 0) {
|
|
|
|
// We don't know the trip - this shouldn't happen. We seem to have
|
|
|
|
// missed some signals!
|
|
|
|
qWarning() << "DiveTripModelTree::tripSelected(): unknown trip";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex tripIdx = createIndex(idx, 0, noParent);
|
|
|
|
emit DiveTripModelBase::tripSelected(tripIdx, diveToIdx(currentDive));
|
|
|
|
}
|
|
|
|
|
2018-12-27 09:06:11 +00:00
|
|
|
bool DiveTripModelTree::lessThan(const QModelIndex &i1, const QModelIndex &i2) const
|
|
|
|
{
|
|
|
|
// In tree mode we don't support any sorting!
|
|
|
|
// Simply keep the original position.
|
|
|
|
return i1.row() < i2.row();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3) ListModel functions
|
|
|
|
|
|
|
|
DiveTripModelList::DiveTripModelList(QObject *parent) : DiveTripModelBase(parent)
|
|
|
|
{
|
|
|
|
// Stay informed of changes to the divelist
|
|
|
|
connect(&diveListNotifier, &DiveListNotifier::divesAdded, this, &DiveTripModelList::divesAdded);
|
|
|
|
connect(&diveListNotifier, &DiveListNotifier::divesDeleted, this, &DiveTripModelList::divesDeleted);
|
|
|
|
connect(&diveListNotifier, &DiveListNotifier::divesChanged, this, &DiveTripModelList::divesChanged);
|
2019-08-31 13:05:11 +00:00
|
|
|
connect(&diveListNotifier, &DiveListNotifier::diveSiteChanged, this, &DiveTripModelList::diveSiteChanged);
|
2018-12-27 09:06:11 +00:00
|
|
|
// Does nothing in list-view
|
|
|
|
//connect(&diveListNotifier, &DiveListNotifier::divesMovedBetweenTrips, this, &DiveTripModelList::divesMovedBetweenTrips);
|
|
|
|
connect(&diveListNotifier, &DiveListNotifier::divesTimeChanged, this, &DiveTripModelList::divesTimeChanged);
|
2022-09-04 06:21:48 +00:00
|
|
|
connect(&diveListNotifier, &DiveListNotifier::divesSelected, this, &DiveTripModelList::divesSelectedSlot);
|
2022-08-27 15:28:34 +00:00
|
|
|
connect(&diveListNotifier, &DiveListNotifier::tripSelected, this, &DiveTripModelList::tripSelected);
|
2019-12-09 14:13:53 +00:00
|
|
|
connect(&diveListNotifier, &DiveListNotifier::filterReset, this, &DiveTripModelList::filterReset);
|
2020-04-19 17:04:56 +00:00
|
|
|
connect(&diveListNotifier, &DiveListNotifier::cylinderAdded, this, &DiveTripModelList::diveChanged);
|
|
|
|
connect(&diveListNotifier, &DiveListNotifier::cylinderEdited, this, &DiveTripModelList::diveChanged);
|
|
|
|
connect(&diveListNotifier, &DiveListNotifier::cylinderRemoved, this, &DiveTripModelList::diveChanged);
|
|
|
|
connect(&diveListNotifier, &DiveListNotifier::pictureOffsetChanged, this, &DiveTripModelList::diveChanged);
|
|
|
|
connect(&diveListNotifier, &DiveListNotifier::picturesRemoved, this, &DiveTripModelList::diveChanged);
|
|
|
|
connect(&diveListNotifier, &DiveListNotifier::picturesAdded, this, &DiveTripModelList::diveChanged);
|
cleanup: invert control-flow when resetting the core structures
To reset the core data structures, the mobile and desktop UIs
were calling into the dive-list models, which then reset the
core data structures, themselves and the unrelated
locationinformation model. The UI code then reset various other
things, such as the TankInformation model or the map. . This was
unsatisfying from a control-flow perspective, as the models should
display the core data, not act on it. Moreover, this meant lots
of intricate intermodule-dependencies.
Thus, straighten up the control flow: give the C core the
possibility to send a "all data reset" event. And do that
in those functions that reset the core data structures.
Let each module react to this event by itself. This removes
inter-module dependencies. For example, the MainWindow now
doesn't have to reset the TankInfoModel or the MapWidget.
Then, to reset the core data structures, let the UI code
simply directly call the respective core functions.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-05-04 22:12:36 +00:00
|
|
|
connect(&diveListNotifier, &DiveListNotifier::dataReset, this, &DiveTripModelList::reset);
|
2018-12-27 09:06:11 +00:00
|
|
|
|
2019-12-11 08:50:38 +00:00
|
|
|
populate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveTripModelList::populate()
|
|
|
|
{
|
2020-10-30 20:28:11 +00:00
|
|
|
DiveFilter::instance()->reset(); // The data was reset - update filter status. TODO: should this really be done here?
|
2020-09-20 11:40:21 +00:00
|
|
|
|
2018-12-27 09:06:11 +00:00
|
|
|
// Fill model
|
2024-06-07 08:25:09 +00:00
|
|
|
items.reserve(divelog.dives.size());
|
|
|
|
for (auto &d: divelog.dives) {
|
2020-10-24 21:43:32 +00:00
|
|
|
if (!d || d->hidden_by_filter)
|
2020-02-09 21:47:47 +00:00
|
|
|
continue;
|
2024-06-07 08:25:09 +00:00
|
|
|
items.push_back(d.get());
|
2020-02-09 21:47:47 +00:00
|
|
|
}
|
2020-03-06 15:57:58 +00:00
|
|
|
|
|
|
|
// Remember the index of the current dive
|
|
|
|
oldCurrent = current_dive;
|
2018-12-27 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int DiveTripModelList::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
// In list-mode there is only one level, i.e only top-level
|
|
|
|
// (=invalid parent) has items.
|
|
|
|
return parent.isValid() ? 0 : items.size();
|
|
|
|
}
|
|
|
|
|
2019-11-18 17:44:17 +00:00
|
|
|
void DiveTripModelTree::clearData()
|
|
|
|
{
|
|
|
|
items.clear();
|
|
|
|
}
|
|
|
|
|
2018-12-27 09:06:11 +00:00
|
|
|
QModelIndex DiveTripModelList::index(int row, int column, const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
if (!hasIndex(row, column, parent))
|
|
|
|
return QModelIndex();
|
|
|
|
|
|
|
|
return createIndex(row, column);
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex DiveTripModelList::parent(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
// In list-mode there is only one level, i.e. no parent
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
dive *DiveTripModelList::diveOrNull(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
int row = index.row();
|
|
|
|
if (row < 0 || row > (int)items.size())
|
|
|
|
return nullptr;
|
|
|
|
return items[row];
|
|
|
|
}
|
|
|
|
|
2019-12-09 14:13:53 +00:00
|
|
|
// This recalculates the filters and sends appropriate changed signals.
|
|
|
|
// Attention: Since this uses / modifies the hidden_by_filter flag of the
|
|
|
|
// core dive structure, only one DiveTripModel[Tree|List] must exist at
|
|
|
|
// a given time!
|
|
|
|
void DiveTripModelList::filterReset()
|
Dive list: cache shown flag in model (quick-fix for undo-crash)
We have a very fundamental problem with data-duplication in
core and qt-models. In a particular case, this led to an easily
reproducible crash:
1) An undo command moved the last dive of a trip to another.
2) When an undo-command removed the last dive of
a trip to a different trip, the dive was removed from the
trip in the core. Then, the model was updated.
3) That lead at first to a rearrangement of the trips, because
the trip with the added dive is moved before the trip with
the removed dive.
4) In such a case, the filter-model checks the visibility of
the trip.
5) Since the trip with the removed dive has no dives in the core,
visibility was determined as false.
6) From this point on the mappings of the QSortFilterProxyModel
were messed up. Accesses led to crashes. It is unclear
whether this is a Qt bug or only a QOI issue.
As a quick-fix, cache the visibility flag of trips directly
in the Qt-models. Don't set the visibility directly in the
core, but go via the Qt-models. Thus, a more clear layering
is achieved.
In the long run, we can hopefully get rid of the data-duplication
in the models.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-06-22 18:48:59 +00:00
|
|
|
{
|
2020-02-09 21:47:47 +00:00
|
|
|
ShownChange change = updateShownAll();
|
|
|
|
removeDives(change.newHidden);
|
|
|
|
addDives(change.newShown);
|
2019-12-10 08:42:05 +00:00
|
|
|
|
|
|
|
// If the current dive changed, instruct the UI of the changed selection
|
|
|
|
// TODO: This is way to heavy, as it reloads the whole selection!
|
2020-02-13 21:56:16 +00:00
|
|
|
if (change.currentChanged)
|
2019-12-10 08:42:05 +00:00
|
|
|
initSelection();
|
Dive list: cache shown flag in model (quick-fix for undo-crash)
We have a very fundamental problem with data-duplication in
core and qt-models. In a particular case, this led to an easily
reproducible crash:
1) An undo command moved the last dive of a trip to another.
2) When an undo-command removed the last dive of
a trip to a different trip, the dive was removed from the
trip in the core. Then, the model was updated.
3) That lead at first to a rearrangement of the trips, because
the trip with the added dive is moved before the trip with
the removed dive.
4) In such a case, the filter-model checks the visibility of
the trip.
5) Since the trip with the removed dive has no dives in the core,
visibility was determined as false.
6) From this point on the mappings of the QSortFilterProxyModel
were messed up. Accesses led to crashes. It is unclear
whether this is a Qt bug or only a QOI issue.
As a quick-fix, cache the visibility flag of trips directly
in the Qt-models. Don't set the visibility directly in the
core, but go via the Qt-models. Thus, a more clear layering
is achieved.
In the long run, we can hopefully get rid of the data-duplication
in the models.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2019-06-22 18:48:59 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 09:06:11 +00:00
|
|
|
QVariant DiveTripModelList::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
dive *d = diveOrNull(index);
|
|
|
|
return d ? diveData(d, index.column(), role) : QVariant();
|
|
|
|
}
|
|
|
|
|
2020-02-09 21:47:47 +00:00
|
|
|
void DiveTripModelList::addDives(QVector<dive *> &dives)
|
2018-12-27 09:06:11 +00:00
|
|
|
{
|
2024-06-07 08:25:09 +00:00
|
|
|
std::sort(dives.begin(), dives.end(), dive_less_than_ptr);
|
2018-12-27 09:06:11 +00:00
|
|
|
addInBatches(items, dives,
|
2024-06-07 08:25:09 +00:00
|
|
|
&dive_less_than_ptr, // comp
|
2018-12-27 09:06:11 +00:00
|
|
|
[&](std::vector<dive *> &items, const QVector<dive *> &dives, int idx, int from, int to) { // inserter
|
|
|
|
beginInsertRows(QModelIndex(), idx, idx + to - from - 1);
|
|
|
|
items.insert(items.begin() + idx, dives.begin() + from, dives.begin() + to);
|
|
|
|
endInsertRows();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-06 15:57:58 +00:00
|
|
|
void DiveTripModelList::removeDives(QVector<dive *> dives)
|
2018-12-27 09:06:11 +00:00
|
|
|
{
|
2024-06-07 08:25:09 +00:00
|
|
|
std::sort(dives.begin(), dives.end(), dive_less_than_ptr);
|
2018-12-27 09:06:11 +00:00
|
|
|
processRangesZip(items, dives,
|
2019-12-13 06:31:16 +00:00
|
|
|
std::equal_to<const dive *>(), // Condition: dive-pointers are equal
|
2018-12-27 09:06:11 +00:00
|
|
|
[&](std::vector<dive *> &items, const QVector<dive *> &, int from, int to, int) -> int { // Action
|
|
|
|
beginRemoveRows(QModelIndex(), from, to - 1);
|
|
|
|
items.erase(items.begin() + from, items.begin() + to);
|
|
|
|
endRemoveRows();
|
|
|
|
return from - to; // Delta: negate the number of items deleted
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-06 15:57:58 +00:00
|
|
|
void DiveTripModelList::divesDeleted(dive_trip *trip, bool, const QVector<dive *> &divesIn)
|
2020-02-09 21:47:47 +00:00
|
|
|
{
|
|
|
|
QVector<dive *> dives = visibleDives(divesIn);
|
2020-03-06 15:57:58 +00:00
|
|
|
if (oldCurrent && std::find(dives.begin(), dives.end(), oldCurrent) != dives.end())
|
|
|
|
oldCurrent = nullptr;
|
|
|
|
divesDeletedInternal(dives);
|
2020-02-09 21:47:47 +00:00
|
|
|
}
|
|
|
|
|
2020-03-06 15:57:58 +00:00
|
|
|
void DiveTripModelList::divesDeletedInternal(const QVector<dive *> &dives)
|
2020-02-09 21:47:47 +00:00
|
|
|
{
|
|
|
|
removeDives(dives);
|
|
|
|
}
|
|
|
|
|
2020-03-06 15:57:58 +00:00
|
|
|
void DiveTripModelList::divesAdded(dive_trip *, bool, const QVector<dive *> &divesIn)
|
|
|
|
{
|
|
|
|
QVector<dive *> dives = visibleDives(divesIn);
|
|
|
|
addDives(dives);
|
|
|
|
}
|
|
|
|
|
2019-08-31 13:05:11 +00:00
|
|
|
void DiveTripModelList::diveSiteChanged(dive_site *ds, int field)
|
|
|
|
{
|
|
|
|
if (!isInterestingDiveSiteField(field))
|
|
|
|
return;
|
|
|
|
divesChanged(getDivesForSite(ds));
|
|
|
|
}
|
|
|
|
|
2019-06-23 07:22:26 +00:00
|
|
|
void DiveTripModelList::divesChanged(const QVector<dive *> &divesIn)
|
2018-12-27 09:06:11 +00:00
|
|
|
{
|
2019-06-23 07:22:26 +00:00
|
|
|
QVector<dive *> dives = divesIn;
|
2024-06-07 08:25:09 +00:00
|
|
|
std::sort(dives.begin(), dives.end(), dive_less_than_ptr);
|
2019-06-23 07:22:26 +00:00
|
|
|
|
2020-02-09 21:47:47 +00:00
|
|
|
ShownChange shownChange = updateShown(dives);
|
|
|
|
removeDives(shownChange.newHidden);
|
|
|
|
addDives(shownChange.newShown);
|
2019-03-22 19:55:05 +00:00
|
|
|
|
2018-12-27 09:06:11 +00:00
|
|
|
// Since we know that the dive list is sorted, we will only ever search for the first element
|
|
|
|
// in dives as this must be the first that we encounter. Once we find a range, increase the
|
|
|
|
// index accordingly.
|
|
|
|
processRangesZip(items, dives,
|
2019-12-13 06:31:16 +00:00
|
|
|
std::equal_to<const dive *>(), // Condition: dive-pointers are equal
|
2018-12-27 09:06:11 +00:00
|
|
|
[&](const std::vector<dive *> &, const QVector<dive *> &, int from, int to, int) -> int { // Action
|
|
|
|
// TODO: We might be smarter about which columns changed!
|
|
|
|
dataChanged(createIndex(from, 0, noParent), createIndex(to - 1, COLUMNS - 1, noParent));
|
|
|
|
return 0; // No items added or deleted
|
|
|
|
});
|
2020-02-13 21:56:16 +00:00
|
|
|
|
|
|
|
// If the current dive changed (because the change caused it to become hidden
|
|
|
|
// by the filter), instruct the UI of the changed selection.
|
|
|
|
// TODO: This is way to heavy, as it reloads the whole selection!
|
|
|
|
if (shownChange.currentChanged)
|
|
|
|
initSelection();
|
2018-12-27 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
2020-04-19 17:04:56 +00:00
|
|
|
void DiveTripModelList::diveChanged(dive *d)
|
2020-05-05 09:42:01 +00:00
|
|
|
{
|
|
|
|
divesChanged(QVector<dive *> { d });
|
|
|
|
}
|
|
|
|
|
2019-06-23 07:22:26 +00:00
|
|
|
void DiveTripModelList::divesTimeChanged(timestamp_t delta, const QVector<dive *> &divesIn)
|
2018-12-27 09:06:11 +00:00
|
|
|
{
|
2020-02-09 21:47:47 +00:00
|
|
|
QVector<dive *> dives = visibleDives(divesIn);
|
|
|
|
if (dives.empty())
|
|
|
|
return;
|
2024-06-07 08:25:09 +00:00
|
|
|
std::sort(dives.begin(), dives.end(), dive_less_than_ptr);
|
2019-06-23 07:22:26 +00:00
|
|
|
|
2018-12-27 09:06:11 +00:00
|
|
|
// See comment for DiveTripModelTree::divesTimeChanged above.
|
2020-03-06 15:57:58 +00:00
|
|
|
divesDeletedInternal(dives); // Use internal version to keep current dive
|
2019-06-23 07:22:26 +00:00
|
|
|
divesAdded(nullptr, false, dives);
|
2018-12-27 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-06 15:57:58 +00:00
|
|
|
QModelIndex DiveTripModelList::diveToIdx(const dive *d) const
|
|
|
|
{
|
|
|
|
if (!d)
|
|
|
|
return QModelIndex();
|
|
|
|
|
|
|
|
auto it = std::find(items.begin(), items.end(), d);
|
|
|
|
if (it == items.end()) {
|
|
|
|
// We don't know this dive. Something is wrong. Warn and bail.
|
|
|
|
qWarning() << "DiveTripModelList::diveToIdx(): unknown dive";
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
return createIndex(it - items.begin(), 0);
|
|
|
|
}
|
|
|
|
|
2022-09-04 06:21:48 +00:00
|
|
|
void DiveTripModelList::divesSelectedSlot(const QVector<dive *> &divesIn, dive *currentDive, int currentDC)
|
2018-12-27 09:06:11 +00:00
|
|
|
{
|
2020-02-09 21:47:47 +00:00
|
|
|
QVector<dive *> dives = visibleDives(divesIn);
|
|
|
|
|
2018-12-27 09:06:11 +00:00
|
|
|
// We got a number of dives that have been selected. Turn this into QModelIndexes and
|
|
|
|
// emit a signal, so that views can change the selection.
|
2020-03-11 10:30:51 +00:00
|
|
|
QVector<QModelIndex> indices;
|
|
|
|
indices.reserve(dives.count());
|
2018-12-27 09:06:11 +00:00
|
|
|
|
|
|
|
// Since both lists are sorted, we can do this linearly. Perhaps a binary search
|
|
|
|
// would be better?
|
|
|
|
int j = 0; // Index in items array
|
2022-10-30 20:24:13 +00:00
|
|
|
for (struct dive *dive: dives) {
|
|
|
|
while (j < (int)items.size() && items[j] != dive)
|
2018-12-27 09:06:11 +00:00
|
|
|
++j;
|
|
|
|
if (j >= (int)items.size())
|
|
|
|
break;
|
2020-03-11 10:30:51 +00:00
|
|
|
indices.append(createIndex(j, 0, noParent));
|
2018-12-27 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
2022-09-04 06:21:48 +00:00
|
|
|
emit divesSelected(indices, diveToIdx(currentDive), currentDC);
|
2018-12-27 09:06:11 +00:00
|
|
|
|
2020-03-06 15:57:58 +00:00
|
|
|
// The current dive has changed. Transform the current dive into an index and pass it on to the view.
|
2022-08-21 09:22:34 +00:00
|
|
|
currentChanged(currentDive);
|
2018-12-27 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
2022-08-27 15:28:34 +00:00
|
|
|
void DiveTripModelList::tripSelected(dive_trip *trip, dive *currentDive)
|
|
|
|
{
|
|
|
|
if (!trip)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// In the list view, there are no trips, so simply transform this into
|
|
|
|
// a dive selection.
|
|
|
|
QVector<dive *> dives;
|
2024-06-02 15:06:18 +00:00
|
|
|
dives.reserve(trip->dives.size());
|
|
|
|
for (auto dive: trip->dives)
|
|
|
|
dives.push_back(dive);
|
2022-08-27 15:28:34 +00:00
|
|
|
|
2022-09-04 06:21:48 +00:00
|
|
|
divesSelectedSlot(dives, currentDive, -1);
|
2022-08-27 15:28:34 +00:00
|
|
|
}
|
|
|
|
|
2018-10-29 19:17:53 +00:00
|
|
|
// Simple sorting helper for sorting against a criterium and if
|
|
|
|
// that is undefined against a different criterium.
|
|
|
|
// Return true if diff1 < 0, false if diff1 > 0.
|
|
|
|
// If diff1 == 0 return true if diff2 < 0;
|
|
|
|
static bool lessThanHelper(int diff1, int diff2)
|
|
|
|
{
|
|
|
|
return diff1 < 0 || (diff1 == 0 && diff2 < 0);
|
|
|
|
}
|
|
|
|
|
2024-05-28 19:31:11 +00:00
|
|
|
static int strCmp(const std::string &s1, const std::string &s2)
|
|
|
|
{
|
|
|
|
return QString::localeAwareCompare(QString::fromStdString(s1), QString::fromStdString(s2)); // TODO: avoid copy
|
|
|
|
}
|
|
|
|
|
2018-12-27 09:06:11 +00:00
|
|
|
bool DiveTripModelList::lessThan(const QModelIndex &i1, const QModelIndex &i2) const
|
2018-10-29 19:17:53 +00:00
|
|
|
{
|
|
|
|
// We assume that i1.column() == i2.column().
|
|
|
|
int row1 = i1.row();
|
|
|
|
int row2 = i2.row();
|
|
|
|
if (row1 < 0 || row1 >= (int)items.size() || row2 < 0 || row2 >= (int)items.size())
|
|
|
|
return false;
|
2018-12-27 09:06:11 +00:00
|
|
|
const dive *d1 = items[row1];
|
|
|
|
const dive *d2 = items[row2];
|
2018-11-06 20:48:18 +00:00
|
|
|
// This is used as a second sort criterion: For equal values, sorting is chronologically *descending*.
|
|
|
|
int row_diff = row2 - row1;
|
2018-10-29 19:17:53 +00:00
|
|
|
switch (i1.column()) {
|
|
|
|
case NR:
|
|
|
|
case DATE:
|
|
|
|
default:
|
2018-11-06 20:48:18 +00:00
|
|
|
return row1 < row2;
|
2018-10-29 19:17:53 +00:00
|
|
|
case RATING:
|
|
|
|
return lessThanHelper(d1->rating - d2->rating, row_diff);
|
|
|
|
case DEPTH:
|
|
|
|
return lessThanHelper(d1->maxdepth.mm - d2->maxdepth.mm, row_diff);
|
|
|
|
case DURATION:
|
|
|
|
return lessThanHelper(d1->duration.seconds - d2->duration.seconds, row_diff);
|
|
|
|
case TEMPERATURE:
|
|
|
|
return lessThanHelper(d1->watertemp.mkelvin - d2->watertemp.mkelvin, row_diff);
|
|
|
|
case TOTALWEIGHT:
|
2024-06-30 09:13:39 +00:00
|
|
|
return lessThanHelper(d1->total_weight().grams - d2->total_weight().grams, row_diff);
|
2018-10-29 19:17:53 +00:00
|
|
|
case SUIT:
|
|
|
|
return lessThanHelper(strCmp(d1->suit, d2->suit), row_diff);
|
|
|
|
case CYLINDER:
|
2024-05-28 19:31:11 +00:00
|
|
|
if (!d1->cylinders.empty() && !d2->cylinders.empty())
|
|
|
|
return lessThanHelper(strCmp(d1->cylinders[0].type.description, d2->cylinders[0].type.description), row_diff);
|
|
|
|
return d1->cylinders.size() < d2->cylinders.size();
|
2018-10-29 19:17:53 +00:00
|
|
|
case GAS:
|
|
|
|
return lessThanHelper(nitrox_sort_value(d1) - nitrox_sort_value(d2), row_diff);
|
|
|
|
case SAC:
|
|
|
|
return lessThanHelper(d1->sac - d2->sac, row_diff);
|
|
|
|
case OTU:
|
|
|
|
return lessThanHelper(d1->otu - d2->otu, row_diff);
|
|
|
|
case MAXCNS:
|
|
|
|
return lessThanHelper(d1->maxcns - d2->maxcns, row_diff);
|
|
|
|
case TAGS: {
|
2024-05-29 15:57:48 +00:00
|
|
|
std::string s1 = taglist_get_tagstring(d1->tags);
|
|
|
|
std::string s2 = taglist_get_tagstring(d2->tags);
|
2024-05-28 19:31:11 +00:00
|
|
|
int diff = strCmp(s1, s2);
|
2018-10-29 19:17:53 +00:00
|
|
|
return lessThanHelper(diff, row_diff);
|
|
|
|
}
|
|
|
|
case PHOTOS:
|
|
|
|
return lessThanHelper(countPhotos(d1) - countPhotos(d2), row_diff);
|
|
|
|
case COUNTRY:
|
2024-06-30 18:55:34 +00:00
|
|
|
return lessThanHelper(strCmp(d1->get_country(), d2->get_country()), row_diff);
|
2018-10-29 19:17:53 +00:00
|
|
|
case BUDDIES:
|
|
|
|
return lessThanHelper(strCmp(d1->buddy, d2->buddy), row_diff);
|
2022-02-12 13:35:22 +00:00
|
|
|
case DIVEGUIDE:
|
|
|
|
return lessThanHelper(strCmp(d1->diveguide, d2->diveguide), row_diff);
|
2018-10-29 19:17:53 +00:00
|
|
|
case LOCATION:
|
2024-06-30 18:55:34 +00:00
|
|
|
return lessThanHelper(strCmp(d1->get_location(), d2->get_location()), row_diff);
|
2023-03-16 00:29:43 +00:00
|
|
|
case NOTES:
|
|
|
|
return lessThanHelper(strCmp(d1->notes, d2->notes), row_diff);
|
2023-09-22 21:35:17 +00:00
|
|
|
case DIVEMODE:
|
2024-05-27 15:09:48 +00:00
|
|
|
return lessThanHelper((int)d1->dcs[0].divemode - (int)d2->dcs[0].divemode, row_diff);
|
2018-10-29 19:17:53 +00:00
|
|
|
}
|
|
|
|
}
|