QGCDataPlot2D.cc 25.3 KB
Newer Older
1 2 3 4 5 6 7 8
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/
lm's avatar
lm committed
9 10 11 12 13 14 15 16


/**
 * @file
 *   @brief Implementation of QGCDataPlot2D
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *
 */
17 18

#include <QTemporaryFile>
dogmaphobic's avatar
dogmaphobic committed
19
#ifndef __mobile__
20
#include <QPrintDialog>
dogmaphobic's avatar
dogmaphobic committed
21 22
#include <QPrinter>
#endif
23 24 25
#include <QProgressDialog>
#include <QHBoxLayout>
#include <QSvgGenerator>
26
#include <QStandardPaths>
Don Gagne's avatar
Don Gagne committed
27 28 29 30
#include <QDebug>

#include <cmath>

31 32 33
#include "QGCDataPlot2D.h"
#include "ui_QGCDataPlot2D.h"
#include "MG.h"
Don Gagne's avatar
Don Gagne committed
34
#include "QGCFileDialog.h"
Don Gagne's avatar
Don Gagne committed
35
#include "QGCMessageBox.h"
36 37

QGCDataPlot2D::QGCDataPlot2D(QWidget *parent) :
38
    QWidget(parent),
39
    plot(new IncrementalPlot(parent)),
40 41
    logFile(NULL),
    ui(new Ui::QGCDataPlot2D)
42 43 44 45 46 47 48
{
    ui->setupUi(this);

    // Add plot to ui
    QHBoxLayout* layout = new QHBoxLayout(ui->plotFrame);
    layout->addWidget(plot);
    ui->plotFrame->setLayout(layout);
49
    ui->gridCheckBox->setChecked(plot->gridEnabled());
50 51

    // Connect user actions
52 53 54 55 56 57 58 59 60 61 62 63 64 65
    connect(ui->selectFileButton, &QPushButton::clicked, this, &QGCDataPlot2D::selectFile);
    connect(ui->saveCsvButton, &QPushButton::clicked, this, &QGCDataPlot2D::saveCsvLog);
    connect(ui->reloadButton, &QPushButton::clicked, this, &QGCDataPlot2D::reloadFile);
    connect(ui->savePlotButton, &QPushButton::clicked, this, &QGCDataPlot2D::savePlot);
    connect(ui->printButton, &QPushButton::clicked, this, &QGCDataPlot2D::print);
    connect(ui->legendCheckBox, &QCheckBox::clicked, plot, &IncrementalPlot::showLegend);
    connect(ui->symmetricCheckBox,&QCheckBox::clicked, plot, &IncrementalPlot::setSymmetric);
    connect(ui->gridCheckBox, &QCheckBox::clicked, plot, &IncrementalPlot::showGrid);

    connect(ui->style, static_cast<void (QComboBox::*)(const QString&)>(&QComboBox::currentIndexChanged),
            plot, &IncrementalPlot::setStyleText);

    //TODO: calculateRegression returns bool, slots are expected to return void, this makes
    // converting to new style way too hard.
66
    connect(ui->regressionButton, SIGNAL(clicked()), this, SLOT(calculateRegression()));
67 68

    // Allow style changes to propagate through this widget
69
    connect(qgcApp(), &QGCApplication::styleChanged, plot, &IncrementalPlot::styleChanged);
70 71 72 73
}

void QGCDataPlot2D::reloadFile()
{
74 75
    if (QFileInfo(fileName).isReadable()) {
        if (ui->inputFileType->currentText().contains("pxIMU") || ui->inputFileType->currentText().contains("RAW")) {
76
            loadRawLog(fileName, ui->xAxis->currentText(), ui->yAxis->text());
77
        } else if (ui->inputFileType->currentText().contains("CSV")) {
78 79 80 81 82 83 84
            loadCsvLog(fileName, ui->xAxis->currentText(), ui->yAxis->text());
        }
    }
}

void QGCDataPlot2D::loadFile()
{
lm's avatar
lm committed
85
    qDebug() << "DATA PLOT: Loading file:" << fileName;
86 87
    if (QFileInfo(fileName).isReadable()) {
        if (ui->inputFileType->currentText().contains("pxIMU") || ui->inputFileType->currentText().contains("RAW")) {
88
            loadRawLog(fileName);
89
        } else if (ui->inputFileType->currentText().contains("CSV")) {
90 91 92 93 94
            loadCsvLog(fileName);
        }
    }
}

95 96
void QGCDataPlot2D::loadFile(QString file)
{
97 98
    // TODO This "filename" is a private/protected member variable. It should be named in such way
    // it indicates so. This same name is used in several places within this file in local scopes.
99
    fileName = file;
100 101 102
    QFileInfo fi(fileName);
    if (fi.isReadable()) {
        if (fi.suffix() == QString("raw") || fi.suffix() == QString("imu")) {
103
            loadRawLog(fileName);
104
        } else if (fi.suffix() == QString("txt") || fi.suffix() == QString("csv")) {
105 106
            loadCsvLog(fileName);
        }
107
        // TODO Else, tell the user it doesn't know what to do with the file...
108 109 110 111
    }
}

/**
112 113 114 115 116
 * This function brings up a file name dialog and asks the user to enter a file to save to
 */
QString QGCDataPlot2D::getSavePlotFilename()
{
    QString fileName = QGCFileDialog::getSaveFileName(
117
        this, "Save Plot File", QStandardPaths::writableLocation(QStandardPaths::DesktopLocation),
118
        "PDF Documents (*.pdf);;SVG Images (*.svg)",
119
        "pdf");
120 121 122 123 124
    return fileName;
}

/**
 * This function aks the user for a filename and exports to either PDF or SVG, depending on the filename
125
 */
126 127
void QGCDataPlot2D::savePlot()
{
128
    QString fileName = getSavePlotFilename();
129 130
    if (fileName.isEmpty())
        return;
lm's avatar
lm committed
131

132
    while(!(fileName.endsWith(".svg") || fileName.endsWith(".pdf"))) {
133 134 135 136 137
        QMessageBox::StandardButton button = QGCMessageBox::warning(
            tr("Unsuitable file extension for Plot document type."),
            tr("Please choose .pdf or .svg as file extension. Click OK to change the file extension, cancel to not save the file."),
            QMessageBox::Ok | QMessageBox::Cancel,
            QMessageBox::Ok);
138
        // Abort if cancelled
Don Gagne's avatar
Don Gagne committed
139 140 141
        if (button == QMessageBox::Cancel) {
            return;
        }
142 143

        fileName = getSavePlotFilename();
144 145
        if (fileName.isEmpty())
            return; //Abort if cancelled
146 147
    }

148
    if (fileName.endsWith(".pdf")) {
149
        exportPDF(fileName);
150
    } else if (fileName.endsWith(".svg")) {
151 152
        exportSVG(fileName);
    }
153 154 155 156 157
}


void QGCDataPlot2D::print()
{
dogmaphobic's avatar
dogmaphobic committed
158
#ifndef __mobile__
159 160 161 162 163 164
    QPrinter printer(QPrinter::HighResolution);
    //    printer.setOutputFormat(QPrinter::PdfFormat);
    //    //QPrinter printer(QPrinter::HighResolution);
    //    printer.setOutputFileName(fileName);

    QString docName = plot->title().text();
165
    if ( !docName.isEmpty() ) {
166 167 168 169 170 171 172 173
        docName.replace (QRegExp (QString::fromLatin1 ("\n")), tr (" -- "));
        printer.setDocName (docName);
    }

    printer.setCreator("QGroundControl");
    printer.setOrientation(QPrinter::Landscape);

    QPrintDialog dialog(&printer);
174
    if ( dialog.exec() ) {
pixhawk's avatar
pixhawk committed
175
        plot->setStyleSheet("QWidget { background-color: #FFFFFF; color: #000000; background-clip: border; font-size: 10pt;}");
176
        plot->setCanvasBackground(Qt::white);
177 178 179 180 181 182 183 184 185 186 187 188 189 190
        // FIXME: QwtPlotPrintFilter no longer exists in Qwt 6.1
        //QwtPlotPrintFilter filter;
        //filter.color(Qt::white, QwtPlotPrintFilter::CanvasBackground);
        //filter.color(Qt::black, QwtPlotPrintFilter::AxisScale);
        //filter.color(Qt::black, QwtPlotPrintFilter::AxisTitle);
        //filter.color(Qt::black, QwtPlotPrintFilter::MajorGrid);
        //filter.color(Qt::black, QwtPlotPrintFilter::MinorGrid);
        //if ( printer.colorMode() == QPrinter::GrayScale ) {
        //    int options = QwtPlotPrintFilter::PrintAll;
        //    options &= ~QwtPlotPrintFilter::PrintBackground;
        //    options |= QwtPlotPrintFilter::PrintFrameWithScales;
        //    filter.setOptions(options);
        //}
        //plot->print(printer);
pixhawk's avatar
pixhawk committed
191 192
        plot->setStyleSheet("QWidget { background-color: #050508; color: #DDDDDF; background-clip: border; font-size: 11pt;}");
        //plot->setCanvasBackground(QColor(5, 5, 8));
193
    }
dogmaphobic's avatar
dogmaphobic committed
194
#endif
195 196
}

197 198
void QGCDataPlot2D::exportPDF(QString fileName)
{
dogmaphobic's avatar
dogmaphobic committed
199 200 201
#ifdef __mobile__
    Q_UNUSED(fileName)
#else
202 203 204 205 206 207 208 209
    QPrinter printer;
    printer.setOutputFormat(QPrinter::PdfFormat);
    printer.setOutputFileName(fileName);
    //printer.setFullPage(true);
    printer.setPageMargins(10.0, 10.0, 10.0, 10.0, QPrinter::Millimeter);
    printer.setPageSize(QPrinter::A4);

    QString docName = plot->title().text();
210
    if ( !docName.isEmpty() ) {
211 212 213 214 215 216 217 218 219
        docName.replace (QRegExp (QString::fromLatin1 ("\n")), tr (" -- "));
        printer.setDocName (docName);
    }

    printer.setCreator("QGroundControl");
    printer.setOrientation(QPrinter::Landscape);

    plot->setStyleSheet("QWidget { background-color: #FFFFFF; color: #000000; background-clip: border; font-size: 10pt;}");
    //        plot->setCanvasBackground(Qt::white);
220
    // FIXME: QwtPlotPrintFilter no longer exists in Qwt 6.1
221 222 223 224 225 226 227 228 229 230 231 232 233
    //        QwtPlotPrintFilter filter;
    //        filter.color(Qt::white, QwtPlotPrintFilter::CanvasBackground);
    //        filter.color(Qt::black, QwtPlotPrintFilter::AxisScale);
    //        filter.color(Qt::black, QwtPlotPrintFilter::AxisTitle);
    //        filter.color(Qt::black, QwtPlotPrintFilter::MajorGrid);
    //        filter.color(Qt::black, QwtPlotPrintFilter::MinorGrid);
    //        if ( printer.colorMode() == QPrinter::GrayScale )
    //        {
    //            int options = QwtPlotPrintFilter::PrintAll;
    //            options &= ~QwtPlotPrintFilter::PrintBackground;
    //            options |= QwtPlotPrintFilter::PrintFrameWithScales;
    //            filter.setOptions(options);
    //        }
234
    //plot->print(printer);
235 236
    plot->setStyleSheet("QWidget { background-color: #050508; color: #DDDDDF; background-clip: border; font-size: 11pt;}");
    //plot->setCanvasBackground(QColor(5, 5, 8));
dogmaphobic's avatar
dogmaphobic committed
237
#endif
238 239
}

240 241
void QGCDataPlot2D::exportSVG(QString fileName)
{
dogmaphobic's avatar
dogmaphobic committed
242 243 244
#ifdef __mobile__
    Q_UNUSED(fileName)
#else
245
    if ( !fileName.isEmpty() ) {
246 247
        plot->setStyleSheet("QWidget { background-color: #FFFFFF; color: #000000; background-clip: border; font-size: 10pt;}");
        //plot->setCanvasBackground(Qt::white);
248 249 250 251
        QSvgGenerator generator;
        generator.setFileName(fileName);
        generator.setSize(QSize(800, 600));

252 253 254 255 256 257 258
        // FIXME: QwtPlotPrintFilter no longer exists in Qwt 6.1
        //QwtPlotPrintFilter filter;
        //filter.color(Qt::white, QwtPlotPrintFilter::CanvasBackground);
        //filter.color(Qt::black, QwtPlotPrintFilter::AxisScale);
        //filter.color(Qt::black, QwtPlotPrintFilter::AxisTitle);
        //filter.color(Qt::black, QwtPlotPrintFilter::MajorGrid);
        //filter.color(Qt::black, QwtPlotPrintFilter::MinorGrid);
259

260
        //plot->print(generator);
261
        plot->setStyleSheet("QWidget { background-color: #050508; color: #DDDDDF; background-clip: border; font-size: 11pt;}");
262
    }
dogmaphobic's avatar
dogmaphobic committed
263
#endif
264 265 266 267 268 269 270
}

/**
 * Selects a filename and attempts immediately to load it.
 */
void QGCDataPlot2D::selectFile()
{
271 272
    // Open a file dialog prompting the user for the file to load.
    // Note the special case for the Pixhawk.
273
    if (ui->inputFileType->currentText().contains("pxIMU") || ui->inputFileType->currentText().contains("RAW")) {
274
        fileName = QGCFileDialog::getOpenFileName(this, tr("Load Log File"), QString(), "Log Files (*.imu *.raw)");
275 276 277
    }
    else
    {
278
        fileName = QGCFileDialog::getOpenFileName(this, tr("Load Log File"), QString(), "Log Files (*.csv);;All Files (*)");
lm's avatar
lm committed
279 280
    }

281
    // Check if the user hit cancel, which results in an empty string.
282
    // If this is the case, we just stop.
283
    if (fileName.isEmpty())
284 285 286
    {
        return;
    }
287

288
    // Now attempt to open the file
289
    QFileInfo fileInfo(fileName);
290
    if (!fileInfo.isReadable())
291
    {
292 293 294 295 296
        // TODO This needs some TLC. File used by another program sounds like a Windows only issue.
        QGCMessageBox::critical(
            tr("Could not open file"),
            tr("The file is owned by user %1. Is the file currently used by another program?").arg(fileInfo.owner()));
        ui->filenameLabel->setText(tr("Could not open %1").arg(fileInfo.fileName()));
297
    }
298 299
    else
    {
300 301 302 303 304 305 306 307 308
        ui->filenameLabel->setText(tr("Opened %1").arg(fileInfo.completeBaseName()+"."+fileInfo.completeSuffix()));
        // Open and import the file
        loadFile();
    }

}

void QGCDataPlot2D::loadRawLog(QString file, QString xAxisName, QString yAxisFilter)
{
309 310
    Q_UNUSED(xAxisName);
    Q_UNUSED(yAxisFilter);
lm's avatar
lm committed
311

312
    if (logFile != NULL) {
313 314 315 316
        logFile->close();
        delete logFile;
    }
    // Postprocess log file
lm's avatar
lm committed
317
    logFile = new QTemporaryFile("qt_qgc_temp_log.XXXXXX.csv");
318
    compressor = new LogCompressor(file, logFile->fileName());
319
    connect(compressor, &LogCompressor::finishedFile, this, static_cast<void (QGCDataPlot2D::*)(QString)>(&QGCDataPlot2D::loadFile));
320
    compressor->startCompression();
321 322
}

323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
/**
 * This function loads a CSV file into the plot. It tries to assign the dimension names
 * based on the first data row and tries to guess the separator char.
 *
 * @param file Name of the file to open
 * @param xAxisName Optional paramater. If given, the x axis dimension will be selected to match this string
 * @param yAxisFilter Optional parameter. If given, only data dimension names present in the filter string will be
 *        plotted
 *
 * @code
 *
 * QString file = "/home/user/datalog.txt"; // With header: x<tab>y<tab>z
 * QString xAxis = "x";
 * QString yAxis = "z";
 *
 * // Plotted result will be x vs z with y ignored.
 * @endcode
 */
341 342
void QGCDataPlot2D::loadCsvLog(QString file, QString xAxisName, QString yAxisFilter)
{
343
    if (logFile != NULL) {
344 345
        logFile->close();
        delete logFile;
346
        curveNames.clear();
347 348 349 350 351 352 353
    }
    logFile = new QFile(file);

    // Load CSV data
    if (!logFile->open(QIODevice::ReadOnly | QIODevice::Text))
        return;

354 355 356 357 358
    // Set plot title
    if (ui->plotTitle->text() != "") plot->setTitle(ui->plotTitle->text());
    if (ui->plotXAxisLabel->text() != "") plot->setAxisTitle(QwtPlot::xBottom, ui->plotXAxisLabel->text());
    if (ui->plotYAxisLabel->text() != "") plot->setAxisTitle(QwtPlot::yLeft, ui->plotYAxisLabel->text());

359 360 361 362 363 364 365 366 367
    // Extract header

    // Read in values
    // Find all keys
    QTextStream in(logFile);

    // First line is header
    QString header = in.readLine();

368 369 370 371 372 373 374 375 376 377 378 379
    bool charRead = false;
    QString separator = "";
    QList<QChar> sepCandidates;
    sepCandidates << '\t';
    sepCandidates << ',';
    sepCandidates << ';';
    sepCandidates << ' ';
    sepCandidates << '~';
    sepCandidates << '|';

    // Iterate until separator is found
    // or full header is parsed
380 381
    for (int i = 0; i < header.length(); i++) {
        if (sepCandidates.contains(header.at(i))) {
382
            // Separator found
383
            if (charRead) {
384 385
                separator += header[i];
            }
386
        } else {
387 388 389 390 391 392 393 394 395 396 397 398 399
            // Char found
            charRead = true;
            // If the separator is not empty, this char
            // has been read after a separator, so detection
            // is now complete
            if (separator != "") break;
        }
    }

    QString out = separator;
    out.replace("\t", "<tab>");
    ui->filenameLabel->setText(file.split("/").last().split("\\").last()+" Separator: \""+out+"\"");
    //qDebug() << "READING CSV:" << header;
400 401 402 403

    // Clear plot
    plot->removeData();

404
    QMap<QString, QVector<double>* > xValues;
405 406
    QMap<QString, QVector<double>* > yValues;

407
    curveNames.append(header.split(separator, QString::SkipEmptyParts));
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422

    // Eliminate any non-string curve names
    for (int i = 0; i < curveNames.count(); ++i)
    {
        if (curveNames.at(i).length() == 0 ||
            curveNames.at(i) == " " ||
            curveNames.at(i) == "\n" ||
            curveNames.at(i) == "\t" ||
            curveNames.at(i) == "\r")
        {
            // Remove bogus curve name
            curveNames.removeAt(i);
        }
    }

423 424 425 426 427
    QString curveName;

    // Clear UI elements
    ui->xAxis->clear();
    ui->yAxis->clear();
428 429 430
    ui->xRegressionComboBox->clear();
    ui->yRegressionComboBox->clear();
    ui->regressionOutput->clear();
431 432 433

    int curveNameIndex = 0;

434
    QString xAxisFilter;
435
    if (xAxisName == "") {
436
        xAxisFilter = curveNames.first();
437
    } else {
438 439
        xAxisFilter = xAxisName;
    }
440

LM's avatar
LM committed
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
    // Fill y-axis renaming lookup table
    // Allow the user to rename data dimensions in the plot
    QMap<QString, QString> renaming;

    QStringList yCurves = yAxisFilter.split("|", QString::SkipEmptyParts);

    // Figure out the correct renaming
    for (int i = 0; i < yCurves.count(); ++i)
    {
        if (yCurves.at(i).contains(":"))
        {
            QStringList parts = yCurves.at(i).split(":", QString::SkipEmptyParts);
            if (parts.count() > 1)
            {
                // Insert renaming map
                renaming.insert(parts.first(), parts.last());
                // Replace curve value with first part only
                yCurves.replace(i, parts.first());
            }
        }
//        else
//        {
//            // Insert same value, not renaming anything
//            renaming.insert(yCurves.at(i), yCurves.at(i));
//        }
    }


469
    foreach(curveName, curveNames) {
470
        // Add to plot x axis selection
471
        ui->xAxis->addItem(curveName);
472 473 474
        // Add to regression selection
        ui->xRegressionComboBox->addItem(curveName);
        ui->yRegressionComboBox->addItem(curveName);
475
        if (curveName != xAxisFilter) {
LM's avatar
LM committed
476
            if ((yAxisFilter == "") || yCurves.contains(curveName)) {
pixhawk's avatar
pixhawk committed
477
                yValues.insert(curveName, new QVector<double>());
478
                xValues.insert(curveName, new QVector<double>());
pixhawk's avatar
pixhawk committed
479
                // Add separator starting with second item
480
                if (curveNameIndex > 0 && curveNameIndex < curveNames.count()) {
pixhawk's avatar
pixhawk committed
481 482
                    ui->yAxis->setText(ui->yAxis->text()+"|");
                }
LM's avatar
LM committed
483 484 485 486 487 488
                // If this curve was renamed, re-add the renaming to the text field
                QString renamingText = "";
                if (renaming.contains(curveName)) renamingText = QString(":%1").arg(renaming.value(curveName));
                ui->yAxis->setText(ui->yAxis->text()+curveName+renamingText);
                // Insert same value, not renaming anything
                if (!renaming.contains(curveName)) renaming.insert(curveName, curveName);
489
                curveNameIndex++;
490 491 492 493
            }
        }
    }

494 495 496
    // Select current axis in UI
    ui->xAxis->setCurrentIndex(curveNames.indexOf(xAxisFilter));

497 498
    // Read data

pixhawk's avatar
pixhawk committed
499 500
    double x = 0;
    double y = 0;
501

502 503
    while (!in.atEnd())
    {
504 505
        QString line = in.readLine();

506 507 508 509
        // Keep empty parts here - we still have to act on them
        QStringList values = line.split(separator, QString::KeepEmptyParts);

        bool headerfound = false;
510

511 512 513 514 515
        // First get header - ORDER MATTERS HERE!
        foreach(curveName, curveNames)
        {
            if (curveName == xAxisFilter)
            {
516
                // X  AXIS HANDLING
517

518
                // Take this value as x if it is selected
519 520 521 522 523 524
                QString text = values.at(curveNames.indexOf(curveName));
                text = text.trimmed();
                if (text.length() > 0 && text != " " && text != "\n" && text != "\r" && text != "\t")
                {
                    bool okx = true;
                    x = text.toDouble(&okx);
525
                    if (okx && !qIsNaN(x) && !qIsInf(x))
526 527 528 529 530 531
                    {
                        headerfound = true;
                    }
                }
            }
        }
532

533 534 535 536 537 538
        if (headerfound)
        {
            // Search again from start for values - ORDER MATTERS HERE!
            foreach(curveName, curveNames)
            {
                // Y  AXIS HANDLING
LM's avatar
LM committed
539 540
                // Only plot non-x curver and those selected in the yAxisFilter (or all if the filter is not set)
                if(curveName != xAxisFilter && (yAxisFilter == "" || yCurves.contains(curveName)))
541 542 543 544 545 546 547 548 549 550
                {
                    bool oky;
                    int curveNameIndex = curveNames.indexOf(curveName);
                    if (values.count() > curveNameIndex)
                    {
                        QString text(values.at(curveNameIndex));
                        text = text.trimmed();
                        y = text.toDouble(&oky);
                        // Only INF is really an issue for the plot
                        // NaN is fine
551
                        if (oky && !qIsNaN(y) && !qIsInf(y) && text.length() > 0 && text != " " && text != "\n" && text != "\r" && text != "\t")
552 553 554
                        {
                            // Only append definitely valid values
                            xValues.value(curveName)->append(x);
555 556 557
                            yValues.value(curveName)->append(y);
                        }
                    }
558 559 560 561 562
                }
            }
        }
    }

563 564
    // Add data array of each curve to the plot at once (fast)
    // Iterates through all x-y curve combinations
565
    for (int i = 0; i < yValues.count(); i++) {
LM's avatar
LM committed
566 567 568 569 570 571 572 573
        if (renaming.contains(yValues.keys().at(i)))
        {
            plot->appendData(renaming.value(yValues.keys().at(i)), xValues.values().at(i)->data(), yValues.values().at(i)->data(), xValues.values().at(i)->count());
        }
        else
        {
            plot->appendData(yValues.keys().at(i), xValues.values().at(i)->data(), yValues.values().at(i)->data(), xValues.values().at(i)->count());
        }
574
    }
575
    plot->updateScale();
576 577 578 579 580
    plot->setStyleText(ui->style->currentText());
}

bool QGCDataPlot2D::calculateRegression()
{
581
    // TODO: Add support for quadratic / cubic curve fitting
582
    return calculateRegression(ui->xRegressionComboBox->currentText(), ui->yRegressionComboBox->currentText(), "linear");
583 584 585 586 587 588 589 590 591 592 593
}

/**
 * @param xName Name of the x dimension
 * @param yName Name of the y dimension
 * @param method Regression method, either "linear", "quadratic" or "cubic". Only linear is supported at this point
 */
bool QGCDataPlot2D::calculateRegression(QString xName, QString yName, QString method)
{
    bool result = false;
    QString function;
594 595
    if (xName != yName) {
        if (QFileInfo(fileName).isReadable()) {
596 597 598 599
            loadCsvLog(fileName, xName, yName);
            ui->xRegressionComboBox->setCurrentIndex(curveNames.indexOf(xName));
            ui->yRegressionComboBox->setCurrentIndex(curveNames.indexOf(yName));
        }
600

601 602 603 604 605
        // Create a couple of arrays for us to use to temporarily store some of the data from the plot.
        // These arrays are allocated on the heap as they are far too big to go in the stack and will
        // cause an overflow.
        // TODO: Look into if this would be better done by having a getter return const double pointers instead
        // of using memcpy().
606
        const int size = 100000;
607 608
        double *x = new double[size];
        double *y = new double[size];
609 610
        int copied = plot->data(yName, x, y, size);

611
        if (method == "linear") {
612 613 614
            double a;  // Y-axis crossing
            double b;  // Slope
            double r;  // Regression coefficient
615
            if (linearRegression(x, y, copied, &a, &b, &r)) {
616 617 618 619 620 621 622 623 624
                function = tr("%1 = %2 * %3 + %4 | R-coefficient: %5").arg(yName, QString::number(b), xName, QString::number(a), QString::number(r));

                // Plot curve
                // y-axis crossing (x = 0)
                // Set plotting to lines only
                plot->appendData(tr("regression %1-%2").arg(xName, yName), 0.0, a);
                plot->setStyleText("lines");
                // x-value of the current rightmost x position in the plot
                plot->appendData(tr("regression %1-%2").arg(xName, yName), plot->invTransform(QwtPlot::xBottom, plot->width() - plot->width()*0.08f), (a + b*plot->invTransform(QwtPlot::xBottom, plot->width() - plot->width() * 0.08f)));
625 626

                result = true;
627
            } else {
628 629
                function = tr("Linear regression failed. (Limit: %1 data points. Try with less)").arg(size);
            }
630
        } else {
631 632
            function = tr("Regression method %1 not found").arg(method);
        }
633

Don Gagne's avatar
Don Gagne committed
634 635
        delete[] x;
        delete[] y;
636
    } else {
637 638
        // xName == yName
        function = tr("Please select different X and Y dimensions, not %1 = %2").arg(xName, yName);
639
    }
640 641
    ui->regressionOutput->setText(function);
    return result;
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
}

/**
 * Linear regression (least squares) for n data points.
 * Computes:
 *
 * y = a * x + b
 *
 * @param x values on x axis
 * @param y corresponding values on y axis
 * @param n Number of values
 * @param a returned slope of line
 * @param b y-axis intersection
 * @param r regression coefficient. The larger the coefficient is, the better is
 *          the match of the regression.
 * @return 1 on success, 0 on failure (e.g. because of infinite slope)
 */
659
bool QGCDataPlot2D::linearRegression(double *x, double *y, int n, double *a, double *b, double *r)
660 661 662 663 664 665 666 667 668
{
    int i;
    double sumx=0,sumy=0,sumx2=0,sumy2=0,sumxy=0;
    double sxx,syy,sxy;

    *a = 0;
    *b = 0;
    *r = 0;
    if (n < 2)
669
        return true;
670 671

    /* Conpute some things we need */
672
    for (i=0; i<n; i++) {
673 674 675 676 677 678 679 680 681 682
        sumx += x[i];
        sumy += y[i];
        sumx2 += (x[i] * x[i]);
        sumy2 += (y[i] * y[i]);
        sumxy += (x[i] * y[i]);
    }
    sxx = sumx2 - sumx * sumx / n;
    syy = sumy2 - sumy * sumy / n;
    sxy = sumxy - sumx * sumy / n;

Ricardo de Almeida Gonzaga's avatar
Ricardo de Almeida Gonzaga committed
683
    /* Infinite slope (b), non existent intercept (a) */
684
    if (fabs(sxx) == 0)
685
        return false;
686 687 688 689 690 691 692 693 694 695 696

    /* Calculate the slope (b) and intercept (a) */
    *b = sxy / sxx;
    *a = sumy / n - (*b) * sumx / n;

    /* Compute the regression coefficient */
    if (fabs(syy) == 0)
        *r = 1;
    else
        *r = sxy / sqrt(sxx * syy);

697
    return false;
698 699 700 701
}

void QGCDataPlot2D::saveCsvLog()
{
702
    QString fileName = QGCFileDialog::getSaveFileName(
703
        this, "Save CSV Log File", QStandardPaths::writableLocation(QStandardPaths::DesktopLocation),
704
        "CSV Files (*.csv)",
dogmaphobic's avatar
dogmaphobic committed
705 706
        "csv",
        true);
707

dogmaphobic's avatar
dogmaphobic committed
708
    if (fileName.isEmpty()) {
709
        return; //User cancelled
dogmaphobic's avatar
dogmaphobic committed
710
    }
lm's avatar
lm committed
711

712 713
    bool success = logFile->copy(fileName);

714
    qDebug() << "Saved CSV log (" << fileName << "). Success: " << success;
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734

    //qDebug() << "READE TO SAVE CSV LOG TO " << fileName;
}

QGCDataPlot2D::~QGCDataPlot2D()
{
    delete ui;
}

void QGCDataPlot2D::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}