2919d83388c47fe3cd520936c4e8d12f8d47c12d
[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 // Find position to add it
18 QList<Journey>::const_iterator it = journeys.constBegin();
19
20 unsigned int position = 0;
21 while ((it != journeys.constEnd()) && (journey.getStart() < (*it).getStart())) {
22 position++;
23 it++;
24 }
25
26 beginInsertRows(QModelIndex(), position, position);
27 journeys.insert(position, journey);
28 endInsertRows();
29 }
30
31 void JourneyModel::addJourney(QDateTime start, quint32 duration, quint32 overtook, quint32 overtakenby) {
32 quint64 startepoch = start.toMSecsSinceEpoch();
33 addJourney(Journey(startepoch, duration, overtook, overtakenby));
34 }
35
36 void JourneyModel::editJourney(quint32 index, QDateTime start, quint32 duration, quint32 overtook, quint32 overtakenby) {
37 quint64 startepoch = start.toMSecsSinceEpoch();
38 journeys.replace(index, Journey(startepoch, duration, overtook, overtakenby));
39 emit dataChanged(createIndex(index, 0), createIndex(index, 0));
40 }
41
42 void JourneyModel::deleteJourney(quint32 index) {
43 beginRemoveRows(QModelIndex(), index, index);
44 journeys.removeAt(index);
45 endRemoveRows();
46 }
47
48 int JourneyModel::rowCount(const QModelIndex & parent) const {
49 Q_UNUSED(parent)
50 return journeys.count();
51 }
52
53 QVariant JourneyModel::data(const QModelIndex & index, int role) const {
54 if (index.row() < 0 || index.row() > journeys.count())
55 return QVariant();
56
57 const Journey &journey = journeys[index.row()];
58 if (role == StartRole)
59 return journey.getStart();
60 else if (role == DurationRole)
61 return journey.getDuration();
62 else if (role == OvertookRole)
63 return journey.getOvertook();
64 else if (role == OvertakenByRole)
65 return journey.getOvertakenBy();
66 return QVariant();
67 }
68
69 void JourneyModel::clear() {
70 journeys.clear();
71 }
72
73 void JourneyModel::exportToFile(QFile & file) {
74 if (file.open(QIODevice::WriteOnly)) {
75 QTextStream out(&file);
76 for (QList<Journey>::iterator journeyIter = journeys.begin(); journeyIter != journeys.end(); journeyIter++) {
77 out << journeyIter->getStart() << ",";
78 out << journeyIter->getDuration() << ",";
79 out << journeyIter->getOvertook() << ",";
80 out << journeyIter->getOvertakenBy() << endl;
81 }
82 file.close();
83 }
84 else {
85 qDebug() << "File failed to export";
86 }
87 }
88
89 void JourneyModel::importFromFile(QFile & file) {
90 if (file.open(QIODevice::ReadOnly)) {
91 QTextStream in(&file);
92 while (!in.atEnd()) {
93 QStringList data;
94 quint64 start;
95 qint32 duration = 0;
96 qint32 overtook = 0;
97 qint32 overtakenby = 0;
98
99 data = in.readLine().split(",");
100 if (data.length() == 4) {
101 start = data[0].toLongLong();
102 duration = data[1].toLongLong();
103 overtook = data[2].toLongLong();
104 overtakenby = data[3].toLongLong();
105
106 addJourney(Journey(start, duration, overtook, overtakenby));
107 }
108 }
109 file.close();
110 }
111 }
112
113
114 QDate JourneyModel::epochToDate(quint64 epoch) {
115 QDateTime date;
116
117 date.setMSecsSinceEpoch(epoch);
118 return date.date();
119 }
120
121 QTime JourneyModel::epochToTime(quint64 epoch) {
122 QDateTime date;
123
124 date.setMSecsSinceEpoch(epoch);
125 return date.time();
126 }
127
128 QDateTime JourneyModel::epochToDateTime(quint64 epoch) {
129 QDateTime date;
130
131 date.setMSecsSinceEpoch(epoch);
132 return date;
133 }
134
135 QList<Journey> const & JourneyModel::getData() const {
136 return journeys;
137 }
138
139 void JourneyModel::sort(int column, Qt::SortOrder order) {
140 switch (column) {
141 case JourneyRoles::StartRole:
142 switch (order) {
143 case Qt::SortOrder::AscendingOrder:
144 std::sort(journeys.begin(), journeys.end(), JourneyTimeAscending());
145 break;
146 case Qt::SortOrder::DescendingOrder:
147 std::sort(journeys.begin(), journeys.end(), JourneyTimeDescending());
148 break;
149 default:
150 // Do nothing
151 break;
152 }
153 break;
154 default:
155 // Do nothing
156 break;
157 }
158 }
159