# HG changeset patch # User Hasan Yavuz Ă–ZDERYA # Date 2015-09-29 17:20:10 # Node ID dd74a99bcfac577847f4f529e7deaf93e9140cd3 # Parent aa3c57d8443c6dba890088cbfac596df6a823ad9 implemented load snapshot from csv file diff --git a/snapshotmanager.cpp b/snapshotmanager.cpp --- a/snapshotmanager.cpp +++ b/snapshotmanager.cpp @@ -2,6 +2,10 @@ #include #include #include +#include +#include +#include +#include #include "snapshotmanager.h" @@ -9,6 +13,7 @@ SnapshotManager::SnapshotManager(QMainWi QList* channelBuffers) : _menu("Snapshots"), _takeSnapshotAction("Take Snapshot", this), + loadSnapshotAction("Load Snapshot", this), clearAction("Clear Snapshots", this) { _mainWindow = mainWindow; @@ -16,11 +21,14 @@ SnapshotManager::SnapshotManager(QMainWi _takeSnapshotAction.setToolTip("Take a snapshot of current plot (F5)"); _takeSnapshotAction.setShortcut(QKeySequence("F5")); + loadSnapshotAction.setToolTip("Load a snapshot from CSV file"); clearAction.setToolTip("Delete all snapshots"); connect(&_takeSnapshotAction, SIGNAL(triggered(bool)), this, SLOT(takeSnapshot())); connect(&clearAction, SIGNAL(triggered(bool)), this, SLOT(clearSnapshots())); + connect(&loadSnapshotAction, SIGNAL(triggered(bool)), + this, SLOT(loadSnapshot())); updateMenu(); } @@ -62,6 +70,7 @@ void SnapshotManager::updateMenu() { _menu.clear(); _menu.addAction(&_takeSnapshotAction); + _menu.addAction(&loadSnapshotAction); if (snapshots.size()) { _menu.addSeparator(); @@ -91,6 +100,70 @@ void SnapshotManager::deleteSnapshot(Sna updateMenu(); } +void SnapshotManager::loadSnapshot() +{ + QString fileName = QFileDialog::getOpenFileName(_mainWindow, tr("Load CSV File")); + + if (fileName.isNull()) return; // user canceled + + QFile file(fileName); + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) + { + qCritical() << "Couldn't open file: " << fileName; + qCritical() << file.errorString(); + return; + } + + // read first row as headlines and determine number of channels + auto headLine = QString(file.readLine()); + unsigned numOfChannels = headLine.split(',').size(); + + // read data + QVector> data(numOfChannels); + unsigned lineNum = 1; + while (file.canReadLine()) + { + // parse line + auto line = QString(file.readLine()); + auto split = line.split(','); + + if (split.size() != (int) numOfChannels) + { + qCritical() << "Parsing error at line " << lineNum + << ": number of columns is not consistent."; + return; + } + + for (unsigned ci = 0; ci < numOfChannels; ci++) + { + // parse column + bool ok; + double y = split[ci].toDouble(&ok); + if (!ok) + { + qCritical() << "Parsing error at line " << lineNum + << ", column " << ci + << ": can't convert \"" << split[ci] + << "\" to double."; + return; + } + data[ci].append(QPointF(lineNum-1, y)); + } + lineNum++; + } + + auto snapShot = new SnapShot(_mainWindow, QFileInfo(fileName).baseName()); + snapShot->data = data; + + snapshots.append(snapShot); + QObject::connect(snapShot, &SnapShot::deleteRequested, + this, &SnapshotManager::deleteSnapshot); + QObject::connect(snapShot, &SnapShot::deleteRequested, + this, &SnapshotManager::updateMenu); + + updateMenu(); +} + QMenu* SnapshotManager::menu() { return &_menu; diff --git a/snapshotmanager.h b/snapshotmanager.h --- a/snapshotmanager.h +++ b/snapshotmanager.h @@ -30,6 +30,7 @@ private: QMenu _menu; QAction _takeSnapshotAction; + QAction loadSnapshotAction; QAction clearAction; void updateMenu(); @@ -38,6 +39,7 @@ private slots: void takeSnapshot(); void clearSnapshots(); void deleteSnapshot(SnapShot* snapshot); + void loadSnapshot(); }; #endif /* SNAPSHOTMANAGER_H */