Add images to buttons on main page
[harbour-pedalo.git] / src / status.cpp
1 #include <QDateTime>
2 #include <QDebug>
3
4 #include "status.h"
5
6 Status::Status(JourneyModel &journeymodel, QObject *parent) : QObject(parent),
7 cycling(false),
8 startTime(0u),
9 journeymodel(journeymodel)
10 {
11
12 }
13
14 bool Status::getCycling() const {
15 return cycling;
16 }
17
18 quint64 Status::getStartTime() const {
19 return startTime;
20 }
21
22 quint64 Status::getDuration() const {
23 return (QDateTime::currentMSecsSinceEpoch() - startTime) / 1000;
24 }
25
26 void Status::setCycling(bool value) {
27 cycling = value;
28 emit cyclingChanged(cycling);
29 }
30
31 void Status::setStartTime(quint64 value) {
32 startTime = value;
33 emit startTimeChanged(startTime);
34 }
35
36 void Status::startJourney() {
37 setCycling(true);
38 setStartTime(QDateTime::currentMSecsSinceEpoch());
39 }
40
41 quint64 Status::getJourneyCount() const {
42 return journeymodel.rowCount();
43 }
44
45 quint64 Status::getTimeSpentCycling() const {
46 quint64 time;
47 QList<Journey> const & journeys = journeymodel.getData();
48
49 time = 0u;
50 foreach(Journey journey, journeys) {
51 time += journey.getDuration();
52 }
53
54 return time;
55 }
56
57 double Status::getAverageDuration() const {
58 quint64 time = getTimeSpentCycling();
59 quint64 count = Status::getJourneyCount();
60
61 return ((double)time / (double)count);
62 }
63
64 double Status::getSpeedPercentile() const {
65 quint64 overtook;
66 quint64 overtakenby;
67 QList<Journey> const & journeys = journeymodel.getData();
68 double percentile;
69
70 overtook = 0u;
71 overtakenby = 0u;
72 foreach(Journey journey, journeys) {
73 overtook += journey.getOvertook();
74 overtakenby += journey.getOvertakenBy();
75 }
76
77 percentile = (double)overtook / (double)(overtook + overtakenby);
78
79 return percentile;
80 }
81
82 QString Status::getFormattedTime(quint64 seconds, int min, int max) {
83 static const QString plural[5] = {"s", "m", "h", "d", "y"};
84 static const QString singular[5] = {"s", "m", "h", "d", "y"};
85 static const quint64 base[5] = {60, 60, 24, 365, (quint64)-1};
86 quint64 remaining;
87 quint64 portion;
88 QString formatted;
89 QList<quint64> portions;
90
91 min = qBound(0, min, static_cast<int>(sizeof(base)));
92 max = qBound(0, max, static_cast<int>(sizeof(base)));
93
94 remaining = seconds;
95 for (int unit = 0; unit < max; unit++) {
96 portion = (unit == max - 1) ? remaining : remaining % base[unit];
97 portions << portion;
98 remaining /= base[unit];
99 qDebug() << plural[unit] << ": " << portion;
100 }
101
102 formatted = "";
103 for (int unit = max - 1; unit >= min; unit--) {
104 portion = portions[unit];
105
106 if (portion != 0) {
107 if (formatted.length() > 0) {
108 formatted += " ";
109 }
110 formatted += QString::number(portion) + " " + ((portion == 1) ? singular[unit] : plural[unit]);
111 }
112 }
113
114 return formatted;
115 }
116