Dive list: replace dive-id by dive pointer

The undo-system now guarantees that pointers to dives are stable
throughout their lifetime. Therefore, replace the unique index by
pointers. This is a small performance improvement, but much more
importantly, it will make it more natural to transport a pointer
to the dive inside QModelIndex's private pointer.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2018-09-06 22:15:37 +02:00 committed by Dirk Hohndel
parent dd9af8e72e
commit c341bc53c3
2 changed files with 57 additions and 75 deletions

View file

@ -93,78 +93,73 @@ static const QString icon_names[4] = {
QVariant DiveItem::data(int column, int role) const QVariant DiveItem::data(int column, int role) const
{ {
QVariant retVal; QVariant retVal;
struct dive *dive = get_dive_by_uniq_id(diveId);
if (!dive)
return QVariant();
switch (role) { switch (role) {
case Qt::TextAlignmentRole: case Qt::TextAlignmentRole:
retVal = dive_table_alignment(column); retVal = dive_table_alignment(column);
break; break;
case DiveTripModel::SORT_ROLE: case DiveTripModel::SORT_ROLE:
Q_ASSERT(dive != NULL);
switch (column) { switch (column) {
case NR: case NR:
retVal = (qlonglong)dive->when; retVal = (qlonglong)d->when;
break; break;
case DATE: case DATE:
retVal = (qlonglong)dive->when; retVal = (qlonglong)d->when;
break; break;
case RATING: case RATING:
retVal = dive->rating; retVal = d->rating;
break; break;
case DEPTH: case DEPTH:
retVal = dive->maxdepth.mm; retVal = d->maxdepth.mm;
break; break;
case DURATION: case DURATION:
retVal = dive->duration.seconds; retVal = d->duration.seconds;
break; break;
case TEMPERATURE: case TEMPERATURE:
retVal = dive->watertemp.mkelvin; retVal = d->watertemp.mkelvin;
break; break;
case TOTALWEIGHT: case TOTALWEIGHT:
retVal = total_weight(dive); retVal = total_weight(d);
break; break;
case SUIT: case SUIT:
retVal = QString(dive->suit); retVal = QString(d->suit);
break; break;
case CYLINDER: case CYLINDER:
retVal = QString(dive->cylinder[0].type.description); retVal = QString(d->cylinder[0].type.description);
break; break;
case GAS: case GAS:
retVal = nitrox_sort_value(dive); retVal = nitrox_sort_value(d);
break; break;
case SAC: case SAC:
retVal = dive->sac; retVal = d->sac;
break; break;
case OTU: case OTU:
retVal = dive->otu; retVal = d->otu;
break; break;
case MAXCNS: case MAXCNS:
retVal = dive->maxcns; retVal = d->maxcns;
break; break;
case TAGS: case TAGS:
retVal = displayTags(); retVal = displayTags();
break; break;
case PHOTOS: case PHOTOS:
retVal = countPhotos(dive); retVal = countPhotos();
break; break;
case COUNTRY: case COUNTRY:
retVal = QString(get_dive_country(dive)); retVal = QString(get_dive_country(d));
break; break;
case BUDDIES: case BUDDIES:
retVal = QString(dive->buddy); retVal = QString(d->buddy);
break; break;
case LOCATION: case LOCATION:
retVal = QString(get_dive_location(dive)); retVal = QString(get_dive_location(d));
break; break;
} }
break; break;
case Qt::DisplayRole: case Qt::DisplayRole:
Q_ASSERT(dive != NULL);
switch (column) { switch (column) {
case NR: case NR:
retVal = dive->number; retVal = d->number;
break; break;
case DATE: case DATE:
retVal = displayDate(); retVal = displayDate();
@ -182,22 +177,22 @@ QVariant DiveItem::data(int column, int role) const
retVal = prefs.units.show_units_table ? retVal = displayWeightWithUnit() : displayWeight(); retVal = prefs.units.show_units_table ? retVal = displayWeightWithUnit() : displayWeight();
break; break;
case SUIT: case SUIT:
retVal = QString(dive->suit); retVal = QString(d->suit);
break; break;
case CYLINDER: case CYLINDER:
retVal = QString(dive->cylinder[0].type.description); retVal = QString(d->cylinder[0].type.description);
break; break;
case SAC: case SAC:
retVal = prefs.units.show_units_table ? retVal = displaySacWithUnit() : displaySac(); retVal = prefs.units.show_units_table ? retVal = displaySacWithUnit() : displaySac();
break; break;
case OTU: case OTU:
retVal = dive->otu; retVal = d->otu;
break; break;
case MAXCNS: case MAXCNS:
if (prefs.units.show_units_table) if (prefs.units.show_units_table)
retVal = QString("%1%").arg(dive->maxcns); retVal = QString("%1%").arg(d->maxcns);
else else
retVal = dive->maxcns; retVal = d->maxcns;
break; break;
case TAGS: case TAGS:
retVal = displayTags(); retVal = displayTags();
@ -205,16 +200,16 @@ QVariant DiveItem::data(int column, int role) const
case PHOTOS: case PHOTOS:
break; break;
case COUNTRY: case COUNTRY:
retVal = QString(get_dive_country(dive)); retVal = QString(get_dive_country(d));
break; break;
case BUDDIES: case BUDDIES:
retVal = QString(dive->buddy); retVal = QString(d->buddy);
break; break;
case LOCATION: case LOCATION:
retVal = QString(get_dive_location(dive)); retVal = QString(get_dive_location(d));
break; break;
case GAS: case GAS:
const char *gas_string = get_dive_gas_string(dive); const char *gas_string = get_dive_gas_string(d);
retVal = QString(gas_string); retVal = QString(gas_string);
free((void*)gas_string); free((void*)gas_string);
break; break;
@ -227,16 +222,16 @@ QVariant DiveItem::data(int column, int role) const
retVal = QVariant(); retVal = QVariant();
break; break;
case LOCATION: case LOCATION:
if (dive_has_gps_location(dive)) { if (dive_has_gps_location(d)) {
IconMetrics im = defaultIconMetrics(); IconMetrics im = defaultIconMetrics();
retVal = QIcon(":globe-icon").pixmap(im.sz_small, im.sz_small); retVal = QIcon(":globe-icon").pixmap(im.sz_small, im.sz_small);
} }
break; break;
case PHOTOS: case PHOTOS:
if (dive->picture_list) if (d->picture_list)
{ {
IconMetrics im = defaultIconMetrics(); IconMetrics im = defaultIconMetrics();
retVal = QIcon(icon_names[countPhotos(dive)]).pixmap(im.sz_small, im.sz_small); retVal = QIcon(icon_names[countPhotos()]).pixmap(im.sz_small, im.sz_small);
} // If there are photos, show one of the three photo icons: fish= photos during dive; } // If there are photos, show one of the three photo icons: fish= photos during dive;
break; // sun=photos before/after dive; sun+fish=photos during dive as well as before/after break; // sun=photos before/after dive; sun+fish=photos during dive as well as before/after
} }
@ -304,15 +299,13 @@ QVariant DiveItem::data(int column, int role) const
} }
if (role == DiveTripModel::STAR_ROLE) { if (role == DiveTripModel::STAR_ROLE) {
Q_ASSERT(dive != NULL); retVal = d->rating;
retVal = dive->rating;
} }
if (role == DiveTripModel::DIVE_ROLE) { if (role == DiveTripModel::DIVE_ROLE) {
retVal = QVariant::fromValue<void *>(dive); retVal = QVariant::fromValue<void *>(d);
} }
if (role == DiveTripModel::DIVE_IDX) { if (role == DiveTripModel::DIVE_IDX) {
Q_ASSERT(dive != NULL); retVal = get_divenr(d);
retVal = get_divenr(dive);
} }
return retVal; return retVal;
} }
@ -337,12 +330,11 @@ bool DiveItem::setData(const QModelIndex &index, const QVariant &value, int role
return false; return false;
int i; int i;
struct dive *d; struct dive *dive;
for_each_dive (i, d) { for_each_dive (i, dive) {
if (d->number == v) if (dive->number == v)
return false; return false;
} }
d = get_dive_by_uniq_id(diveId);
d->number = value.toInt(); d->number = value.toInt();
mark_divelist_changed(true); mark_divelist_changed(true);
return true; return true;
@ -350,28 +342,25 @@ bool DiveItem::setData(const QModelIndex &index, const QVariant &value, int role
QString DiveItem::displayDate() const QString DiveItem::displayDate() const
{ {
struct dive *dive = get_dive_by_uniq_id(diveId); return get_dive_date_string(d->when);
return get_dive_date_string(dive->when);
} }
QString DiveItem::displayDepth() const QString DiveItem::displayDepth() const
{ {
struct dive *dive = get_dive_by_uniq_id(diveId); return get_depth_string(d->maxdepth);
return get_depth_string(dive->maxdepth);
} }
QString DiveItem::displayDepthWithUnit() const QString DiveItem::displayDepthWithUnit() const
{ {
struct dive *dive = get_dive_by_uniq_id(diveId); return get_depth_string(d->maxdepth, true);
return get_depth_string(dive->maxdepth, true);
} }
int DiveItem::countPhotos(dive *dive) const int DiveItem::countPhotos() const
{ // Determine whether dive has pictures, and whether they were taken during or before/after dive. { // 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 const int bufperiod = 120; // A 2-min buffer period. Photos within 2 min of dive are assumed as
int diveTotaltime = dive_endtime(dive) - dive->when; // taken during the dive, not before/after. int diveTotaltime = dive_endtime(d) - d->when; // taken during the dive, not before/after.
int pic_offset, icon_index = 0; int pic_offset, icon_index = 0;
FOR_EACH_PICTURE (dive) { // Step through each of the pictures for this dive: FOR_EACH_PICTURE (d) { // Step through each of the pictures for this dive:
pic_offset = picture->offset.seconds; pic_offset = picture->offset.seconds;
if ((pic_offset < -bufperiod) | (pic_offset > diveTotaltime+bufperiod)) { if ((pic_offset < -bufperiod) | (pic_offset > diveTotaltime+bufperiod)) {
icon_index |= 0x02; // If picture is before/after the dive icon_index |= 0x02; // If picture is before/after the dive
@ -385,43 +374,38 @@ int DiveItem::countPhotos(dive *dive) const
QString DiveItem::displayDuration() const QString DiveItem::displayDuration() const
{ {
struct dive *dive = get_dive_by_uniq_id(diveId);
if (prefs.units.show_units_table) if (prefs.units.show_units_table)
return get_dive_duration_string(dive->duration.seconds, tr("h"), tr("min"), "", ":", dive->dc.divemode == FREEDIVE); return get_dive_duration_string(d->duration.seconds, tr("h"), tr("min"), "", ":", d->dc.divemode == FREEDIVE);
else else
return get_dive_duration_string(dive->duration.seconds, "", "", "", ":", dive->dc.divemode == FREEDIVE); return get_dive_duration_string(d->duration.seconds, "", "", "", ":", d->dc.divemode == FREEDIVE);
} }
QString DiveItem::displayTemperature() const QString DiveItem::displayTemperature() const
{ {
struct dive *dive = get_dive_by_uniq_id(diveId); if (!d->watertemp.mkelvin)
if (!dive->watertemp.mkelvin)
return QString(); return QString();
return get_temperature_string(dive->watertemp, false); return get_temperature_string(d->watertemp, false);
} }
QString DiveItem::displayTemperatureWithUnit() const QString DiveItem::displayTemperatureWithUnit() const
{ {
struct dive *dive = get_dive_by_uniq_id(diveId); if (!d->watertemp.mkelvin)
if (!dive->watertemp.mkelvin)
return QString(); return QString();
return get_temperature_string(dive->watertemp, true); return get_temperature_string(d->watertemp, true);
} }
QString DiveItem::displaySac() const QString DiveItem::displaySac() const
{ {
struct dive *dive = get_dive_by_uniq_id(diveId); if (!d->sac)
if (!dive->sac)
return QString(); return QString();
return get_volume_string(dive->sac, false); return get_volume_string(d->sac, false);
} }
QString DiveItem::displaySacWithUnit() const QString DiveItem::displaySacWithUnit() const
{ {
struct dive *dive = get_dive_by_uniq_id(diveId); if (!d->sac)
if (!dive->sac)
return QString(); return QString();
return get_volume_string(dive->sac, true).append(tr("/min")); return get_volume_string(d->sac, true).append(tr("/min"));
} }
QString DiveItem::displayWeight() const QString DiveItem::displayWeight() const
@ -436,14 +420,12 @@ QString DiveItem::displayWeightWithUnit() const
QString DiveItem::displayTags() const QString DiveItem::displayTags() const
{ {
struct dive *dive = get_dive_by_uniq_id(diveId); return get_taglist_string(d->tag_list);
return get_taglist_string(dive->tag_list);
} }
int DiveItem::weight() const int DiveItem::weight() const
{ {
struct dive *dive = get_dive_by_uniq_id(diveId); weight_t tw = { total_weight(d) };
weight_t tw = { total_weight(dive) };
return tw.grams; return tw.grams;
} }
@ -618,7 +600,7 @@ void DiveTripModel::setupModelData()
dive_trip_t *trip = dive->divetrip; dive_trip_t *trip = dive->divetrip;
DiveItem *diveItem = new DiveItem(); DiveItem *diveItem = new DiveItem();
diveItem->diveId = dive->id; diveItem->d = dive;
if (!trip || currentLayout == LIST) { if (!trip || currentLayout == LIST) {
diveItem->parent = rootItem.get(); diveItem->parent = rootItem.get();

View file

@ -32,7 +32,7 @@ public:
}; };
QVariant data(int column, int role) const override; QVariant data(int column, int role) const override;
int diveId; dive *d;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
Qt::ItemFlags flags(const QModelIndex &index) const override; Qt::ItemFlags flags(const QModelIndex &index) const override;
QString displayDate() const; QString displayDate() const;
@ -46,7 +46,7 @@ public:
QString displaySac() const; QString displaySac() const;
QString displaySacWithUnit() const; QString displaySacWithUnit() const;
QString displayTags() const; QString displayTags() const;
int countPhotos(dive *dive) const; int countPhotos() const;
int weight() const; int weight() const;
}; };