Add graph animation; support for line graphs
[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 double average = 0.0;
61
62 if (count > 0) {
63 average = ((double)time / (double)count);
64 }
65 else {
66 average = 0.0;
67 }
68
69 return average;
70 }
71
72 double Status::getSpeedPercentile() const {
73 quint64 overtook;
74 quint64 overtakenby;
75 QList<Journey> const & journeys = journeymodel.getData();
76 double percentile;
77
78 overtook = 0u;
79 overtakenby = 0u;
80 foreach(Journey journey, journeys) {
81 overtook += journey.getOvertook();
82 overtakenby += journey.getOvertakenBy();
83 }
84
85 if (overtook + overtakenby > 0.0) {
86 percentile = (double)overtook / (double)(overtook + overtakenby);
87 }
88 else {
89 percentile = 0.0;
90 }
91
92 return percentile;
93 }
94
95 QString Status::getFormattedTime(quint64 seconds, int min, int max) {
96 static const QString plural[5] = {"s", "m", "h", "d", "y"};
97 static const QString singular[5] = {"s", "m", "h", "d", "y"};
98 static const quint64 base[5] = {60, 60, 24, 365, (quint64)-1};
99 quint64 remaining;
100 quint64 portion;
101 QString formatted;
102 QList<quint64> portions;
103
104 min = qBound(0, min, static_cast<int>(sizeof(base)));
105 max = qBound(0, max, static_cast<int>(sizeof(base)));
106
107 remaining = seconds;
108 for (int unit = 0; unit < max; unit++) {
109 portion = (unit == max - 1) ? remaining : remaining % base[unit];
110 portions << portion;
111 remaining /= base[unit];
112 }
113
114 formatted = "";
115 for (int unit = max - 1; unit >= min; unit--) {
116 portion = portions[unit];
117
118 if (portion != 0) {
119 if (formatted.length() > 0) {
120 formatted += " ";
121 }
122 formatted += QString::number(portion) + " " + ((portion == 1) ? singular[unit] : plural[unit]);
123 }
124 }
125
126 if (formatted == "") {
127 formatted = "None";
128 }
129
130 return formatted;
131 }
132
133 QList<float> Status::getGraphData() {
134 static QList<float> data({0.1, 0.2, 0.3, 0.4, 1.0, 0.8, 0.7, 0.9, 0.5, 1.0});
135
136 return data;
137 }