2015-06-09 19:20:44 +00:00
|
|
|
#include "divelistmodel.h"
|
|
|
|
#include "helpers.h"
|
|
|
|
|
|
|
|
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-11-30 18:09:46 +00:00
|
|
|
void DiveListModel::clear()
|
|
|
|
{
|
|
|
|
beginRemoveRows(QModelIndex(), 0, m_dives.count() - 1);
|
|
|
|
m_dives.clear();
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
|
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-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-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-08-16 08:57:18 +00:00
|
|
|
void DiveListModel::startAddDive()
|
2015-08-12 12:01:39 +00:00
|
|
|
{
|
2015-08-16 08:57:18 +00:00
|
|
|
struct dive *d;
|
|
|
|
d = alloc_dive();
|
|
|
|
add_single_dive(get_divenr(d), d);
|
|
|
|
addDive(d);
|
2015-08-12 12:01:39 +00:00
|
|
|
}
|
|
|
|
|
2015-06-09 19:20:44 +00:00
|
|
|
DiveListModel *DiveListModel::instance()
|
|
|
|
{
|
|
|
|
return m_instance;
|
|
|
|
}
|