X-Git-Url: https://www.flypig.org.uk/git/?p=harbour-pedalo.git;a=blobdiff_plain;f=src%2Fstatus.cpp;h=aeff901f69d5f67ea29d7ffd3f42a62049cbb8c4;hp=2aa9f7a18d630a711d2bcf5cb0c4f8d18b110847;hb=41ee443df2b12e7a4373be580006f57cc5fd768a;hpb=371dcf3335b355f8d421352a394161dc6d9b9f24 diff --git a/src/status.cpp b/src/status.cpp index 2aa9f7a..aeff901 100644 --- a/src/status.cpp +++ b/src/status.cpp @@ -1,10 +1,12 @@ #include +#include #include "status.h" -Status::Status(QObject *parent) : QObject(parent), +Status::Status(JourneyModel &journeymodel, QObject *parent) : QObject(parent), cycling(false), - startTime(0u) + startTime(0u), + journeymodel(journeymodel) { } @@ -35,3 +37,101 @@ void Status::startJourney() { setCycling(true); setStartTime(QDateTime::currentMSecsSinceEpoch()); } + +quint64 Status::getJourneyCount() const { + return journeymodel.rowCount(); +} + +quint64 Status::getTimeSpentCycling() const { + quint64 time; + QList const & journeys = journeymodel.getData(); + + time = 0u; + foreach(Journey journey, journeys) { + time += journey.getDuration(); + } + + return time; +} + +double Status::getAverageDuration() const { + quint64 time = getTimeSpentCycling(); + quint64 count = Status::getJourneyCount(); + double average = 0.0; + + if (count > 0) { + average = ((double)time / (double)count); + } + else { + average = 0.0; + } + + return average; +} + +double Status::getSpeedPercentile() const { + quint64 overtook; + quint64 overtakenby; + QList const & journeys = journeymodel.getData(); + double percentile; + + overtook = 0u; + overtakenby = 0u; + foreach(Journey journey, journeys) { + overtook += journey.getOvertook(); + overtakenby += journey.getOvertakenBy(); + } + + if (overtook + overtakenby > 0.0) { + percentile = (double)overtook / (double)(overtook + overtakenby); + } + else { + percentile = 0.0; + } + + return percentile; +} + +QString Status::getFormattedTime(quint64 seconds, int min, int max) { + static const QString plural[5] = {"s", "m", "h", "d", "y"}; + static const QString singular[5] = {"s", "m", "h", "d", "y"}; + static const quint64 base[5] = {60, 60, 24, 365, (quint64)-1}; + quint64 remaining; + quint64 portion; + QString formatted; + QList portions; + + min = qBound(0, min, static_cast(sizeof(base))); + max = qBound(0, max, static_cast(sizeof(base))); + + remaining = seconds; + for (int unit = 0; unit < max; unit++) { + portion = (unit == max - 1) ? remaining : remaining % base[unit]; + portions << portion; + remaining /= base[unit]; + } + + formatted = ""; + for (int unit = max - 1; unit >= min; unit--) { + portion = portions[unit]; + + if (portion != 0) { + if (formatted.length() > 0) { + formatted += " "; + } + formatted += QString::number(portion) + " " + ((portion == 1) ? singular[unit] : plural[unit]); + } + } + + if (formatted == "") { + formatted = "None"; + } + + return formatted; +} + +QList Status::getGraphData() { + static QList data({0.1, 0.2, 0.3, 0.4, 1.0, 0.8, 0.7, 0.9, 0.5, 1.0}); + + return data; +}