/* * Copyright (c) 2010 Mark Liversedge (liversedge@gmail.com) * * 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 "LTMSettings.h" #include "MainWindow.h" #include "LTMTool.h" #include "Context.h" #include "LTMChartParser.h" #include #include #include #include /*---------------------------------------------------------------------- * EDIT CHART DIALOG *--------------------------------------------------------------------*/ EditChartDialog::EditChartDialog(Context *context, LTMSettings *settings, QListpresets) : QDialog(context->mainWindow, Qt::Dialog), context(context), settings(settings), presets(presets) { setWindowTitle(tr("Enter Chart Name")); QVBoxLayout *mainLayout = new QVBoxLayout(this); // Metric Name mainLayout->addSpacing(5); chartName = new QLineEdit; mainLayout->addWidget(chartName); mainLayout->addStretch(); // Buttons QHBoxLayout *buttonLayout = new QHBoxLayout; buttonLayout->addStretch(); okButton = new QPushButton(tr("&OK"), this); cancelButton = new QPushButton(tr("&Cancel"), this); buttonLayout->addWidget(cancelButton); buttonLayout->addWidget(okButton); mainLayout->addLayout(buttonLayout); // make it wide enough setMinimumWidth(250); // connect up slots connect(okButton, SIGNAL(clicked()), this, SLOT(okClicked())); connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancelClicked())); } void EditChartDialog::okClicked() { // mustn't be blank if (chartName->text() == "") { QMessageBox::warning( 0, "Entry Error", "Name is blank"); return; } // does it already exist? foreach (LTMSettings chart, presets) { if (chart.name == chartName->text()) { QMessageBox::warning( 0, "Entry Error", "Chart already exists"); return; } } settings->name = chartName->text(); accept(); } void EditChartDialog::cancelClicked() { reject(); } /*---------------------------------------------------------------------- * Write to charts.xml *--------------------------------------------------------------------*/ void LTMSettings::writeChartXML(QDir home, QList charts) { LTMChartParser::serialize(QString(home.path() + "/charts.xml"), charts); } /*---------------------------------------------------------------------- * Read charts.xml *--------------------------------------------------------------------*/ void LTMSettings::readChartXML(QDir home, QList &charts) { QFileInfo chartFile(home.absolutePath() + "/charts.xml"); QFile chartsFile; // if it doesn't exist use our built-in default version if (chartFile.exists()) chartsFile.setFileName(chartFile.filePath()); else chartsFile.setFileName(":/xml/charts.xml"); QXmlInputSource source( &chartsFile ); QXmlSimpleReader xmlReader; LTMChartParser( handler ); xmlReader.setContentHandler(&handler); xmlReader.setErrorHandler(&handler); xmlReader.parse( source ); charts = handler.getSettings(); } /*---------------------------------------------------------------------- * Marshall/Unmarshall to DataStream to store as a QVariant *----------------------------------------------------------------------*/ QDataStream &operator<<(QDataStream &out, const LTMSettings &settings) { // 4.6 - 4.9 all the same out.setVersion(QDataStream::Qt_4_6); // all the baisc fields first out<(metric.curveStyle-1); // curveStyle change between qwt 5 and 6 out<(metric.symbolStyle); out<(metric.series); out<>(QDataStream &in, LTMSettings &settings) { // 4.6 - 4.9 all the same in.setVersion(QDataStream::Qt_4_6); RideMetricFactory &factory = RideMetricFactory::instance(); int counter=0; int version=0; // all the basic fields first in>>settings.name; in>>settings.title; in>>settings.start; in>>settings.end; in>>settings.groupBy; in>>settings.shadeZones; in>>settings.legend; in>>settings.field1; in>>settings.field2; in>>counter; // we now add version number before the counter // if counter is -1 -- to make settings extensible if (counter == -1) { in>>version; in>>counter; } while(counter-- && !in.atEnd()) { MetricDetail m; in>>m.type; in>>m.stack; in>>m.symbol; in>>m.name; in>>m.uname; in>>m.uunits; in>>m.smooth; in>>m.trend; in>>m.topN; in>>m.topOut; in>>m.baseline; in>>m.showOnPlot; in>>m.filter; in>>m.from; in>>m.to; int x; in>> x; m.curveStyle = static_cast(x+1); // curveStyle change between qwt 5 and 6 in>> x; m.symbolStyle = static_cast(x); in>>m.penColor; in>>m.penAlpha; in>>m.penWidth; in>>m.penStyle; in>>m.brushColor; in>>m.brushAlpha; // added curve filling in v1.0 if (version >=1) { in>>m.fillCurve; } else { m.fillCurve = false; } if (version >= 2) { // get bests info in>>m.duration; in>>m.duration_units; in>>m.bestSymbol; in>>x; m.series = static_cast(x); } if (version >= 3) { // trendtype added in>>m.trendtype; } else { m.trendtype = 0; // default! } if (m.trend == true) { // migrating from old trendline checkbox m.trendtype = 1; m.trend = false; // lets forget it now } if (version >= 5) { in >>m.labels; } // get a metric pointer (if it exists) m.metric = factory.rideMetric(m.symbol); settings.metrics.append(m); } if (version >= 4) in >> settings.showData; if (version >= 6) { in >>settings.stack; } if (version >= 7) { in >>settings.stackWidth; } return in; }