Changeset - 51bdd9cadae9
[Not reviewed]
Merge default
0 14 4
Hasan Yavuz ÖZDERYA - 10 years ago 2016-04-03 15:21:48
hy@ozderya.net
Merge with channelmanager
18 files changed with 425 insertions and 71 deletions:
0 comments (0 inline, 0 general)
CMakeLists.txt
Show inline comments
 
@@ -86,12 +86,14 @@ add_executable(${PROGRAM_NAME} WIN32
 
  src/commandpanel.cpp
 
  src/commandwidget.cpp
 
  src/commandedit.cpp
 
  src/dataformatpanel.cpp
 
  src/tooltipfilter.cpp
 
  src/sneakylineedit.cpp
 
  src/channelmanager.cpp
 
  src/framebufferseries.cpp
 
  misc/windows_icon.rc
 
  ${UI_FILES}
 
  ${RES_FILES}
 
  )
 

	
 
# Use the Widgets module from Qt 5.
serialplot.pro
Show inline comments
 
@@ -49,13 +49,15 @@ SOURCES += \
 
    src/plotsnapshotoverlay.cpp \
 
    src/commandpanel.cpp \
 
    src/commandwidget.cpp \
 
    src/commandedit.cpp \
 
    src/dataformatpanel.cpp \
 
    src/tooltipfilter.cpp \
 
    src/sneakylineedit.cpp
 
    src/sneakylineedit.cpp \
 
    src/channelmanager.cpp \
 
    src/framebufferseries.cpp
 

	
 
HEADERS += \
 
    src/mainwindow.h \
 
    src/utils.h \
 
    src/portcontrol.h \
 
    src/floatswap.h \
 
@@ -72,13 +74,15 @@ HEADERS += \
 
    src/plotsnapshotoverlay.h \
 
    src/commandpanel.h \
 
    src/commandwidget.h \
 
    src/commandedit.h \
 
    src/dataformatpanel.h \
 
    src/tooltipfilter.h \
 
    src/sneakylineedit.h
 
    src/sneakylineedit.h \
 
    src/channelmanager.h \
 
    src/framebufferseries.h
 

	
 
FORMS += \
 
    src/mainwindow.ui \
 
    src/about_dialog.ui \
 
    src/portcontrol.ui \
 
    src/snapshotview.ui \
src/channelmanager.cpp
Show inline comments
 
new file 100644
 
/*
 
  Copyright © 2016 Hasan Yavuz Özderya
 

	
 
  This file is part of serialplot.
 

	
 
  serialplot is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU General Public License as published by
 
  the Free Software Foundation, either version 3 of the License, or
 
  (at your option) any later version.
 

	
 
  serialplot is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU General Public License for more details.
 

	
 
  You should have received a copy of the GNU General Public License
 
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#include "channelmanager.h"
 

	
 
#include <QStringList>
 
#include <QModelIndex>
 

	
 
ChannelManager::ChannelManager(unsigned numberOfChannels, unsigned numberOfSamples, QObject *parent) :
 
    QObject(parent)
 
{
 
    _numOfChannels = numberOfChannels;
 
    _numOfSamples = numberOfSamples;
 

	
 
    QStringList channelNamesList;
 

	
 
    for (unsigned int i = 0; i < numberOfChannels; i++)
 
    {
 
        channelBuffers.append(new FrameBuffer(numberOfSamples));
 
        channelNamesList << QString("Channel %1").arg(i+1);
 
    }
 

	
 
    _channelNames.setStringList(channelNamesList);
 

	
 
    connect(&_channelNames, &QStringListModel::dataChanged,
 
            this, &ChannelManager::onChannelNameDataChange);
 
}
 

	
 
ChannelManager::~ChannelManager()
 
{
 
    for (auto buffer : channelBuffers)
 
    {
 
        delete buffer;
 
    }
 
}
 

	
 
unsigned ChannelManager::numOfChannels()
 
{
 
    return channelBuffers.size();
 
}
 

	
 
unsigned ChannelManager::numOfSamples()
 
{
 
    return _numOfSamples;
 
}
 

	
 
void ChannelManager::setNumOfChannels(unsigned number)
 
{
 
    unsigned int oldNum = channelBuffers.size();
 

	
 
    if (number > oldNum)
 
    {
 
        // add new channels
 
        for (unsigned int i = 0; i < number - oldNum; i++)
 
        {
 
            channelBuffers.append(new FrameBuffer(_numOfSamples));
 
            addChannelName(QString("Channel %1").arg(oldNum+i+1));
 
        }
 
    }
 
    else if(number < oldNum)
 
    {
 
        // remove channels
 
        for (unsigned int i = oldNum-1; i > number-1; i--)
 
        {
 
            delete channelBuffers.takeLast();
 
            _channelNames.removeRow(i);
 
        }
 
    }
 

	
 
    emit numOfChannelsChanged(number);
 
}
 

	
 
void ChannelManager::setNumOfSamples(unsigned number)
 
{
 
    _numOfSamples = number;
 

	
 
    for (int ci = 0; ci < channelBuffers.size(); ci++)
 
    {
 
        channelBuffers[ci]->resize(_numOfSamples);
 
    }
 

	
 
    emit numOfSamplesChanged(number);
 
}
 

	
 
FrameBuffer* ChannelManager::channelBuffer(unsigned channel)
 
{
 
    return channelBuffers[channel];
 
}
 

	
 
QStringListModel* ChannelManager::channelNames()
 
{
 
    return &_channelNames;
 
}
 

	
 
QString ChannelManager::channelName(unsigned channel)
 
{
 
    return _channelNames.data(_channelNames.index(channel, 0), Qt::DisplayRole).toString();
 
}
 

	
 
void ChannelManager::setChannelName(unsigned channel, QString name)
 
{
 
    _channelNames.setData(_channelNames.index(channel, 0), QVariant(name), Qt::DisplayRole);
 
}
 

	
 
void ChannelManager::addChannelName(QString name)
 
{
 
    _channelNames.insertRow(_channelNames.rowCount());
 
    setChannelName(_channelNames.rowCount()-1, name);
 
}
 

	
 
void ChannelManager::onChannelNameDataChange(const QModelIndex & topLeft,
 
                                             const QModelIndex & bottomRight,
 
                                             const QVector<int> & roles)
 
{
 
    Q_UNUSED(roles);
 
    int start = topLeft.row();
 
    int end = bottomRight.row();
 

	
 
    // TODO: maybe check `roles` parameter, can't think of a reason for current use case
 
    for (int i = start; i <= end; i++)
 
    {
 
        emit channelNameChanged(i, channelName(i));
 
    }
 
}
 

	
 
void ChannelManager::addChannelData(unsigned channel, double* data, unsigned size)
 
{
 
    channelBuffer(channel)->addSamples(data, size);
 
}
src/channelmanager.h
Show inline comments
 
new file 100644
 
/*
 
  Copyright © 2016 Hasan Yavuz Özderya
 

	
 
  This file is part of serialplot.
 

	
 
  serialplot is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU General Public License as published by
 
  the Free Software Foundation, either version 3 of the License, or
 
  (at your option) any later version.
 

	
 
  serialplot is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU General Public License for more details.
 

	
 
  You should have received a copy of the GNU General Public License
 
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#ifndef CHANNELMANAGER_H
 
#define CHANNELMANAGER_H
 

	
 
#include <QObject>
 
#include <QStringListModel>
 
#include <QModelIndex>
 
#include <QVector>
 

	
 
#include "framebuffer.h"
 

	
 
class ChannelManager : public QObject
 
{
 
    Q_OBJECT
 
public:
 
    explicit ChannelManager(unsigned numberOfChannels, unsigned numberOfSamples, QObject *parent = 0);
 
    ~ChannelManager();
 

	
 
    unsigned numOfChannels();
 
    unsigned numOfSamples();
 
    FrameBuffer* channelBuffer(unsigned channel);
 
    QStringListModel* channelNames();
 
    QString channelName(unsigned channel);
 

	
 
signals:
 
    void numOfChannelsChanged(unsigned value);
 
    void numOfSamplesChanged(unsigned value);
 
    void channelNameChanged(unsigned channel, QString name);
 

	
 
public slots:
 
    void setNumOfChannels(unsigned number);
 
    void setNumOfSamples(unsigned number);
 
    void setChannelName(unsigned channel, QString name);
 
    void addChannelData(unsigned channel, double* data, unsigned size);
 

	
 
private:
 
    unsigned _numOfChannels;
 
    unsigned _numOfSamples;
 
    QList<FrameBuffer*> channelBuffers;
 
    QStringListModel _channelNames;
 

	
 
    void addChannelName(QString name); ///< appends a new channel name at the end of list
 

	
 
private slots:
 
    void onChannelNameDataChange(const QModelIndex & topLeft,
 
                                 const QModelIndex & bottomRight,
 
                                 const QVector<int> & roles = QVector<int> ());
 
};
 

	
 
#endif // CHANNELMANAGER_H
src/dataformatpanel.cpp
Show inline comments
 
@@ -18,26 +18,27 @@
 
*/
 

	
 
#include "dataformatpanel.h"
 
#include "ui_dataformatpanel.h"
 

	
 
#include <QtEndian>
 
#include <QtDebug>
 

	
 
#include "utils.h"
 
#include "floatswap.h"
 

	
 
DataFormatPanel::DataFormatPanel(QSerialPort* port,
 
                                 QList<FrameBuffer*>* channelBuffers,
 
                                 ChannelManager* channelMan,
 
                                 QWidget *parent) :
 
    QWidget(parent),
 
    ui(new Ui::DataFormatPanel)
 
{
 
    ui->setupUi(this);
 

	
 
    serialPort = port;
 
    _channelBuffers = channelBuffers;
 
    _channelMan = channelMan;
 
    paused = false;
 

	
 
    // setup number format buttons
 
    numberFormatButtons.addButton(ui->rbUint8,  NumberFormat_uint8);
 
    numberFormatButtons.addButton(ui->rbUint16, NumberFormat_uint16);
 
    numberFormatButtons.addButton(ui->rbUint32, NumberFormat_uint32);
 
@@ -316,9 +317,9 @@ template<typename T> double DataFormatPa
 
    return double(data);
 
}
 

	
 
void DataFormatPanel::addChannelData(unsigned int channel,
 
                                     double* data, unsigned size)
 
{
 
    (*_channelBuffers)[channel]->addSamples(data, size);
 
    _channelMan->addChannelData(channel, data, size);
 
    sampleCount += size;
 
}
src/dataformatpanel.h
Show inline comments
 
@@ -25,24 +25,25 @@
 
#include <QTimer>
 
#include <QSerialPort>
 
#include <QList>
 
#include <QtGlobal>
 

	
 
#include "framebuffer.h"
 
#include "channelmanager.h"
 

	
 
namespace Ui {
 
class DataFormatPanel;
 
}
 

	
 
class DataFormatPanel : public QWidget
 
{
 
    Q_OBJECT
 

	
 
public:
 
    explicit DataFormatPanel(QSerialPort* port,
 
                             QList<FrameBuffer*>* channelBuffers,
 
                             ChannelManager* channelMan,
 
                             QWidget *parent = 0);
 
    ~DataFormatPanel();
 

	
 
    unsigned numOfChannels();
 
    unsigned samplesPerSecond();
 
    bool skipByteEnabled(void); // true for binary formats
 
@@ -74,13 +75,13 @@ private:
 
    };
 

	
 
    Ui::DataFormatPanel *ui;
 
    QButtonGroup numberFormatButtons;
 

	
 
    QSerialPort* serialPort;
 
    QList<FrameBuffer*>* _channelBuffers;
 
    ChannelManager* _channelMan;
 

	
 
    unsigned int _numOfChannels;
 
    NumberFormat numberFormat;
 
    unsigned int sampleSize; // number of bytes in the selected number format
 
    bool skipByteRequested;
 
    bool paused;
src/framebuffer.cpp
Show inline comments
 
@@ -42,13 +42,13 @@ void FrameBuffer::resize(size_t size)
 

	
 
    // move data to new array
 
    int fill_start = offset > 0 ? offset : 0;
 

	
 
    for (int i = fill_start; i < int(size); i++)
 
    {
 
        newData[i] = _sample(i - offset);
 
        newData[i] = sample(i - offset);
 
    }
 

	
 
    // fill the beginning of the new data
 
    if (fill_start > 0)
 
    {
 
        for (int i = 0; i < fill_start; i++)
 
@@ -138,22 +138,17 @@ void FrameBuffer::clear()
 

	
 
size_t FrameBuffer::size() const
 
{
 
    return _size;
 
}
 

	
 
QPointF FrameBuffer::sample(size_t i) const
 
{
 
    return QPointF(i, _sample(i));
 
}
 

	
 
QRectF FrameBuffer::boundingRect() const
 
{
 
    return _boundingRect;
 
}
 

	
 
double FrameBuffer::_sample(size_t i) const
 
double FrameBuffer::sample(size_t i) const
 
{
 
    size_t index = headIndex + i;
 
    if (index >= _size) index -= _size;
 
    return data[index];
 
}
src/framebuffer.h
Show inline comments
 
@@ -17,36 +17,33 @@
 
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#ifndef FRAMEBUFFER_H
 
#define FRAMEBUFFER_H
 

	
 
#include <qwt_series_data.h>
 
#include <QPointF>
 
#include <QRectF>
 

	
 
class FrameBuffer : public QwtSeriesData<QPointF>
 
class FrameBuffer
 
{
 
public:
 
    FrameBuffer(size_t size);
 
    ~FrameBuffer();
 

	
 
    void resize(size_t size);
 
    void addSamples(double* samples, size_t size);
 
    void clear(); // fill 0
 

	
 
    // QwtSeriesData implementations
 
    // QwtSeriesData related implementations
 
    size_t size() const;
 
    QPointF sample(size_t i) const;
 
    QRectF boundingRect() const;
 
    double sample(size_t i) const;
 

	
 
private:
 
    size_t _size; // size of `data`
 
    double* data;
 
    size_t headIndex; // indicates the actual `0` index of the ring buffer
 

	
 
    QRectF _boundingRect;
 

	
 
    double _sample(size_t i) const;
 
};
 

	
 
#endif // FRAMEBUFFER_H
src/framebufferseries.cpp
Show inline comments
 
new file 100644
 
/*
 
  Copyright © 2016 Hasan Yavuz Özderya
 

	
 
  This file is part of serialplot.
 

	
 
  serialplot is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU General Public License as published by
 
  the Free Software Foundation, either version 3 of the License, or
 
  (at your option) any later version.
 

	
 
  serialplot is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU General Public License for more details.
 

	
 
  You should have received a copy of the GNU General Public License
 
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#include "framebufferseries.h"
 

	
 
FrameBufferSeries::FrameBufferSeries(FrameBuffer* buffer)
 
{
 
    _buffer = buffer;
 
}
 

	
 
size_t FrameBufferSeries::size() const
 
{
 
    return _buffer->size();
 
}
 

	
 
QPointF FrameBufferSeries::sample(size_t i) const
 
{
 
    return QPointF(i, _buffer->sample(i));
 
}
 

	
 
QRectF FrameBufferSeries::boundingRect() const
 
{
 
    return _buffer->boundingRect();
 
}
src/framebufferseries.h
Show inline comments
 
new file 100644
 
/*
 
  Copyright © 2016 Hasan Yavuz Özderya
 

	
 
  This file is part of serialplot.
 

	
 
  serialplot is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU General Public License as published by
 
  the Free Software Foundation, either version 3 of the License, or
 
  (at your option) any later version.
 

	
 
  serialplot is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU General Public License for more details.
 

	
 
  You should have received a copy of the GNU General Public License
 
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#ifndef FRAMEBUFFERSERIES_H
 
#define FRAMEBUFFERSERIES_H
 

	
 
#include <QPointF>
 
#include <QRectF>
 
#include <qwt_series_data.h>
 

	
 
#include "framebuffer.h"
 

	
 
/**
 
 * This class provides an interface for actual FrameBuffer
 
 * object. That way we can keep our data structures relatively
 
 * isolated from Qwt. Otherwise QwtPlotCurve owns FrameBuffer
 
 * structures.
 
 */
 
class FrameBufferSeries : public QwtSeriesData<QPointF>
 
{
 
public:
 
    FrameBufferSeries(FrameBuffer* buffer);
 

	
 
    // QwtSeriesData implementations
 
    size_t size() const;
 
    QPointF sample(size_t i) const;
 
    QRectF boundingRect() const;
 

	
 
private:
 
    FrameBuffer* _buffer;
 
};
 

	
 
#endif // FRAMEBUFFERSERIES_H
src/mainwindow.cpp
Show inline comments
 
@@ -31,12 +31,13 @@
 
#include <limits.h>
 
#include <cmath>
 
#include <iostream>
 

	
 
#include <plot.h>
 

	
 
#include "framebufferseries.h"
 
#include "utils.h"
 
#include "defines.h"
 
#include "version.h"
 

	
 
#if defined(Q_OS_WIN) && defined(QT_STATIC)
 
#include <QtPlugin>
 
@@ -53,15 +54,16 @@ Q_DECLARE_METATYPE(Range);
 

	
 
MainWindow::MainWindow(QWidget *parent) :
 
    QMainWindow(parent),
 
    ui(new Ui::MainWindow),
 
    aboutDialog(this),
 
    portControl(&serialPort),
 
    channelMan(1, 1, this),
 
    commandPanel(&serialPort),
 
    dataFormatPanel(&serialPort, &channelBuffers),
 
    snapshotMan(this, &channelBuffers)
 
    dataFormatPanel(&serialPort, &channelMan),
 
    snapshotMan(this, &channelMan)
 
{
 
    ui->setupUi(this);
 
    ui->tabWidget->insertTab(0, &portControl, "Port");
 
    ui->tabWidget->insertTab(1, &dataFormatPanel, "Data Format");
 
    ui->tabWidget->insertTab(3, &commandPanel, "Commands");
 
    ui->tabWidget->setCurrentIndex(0);
 
@@ -150,23 +152,32 @@ MainWindow::MainWindow(QWidget *parent) 
 
                     &dataFormatPanel, &DataFormatPanel::pause);
 

	
 
    // init data arrays and plot
 
    numOfSamples = ui->spNumOfSamples->value();
 
    unsigned numOfChannels = dataFormatPanel.numOfChannels();
 

	
 
    QObject::connect(&dataFormatPanel,
 
                     &DataFormatPanel::numOfChannelsChanged,
 
                     this,
 
                     &MainWindow::onNumOfChannelsChanged);
 
    channelMan.setNumOfSamples(ui->spNumOfSamples->value());
 
    channelMan.setNumOfChannels(dataFormatPanel.numOfChannels());
 

	
 
    connect(&dataFormatPanel, &DataFormatPanel::numOfChannelsChanged,
 
            &channelMan, &ChannelManager::setNumOfChannels);
 

	
 
    // init channel data and curve list
 
    connect(&channelMan, &ChannelManager::numOfChannelsChanged,
 
            this, &MainWindow::onNumOfChannelsChanged);
 

	
 
    connect(&channelMan, &ChannelManager::channelNameChanged,
 
            this, &MainWindow::onChannelNameChanged);
 

	
 
    ui->lvChannelNames->setModel(channelMan.channelNames());
 

	
 
    // init curve list
 
    for (unsigned int i = 0; i < numOfChannels; i++)
 
    {
 
        channelBuffers.append(new FrameBuffer(numOfSamples));
 
        curves.append(new QwtPlotCurve(QString("Channel %1").arg(i+1)));
 
        curves[i]->setSamples(channelBuffers[i]);
 
        curves.append(new QwtPlotCurve(channelMan.channelName(i)));
 
        curves[i]->setSamples(
 
            new FrameBufferSeries(channelMan.channelBuffer(i)));
 
        curves[i]->setPen(Plot::makeColor(i));
 
        curves[i]->attach(ui->plot);
 
    }
 

	
 
    // init auto scale
 
    ui->plot->setAxis(ui->cbAutoScale->isChecked(),
 
@@ -316,58 +327,66 @@ required privileges or device is already
 
            break;
 
    }
 
}
 

	
 
void MainWindow::clearPlot()
 
{
 
    for (int ci = 0; ci < channelBuffers.size(); ci++)
 
    for (unsigned ci = 0; ci < channelMan.numOfChannels(); ci++)
 
    {
 
        channelBuffers[ci]->clear();
 
        channelMan.channelBuffer(ci)->clear();
 
    }
 
    ui->plot->replot();
 
}
 

	
 
void MainWindow::onNumOfSamplesChanged(int value)
 
{
 
    numOfSamples = value;
 

	
 
    for (int ci = 0; ci < channelBuffers.size(); ci++)
 
    {
 
        channelBuffers[ci]->resize(numOfSamples);
 
    }
 

	
 
    channelMan.setNumOfSamples(value);
 
    ui->plot->replot();
 
}
 

	
 
void MainWindow::onNumOfChannelsChanged(unsigned value)
 
{
 
    unsigned int oldNum = channelBuffers.size();
 
    unsigned int oldNum = curves.size();
 
    unsigned numOfChannels = value;
 

	
 
    if (numOfChannels > oldNum)
 
    {
 
        // add new channels
 
        for (unsigned int i = 0; i < numOfChannels - oldNum; i++)
 
        for (unsigned int i = oldNum; i < numOfChannels; i++)
 
        {
 
            channelBuffers.append(new FrameBuffer(numOfSamples));
 
            curves.append(new QwtPlotCurve(QString("Channel %1").arg(oldNum+i+1)));
 
            curves.last()->setSamples(channelBuffers.last());
 
            curves.last()->setPen(Plot::makeColor(curves.length()-1));
 
            curves.last()->attach(ui->plot);
 
            QwtPlotCurve* curve = new QwtPlotCurve(channelMan.channelName(i));
 
            curve->setSamples(
 
                new FrameBufferSeries(channelMan.channelBuffer(i)));
 
            curve->setPen(Plot::makeColor(i));
 
            curve->attach(ui->plot);
 
            curves.append(curve);
 
        }
 
    }
 
    else if(numOfChannels < oldNum)
 
    {
 
        // remove channels
 
        for (unsigned int i = 0; i < oldNum - numOfChannels; i++)
 
        {
 
            // also deletes owned FrameBuffer
 
            delete curves.takeLast();
 
            channelBuffers.removeLast();
 
        }
 
    }
 

	
 
    ui->plot->replot();
 
}
 

	
 
void MainWindow::onChannelNameChanged(unsigned channel, QString name)
 
{
 
    // This slot is triggered also when a new channel is added, in
 
    // this case curve list doesn't contain said channel. No worries,
 
    // since `onNumOfChannelsChanged` slot will update curve list.
 
    if ((int) channel < curves.size()) // check if channel exists in curve list
 
    {
 
        curves[channel]->setTitle(name);
 
        ui->plot->replot();
 
    }
 
}
 

	
 
void MainWindow::onAutoScaleChecked(bool checked)
 
{
 
    if (checked)
 
    {
 
@@ -451,25 +470,25 @@ void MainWindow::onExportCsv()
 
    {
 
        QFile file(fileName);
 
        if (file.open(QIODevice::WriteOnly | QIODevice::Text))
 
        {
 
            QTextStream fileStream(&file);
 

	
 
            unsigned numOfChannels = channelBuffers.size();
 
            unsigned numOfChannels = channelMan.numOfChannels();
 
            for (unsigned int ci = 0; ci < numOfChannels; ci++)
 
            {
 
                fileStream << "Channel " << ci;
 
                if (ci != numOfChannels-1) fileStream << ",";
 
            }
 
            fileStream << '\n';
 

	
 
            for (unsigned int i = 0; i < numOfSamples; i++)
 
            {
 
                for (unsigned int ci = 0; ci < numOfChannels; ci++)
 
                {
 
                    fileStream << channelBuffers[ci]->sample(i).y();
 
                    fileStream << channelMan.channelBuffer(ci)->sample(i);
 
                    if (ci != numOfChannels-1) fileStream << ",";
 
                }
 
                fileStream << '\n';
 
            }
 
        }
 
        else
src/mainwindow.h
Show inline comments
 
@@ -36,12 +36,13 @@
 

	
 
#include "portcontrol.h"
 
#include "commandpanel.h"
 
#include "dataformatpanel.h"
 
#include "ui_about_dialog.h"
 
#include "framebuffer.h"
 
#include "channelmanager.h"
 
#include "snapshotmanager.h"
 

	
 
namespace Ui {
 
class MainWindow;
 
}
 

	
 
@@ -66,13 +67,14 @@ private:
 
    PortControl portControl;
 

	
 
    unsigned int numOfSamples;
 

	
 
    QList<QwtPlotCurve*> curves;
 
    // Note: FrameBuffer s are owned by their respective QwtPlotCurve s.
 
    QList<FrameBuffer*> channelBuffers;
 
    // QList<FrameBuffer*> channelBuffers;
 
    ChannelManager channelMan;
 

	
 
    QLabel spsLabel;
 

	
 
    CommandPanel commandPanel;
 
    DataFormatPanel dataFormatPanel;
 

	
 
@@ -87,12 +89,13 @@ private slots:
 

	
 
    void onNumOfSamplesChanged(int value);
 
    void onAutoScaleChecked(bool checked);
 
    void onYScaleChanged();
 
    void onRangeSelected();
 
    void onNumOfChannelsChanged(unsigned value);
 
    void onChannelNameChanged(unsigned channel, QString name);
 

	
 
    void clearPlot();
 

	
 
    void onSpsChanged(unsigned sps);
 

	
 
    void enableDemo(bool enabled);
src/mainwindow.ui
Show inline comments
 
@@ -54,23 +54,34 @@
 
      <widget class="QWidget" name="tabPlot">
 
       <attribute name="title">
 
        <string>Plot</string>
 
       </attribute>
 
       <layout class="QHBoxLayout" name="horizontalLayout_4">
 
        <item>
 
         <spacer name="horizontalSpacer_2">
 
          <property name="orientation">
 
           <enum>Qt::Horizontal</enum>
 
          </property>
 
          <property name="sizeHint" stdset="0">
 
           <size>
 
            <width>370</width>
 
            <height>20</height>
 
           </size>
 
          </property>
 
         </spacer>
 
         <layout class="QVBoxLayout" name="verticalLayout">
 
          <item>
 
           <widget class="QLabel" name="label_2">
 
            <property name="styleSheet">
 
             <string notr="true">font-weight: bold;</string>
 
            </property>
 
            <property name="text">
 
             <string>Channel Names:</string>
 
            </property>
 
           </widget>
 
          </item>
 
          <item>
 
           <widget class="QListView" name="lvChannelNames">
 
            <property name="maximumSize">
 
             <size>
 
              <width>16777215</width>
 
              <height>170</height>
 
             </size>
 
            </property>
 
           </widget>
 
          </item>
 
         </layout>
 
        </item>
 
        <item>
 
         <widget class="Line" name="line">
 
          <property name="orientation">
 
           <enum>Qt::Vertical</enum>
 
          </property>
src/snapshot.cpp
Show inline comments
 
@@ -89,12 +89,22 @@ void Snapshot::setName(QString name)
 
{
 
    _name = name;
 
    _showAction.setText(_name);
 
    emit nameChanged(this);
 
}
 

	
 
void Snapshot::setChannelNames(QStringList names)
 
{
 
    _channelNames = names;
 
}
 

	
 
QString Snapshot::channelName(unsigned channel)
 
{
 
    return _channelNames[channel];
 
}
 

	
 
void Snapshot::save(QString fileName)
 
{
 
    // TODO: remove code duplication (MainWindow::onExportCsv)
 
    QSaveFile file(fileName);
 

	
 
    if (file.open(QIODevice::WriteOnly | QIODevice::Text))
 
@@ -104,13 +114,13 @@ void Snapshot::save(QString fileName)
 
        unsigned numOfChannels = data.size();
 
        unsigned numOfSamples = data[0].size();
 

	
 
        // print header
 
        for (unsigned int ci = 0; ci < numOfChannels; ci++)
 
        {
 
            fileStream << "Channel " << ci;
 
            fileStream << channelName(ci);
 
            if (ci != numOfChannels-1) fileStream << ",";
 
        }
 
        fileStream << '\n';
 

	
 
        // print rows
 
        for (unsigned int i = 0; i < numOfSamples; i++)
src/snapshot.h
Show inline comments
 
@@ -22,12 +22,13 @@
 

	
 
#include <QObject>
 
#include <QMainWindow>
 
#include <QAction>
 
#include <QVector>
 
#include <QString>
 
#include <QStringList>
 

	
 
class SnapshotView;
 

	
 
class Snapshot : public QObject
 
{
 
    Q_OBJECT
 
@@ -39,21 +40,24 @@ public:
 
    QVector<QVector<QPointF>> data;
 
    QAction* showAction();
 
    QAction* deleteAction();
 

	
 
    QString name();
 
    void setName(QString name);
 
    void setChannelNames(QStringList names); // must be called when setting data!
 
    QString channelName(unsigned channel);
 

	
 
    void save(QString fileName); /// save snapshot data as CSV
 

	
 
signals:
 
    void deleteRequested(Snapshot*);
 
    void nameChanged(Snapshot*);
 

	
 
private:
 
    QString _name;
 
    QStringList _channelNames;
 
    QAction _showAction;
 
    QAction _deleteAction;
 
    QMainWindow* mainWindow;
 
    SnapshotView* view;
 

	
 
private slots:
src/snapshotmanager.cpp
Show inline comments
 
@@ -21,24 +21,25 @@
 
#include <QMenuBar>
 
#include <QKeySequence>
 
#include <QFileDialog>
 
#include <QFile>
 
#include <QVector>
 
#include <QPointF>
 
#include <QtDebug>
 

	
 
#include "snapshotmanager.h"
 

	
 
SnapshotManager::SnapshotManager(QMainWindow* mainWindow,
 
                                 QList<FrameBuffer*>* channelBuffers) :
 
                                 ChannelManager* channelMan) :
 
    _menu("Snapshots"),
 
    _takeSnapshotAction("Take Snapshot", this),
 
    loadSnapshotAction("Load Snapshots", this),
 
    clearAction("Clear Snapshots", this)
 
{
 
    _mainWindow = mainWindow;
 
    _channelBuffers = channelBuffers;
 
    _channelMan = channelMan;
 

	
 
    _takeSnapshotAction.setToolTip("Take a snapshot of current plot");
 
    _takeSnapshotAction.setShortcut(QKeySequence("F5"));
 
    loadSnapshotAction.setToolTip("Load snapshots from CSV files");
 
    clearAction.setToolTip("Delete all snapshots");
 
    connect(&_takeSnapshotAction, SIGNAL(triggered(bool)),
 
@@ -61,23 +62,24 @@ SnapshotManager::~SnapshotManager()
 

	
 
void SnapshotManager::takeSnapshot()
 
{
 
    QString name = QTime::currentTime().toString("'Snapshot ['HH:mm:ss']'");
 
    auto snapshot = new Snapshot(_mainWindow, name);
 

	
 
    unsigned numOfChannels = _channelBuffers->size();
 
    unsigned numOfSamples = _channelBuffers->at(0)->size();
 
    unsigned numOfChannels = _channelMan->numOfChannels();
 
    unsigned numOfSamples = _channelMan->numOfSamples();
 

	
 
    for (unsigned ci = 0; ci < numOfChannels; ci++)
 
    {
 
        snapshot->data.append(QVector<QPointF>(numOfSamples));
 
        for (unsigned i = 0; i < numOfSamples; i++)
 
        {
 
            snapshot->data[ci][i] = _channelBuffers->at(ci)->sample(i);
 
            snapshot->data[ci][i] = QPointF(i, _channelMan->channelBuffer(ci)->sample(i));
 
        }
 
    }
 
    snapshot->setChannelNames(_channelMan->channelNames()->stringList());
 

	
 
    addSnapshot(snapshot);
 
}
 

	
 
void SnapshotManager::addSnapshot(Snapshot* snapshot, bool update_menu)
 
{
 
@@ -142,13 +144,14 @@ void SnapshotManager::loadSnapshotFromFi
 
        qCritical() << file.errorString();
 
        return;
 
    }
 

	
 
    // read first row as headlines and determine number of channels
 
    auto headLine = QString(file.readLine());
 
    unsigned numOfChannels = headLine.split(',').size();
 
    QStringList channelNames = headLine.split(',');
 
    unsigned numOfChannels = channelNames.size();
 

	
 
    // read data
 
    QVector<QVector<QPointF>> data(numOfChannels);
 
    unsigned lineNum = 1;
 
    while (file.canReadLine())
 
    {
 
@@ -180,12 +183,13 @@ void SnapshotManager::loadSnapshotFromFi
 
        }
 
        lineNum++;
 
    }
 

	
 
    auto snapshot = new Snapshot(_mainWindow, QFileInfo(fileName).baseName());
 
    snapshot->data = data;
 
    snapshot->setChannelNames(channelNames);
 

	
 
    addSnapshot(snapshot, false);
 
}
 

	
 
QMenu* SnapshotManager::menu()
 
{
src/snapshotmanager.h
Show inline comments
 
@@ -22,28 +22,29 @@
 

	
 
#include <QObject>
 
#include <QAction>
 
#include <QMenu>
 

	
 
#include "framebuffer.h"
 
#include "channelmanager.h"
 
#include "snapshot.h"
 

	
 
class SnapshotManager : public QObject
 
{
 
    Q_OBJECT
 

	
 
public:
 
    SnapshotManager(QMainWindow* mainWindow, QList<FrameBuffer*>* channelBuffers);
 
    SnapshotManager(QMainWindow* mainWindow, ChannelManager* channelMan);
 
    ~SnapshotManager();
 

	
 
    QMenu* menu();
 
    QAction* takeSnapshotAction();
 

	
 
private:
 
    QMainWindow* _mainWindow;
 
    QList<FrameBuffer*>* _channelBuffers;
 
    ChannelManager* _channelMan;
 

	
 
    QList<Snapshot*> snapshots;
 

	
 
    QMenu _menu;
 
    QAction _takeSnapshotAction;
 
    QAction loadSnapshotAction;
src/snapshotview.cpp
Show inline comments
 
@@ -32,13 +32,13 @@ SnapshotView::SnapshotView(QWidget *pare
 
    this->setWindowTitle(snapshot->name());
 

	
 
    unsigned numOfChannels = snapshot->data.size();
 

	
 
    for (unsigned ci = 0; ci < numOfChannels; ci++)
 
    {
 
        QwtPlotCurve* curve = new QwtPlotCurve();
 
        QwtPlotCurve* curve = new QwtPlotCurve(snapshot->channelName(ci));
 
        curves.append(curve);
 
        curve->setSamples(snapshot->data[ci]);
 
        curve->setPen(Plot::makeColor(ci));
 
        curve->attach(ui->plot);
 
    }
 

	
0 comments (0 inline, 0 general)