2015-06-09 19:20:44 +00:00
|
|
|
#include "divelistmodel.h"
|
|
|
|
#include "helpers.h"
|
2015-12-27 05:34:45 +00:00
|
|
|
#include <QDateTime>
|
2015-06-09 19:20:44 +00:00
|
|
|
|
|
|
|
DiveListModel *DiveListModel::m_instance = NULL;
|
|
|
|
|
|
|
|
DiveListModel::DiveListModel(QObject *parent) : QAbstractListModel(parent)
|
|
|
|
{
|
|
|
|
m_instance = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveListModel::addDive(dive *d)
|
|
|
|
{
|
|
|
|
beginInsertRows(QModelIndex(), rowCount(), rowCount());
|
2015-08-18 09:03:30 +00:00
|
|
|
m_dives.append(Dive(d));
|
2015-06-09 19:20:44 +00:00
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
2015-12-08 06:23:09 +00:00
|
|
|
void DiveListModel::updateDive(dive *d)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < m_dives.count(); i++) {
|
|
|
|
if (m_dives.at(i).id() == d->id) {
|
|
|
|
Dive newDive(d);
|
|
|
|
m_dives.replace(i, newDive);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-30 18:09:46 +00:00
|
|
|
void DiveListModel::clear()
|
|
|
|
{
|
2015-12-03 20:22:18 +00:00
|
|
|
if (m_dives.count()) {
|
|
|
|
beginRemoveRows(QModelIndex(), 0, m_dives.count() - 1);
|
|
|
|
m_dives.clear();
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
2015-11-30 18:09:46 +00:00
|
|
|
}
|
|
|
|
|
2015-06-09 19:20:44 +00:00
|
|
|
int DiveListModel::rowCount(const QModelIndex &) const
|
|
|
|
{
|
|
|
|
return m_dives.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant DiveListModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if(index.row() < 0 || index.row() > m_dives.count())
|
|
|
|
return QVariant();
|
|
|
|
|
2015-08-18 09:03:30 +00:00
|
|
|
const Dive &dive = m_dives[index.row()];
|
2015-06-09 19:20:44 +00:00
|
|
|
|
|
|
|
if (role == DiveNumberRole)
|
2015-08-18 09:03:30 +00:00
|
|
|
return dive.number();
|
2015-06-11 06:39:32 +00:00
|
|
|
else if (role == DiveTripRole)
|
|
|
|
return dive.trip();
|
2015-06-09 19:20:44 +00:00
|
|
|
else if (role == DiveDateRole)
|
2015-08-18 09:03:30 +00:00
|
|
|
return (qlonglong)dive.timestamp();
|
2015-07-24 18:17:48 +00:00
|
|
|
else if (role == DiveDateStringRole)
|
2015-08-18 09:03:30 +00:00
|
|
|
return dive.date() + " " + dive.time();
|
2015-06-09 19:20:44 +00:00
|
|
|
else if (role == DiveRatingRole)
|
2015-08-18 09:03:30 +00:00
|
|
|
return QString::number(dive.rating());
|
2015-06-09 19:20:44 +00:00
|
|
|
else if (role == DiveDepthRole)
|
|
|
|
return dive.depth();
|
|
|
|
else if (role == DiveDurationRole)
|
|
|
|
return dive.duration();
|
2015-06-18 06:12:52 +00:00
|
|
|
else if (role == DiveAirTemperatureRole)
|
2015-08-18 09:03:30 +00:00
|
|
|
return dive.airTemp();
|
2015-06-18 06:12:52 +00:00
|
|
|
else if (role == DiveWaterTemperatureRole)
|
2015-08-18 09:03:30 +00:00
|
|
|
return dive.waterTemp();
|
2015-06-09 19:20:44 +00:00
|
|
|
else if (role == DiveWeightRole)
|
2015-11-16 23:37:16 +00:00
|
|
|
return dive.weight(0);
|
2015-06-09 19:20:44 +00:00
|
|
|
else if (role == DiveSuitRole)
|
|
|
|
return dive.suit();
|
|
|
|
else if (role == DiveCylinderRole)
|
2015-11-16 23:37:16 +00:00
|
|
|
return dive.cylinder(0);
|
2015-06-09 19:20:44 +00:00
|
|
|
else if (role == DiveGasRole)
|
|
|
|
return dive.gas();
|
|
|
|
else if (role == DiveSacRole)
|
|
|
|
return dive.sac();
|
|
|
|
else if (role == DiveLocationRole)
|
|
|
|
return dive.location();
|
2015-12-26 21:19:43 +00:00
|
|
|
else if (role == DiveGPSRole)
|
|
|
|
return dive.gps();
|
2015-06-10 18:38:14 +00:00
|
|
|
else if (role == DiveNotesRole)
|
|
|
|
return dive.notes();
|
2015-06-18 06:12:52 +00:00
|
|
|
else if (role == DiveBuddyRole)
|
|
|
|
return dive.buddy();
|
|
|
|
else if (role == DiveMasterRole)
|
|
|
|
return dive.divemaster();
|
2015-07-13 10:06:45 +00:00
|
|
|
else if (role == DiveIdRole)
|
2015-08-18 09:03:30 +00:00
|
|
|
return QString::number(dive.id());
|
2015-06-09 19:20:44 +00:00
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray> DiveListModel::roleNames() const
|
|
|
|
{
|
|
|
|
QHash<int, QByteArray> roles;
|
|
|
|
roles[DiveNumberRole] = "diveNumber";
|
2015-06-11 06:39:32 +00:00
|
|
|
roles[DiveTripRole] = "trip";
|
2015-07-24 18:17:48 +00:00
|
|
|
roles[DiveDateStringRole] = "date";
|
2015-06-09 19:20:44 +00:00
|
|
|
roles[DiveRatingRole] = "rating";
|
|
|
|
roles[DiveDepthRole] = "depth";
|
|
|
|
roles[DiveDurationRole] = "duration";
|
2015-06-18 06:12:52 +00:00
|
|
|
roles[DiveAirTemperatureRole] = "airtemp";
|
|
|
|
roles[DiveWaterTemperatureRole] = "watertemp";
|
2015-06-09 19:20:44 +00:00
|
|
|
roles[DiveWeightRole] = "weight";
|
|
|
|
roles[DiveSuitRole] = "suit";
|
|
|
|
roles[DiveCylinderRole] = "cylinder";
|
|
|
|
roles[DiveGasRole] = "gas";
|
|
|
|
roles[DiveSacRole] = "sac";
|
|
|
|
roles[DiveLocationRole] = "location";
|
2015-12-26 21:19:43 +00:00
|
|
|
roles[DiveGPSRole] = "gps";
|
2015-06-10 18:38:14 +00:00
|
|
|
roles[DiveNotesRole] = "notes";
|
2015-06-18 06:12:52 +00:00
|
|
|
roles[DiveBuddyRole] = "buddy";
|
|
|
|
roles[DiveMasterRole] = "divemaster";
|
2015-07-13 10:06:45 +00:00
|
|
|
roles[DiveIdRole] = "id";
|
2015-06-09 19:20:44 +00:00
|
|
|
|
|
|
|
return roles;
|
|
|
|
}
|
|
|
|
|
2015-12-27 05:34:45 +00:00
|
|
|
// create a new dive. set the current time and add it to the end of the dive list
|
2015-12-27 05:37:18 +00:00
|
|
|
QString DiveListModel::startAddDive()
|
2015-08-12 12:01:39 +00:00
|
|
|
{
|
2015-08-16 08:57:18 +00:00
|
|
|
struct dive *d;
|
|
|
|
d = alloc_dive();
|
2015-12-27 05:34:45 +00:00
|
|
|
d->when = QDateTime::currentMSecsSinceEpoch() / 1000L + gettimezoneoffset();
|
|
|
|
struct dive *pd = get_dive(dive_table.nr - 1);
|
|
|
|
int nr = 1;
|
|
|
|
if (pd && pd->number > 0)
|
|
|
|
nr = pd->number + 1;
|
|
|
|
d->number = nr;
|
2016-01-01 08:32:30 +00:00
|
|
|
d->dc.model = strdup("manually added dive");
|
2015-12-27 05:34:45 +00:00
|
|
|
add_single_dive(-1, d);
|
2015-08-16 08:57:18 +00:00
|
|
|
addDive(d);
|
2015-12-27 05:37:18 +00:00
|
|
|
return QString::number(d->id);
|
2015-08-12 12:01:39 +00:00
|
|
|
}
|
|
|
|
|
2015-06-09 19:20:44 +00:00
|
|
|
DiveListModel *DiveListModel::instance()
|
|
|
|
{
|
|
|
|
return m_instance;
|
|
|
|
}
|