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 void JourneyModel::deleteJourney(quint32 index
) {
34 beginRemoveRows(QModelIndex(), index
, index
);
35 journeys
.removeAt(index
);
39 int JourneyModel::rowCount(const QModelIndex
& parent
) const {
41 return journeys
.count();
44 QVariant
JourneyModel::data(const QModelIndex
& index
, int role
) const {
45 if (index
.row() < 0 || index
.row() > journeys
.count())
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();
60 void JourneyModel::clear() {
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
;
76 qDebug() << "File failed to export";
80 void JourneyModel::importFromFile(QFile
& file
) {
81 if (file
.open(QIODevice::ReadOnly
)) {
82 QTextStream
in(&file
);
88 qint32 overtakenby
= 0;
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();
97 addJourney(Journey(start
, duration
, overtook
, overtakenby
));
105 QDate
JourneyModel::epochToDate(quint64 epoch
) {
108 date
.setMSecsSinceEpoch(epoch
);
112 QTime
JourneyModel::epochToTime(quint64 epoch
) {
115 date
.setMSecsSinceEpoch(epoch
);
119 QDateTime
JourneyModel::epochToDateTime(quint64 epoch
) {
122 date
.setMSecsSinceEpoch(epoch
);
126 QList
<Journey
> const & JourneyModel::getData() const {