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 // Find position to add it
18 QList
<Journey
>::const_iterator it
= journeys
.constBegin();
20 unsigned int position
= 0;
21 while ((it
!= journeys
.constEnd()) && (journey
.getStart() < (*it
).getStart())) {
26 beginInsertRows(QModelIndex(), position
, position
);
27 journeys
.insert(position
, journey
);
31 void JourneyModel::addJourney(QDateTime start
, quint32 duration
, quint32 overtook
, quint32 overtakenby
) {
32 quint64 startepoch
= start
.toMSecsSinceEpoch();
33 addJourney(Journey(startepoch
, duration
, overtook
, overtakenby
));
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));
42 void JourneyModel::deleteJourney(quint32 index
) {
43 beginRemoveRows(QModelIndex(), index
, index
);
44 journeys
.removeAt(index
);
48 int JourneyModel::rowCount(const QModelIndex
& parent
) const {
50 return journeys
.count();
53 QVariant
JourneyModel::data(const QModelIndex
& index
, int role
) const {
54 if (index
.row() < 0 || index
.row() > journeys
.count())
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();
69 void JourneyModel::clear() {
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
;
85 qDebug() << "File failed to export";
89 void JourneyModel::importFromFile(QFile
& file
) {
90 if (file
.open(QIODevice::ReadOnly
)) {
91 QTextStream
in(&file
);
97 qint32 overtakenby
= 0;
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();
106 addJourney(Journey(start
, duration
, overtook
, overtakenby
));
114 QDate
JourneyModel::epochToDate(quint64 epoch
) {
117 date
.setMSecsSinceEpoch(epoch
);
121 QTime
JourneyModel::epochToTime(quint64 epoch
) {
124 date
.setMSecsSinceEpoch(epoch
);
128 QDateTime
JourneyModel::epochToDateTime(quint64 epoch
) {
131 date
.setMSecsSinceEpoch(epoch
);
135 QList
<Journey
> const & JourneyModel::getData() const {
139 void JourneyModel::sort(int column
, Qt::SortOrder order
) {
141 case JourneyRoles::StartRole
:
143 case Qt::SortOrder::AscendingOrder
:
144 std::sort(journeys
.begin(), journeys
.end(), JourneyTimeAscending());
146 case Qt::SortOrder::DescendingOrder
:
147 std::sort(journeys
.begin(), journeys
.end(), JourneyTimeDescending());