6 Status::Status(JourneyModel
&journeymodel
, QObject
*parent
) : QObject(parent
),
9 journeymodel(journeymodel
)
14 bool Status::getCycling() const {
18 quint64
Status::getStartTime() const {
22 quint64
Status::getDuration() const {
23 return (QDateTime::currentMSecsSinceEpoch() - startTime
) / 1000;
26 void Status::setCycling(bool value
) {
28 emit
cyclingChanged(cycling
);
31 void Status::setStartTime(quint64 value
) {
33 emit
startTimeChanged(startTime
);
36 void Status::startJourney() {
38 setStartTime(QDateTime::currentMSecsSinceEpoch());
41 quint64
Status::getJourneyCount() const {
42 return journeymodel
.rowCount();
45 quint64
Status::getTimeSpentCycling() const {
47 QList
<Journey
> const & journeys
= journeymodel
.getData();
50 foreach(Journey journey
, journeys
) {
51 time
+= journey
.getDuration();
57 double Status::getAverageDuration() const {
58 quint64 time
= getTimeSpentCycling();
59 quint64 count
= Status::getJourneyCount();
61 return ((double)time
/ (double)count
);
64 double Status::getSpeedPercentile() const {
67 QList
<Journey
> const & journeys
= journeymodel
.getData();
72 foreach(Journey journey
, journeys
) {
73 overtook
+= journey
.getOvertook();
74 overtakenby
+= journey
.getOvertakenBy();
77 percentile
= (double)overtook
/ (double)(overtook
+ overtakenby
);
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};
89 QList
<quint64
> portions
;
91 min
= qBound(0, min
, static_cast<int>(sizeof(base
)));
92 max
= qBound(0, max
, static_cast<int>(sizeof(base
)));
95 for (int unit
= 0; unit
< max
; unit
++) {
96 portion
= (unit
== max
- 1) ? remaining
: remaining
% base
[unit
];
98 remaining
/= base
[unit
];
99 qDebug() << plural
[unit
] << ": " << portion
;
103 for (int unit
= max
- 1; unit
>= min
; unit
--) {
104 portion
= portions
[unit
];
107 if (formatted
.length() > 0) {
110 formatted
+= QString::number(portion
) + " " + ((portion
== 1) ? singular
[unit
] : plural
[unit
]);