/* * Copyright (c) 2009 Sean C. Rhea (srhea@srhea.net) * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., 51 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "RideSummaryWindow.h" #include "MainWindow.h" #include "RideFile.h" #include "RideItem.h" #include "RideMetric.h" #include "Settings.h" #include "TimeUtils.h" #include "Units.h" #include "Zones.h" #include #include #include #include RideSummaryWindow::RideSummaryWindow(MainWindow *mainWindow) : QWidget(mainWindow), mainWindow(mainWindow) { QVBoxLayout *vlayout = new QVBoxLayout; rideSummary = new QTextEdit(this); rideSummary->setReadOnly(true); vlayout->addWidget(rideSummary); connect(mainWindow, SIGNAL(rideSelected()), this, SLOT(refresh())); connect(mainWindow, SIGNAL(zonesChanged()), this, SLOT(refresh())); connect(mainWindow, SIGNAL(intervalsChanged()), this, SLOT(refresh())); setLayout(vlayout); } void RideSummaryWindow::refresh() { if (!mainWindow->rideItem()) { rideSummary->clear(); return; } rideSummary->setHtml(htmlSummary()); rideSummary->setAlignment(Qt::AlignCenter); } QString RideSummaryWindow::htmlSummary() const { QString summary; RideItem *rideItem = mainWindow->rideItem(); RideFile *ride = rideItem->ride(); // ridefile read errors? if (!ride) { summary = tr("

Couldn't read file \""); summary += rideItem->fileName + "\":"; QListIterator i(mainWindow->rideItem()->errors()); while (i.hasNext()) summary += "
" + i.next(); return summary; } summary = ("

" + rideItem->dateTime.toString(tr("dddd MMMM d, yyyy, h:mm AP")) + "

" + tr("Device Type: ") + ride->deviceType() + "

"); rideItem->computeMetrics(); boost::shared_ptr settings = GetApplicationSettings(); QVariant unit = settings->value(GC_UNIT); summary += "

"; bool metricUnits = (unit.toString() == "Metric"); const int columns = 3; const char *columnNames[] = { "Totals", "Averages", "Metrics*" }; const char *totalColumn[] = { "workout_time", "time_riding", "total_distance", "total_work", "elevation_gain", NULL }; const char *averageColumn[] = { "average_speed", "average_power", "average_hr", "average_cad", NULL }; const char *metricColumn[] = { "skiba_xpower", "skiba_relative_intensity", "skiba_bike_score", "daniels_points", "aerobic_decoupling", NULL }; summary += ""; for (int i = 0; i < columns; ++i) { summary += ""; } summary += "
" ""; summary = summary.arg(90 / columns); summary = summary.arg(columnNames[i]); const char **metricsList; switch (i) { case 0: metricsList = totalColumn; break; case 1: metricsList = averageColumn; break; case 2: metricsList = metricColumn; break; default: assert(false); } for (int j = 0;; ++j) { const char *symbol = metricsList[j]; if (!symbol) break; RideMetricPtr m = rideItem->metrics.value(symbol); QString name = m->name().replace(QRegExp(tr("^Average ")), ""); if (m->units(metricUnits) == "seconds") { QString s(""); s = s.arg(name); s = s.arg(time_to_string(m->value(metricUnits))); summary += s; } else { QString s = ""; if (m->precision() == 0) s = s.arg((unsigned) round(m->value(metricUnits))); else s = s.arg(m->value(metricUnits), 0, 'f', m->precision()); summary += s; } } summary += "

%2

%1:%2
" + name; if (m->units(metricUnits) != "") s += " (" + m->units(metricUnits) + ")"; s += ":%1
"; if (rideItem->numZones() > 0) { QVector time_in_zone(rideItem->numZones()); for (int i = 0; i < rideItem->numZones(); ++i) time_in_zone[i] = rideItem->timeInZone(i); summary += tr("

Power Zones

"); summary += mainWindow->zones()->summarize(rideItem->zoneRange(), time_in_zone); } if (ride->intervals().size() > 0) { bool firstRow = true; QString s; boost::shared_ptr settings = GetApplicationSettings(); if (settings->contains(GC_SETTINGS_INTERVAL_METRICS)) s = settings->value(GC_SETTINGS_INTERVAL_METRICS).toString(); else s = GC_SETTINGS_INTERVAL_METRICS_DEFAULT; QStringList intervalMetrics = s.split(","); summary += "

"+tr("Intervals")+"

\n

\n"; summary += "intervals()) { RideFile f(ride->startTime(), ride->recIntSecs()); for (int i = ride->intervalBegin(interval); i < ride->dataPoints().size(); ++i) { const RideFilePoint *p = ride->dataPoints()[i]; if (p->secs >= interval.stop) break; f.appendPoint(p->secs, p->cad, p->hr, p->km, p->kph, p->nm, p->watts, p->alt, p->lon, p->lat, 0); } if (f.dataPoints().size() == 0) { // Interval empty, do not compute any metrics continue; } QHash metrics = RideMetric::computeMetrics(&f, mainWindow->zones(), intervalMetrics); if (firstRow) { summary += ""; summary += ""; foreach (QString symbol, intervalMetrics) { RideMetricPtr m = metrics.value(symbol); summary += ""; } summary += ""; firstRow = false; } if (even) summary += ""; else { QColor color = QApplication::palette().alternateBase().color(); color = QColor::fromHsv(color.hue(), color.saturation() * 2, color.value()); summary += ""; } even = !even; summary += ""; foreach (QString symbol, intervalMetrics) { RideMetricPtr m = metrics.value(symbol); QString s(""); if (m->units(metricUnits) == "seconds") summary += s.arg(time_to_string(m->value(metricUnits))); else summary += s.arg(m->value(metricUnits), 0, 'f', m->precision()); } summary += ""; } summary += "
Interval Name" + m->name(); if (m->units(metricUnits) == "seconds") ; // don't do anything else if (m->units(metricUnits).size() > 0) summary += " (" + m->units(metricUnits) + ")"; summary += "
" + interval.name + "%1
"; } if (!rideItem->errors().empty()) { summary += tr("

Errors reading file:

    "); QStringListIterator i(rideItem->errors()); while(i.hasNext()) summary += "
  • " + i.next(); summary += "
"; } summary += "

"; // The extra
works around a bug in QT 4.3.1, // which will otherwise put the following above the
. summary += "
BikeScore is a trademark of Dr. Philip " "Friere Skiba, PhysFarm Training Systems LLC
"; return summary; }