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"
|
|
|
|
#include "core/gettextfromc.h"
|
|
|
|
#include "core/metrics.h"
|
|
|
|
#include "core/divelist.h"
|
2018-06-03 20:15:19 +00:00
|
|
|
#include "core/qthelper.h"
|
2018-05-11 15:25:41 +00:00
|
|
|
#include "core/subsurface-string.h"
|
2015-06-29 17:39:12 +00:00
|
|
|
#include <QIcon>
|
2017-10-03 20:16:15 +00:00
|
|
|
#include <QDebug>
|
2015-05-28 21:33:51 +00:00
|
|
|
|
|
|
|
static int nitrox_sort_value(struct dive *dive)
|
|
|
|
{
|
|
|
|
int o2, he, o2max;
|
|
|
|
get_dive_gas(dive, &o2, &he, &o2max);
|
|
|
|
return he * 1000 + o2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static QVariant dive_table_alignment(int column)
|
|
|
|
{
|
|
|
|
QVariant retVal;
|
|
|
|
switch (column) {
|
|
|
|
case DiveTripModel::DEPTH:
|
|
|
|
case DiveTripModel::DURATION:
|
|
|
|
case DiveTripModel::TEMPERATURE:
|
|
|
|
case DiveTripModel::TOTALWEIGHT:
|
|
|
|
case DiveTripModel::SAC:
|
|
|
|
case DiveTripModel::OTU:
|
|
|
|
case DiveTripModel::MAXCNS:
|
|
|
|
// Right align numeric columns
|
|
|
|
retVal = int(Qt::AlignRight | Qt::AlignVCenter);
|
|
|
|
break;
|
|
|
|
// NR needs to be left aligned becase its the indent marker for trips too
|
|
|
|
case DiveTripModel::NR:
|
|
|
|
case DiveTripModel::DATE:
|
|
|
|
case DiveTripModel::RATING:
|
|
|
|
case DiveTripModel::SUIT:
|
|
|
|
case DiveTripModel::CYLINDER:
|
|
|
|
case DiveTripModel::GAS:
|
2018-04-04 06:18:29 +00:00
|
|
|
case DiveTripModel::TAGS:
|
2016-11-21 09:26:58 +00:00
|
|
|
case DiveTripModel::PHOTOS:
|
2017-10-02 15:52:07 +00:00
|
|
|
case DiveTripModel::COUNTRY:
|
2015-05-28 21:33:51 +00:00
|
|
|
case DiveTripModel::LOCATION:
|
|
|
|
retVal = int(Qt::AlignLeft | Qt::AlignVCenter);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant TripItem::data(int column, int role) const
|
|
|
|
{
|
|
|
|
QVariant ret;
|
2015-10-06 19:08:27 +00:00
|
|
|
bool oneDayTrip=true;
|
2015-05-28 21:33:51 +00:00
|
|
|
|
|
|
|
if (role == DiveTripModel::TRIP_ROLE)
|
|
|
|
return QVariant::fromValue<void *>(trip);
|
|
|
|
|
|
|
|
if (role == DiveTripModel::SORT_ROLE)
|
|
|
|
return (qulonglong)trip->when;
|
|
|
|
|
|
|
|
if (role == Qt::DisplayRole) {
|
|
|
|
switch (column) {
|
|
|
|
case DiveTripModel::NR:
|
|
|
|
QString shownText;
|
|
|
|
struct dive *d = trip->dives;
|
|
|
|
int countShown = 0;
|
|
|
|
while (d) {
|
|
|
|
if (!d->hidden_by_filter)
|
|
|
|
countShown++;
|
2015-10-06 19:08:27 +00:00
|
|
|
oneDayTrip &= is_same_day (trip->when, d->when);
|
2015-05-28 21:33:51 +00:00
|
|
|
d = d->next;
|
|
|
|
}
|
|
|
|
if (countShown < trip->nrdives)
|
2015-10-08 09:02:42 +00:00
|
|
|
shownText = tr("(%1 shown)").arg(countShown);
|
2018-01-07 14:42:28 +00:00
|
|
|
if (!empty_string(trip->location))
|
2015-10-06 19:08:27 +00:00
|
|
|
ret = QString(trip->location) + ", " + get_trip_date_string(trip->when, trip->nrdives, oneDayTrip) + " "+ shownText;
|
2015-05-28 21:33:51 +00:00
|
|
|
else
|
2015-10-06 19:08:27 +00:00
|
|
|
ret = get_trip_date_string(trip->when, trip->nrdives, oneDayTrip) + shownText;
|
2015-05-28 21:33:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QVariant DiveItem::data(int column, int role) const
|
|
|
|
{
|
|
|
|
QVariant retVal;
|
2017-11-29 09:57:08 +00:00
|
|
|
QString icon_names[4] = {":zero",":photo-in-icon", ":photo-out-icon", ":photo-in-out-icon" };
|
2015-05-28 21:33:51 +00:00
|
|
|
struct dive *dive = get_dive_by_uniq_id(diveId);
|
|
|
|
if (!dive)
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
switch (role) {
|
|
|
|
case Qt::TextAlignmentRole:
|
|
|
|
retVal = dive_table_alignment(column);
|
|
|
|
break;
|
|
|
|
case DiveTripModel::SORT_ROLE:
|
|
|
|
Q_ASSERT(dive != NULL);
|
|
|
|
switch (column) {
|
|
|
|
case NR:
|
2017-02-17 20:47:09 +00:00
|
|
|
retVal = (qlonglong)dive->when;
|
2015-05-28 21:33:51 +00:00
|
|
|
break;
|
|
|
|
case DATE:
|
2017-02-17 20:47:09 +00:00
|
|
|
retVal = (qlonglong)dive->when;
|
2015-05-28 21:33:51 +00:00
|
|
|
break;
|
|
|
|
case RATING:
|
|
|
|
retVal = dive->rating;
|
|
|
|
break;
|
|
|
|
case DEPTH:
|
|
|
|
retVal = dive->maxdepth.mm;
|
|
|
|
break;
|
|
|
|
case DURATION:
|
|
|
|
retVal = dive->duration.seconds;
|
|
|
|
break;
|
|
|
|
case TEMPERATURE:
|
|
|
|
retVal = dive->watertemp.mkelvin;
|
|
|
|
break;
|
|
|
|
case TOTALWEIGHT:
|
|
|
|
retVal = total_weight(dive);
|
|
|
|
break;
|
|
|
|
case SUIT:
|
|
|
|
retVal = QString(dive->suit);
|
|
|
|
break;
|
|
|
|
case CYLINDER:
|
|
|
|
retVal = QString(dive->cylinder[0].type.description);
|
|
|
|
break;
|
|
|
|
case GAS:
|
|
|
|
retVal = nitrox_sort_value(dive);
|
|
|
|
break;
|
|
|
|
case SAC:
|
|
|
|
retVal = dive->sac;
|
|
|
|
break;
|
|
|
|
case OTU:
|
|
|
|
retVal = dive->otu;
|
|
|
|
break;
|
|
|
|
case MAXCNS:
|
|
|
|
retVal = dive->maxcns;
|
|
|
|
break;
|
2018-04-04 06:18:29 +00:00
|
|
|
case TAGS:
|
|
|
|
retVal = displayTags();
|
|
|
|
break;
|
2016-11-19 12:23:54 +00:00
|
|
|
case PHOTOS:
|
|
|
|
retVal = countPhotos(dive);
|
|
|
|
break;
|
2017-10-02 15:52:07 +00:00
|
|
|
case COUNTRY:
|
|
|
|
retVal = QString(get_dive_country(dive));
|
|
|
|
break;
|
2015-05-28 21:33:51 +00:00
|
|
|
case LOCATION:
|
|
|
|
retVal = QString(get_dive_location(dive));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
Q_ASSERT(dive != NULL);
|
|
|
|
switch (column) {
|
|
|
|
case NR:
|
|
|
|
retVal = dive->number;
|
|
|
|
break;
|
|
|
|
case DATE:
|
|
|
|
retVal = displayDate();
|
|
|
|
break;
|
|
|
|
case DEPTH:
|
2017-10-09 06:46:18 +00:00
|
|
|
retVal = prefs.units.show_units_table ? displayDepthWithUnit() : displayDepth();
|
2015-05-28 21:33:51 +00:00
|
|
|
break;
|
|
|
|
case DURATION:
|
|
|
|
retVal = displayDuration();
|
|
|
|
break;
|
|
|
|
case TEMPERATURE:
|
2017-10-09 06:46:18 +00:00
|
|
|
retVal = prefs.units.show_units_table ? retVal = displayTemperatureWithUnit() : displayTemperature();
|
2015-05-28 21:33:51 +00:00
|
|
|
break;
|
|
|
|
case TOTALWEIGHT:
|
2017-10-09 06:46:18 +00:00
|
|
|
retVal = prefs.units.show_units_table ? retVal = displayWeightWithUnit() : displayWeight();
|
2015-05-28 21:33:51 +00:00
|
|
|
break;
|
|
|
|
case SUIT:
|
|
|
|
retVal = QString(dive->suit);
|
|
|
|
break;
|
|
|
|
case CYLINDER:
|
|
|
|
retVal = QString(dive->cylinder[0].type.description);
|
|
|
|
break;
|
|
|
|
case SAC:
|
2017-10-09 06:46:18 +00:00
|
|
|
retVal = prefs.units.show_units_table ? retVal = displaySacWithUnit() : displaySac();
|
2015-05-28 21:33:51 +00:00
|
|
|
break;
|
|
|
|
case OTU:
|
|
|
|
retVal = dive->otu;
|
|
|
|
break;
|
|
|
|
case MAXCNS:
|
2017-10-09 06:46:18 +00:00
|
|
|
if (prefs.units.show_units_table)
|
|
|
|
retVal = QString("%1%").arg(dive->maxcns);
|
|
|
|
else
|
|
|
|
retVal = dive->maxcns;
|
2015-05-28 21:33:51 +00:00
|
|
|
break;
|
2018-04-04 06:18:29 +00:00
|
|
|
case TAGS:
|
|
|
|
retVal = displayTags();
|
|
|
|
break;
|
2016-11-19 12:23:54 +00:00
|
|
|
case PHOTOS:
|
|
|
|
break;
|
2017-10-02 15:52:07 +00:00
|
|
|
case COUNTRY:
|
|
|
|
retVal = QString(get_dive_country(dive));
|
|
|
|
break;
|
2015-05-28 21:33:51 +00:00
|
|
|
case LOCATION:
|
|
|
|
retVal = QString(get_dive_location(dive));
|
|
|
|
break;
|
|
|
|
case GAS:
|
|
|
|
const char *gas_string = get_dive_gas_string(dive);
|
|
|
|
retVal = QString(gas_string);
|
|
|
|
free((void*)gas_string);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
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:
|
|
|
|
retVal = QVariant();
|
|
|
|
break;
|
2016-11-19 12:23:54 +00:00
|
|
|
case LOCATION:
|
2015-06-29 17:39:12 +00:00
|
|
|
if (dive_has_gps_location(dive)) {
|
|
|
|
IconMetrics im = defaultIconMetrics();
|
2016-11-19 12:23:54 +00:00
|
|
|
retVal = QIcon(":globe-icon").pixmap(im.sz_small, im.sz_small);
|
2015-06-29 17:39:12 +00:00
|
|
|
}
|
2016-11-19 12:23:54 +00:00
|
|
|
break;
|
2016-11-21 09:26:58 +00:00
|
|
|
case PHOTOS:
|
|
|
|
if (dive->picture_list)
|
2016-11-19 12:23:54 +00:00
|
|
|
{
|
|
|
|
IconMetrics im = defaultIconMetrics();
|
|
|
|
retVal = QIcon(icon_names[countPhotos(dive)]).pixmap(im.sz_small, im.sz_small);
|
2016-11-21 09:26:58 +00:00
|
|
|
} // 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
|
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:
|
|
|
|
switch (column) {
|
|
|
|
case NR:
|
|
|
|
retVal = tr("#");
|
|
|
|
break;
|
|
|
|
case DATE:
|
|
|
|
retVal = tr("Date");
|
|
|
|
break;
|
|
|
|
case RATING:
|
|
|
|
retVal = tr("Rating");
|
|
|
|
break;
|
|
|
|
case DEPTH:
|
|
|
|
retVal = tr("Depth(%1)").arg((get_units()->length == units::METERS) ? tr("m") : tr("ft"));
|
|
|
|
break;
|
|
|
|
case DURATION:
|
|
|
|
retVal = tr("Duration");
|
|
|
|
break;
|
|
|
|
case TEMPERATURE:
|
2017-03-06 12:05:13 +00:00
|
|
|
retVal = tr("Temp.(%1%2)").arg(UTF8_DEGREE).arg((get_units()->temperature == units::CELSIUS) ? "C" : "F");
|
2015-05-28 21:33:51 +00:00
|
|
|
break;
|
|
|
|
case TOTALWEIGHT:
|
|
|
|
retVal = tr("Weight(%1)").arg((get_units()->weight == units::KG) ? tr("kg") : tr("lbs"));
|
|
|
|
break;
|
|
|
|
case SUIT:
|
|
|
|
retVal = tr("Suit");
|
|
|
|
break;
|
|
|
|
case CYLINDER:
|
2017-03-06 12:12:27 +00:00
|
|
|
retVal = tr("Cylinder");
|
2015-05-28 21:33:51 +00:00
|
|
|
break;
|
|
|
|
case GAS:
|
|
|
|
retVal = tr("Gas");
|
|
|
|
break;
|
|
|
|
case SAC:
|
|
|
|
const char *unit;
|
|
|
|
get_volume_units(0, NULL, &unit);
|
|
|
|
retVal = tr("SAC(%1)").arg(QString(unit).append(tr("/min")));
|
|
|
|
break;
|
|
|
|
case OTU:
|
|
|
|
retVal = tr("OTU");
|
|
|
|
break;
|
|
|
|
case MAXCNS:
|
2017-03-06 12:27:39 +00:00
|
|
|
retVal = tr("Max. CNS");
|
2015-05-28 21:33:51 +00:00
|
|
|
break;
|
2018-04-04 06:18:29 +00:00
|
|
|
case TAGS:
|
|
|
|
retVal = tr("Tags");
|
|
|
|
break;
|
2016-11-19 12:23:54 +00:00
|
|
|
case PHOTOS:
|
2018-07-13 18:46:45 +00:00
|
|
|
retVal = tr("Media before/during/after dive");
|
2016-11-19 12:23:54 +00:00
|
|
|
break;
|
2017-10-02 15:52:07 +00:00
|
|
|
case COUNTRY:
|
|
|
|
retVal = tr("Country");
|
|
|
|
break;
|
2015-05-28 21:33:51 +00:00
|
|
|
case LOCATION:
|
|
|
|
retVal = tr("Location");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (role == DiveTripModel::STAR_ROLE) {
|
|
|
|
Q_ASSERT(dive != NULL);
|
|
|
|
retVal = dive->rating;
|
|
|
|
}
|
|
|
|
if (role == DiveTripModel::DIVE_ROLE) {
|
|
|
|
retVal = QVariant::fromValue<void *>(dive);
|
|
|
|
}
|
|
|
|
if (role == DiveTripModel::DIVE_IDX) {
|
|
|
|
Q_ASSERT(dive != NULL);
|
|
|
|
retVal = get_divenr(dive);
|
|
|
|
}
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::ItemFlags DiveItem::flags(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
if (index.column() == NR) {
|
|
|
|
return TreeItem::flags(index) | Qt::ItemIsEditable;
|
|
|
|
}
|
|
|
|
return TreeItem::flags(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DiveItem::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
|
|
{
|
|
|
|
if (role != Qt::EditRole)
|
|
|
|
return false;
|
|
|
|
if (index.column() != NR)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
int v = value.toInt();
|
|
|
|
if (v == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
struct dive *d;
|
|
|
|
for_each_dive (i, d) {
|
|
|
|
if (d->number == v)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
d = get_dive_by_uniq_id(diveId);
|
|
|
|
d->number = value.toInt();
|
|
|
|
mark_divelist_changed(true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString DiveItem::displayDate() const
|
|
|
|
{
|
|
|
|
struct dive *dive = get_dive_by_uniq_id(diveId);
|
|
|
|
return get_dive_date_string(dive->when);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString DiveItem::displayDepth() const
|
|
|
|
{
|
|
|
|
struct dive *dive = get_dive_by_uniq_id(diveId);
|
|
|
|
return get_depth_string(dive->maxdepth);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString DiveItem::displayDepthWithUnit() const
|
|
|
|
{
|
|
|
|
struct dive *dive = get_dive_by_uniq_id(diveId);
|
|
|
|
return get_depth_string(dive->maxdepth, true);
|
|
|
|
}
|
|
|
|
|
2016-11-19 12:23:54 +00:00
|
|
|
int DiveItem::countPhotos(dive *dive) const
|
2016-11-21 09:26:58 +00:00
|
|
|
{ // 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
|
2017-09-30 16:00:34 +00:00
|
|
|
int diveTotaltime = dive_endtime(dive) - dive->when; // taken during the dive, not before/after.
|
2016-11-19 12:23:54 +00:00
|
|
|
int pic_offset, icon_index = 0;
|
2016-11-21 09:26:58 +00:00
|
|
|
FOR_EACH_PICTURE (dive) { // Step through each of the pictures for this dive:
|
|
|
|
pic_offset = picture->offset.seconds;
|
2017-09-30 16:00:34 +00:00
|
|
|
if ((pic_offset < -bufperiod) | (pic_offset > diveTotaltime+bufperiod)) {
|
2016-11-19 12:23:54 +00:00
|
|
|
icon_index |= 0x02; // If picture is before/after the dive
|
|
|
|
} // then set the appropriate bit ...
|
|
|
|
else {
|
|
|
|
icon_index |= 0x01; // else set the bit for picture during the dive
|
|
|
|
}
|
2016-11-21 09:26:58 +00:00
|
|
|
}
|
2016-11-19 12:23:54 +00:00
|
|
|
return icon_index; // return value: 0=no pictures; 1=pictures during dive;
|
|
|
|
} // 2=pictures before/after; 3=pictures during as well as before/after
|
|
|
|
|
2015-05-28 21:33:51 +00:00
|
|
|
QString DiveItem::displayDuration() const
|
|
|
|
{
|
|
|
|
struct dive *dive = get_dive_by_uniq_id(diveId);
|
2017-10-09 06:46:18 +00:00
|
|
|
if (prefs.units.show_units_table)
|
|
|
|
return get_dive_duration_string(dive->duration.seconds, tr("h"), tr("min"), "", ":", dive->dc.divemode == FREEDIVE);
|
|
|
|
else
|
|
|
|
return get_dive_duration_string(dive->duration.seconds, "", "", "", ":", dive->dc.divemode == FREEDIVE);
|
2015-05-28 21:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString DiveItem::displayTemperature() const
|
|
|
|
{
|
|
|
|
struct dive *dive = get_dive_by_uniq_id(diveId);
|
|
|
|
if (!dive->watertemp.mkelvin)
|
2018-02-27 06:06:39 +00:00
|
|
|
return QString();
|
2017-10-09 06:46:18 +00:00
|
|
|
return get_temperature_string(dive->watertemp, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString DiveItem::displayTemperatureWithUnit() const
|
|
|
|
{
|
|
|
|
struct dive *dive = get_dive_by_uniq_id(diveId);
|
|
|
|
if (!dive->watertemp.mkelvin)
|
2018-02-27 06:06:39 +00:00
|
|
|
return QString();
|
2017-10-09 06:46:18 +00:00
|
|
|
return get_temperature_string(dive->watertemp, true);
|
2015-05-28 21:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString DiveItem::displaySac() const
|
|
|
|
{
|
|
|
|
struct dive *dive = get_dive_by_uniq_id(diveId);
|
2018-02-19 20:55:18 +00:00
|
|
|
if (!dive->sac)
|
|
|
|
return QString();
|
|
|
|
return get_volume_string(dive->sac, false);
|
2015-05-28 21:33:51 +00:00
|
|
|
}
|
|
|
|
|
2017-10-09 06:46:18 +00:00
|
|
|
QString DiveItem::displaySacWithUnit() const
|
|
|
|
{
|
|
|
|
struct dive *dive = get_dive_by_uniq_id(diveId);
|
2018-02-19 20:55:18 +00:00
|
|
|
if (!dive->sac)
|
|
|
|
return QString();
|
|
|
|
return get_volume_string(dive->sac, true).append(tr("/min"));
|
2017-10-09 06:46:18 +00:00
|
|
|
}
|
|
|
|
|
2015-05-28 21:33:51 +00:00
|
|
|
QString DiveItem::displayWeight() const
|
|
|
|
{
|
2018-02-27 06:06:39 +00:00
|
|
|
return weight_string(weight());
|
2017-10-09 06:46:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString DiveItem::displayWeightWithUnit() const
|
|
|
|
{
|
2018-02-27 06:06:39 +00:00
|
|
|
return weight_string(weight()) + ((get_units()->weight == units::KG) ? tr("kg") : tr("lbs"));
|
2015-05-28 21:33:51 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 06:18:29 +00:00
|
|
|
QString DiveItem::displayTags() const
|
|
|
|
{
|
|
|
|
struct dive *dive = get_dive_by_uniq_id(diveId);
|
2018-04-09 08:09:34 +00:00
|
|
|
return get_taglist_string(dive->tag_list);
|
2018-04-04 06:18:29 +00:00
|
|
|
}
|
|
|
|
|
2015-05-28 21:33:51 +00:00
|
|
|
int DiveItem::weight() const
|
|
|
|
{
|
|
|
|
struct dive *dive = get_dive_by_uniq_id(diveId);
|
|
|
|
weight_t tw = { total_weight(dive) };
|
|
|
|
return tw.grams;
|
|
|
|
}
|
|
|
|
|
2015-06-22 13:42:02 +00:00
|
|
|
DiveTripModel::DiveTripModel(QObject *parent) :
|
|
|
|
TreeModel(parent),
|
|
|
|
currentLayout(TREE)
|
2015-05-28 21:33:51 +00:00
|
|
|
{
|
|
|
|
columns = COLUMNS;
|
2017-10-03 20:16:15 +00:00
|
|
|
// setup the default width of columns (px)
|
|
|
|
columnWidthMap = QVector<int>(COLUMNS);
|
|
|
|
// pre-fill with 50px; the rest are explicit
|
|
|
|
for(int i = 0; i < COLUMNS; i++)
|
|
|
|
columnWidthMap[i] = 50;
|
|
|
|
columnWidthMap[NR] = 70;
|
|
|
|
columnWidthMap[DATE] = 140;
|
|
|
|
columnWidthMap[RATING] = 90;
|
|
|
|
columnWidthMap[SUIT] = 70;
|
|
|
|
columnWidthMap[SAC] = 70;
|
|
|
|
columnWidthMap[PHOTOS] = 5;
|
|
|
|
columnWidthMap[LOCATION] = 500;
|
2015-05-28 21:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Qt::ItemFlags DiveTripModel::flags(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
TripItem *item = static_cast<TripItem *>(index.internalPointer());
|
|
|
|
return item->flags(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant DiveTripModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
|
|
{
|
|
|
|
QVariant ret;
|
|
|
|
if (orientation == Qt::Vertical)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
switch (role) {
|
|
|
|
case Qt::TextAlignmentRole:
|
|
|
|
ret = dive_table_alignment(section);
|
|
|
|
break;
|
|
|
|
case Qt::FontRole:
|
|
|
|
ret = defaultModelFont();
|
|
|
|
break;
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
switch (section) {
|
|
|
|
case NR:
|
|
|
|
ret = tr("#");
|
|
|
|
break;
|
|
|
|
case DATE:
|
|
|
|
ret = tr("Date");
|
|
|
|
break;
|
|
|
|
case RATING:
|
|
|
|
ret = tr("Rating");
|
|
|
|
break;
|
|
|
|
case DEPTH:
|
|
|
|
ret = tr("Depth");
|
|
|
|
break;
|
|
|
|
case DURATION:
|
|
|
|
ret = tr("Duration");
|
|
|
|
break;
|
|
|
|
case TEMPERATURE:
|
2017-03-06 12:05:13 +00:00
|
|
|
ret = tr("Temp.");
|
2015-05-28 21:33:51 +00:00
|
|
|
break;
|
|
|
|
case TOTALWEIGHT:
|
|
|
|
ret = tr("Weight");
|
|
|
|
break;
|
|
|
|
case SUIT:
|
|
|
|
ret = tr("Suit");
|
|
|
|
break;
|
|
|
|
case CYLINDER:
|
2017-03-06 12:12:27 +00:00
|
|
|
ret = tr("Cylinder");
|
2015-05-28 21:33:51 +00:00
|
|
|
break;
|
|
|
|
case GAS:
|
|
|
|
ret = tr("Gas");
|
|
|
|
break;
|
|
|
|
case SAC:
|
|
|
|
ret = tr("SAC");
|
|
|
|
break;
|
|
|
|
case OTU:
|
|
|
|
ret = tr("OTU");
|
|
|
|
break;
|
|
|
|
case MAXCNS:
|
|
|
|
ret = tr("Max CNS");
|
|
|
|
break;
|
2018-04-04 06:18:29 +00:00
|
|
|
case TAGS:
|
|
|
|
ret = tr("Tags");
|
|
|
|
break;
|
2016-11-19 12:23:54 +00:00
|
|
|
case PHOTOS:
|
2018-07-13 18:46:45 +00:00
|
|
|
ret = tr("Media");
|
2016-11-19 12:23:54 +00:00
|
|
|
break;
|
2017-10-02 15:52:07 +00:00
|
|
|
case COUNTRY:
|
|
|
|
ret = tr("Country");
|
|
|
|
break;
|
2015-05-28 21:33:51 +00:00
|
|
|
case LOCATION:
|
|
|
|
ret = tr("Location");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Qt::ToolTipRole:
|
|
|
|
switch (section) {
|
|
|
|
case NR:
|
|
|
|
ret = tr("#");
|
|
|
|
break;
|
|
|
|
case DATE:
|
|
|
|
ret = tr("Date");
|
|
|
|
break;
|
|
|
|
case RATING:
|
|
|
|
ret = tr("Rating");
|
|
|
|
break;
|
|
|
|
case DEPTH:
|
|
|
|
ret = tr("Depth(%1)").arg((get_units()->length == units::METERS) ? tr("m") : tr("ft"));
|
|
|
|
break;
|
|
|
|
case DURATION:
|
|
|
|
ret = tr("Duration");
|
|
|
|
break;
|
|
|
|
case TEMPERATURE:
|
2017-03-06 12:05:13 +00:00
|
|
|
ret = tr("Temp.(%1%2)").arg(UTF8_DEGREE).arg((get_units()->temperature == units::CELSIUS) ? "C" : "F");
|
2015-05-28 21:33:51 +00:00
|
|
|
break;
|
|
|
|
case TOTALWEIGHT:
|
|
|
|
ret = tr("Weight(%1)").arg((get_units()->weight == units::KG) ? tr("kg") : tr("lbs"));
|
|
|
|
break;
|
|
|
|
case SUIT:
|
|
|
|
ret = tr("Suit");
|
|
|
|
break;
|
|
|
|
case CYLINDER:
|
2017-03-06 12:12:27 +00:00
|
|
|
ret = tr("Cylinder");
|
2015-05-28 21:33:51 +00:00
|
|
|
break;
|
|
|
|
case GAS:
|
|
|
|
ret = tr("Gas");
|
|
|
|
break;
|
|
|
|
case SAC:
|
|
|
|
const char *unit;
|
|
|
|
get_volume_units(0, NULL, &unit);
|
|
|
|
ret = tr("SAC(%1)").arg(QString(unit).append(tr("/min")));
|
|
|
|
break;
|
|
|
|
case OTU:
|
|
|
|
ret = tr("OTU");
|
|
|
|
break;
|
|
|
|
case MAXCNS:
|
|
|
|
ret = tr("Max CNS");
|
|
|
|
break;
|
2018-04-04 06:18:29 +00:00
|
|
|
case TAGS:
|
|
|
|
ret = tr("Tags");
|
|
|
|
break;
|
2016-11-19 12:23:54 +00:00
|
|
|
case PHOTOS:
|
2018-07-13 18:46:45 +00:00
|
|
|
ret = tr("Media before/during/after dive");
|
2016-11-19 12:23:54 +00:00
|
|
|
break;
|
2015-05-28 21:33:51 +00:00
|
|
|
case LOCATION:
|
|
|
|
ret = tr("Location");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveTripModel::setupModelData()
|
|
|
|
{
|
|
|
|
int i = dive_table.nr;
|
|
|
|
|
|
|
|
if (rowCount()) {
|
|
|
|
beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (autogroup)
|
|
|
|
autogroup_dives();
|
|
|
|
dive_table.preexisting = dive_table.nr;
|
|
|
|
while (--i >= 0) {
|
|
|
|
struct dive *dive = get_dive(i);
|
|
|
|
update_cylinder_related_info(dive);
|
|
|
|
dive_trip_t *trip = dive->divetrip;
|
|
|
|
|
|
|
|
DiveItem *diveItem = new DiveItem();
|
|
|
|
diveItem->diveId = dive->id;
|
|
|
|
|
|
|
|
if (!trip || currentLayout == LIST) {
|
|
|
|
diveItem->parent = rootItem;
|
|
|
|
rootItem->children.push_back(diveItem);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (currentLayout == LIST)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!trips.keys().contains(trip)) {
|
|
|
|
TripItem *tripItem = new TripItem();
|
|
|
|
tripItem->trip = trip;
|
|
|
|
tripItem->parent = rootItem;
|
|
|
|
tripItem->children.push_back(diveItem);
|
|
|
|
trips[trip] = tripItem;
|
|
|
|
rootItem->children.push_back(tripItem);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
TripItem *tripItem = trips[trip];
|
|
|
|
tripItem->children.push_back(diveItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rowCount()) {
|
|
|
|
beginInsertRows(QModelIndex(), 0, rowCount() - 1);
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DiveTripModel::Layout DiveTripModel::layout() const
|
|
|
|
{
|
|
|
|
return currentLayout;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveTripModel::setLayout(DiveTripModel::Layout layout)
|
|
|
|
{
|
|
|
|
currentLayout = layout;
|
|
|
|
setupModelData();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DiveTripModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
|
|
{
|
|
|
|
TreeItem *item = static_cast<TreeItem *>(index.internalPointer());
|
|
|
|
DiveItem *diveItem = dynamic_cast<DiveItem *>(item);
|
|
|
|
if (!diveItem)
|
|
|
|
return false;
|
|
|
|
return diveItem->setData(index, value, role);
|
|
|
|
}
|
2017-10-03 20:16:15 +00:00
|
|
|
|
|
|
|
int DiveTripModel::columnWidth(int column)
|
|
|
|
{
|
|
|
|
if (column > COLUMNS - 1 || column < 0) {
|
|
|
|
qWarning() << "DiveTripModel::columnWidth(): not a valid column index -" << column;
|
|
|
|
return 50;
|
|
|
|
}
|
|
|
|
return columnWidthMap[column];
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveTripModel::setColumnWidth(int column, int width)
|
|
|
|
{
|
|
|
|
if (column > COLUMNS - 1 || column < 0) {
|
|
|
|
qWarning() << "DiveTripModel::setColumnWidth(): not a valid column index -" << column;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
columnWidthMap[column] = width;
|
|
|
|
}
|