Changeset - b006e7ce4816
[Not reviewed]
default
0 3 0
Hasan Yavuz Ă–ZDERYA - 10 years ago 2016-02-28 15:15:37
hy@ozderya.net
moved snapshot save function to `Snapshot` class
3 files changed with 47 insertions and 40 deletions:
0 comments (0 inline, 0 general)
src/snapshot.cpp
Show inline comments
 
@@ -18,6 +18,8 @@
 
*/
 

	
 
#include <stddef.h>
 
#include <QSaveFile>
 
#include <QTextStream>
 

	
 
#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();
 
    }
 
}
src/snapshot.h
Show inline comments
 
@@ -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*);
src/snapshotview.cpp
Show inline comments
 
@@ -17,8 +17,6 @@
 
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#include <QSaveFile>
 

	
 
#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 << ",";
 
    _snapshot->save(fileName);
 
        }
 
        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();
 
    }
 
}
0 comments (0 inline, 0 general)