# HG changeset patch # User Hasan Yavuz Ă–ZDERYA # Date 2015-09-21 12:42:34 # Node ID aa3c57d8443c6dba890088cbfac596df6a823ad9 # Parent 1ab6199146580076452a136344bfeee29eea9235 implemented snapshot export diff --git a/snapshotview.cpp b/snapshotview.cpp --- a/snapshotview.cpp +++ b/snapshotview.cpp @@ -1,6 +1,8 @@ #include "snapshotview.h" #include "ui_snapshotview.h" +#include + SnapShotView::SnapShotView(QWidget *parent, SnapShot* snapShot) : QMainWindow(parent), ui(new Ui::SnapShotView), @@ -27,6 +29,9 @@ SnapShotView::SnapShotView(QWidget *pare renameDialog.setLabelText("Enter new name:"); connect(ui->actionRename, &QAction::triggered, this, &SnapShotView::showRenameDialog); + + connect(ui->actionExport, &QAction::triggered, + this, &SnapShotView::save); } SnapShotView::~SnapShotView() @@ -55,3 +60,47 @@ void SnapShotView::renameSnapshot(QStrin _snapShot->setName(name); setWindowTitle(name); } + +void SnapShotView::save() +{ + QString fileName = QFileDialog::getSaveFileName(this, tr("Export CSV File")); + + // 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(); + } +} diff --git a/snapshotview.h b/snapshotview.h --- a/snapshotview.h +++ b/snapshotview.h @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -37,6 +38,7 @@ private: private slots: void showRenameDialog(); void renameSnapshot(QString name); + void save(); }; #endif // SNAPSHOTVIEW_H diff --git a/snapshotview.ui b/snapshotview.ui --- a/snapshotview.ui +++ b/snapshotview.ui @@ -34,7 +34,7 @@ Snapshot - + @@ -44,10 +44,13 @@ - + Export CSV + + Save snapshot as CSV file +