export to XML functionality

This commit is contained in:
Sean C. Rhea
2009-05-02 22:58:57 +00:00
parent 7e6ee45d9a
commit 9dc3c00023
4 changed files with 57 additions and 29 deletions

View File

@@ -404,6 +404,8 @@ MainWindow::MainWindow(const QDir &home) :
SLOT(downloadRide()), tr("Ctrl+D"));
rideMenu->addAction(tr("&Export to CSV..."), this,
SLOT(exportCSV()), tr("Ctrl+E"));
rideMenu->addAction(tr("&Export to XML..."), this,
SLOT(exportXML()));
rideMenu->addAction(tr("&Import from SRM..."), this,
SLOT(importSRM()), tr("Ctrl+I"));
rideMenu->addAction(tr("&Import from CSV..."), this,
@@ -618,6 +620,32 @@ MainWindow::exportCSV()
ride->ride->writeAsCsv(file, units!="English");
}
void
MainWindow::exportXML()
{
if ((treeWidget->selectedItems().size() != 1)
|| (treeWidget->selectedItems().first()->type() != RIDE_TYPE)) {
QMessageBox::critical(this, tr("Select Ride"), tr("No ride selected!"));
return;
}
RideItem *ride = (RideItem*) treeWidget->selectedItems().first();
QString fileName = QFileDialog::getSaveFileName(
this, tr("Export XML"), QDir::homePath(), tr("XML (*.xml)"));
if (fileName.length() == 0)
return;
QString err;
QFile file(fileName);
ride->ride->writeAsXml(file, err);
if (err.length() > 0) {
QMessageBox::critical(this, tr("Export XML"),
tr("Error writing %1: %2").arg(fileName).arg(err));
return;
}
}
void MainWindow::importCSV()
{
// Prompt the user for the ride date

View File

@@ -54,6 +54,7 @@ class MainWindow : public QMainWindow
void openCyclist();
void downloadRide();
void exportCSV();
void exportXML();
void importCSV();
void importSRM();
void importTCX();

View File

@@ -22,44 +22,43 @@
#include "Settings.h"
static void
markInterval(QDomDocument &xroot, QDomNode &xride, QDomNode &xintervals,
markInterval(QDomDocument &doc, QDomNode &xride, QDomNode &xintervals,
double &startSecs, double prevSecs,
int &thisInterval, RideFilePoint *sample)
{
if (xintervals.isNull()) {
xintervals = xroot.createElement("intervals");
xintervals = doc.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));
QDomElement xint = doc.createElement("interval").toElement();
xintervals.appendChild(xint);
xint.setAttribute("name", thisInterval);
xint.setAttribute("from_secs", QString("%1").arg(startSecs, 0, 'f', 2));
xint.setAttribute("thru_secs", QString("%1").arg(prevSecs, 0, 'f', 2));
startSecs = sample->secs;
thisInterval = sample->interval;
}
static void
append_text(QDomDocument &doc, QDomNode &parent,
const QString &child_name, const QString &child_value)
{
QDomNode child = parent.appendChild(doc.createElement(child_name));
child.appendChild(doc.createTextNode(child_value));
}
bool
RideFile::writeAsXml(QFile &file, QString &err)
RideFile::writeAsXml(QFile &file, QString &err) const
{
(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 xdevtype = xroot.createElement("device_type").toElement();
xride.appendChild(xdevtype);
xdevtype.setAttribute("name", deviceType_);
QDomElement xrecint = xroot.createElement("sampling_period").toElement();
xride.appendChild(xrecint);
xrecint.setAttribute("secs", QString("%1").arg(recIntSecs_, 0, 'f', 3));
QDomDocument doc("GoldenCheetah 1.0");
QDomNode xride = doc.appendChild(doc.createElement("ride"));
QDomNode xheader = xride.appendChild(doc.createElement("header"));
append_text(doc, xheader, "start_time", startTime_.toString("yyyy/MM/dd hh:mm:ss"));
append_text(doc, xheader, "device_type", deviceType_);
append_text(doc, xheader, "rec_int_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_);
@@ -70,21 +69,21 @@ RideFile::writeAsXml(QFile &file, QString &err)
hasNm = true;
assert(sample->secs >= 0.0);
if (sample->interval != thisInterval) {
markInterval(xroot, xride, xintervals, startSecs,
markInterval(doc, xride, xintervals, startSecs,
prevSecs, thisInterval, sample);
}
prevSecs = sample->secs;
}
if (sample) {
markInterval(xroot, xride, xintervals, startSecs,
markInterval(doc, xride, xintervals, startSecs,
prevSecs, thisInterval, sample);
}
QDomNode xsamples = xroot.createElement("samples");
QDomNode xsamples = doc.createElement("samples");
xride.appendChild(xsamples);
i.toFront();
while (i.hasNext()) {
RideFilePoint *sample = i.next();
QDomElement xsamp = xroot.createElement("sample").toElement();
QDomElement xsamp = doc.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));
@@ -99,7 +98,7 @@ RideFile::writeAsXml(QFile &file, QString &err)
}
file.open(QFile::WriteOnly);
QTextStream ts(&file);
xroot.save(ts, 4);
doc.save(ts, 4);
file.close();
return true;
}

View File

@@ -89,7 +89,7 @@ class RideFile
nm, watts, interval));
}
bool writeAsXml(QFile &file, QString &err);
bool writeAsXml(QFile &file, QString &err) const;
void writeAsCsv(QFile &file, bool bIsMetric) const;
};