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());
|
2016-01-07 18:01:24 +00:00
|
|
|
m_dives.append(new DiveObjectHelper(d));
|
2015-06-09 19:20:44 +00:00
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
2016-01-27 19:27:41 +00:00
|
|
|
void DiveListModel::insertDive(int i, DiveObjectHelper *newDive)
|
|
|
|
{
|
|
|
|
beginInsertRows(QModelIndex(), i, i);
|
|
|
|
m_dives.insert(i, newDive);
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveListModel::removeDive(int i)
|
|
|
|
{
|
|
|
|
beginRemoveRows(QModelIndex(), i, i);
|
|
|
|
m_dives.removeAt(i);
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
|
2015-12-08 06:23:09 +00:00
|
|
|
void DiveListModel::updateDive(dive *d)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < m_dives.count(); i++) {
|
2016-01-07 18:01:24 +00:00
|
|
|
if (m_dives.at(i)->id() == d->id) {
|
|
|
|
DiveObjectHelper *newDive = new DiveObjectHelper(d);
|
2016-01-27 19:27:41 +00:00
|
|
|
removeDive(i);
|
|
|
|
insertDive(i, newDive);
|
2015-12-08 06:23:09 +00:00
|
|
|
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);
|
2016-01-07 18:01:24 +00:00
|
|
|
qDeleteAll(m_dives);
|
2015-12-03 20:22:18 +00:00
|
|
|
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();
|
|
|
|
|
2016-01-07 18:01:24 +00:00
|
|
|
DiveObjectHelper *curr_dive = m_dives[index.row()];
|
|
|
|
switch(role) {
|
|
|
|
case DiveRole: return QVariant::fromValue<QObject*>(curr_dive);
|
|
|
|
case DiveDateRole: return (qlonglong)curr_dive->timestamp();
|
|
|
|
}
|
2015-06-09 19:20:44 +00:00
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray> DiveListModel::roleNames() const
|
|
|
|
{
|
|
|
|
QHash<int, QByteArray> roles;
|
2016-01-07 18:01:24 +00:00
|
|
|
roles[DiveRole] = "dive";
|
|
|
|
roles[DiveDateRole] = "date";
|
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;
|
|
|
|
}
|
2016-01-07 18:01:24 +00:00
|
|
|
|
|
|
|
DiveObjectHelper* DiveListModel::at(int i){
|
|
|
|
return m_dives.at(i);
|
2016-01-27 19:27:41 +00:00
|
|
|
}
|