diff --git a/src/snapshot.cpp b/src/snapshot.cpp --- a/src/snapshot.cpp +++ b/src/snapshot.cpp @@ -18,6 +18,8 @@ */ #include +#include +#include #include "snapshot.h" #include "snapshotview.h" @@ -89,3 +91,45 @@ void Snapshot::setName(QString name) _showAction.setText(_name); emit nameChanged(this); } + +void Snapshot::save(QString fileName) +{ + // TODO: remove code duplication (MainWindow::onExportCsv) + QSaveFile file(fileName); + + if (file.open(QIODevice::WriteOnly | QIODevice::Text)) + { + QTextStream fileStream(&file); + + unsigned numOfChannels = data.size(); + unsigned numOfSamples = data[0].size(); + + // print header + for (unsigned int ci = 0; ci < numOfChannels; ci++) + { + fileStream << "Channel " << ci; + if (ci != numOfChannels-1) fileStream << ","; + } + fileStream << '\n'; + + // print rows + for (unsigned int i = 0; i < numOfSamples; i++) + { + for (unsigned int ci = 0; ci < numOfChannels; ci++) + { + fileStream << data[ci][i].y(); + if (ci != numOfChannels-1) fileStream << ","; + } + fileStream << '\n'; + } + + if (!file.commit()) + { + qCritical() << "File save error during snapshot save: " << file.error(); + } + } + else + { + qCritical() << "File open error during snapshot save: " << file.error(); + } +} diff --git a/src/snapshot.h b/src/snapshot.h --- a/src/snapshot.h +++ b/src/snapshot.h @@ -43,6 +43,8 @@ public: QString name(); void setName(QString name); + void save(QString fileName); /// save snapshot data as CSV + signals: void deleteRequested(Snapshot*); void nameChanged(Snapshot*); diff --git a/src/snapshotview.cpp b/src/snapshotview.cpp --- a/src/snapshotview.cpp +++ b/src/snapshotview.cpp @@ -17,8 +17,6 @@ along with serialplot. If not, see . */ -#include - #include "snapshotview.h" #include "ui_snapshotview.h" @@ -91,42 +89,5 @@ void SnapshotView::save() if (fileName.isNull()) return; // user canceled - // TODO: remove code duplication (MainWindow::onExportCsv) - QSaveFile file(fileName); - - if (file.open(QIODevice::WriteOnly | QIODevice::Text)) - { - QTextStream fileStream(&file); - - unsigned numOfChannels = _snapshot->data.size(); - unsigned numOfSamples = _snapshot->data[0].size(); - - // print header - for (unsigned int ci = 0; ci < numOfChannels; ci++) - { - fileStream << "Channel " << ci; - if (ci != numOfChannels-1) fileStream << ","; - } - fileStream << '\n'; - - // print rows - for (unsigned int i = 0; i < numOfSamples; i++) - { - for (unsigned int ci = 0; ci < numOfChannels; ci++) - { - fileStream << _snapshot->data[ci][i].y(); - if (ci != numOfChannels-1) fileStream << ","; - } - fileStream << '\n'; - } - - if (!file.commit()) - { - qCritical() << "File save error during snapshot save: " << file.error(); - } - } - else - { - qCritical() << "File open error during snapshot save: " << file.error(); - } + _snapshot->save(fileName); }