X-Git-Url: https://www.flypig.org.uk/git/?p=harbour-pedalo.git;a=blobdiff_plain;f=src%2Fstatus.cpp;h=b4bb630abf99c36d9944c5fe495e0b2a916bd683;hp=2aa9f7a18d630a711d2bcf5cb0c4f8d18b110847;hb=5fc520bad80dccf9bf2e0f16552c9f2605417067;hpb=c0284f613fd20fdedc5ef60de9893c74601ede6d diff --git a/src/status.cpp b/src/status.cpp index 2aa9f7a..b4bb630 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,80 @@ 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(); + + return ((double)time / (double)count); +} + +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(); + } + + percentile = (double)overtook / (double)(overtook + overtakenby); + + 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]; + qDebug() << plural[unit] << ": " << portion; + } + + 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]); + } + } + + return formatted; +} +