playing around with export to XML,

may eventually become the "native" GC file format
This commit is contained in:
Sean C. Rhea
2008-05-27 02:56:07 +00:00
parent a9ce6ae947
commit 5a3c3c8eb7
2 changed files with 83 additions and 0 deletions

View File

@@ -17,8 +17,89 @@
*/
#include "RideFile.h"
#include <QtXml/QtXml>
#include <assert.h>
static void
markInterval(QDomDocument &xroot, QDomNode &xride, QDomNode &xintervals,
double &startSecs, double prevSecs,
int &thisInterval, RideFilePoint *sample)
{
if (xintervals.isNull()) {
xintervals = xroot.createElement("intervals");
xride.appendChild(xintervals);
}
QDomElement xinterval = xroot.createElement("interval").toElement();
xintervals.appendChild(xinterval);
xinterval.setAttribute("name", thisInterval);
xinterval.setAttribute("begin_secs",
QString("%1").arg(startSecs, 0, 'f', 2));
xinterval.setAttribute("end_secs",
QString("%1").arg(prevSecs, 0, 'f', 2));
startSecs = sample->secs;
thisInterval = sample->interval;
}
bool
RideFile::writeAsXml(QFile &file, QString &err)
{
(void) err;
QDomDocument xroot("GoldenCheetah 1.0");
QDomNode xride = xroot.createElement("ride");
xroot.appendChild(xride);
QDomElement xstart = xroot.createElement("start").toElement();
xride.appendChild(xstart);
xstart.setAttribute("date", startTime_.toString("yyyy/MM/dd hh:mm:ss"));
QDomElement xrecint = xroot.createElement("sampling_period").toElement();
xride.appendChild(xrecint);
xrecint.setAttribute("secs", QString("%1").arg(recIntSecs_, 0, 'f', 3));
QDomNode xintervals;
bool hasNm = false;
QVector<double> intervalStart, intervalStop;
double startSecs = 0.0, prevSecs = 0.0;
int thisInterval = 0;
QListIterator<RideFilePoint*> i(dataPoints_);
RideFilePoint *sample = NULL;
while (i.hasNext()) {
sample = i.next();
if (sample->nm > 0.0)
hasNm = true;
assert(sample->secs >= 0.0);
if (sample->interval != thisInterval) {
markInterval(xroot, xride, xintervals, startSecs,
prevSecs, thisInterval, sample);
}
prevSecs = sample->secs;
}
if (sample) {
markInterval(xroot, xride, xintervals, startSecs,
prevSecs, thisInterval, sample);
}
QDomNode xsamples = xroot.createElement("samples");
xride.appendChild(xsamples);
i.toFront();
while (i.hasNext()) {
RideFilePoint *sample = i.next();
QDomElement xsamp = xroot.createElement("sample").toElement();
xsamples.appendChild(xsamp);
xsamp.setAttribute("secs", QString("%1").arg(sample->secs, 0, 'f', 2));
xsamp.setAttribute("cad", QString("%1").arg(sample->cad, 0, 'f', 0));
xsamp.setAttribute("hr", QString("%1").arg(sample->hr, 0, 'f', 0));
xsamp.setAttribute("km", QString("%1").arg(sample->km, 0, 'f', 3));
xsamp.setAttribute("kph", QString("%1").arg(sample->kph, 0, 'f', 1));
xsamp.setAttribute("watts", sample->watts);
if (hasNm) {
double nm = (sample->watts > 0.0) ? sample->nm : 0.0;
xsamp.setAttribute("nm", QString("%1").arg(nm, 0,'f', 1));
}
}
file.open(QFile::WriteOnly);
QTextStream ts(&file);
xroot.save(ts, 4);
file.close();
return true;
}
RideFileFactory *RideFileFactory::instance_;
RideFileFactory &RideFileFactory::instance()

View File

@@ -83,6 +83,8 @@ class RideFile
dataPoints_.append(new RideFilePoint(secs, cad, hr, km, kph,
nm, watts, interval));
}
bool writeAsXml(QFile &file, QString &err);
};
struct RideFileReader {