Changeset - 797666010f26
[Not reviewed]
Hasan Yavuz Ă–ZDERYA - 10 years ago 2016-03-24 17:43:38
hy@ozderya.net
implemented framebufferseries to isolate qwtplotcurve from framebuffer
10 files changed with 73 insertions and 24 deletions:
0 comments (0 inline, 0 general)
CMakeLists.txt
Show inline comments
 
@@ -81,24 +81,25 @@ add_executable(${PROGRAM_NAME} WIN32
 
  src/portlist.cpp
 
  src/snapshot.cpp
 
  src/snapshotview.cpp
 
  src/snapshotmanager.cpp
 
  src/plotsnapshotoverlay.cpp
 
  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.
 
target_link_libraries(${PROGRAM_NAME} ${QWT_LIBRARY})
 
qt5_use_modules(${PROGRAM_NAME} Widgets SerialPort Svg)
 

	
 
# set compiler flags
 
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
 

	
serialplot.pro
Show inline comments
 
@@ -44,49 +44,51 @@ SOURCES += \
 
    src/scalezoomer.cpp \
 
    src/portlist.cpp \
 
    src/snapshotview.cpp \
 
    src/snapshotmanager.cpp \
 
    src/snapshot.cpp \
 
    src/plotsnapshotoverlay.cpp \
 
    src/commandpanel.cpp \
 
    src/commandwidget.cpp \
 
    src/commandedit.cpp \
 
    src/dataformatpanel.cpp \
 
    src/tooltipfilter.cpp \
 
    src/sneakylineedit.cpp \
 
    src/channelmanager.cpp
 
    src/channelmanager.cpp \
 
    src/framebufferseries.cpp
 

	
 
HEADERS += \
 
    src/mainwindow.h \
 
    src/utils.h \
 
    src/portcontrol.h \
 
    src/floatswap.h \
 
    src/plot.h \
 
    src/zoomer.h \
 
    src/hidabletabwidget.h \
 
    src/framebuffer.h \
 
    src/scalepicker.h \
 
    src/scalezoomer.h \
 
    src/portlist.h \
 
    src/snapshotview.h \
 
    src/snapshotmanager.h \
 
    src/snapshot.h \
 
    src/plotsnapshotoverlay.h \
 
    src/commandpanel.h \
 
    src/commandwidget.h \
 
    src/commandedit.h \
 
    src/dataformatpanel.h \
 
    src/tooltipfilter.h \
 
    src/sneakylineedit.h \
 
    src/channelmanager.h
 
    src/channelmanager.h \
 
    src/framebufferseries.h
 

	
 
FORMS += \
 
    src/mainwindow.ui \
 
    src/about_dialog.ui \
 
    src/portcontrol.ui \
 
    src/snapshotview.ui \
 
    src/commandpanel.ui \
 
    src/commandwidget.ui \
 
    src/dataformatpanel.ui
 

	
 
INCLUDEPATH += qmake/ src/
 

	
src/channelmanager.cpp
Show inline comments
 
@@ -25,40 +25,38 @@
 
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);
 

	
 
        // curves.append(new QwtPlotCurve(QString("Channel %1").arg(i+1)));
 
        // curves[i]->setSamples(channelBuffers[i]);
 
        // curves[i]->setPen(Plot::makeColor(i));
 
        // curves[i]->attach(ui->plot);
 
    }
 

	
 
    _channelNames.setStringList(channelNamesList);
 

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

	
 
ChannelManager::~ChannelManager()
 
{
 
    // TODO: remove all channelBuffers
 
    for (auto buffer : channelBuffers)
 
    {
 
        delete buffer;
 
    }
 
}
 

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

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

	
 
@@ -71,28 +69,25 @@ void ChannelManager::setNumOfChannels(un
 
        // 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--)
 
        {
 
            // also deletes owned FrameBuffer
 
            // delete curves.takeLast();
 
            // TODO: important, remove channelBuffer
 
            channelBuffers.removeLast();
 
            delete channelBuffers.takeLast();
 
            _channelNames.removeRow(i);
 
        }
 
    }
 

	
 
    emit numOfChannelsChanged(number);
 
}
 

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

	
 
    for (int ci = 0; ci < channelBuffers.size(); ci++)
 
@@ -124,24 +119,25 @@ void ChannelManager::setChannelName(unsi
 
}
 

	
 
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)
 
{
src/channelmanager.h
Show inline comments
 
@@ -48,21 +48,21 @@ signals:
 
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
 
    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
 
@@ -12,24 +12,25 @@
 
  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 "dataformatpanel.h"
 
#include "ui_dataformatpanel.h"
 

	
 
#include <QtEndian>
 
#include <QtDebug>
 

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

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

	
src/framebuffer.h
Show inline comments
 
@@ -11,29 +11,28 @@
 
  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 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
 
    size_t size() const;
 
    QPointF sample(size_t i) const;
src/framebufferseries.cpp
Show inline comments
 
new file 100644
 

	
 
#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 _buffer->sample(i);
 
}
 

	
 
QRectF FrameBufferSeries::boundingRect() const
 
{
 
    return _buffer->boundingRect();
 
}
src/framebufferseries.h
Show inline comments
 
new file 100644
 

	
 
#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
 
@@ -22,29 +22,27 @@
 
#include <QByteArray>
 
#include <QApplication>
 
#include <QFileDialog>
 
#include <QFile>
 
#include <QTextStream>
 
#include <QMenu>
 
#include <QtDebug>
 
#include <qwt_plot.h>
 
#include <limits.h>
 
#include <cmath>
 
#include <iostream>
 

	
 
// test code
 
#include <QListView>
 

	
 
#include <plot.h>
 

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

	
 
#if defined(Q_OS_WIN) && defined(QT_STATIC)
 
#include <QtPlugin>
 
Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
 
#endif
 

	
 
struct Range
 
{
 
    double rmin;
 
    double rmax;
 
@@ -161,25 +159,26 @@ MainWindow::MainWindow(QWidget *parent) 
 
    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++)
 
    {
 
        curves.append(new QwtPlotCurve(channelMan.channelName(i)));
 
        curves[i]->setSamples(channelMan.channelBuffer(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(),
 
                      ui->spYmin->value(), ui->spYmax->value());
 

	
 
    // init scale range preset list
 
    for (int nbits = 8; nbits <= 24; nbits++) // signed binary formats
 
    {
 
        int rmax = pow(2, nbits-1)-1;
 
@@ -342,39 +341,36 @@ void MainWindow::onNumOfSamplesChanged(i
 

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

	
 
    if (numOfChannels > oldNum)
 
    {
 
        // add new channels
 
        for (unsigned int i = oldNum; i < numOfChannels; i++)
 
        {
 
            QwtPlotCurve* curve = new QwtPlotCurve(channelMan.channelName(i));
 
            // TODO: create a wrapper around FrameBuffer that holds a
 
            // pointer to it and provides the QwtDataSeries interface,
 
            // that wrapper should be created for and owned by 'curve'
 
            curve->setSamples(channelMan.channelBuffer(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 TODO: which souldn't happen
 
            delete curves.takeLast();
 
        }
 
    }
 

	
 
    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.
src/snapshotmanager.cpp
Show inline comments
 
@@ -15,24 +15,25 @@
 

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

	
 
#include <QTime>
 
#include <QMenuBar>
 
#include <QKeySequence>
 
#include <QFileDialog>
 
#include <QFile>
 
#include <QVector>
 
#include <QPointF>
 
#include <QtDebug>
 

	
 
#include "snapshotmanager.h"
 

	
 
SnapshotManager::SnapshotManager(QMainWindow* mainWindow,
 
                                 ChannelManager* channelMan) :
 
    _menu("Snapshots"),
 
    _takeSnapshotAction("Take Snapshot", this),
 
    loadSnapshotAction("Load Snapshots", this),
 
    clearAction("Clear Snapshots", this)
 
{
 
    _mainWindow = mainWindow;
 
    _channelMan = channelMan;
0 comments (0 inline, 0 general)