1dc7d58685d1e61e3a06921934b10d56c6ef570b
[harbour-pedalo.git] / src / journeymodel.cpp
1 #include "journeymodel.h"
2 #include <QDebug>
3
4 JourneyModel::JourneyModel(QObject *parent) : QAbstractListModel(parent) {
5 roles[StartRole] = "start";
6 roles[DurationRole] = "duration";
7 roles[OvertookRole] = "overtook";
8 roles[OvertakenByRole] = "overtakenby";
9 }
10
11 QHash<int, QByteArray> JourneyModel::roleNames() const {
12 return roles;
13 }
14
15 void JourneyModel::addJourney(const Journey &journey)
16 {
17 beginInsertRows(QModelIndex(), rowCount(), rowCount());
18 journeys << journey;
19 endInsertRows();
20 }
21
22 void JourneyModel::addJourney(QDateTime start, quint32 duration, quint32 overtook, quint32 overtakenby) {
23 quint64 startepoch = start.toMSecsSinceEpoch();
24 addJourney(Journey(startepoch, duration, overtook, overtakenby));
25 }
26
27 void JourneyModel::editJourney(quint32 index, QDateTime start, quint32 duration, quint32 overtook, quint32 overtakenby) {
28 quint64 startepoch = start.toMSecsSinceEpoch();
29 journeys.replace(index, Journey(startepoch, duration, overtook, overtakenby));
30 emit dataChanged(createIndex(index, 0), createIndex(index, 0));
31 }
32
33 void JourneyModel::deleteJourney(quint32 index) {
34 beginRemoveRows(QModelIndex(), index, index);
35 journeys.removeAt(index);
36 endRemoveRows();
37 }
38
39 int JourneyModel::rowCount(const QModelIndex & parent) const {
40 Q_UNUSED(parent)
41 return journeys.count();
42 }
43
44 QVariant JourneyModel::data(const QModelIndex & index, int role) const {
45 if (index.row() < 0 || index.row() > journeys.count())
46 return QVariant();
47
48 const Journey &journey = journeys[index.row()];
49 if (role == StartRole)
50 return journey.getStart();
51 else if (role == DurationRole)
52 return journey.getDuration();
53 else if (role == OvertookRole)
54 return journey.getOvertook();
55 else if (role == OvertakenByRole)
56 return journey.getOvertakenBy();
57 return QVariant();
58 }
59
60 void JourneyModel::clear() {
61 journeys.clear();
62 }
63
64 void JourneyModel::exportToFile(QFile & file) {
65 if (file.open(QIODevice::WriteOnly)) {
66 QTextStream out(&file);
67 for (QList<Journey>::iterator journeyIter = journeys.begin(); journeyIter != journeys.end(); journeyIter++) {
68 out << journeyIter->getStart() << ",";
69 out << journeyIter->getDuration() << ",";
70 out << journeyIter->getOvertook() << ",";
71 out << journeyIter->getOvertakenBy() << endl;
72 }
73 file.close();
74 }
75 else {
76 qDebug() << "File failed to export";
77 }
78 }
79
80 void JourneyModel::importFromFile(QFile & file) {
81 if (file.open(QIODevice::ReadOnly)) {
82 QTextStream in(&file);
83 while (!in.atEnd()) {
84 QStringList data;
85 quint64 start;
86 qint32 duration = 0;
87 qint32 overtook = 0;
88 qint32 overtakenby = 0;
89
90 data = in.readLine().split(",");
91 if (data.length() == 4) {
92 start = data[0].toLongLong();
93 duration = data[1].toLongLong();
94 overtook = data[2].toLongLong();
95 overtakenby = data[3].toLongLong();
96
97 addJourney(Journey(start, duration, overtook, overtakenby));
98 }
99 }
100 file.close();
101 }
102 }
103
104
105 QDate JourneyModel::epochToDate(quint64 epoch) {
106 QDateTime date;
107
108 date.setMSecsSinceEpoch(epoch);
109 return date.date();
110 }
111
112 QTime JourneyModel::epochToTime(quint64 epoch) {
113 QDateTime date;
114
115 date.setMSecsSinceEpoch(epoch);
116 return date.time();
117 }
118
119 QDateTime JourneyModel::epochToDateTime(quint64 epoch) {
120 QDateTime date;
121
122 date.setMSecsSinceEpoch(epoch);
123 return date;
124 }
125
126 QList<Journey> const & JourneyModel::getData() const {
127 return journeys;
128 }