diff --git a/mainwindow.cpp b/mainwindow.cpp --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -21,6 +21,9 @@ #include "ui_mainwindow.h" #include #include +#include +#include +#include #include #include #include @@ -45,6 +48,9 @@ MainWindow::MainWindow(QWidget *parent) QObject::connect(ui->actionHelpAbout, &QAction::triggered, &aboutDialog, &QWidget::show); + QObject::connect(ui->actionExportCsv, &QAction::triggered, + this, &MainWindow::onExportCsv); + QObject::connect(&portControl, &PortControl::portToggled, this, &MainWindow::onPortToggled); @@ -550,3 +556,45 @@ QColor MainWindow::makeColor(unsigned in return QColor::fromHsv(360*i/n + 360/pow(2,p+1), 255, 230); } } + +void MainWindow::onExportCsv() +{ + bool wasPaused = ui->actionPause->isChecked(); + ui->actionPause->setChecked(true); // pause plotting + + QString fileName = QFileDialog::getSaveFileName(this, tr("Export CSV File")); + + if (fileName.isNull()) // user canceled export + { + ui->actionPause->setChecked(wasPaused); + } + else + { + QFile file(fileName); + if (file.open(QIODevice::WriteOnly | QIODevice::Text)) + { + QTextStream fileStream(&file); + + for (unsigned int ci = 0; ci < numOfChannels; ci++) + { + fileStream << "Channel " << ci; + if (ci != numOfChannels-1) fileStream << ","; + } + fileStream << '\n'; + + for (unsigned int i = 0; i < numOfSamples; i++) + { + for (unsigned int ci = 0; ci < numOfChannels; ci++) + { + fileStream << channelsData[ci][i]; + if (ci != numOfChannels-1) fileStream << ","; + } + fileStream << '\n'; + } + } + else + { + qDebug() << "File open error during export: " << file.error(); + } + } +} diff --git a/mainwindow.h b/mainwindow.h --- a/mainwindow.h +++ b/mainwindow.h @@ -115,6 +115,8 @@ private slots: void demoTimerTimeout(); void enableDemo(bool enabled); + + void onExportCsv(); }; #endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui --- a/mainwindow.ui +++ b/mainwindow.ui @@ -324,6 +324,13 @@ + + + File + + + + @@ -372,6 +379,14 @@ Toggle Demo Mode + + + Export CSV + + + Export plot data to CSV + +