1 #include "journeymodel.h"
4 JourneyModel::JourneyModel(QObject
*parent
) : QAbstractListModel(parent
) {
5 roles
[StartRole
] = "start";
6 roles
[DurationRole
] = "duration";
7 roles
[OvertookRole
] = "overtook";
8 roles
[OvertakenByRole
] = "overtakenby";
11 QHash
<int, QByteArray
> JourneyModel::roleNames() const {
15 void JourneyModel::addJourney(const Journey
&journey
)
17 beginInsertRows(QModelIndex(), rowCount(), rowCount());
22 void JourneyModel::addJourney(QDateTime start
, quint32 duration
, quint32 overtook
, quint32 overtakenby
) {
23 quint64 startepoch
= start
.toMSecsSinceEpoch();
24 addJourney(Journey(startepoch
, duration
, overtook
, overtakenby
));
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));
33 int JourneyModel::rowCount(const QModelIndex
& parent
) const {
35 return journeys
.count();
38 QVariant
JourneyModel::data(const QModelIndex
& index
, int role
) const {
39 if (index
.row() < 0 || index
.row() > journeys
.count())
42 const Journey
&journey
= journeys
[index
.row()];
43 if (role
== StartRole
)
44 return journey
.getStart();
45 else if (role
== DurationRole
)
46 return journey
.getDuration();
47 else if (role
== OvertookRole
)
48 return journey
.getOvertook();
49 else if (role
== OvertakenByRole
)
50 return journey
.getOvertakenBy();
54 void JourneyModel::clear() {
58 void JourneyModel::exportToFile(QFile
& file
) {
59 if (file
.open(QIODevice::WriteOnly
)) {
60 QTextStream
out(&file
);
61 for (QList
<Journey
>::iterator journeyIter
= journeys
.begin(); journeyIter
!= journeys
.end(); journeyIter
++) {
62 out
<< journeyIter
->getStart() << ",";
63 out
<< journeyIter
->getDuration() << ",";
64 out
<< journeyIter
->getOvertook() << ",";
65 out
<< journeyIter
->getOvertakenBy() << endl
;
70 qDebug() << "File failed to export";
74 void JourneyModel::importFromFile(QFile
& file
) {
75 if (file
.open(QIODevice::ReadOnly
)) {
76 QTextStream
in(&file
);
82 qint32 overtakenby
= 0;
84 data
= in
.readLine().split(",");
85 if (data
.length() == 4) {
86 start
= data
[0].toLongLong();
87 duration
= data
[1].toLongLong();
88 overtook
= data
[2].toLongLong();
89 overtakenby
= data
[3].toLongLong();
91 addJourney(Journey(start
, duration
, overtook
, overtakenby
));
99 QDate
JourneyModel::epochToDate(quint64 epoch
) {
102 qDebug() << "Epoch: " << epoch
;
103 date
.setMSecsSinceEpoch(epoch
);
104 qDebug() << "Date: " << date
.date();
108 QTime
JourneyModel::epochToTime(quint64 epoch
) {
111 date
.setMSecsSinceEpoch(epoch
);
115 QDateTime
JourneyModel::epochToDateTime(quint64 epoch
) {
118 date
.setMSecsSinceEpoch(epoch
);