Changeset - dd74a99bcfac
[Not reviewed]
snapshots
0 2 0
Hasan Yavuz Ă–ZDERYA - 10 years ago 2015-09-29 17:20:10
hy@ozderya.net
implemented load snapshot from csv file
2 files changed with 75 insertions and 0 deletions:
0 comments (0 inline, 0 general)
snapshotmanager.cpp
Show inline comments
 
@@ -2,6 +2,10 @@
 
#include <QTime>
 
#include <QMenuBar>
 
#include <QKeySequence>
 
#include <QFileDialog>
 
#include <QFile>
 
#include <QVector>
 
#include <QPointF>
 

	
 
#include "snapshotmanager.h"
 

	
 
@@ -9,6 +13,7 @@ SnapshotManager::SnapshotManager(QMainWi
 
                                 QList<FrameBuffer*>* 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<QVector<QPointF>> 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;
snapshotmanager.h
Show inline comments
 
@@ -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 */
0 comments (0 inline, 0 general)