7286c6db7b104d4370edc29464cf2d6fc4339754
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 addJourney(Journey(start
.currentMSecsSinceEpoch(), duration
, overtook
, overtakenby
));
26 int JourneyModel::rowCount(const QModelIndex
& parent
) const {
28 return journeys
.count();
31 QVariant
JourneyModel::data(const QModelIndex
& index
, int role
) const {
32 if (index
.row() < 0 || index
.row() > journeys
.count())
35 const Journey
&journey
= journeys
[index
.row()];
36 if (role
== StartRole
)
37 return journey
.getStart();
38 else if (role
== DurationRole
)
39 return journey
.getDuration();
40 else if (role
== OvertookRole
)
41 return journey
.getOvertook();
42 else if (role
== OvertakenByRole
)
43 return journey
.getOvertakenBy();
47 void JourneyModel::clear() {
51 void JourneyModel::exportToFile(QFile
& file
) {
52 if (file
.open(QIODevice::WriteOnly
)) {
53 QTextStream
out(&file
);
54 for (QList
<Journey
>::iterator journeyIter
= journeys
.begin(); journeyIter
!= journeys
.end(); journeyIter
++) {
55 out
<< journeyIter
->getStart() << ",";
56 out
<< journeyIter
->getDuration() << ",";
57 out
<< journeyIter
->getOvertook() << ",";
58 out
<< journeyIter
->getOvertakenBy() << endl
;
63 qDebug() << "File failed to export";
67 void JourneyModel::importFromFile(QFile
& file
) {
68 if (file
.open(QIODevice::ReadOnly
)) {
69 QTextStream
in(&file
);
75 qint32 overtakenby
= 0;
77 data
= in
.readLine().split(",");
78 if (data
.length() == 4) {
79 start
= data
[0].toLongLong();
80 duration
= data
[1].toLongLong();
81 overtook
= data
[2].toLongLong();
82 overtakenby
= data
[3].toLongLong();
84 addJourney(Journey(start
, duration
, overtook
, overtakenby
));
92 QDate
JourneyModel::epochToDate(quint64 epoch
) {
95 date
.setMSecsSinceEpoch(epoch
);
99 QTime
JourneyModel::epochToTime(quint64 epoch
) {
102 date
.setMSecsSinceEpoch(epoch
);