Changeset - 5a4e6dd9042e
[Not reviewed]
plot-manager
0 4 2
Hasan Yavuz ÖZDERYA - 9 years ago 2016-08-21 16:43:07
hy@ozderya.net
plot manager implementation for multi plot display (not integrated yet)
6 files changed with 255 insertions and 2 deletions:
0 comments (0 inline, 0 general)
CMakeLists.txt
Show inline comments
 
@@ -114,6 +114,7 @@ add_executable(${PROGRAM_NAME} WIN32
 
  src/demoreader.cpp
 
  src/framedreader.cpp
 
  src/framedreadersettings.cpp
 
  src/plotmanager.cpp
 
  misc/windows_icon.rc
 
  ${UI_FILES}
 
  ${RES_FILES}
serialplot.pro
Show inline comments
 
@@ -65,7 +65,8 @@ SOURCES += \
 
    src/asciireadersettings.cpp \
 
    src/asciireader.cpp \
 
    src/demoreader.cpp \
 
    src/framedreader.cpp
 
    src/framedreader.cpp \
 
    src/plotmanager.cpp
 

	
 
HEADERS += \
 
    src/mainwindow.h \
 
@@ -101,7 +102,8 @@ HEADERS += \
 
    src/asciireadersettings.h \
 
    src/asciireader.h \
 
    src/demoreader.h \
 
    src/framedreader.h
 
    src/framedreader.h \
 
    src/plotmanager.h
 

	
 
FORMS += \
 
    src/mainwindow.ui \
src/mainwindow.cpp
Show inline comments
 
@@ -54,7 +54,17 @@ MainWindow::MainWindow(QWidget *parent) 
 
    dataFormatPanel(&serialPort, &channelMan),
 
    snapshotMan(this, &channelMan)
 
{
 

	
 
    ui->setupUi(this);
 
    ui->plot->setParent(NULL);
 
    ui->plot->show();
 

	
 
    ((QVBoxLayout*) ui->centralWidget->layout())->insertWidget(0, &plotArea);
 
    plotMan = new PlotManager(&plotArea);
 

	
 
    // test code
 
    // plotArea.show();
 

	
 
    ui->tabWidget->insertTab(0, &portControl, "Port");
 
    ui->tabWidget->insertTab(1, &dataFormatPanel, "Data Format");
 
    ui->tabWidget->insertTab(2, &plotControlPanel, "Plot");
src/mainwindow.h
Show inline comments
 
@@ -42,6 +42,7 @@
 
#include "framebuffer.h"
 
#include "channelmanager.h"
 
#include "snapshotmanager.h"
 
#include "plotmanager.h"
 

	
 
namespace Ui {
 
class MainWindow;
 
@@ -85,6 +86,10 @@ private:
 
    QwtPlotTextLabel demoIndicator;
 
    bool isDemoRunning();
 

	
 
    // test widget
 
    QWidget plotArea;
 
    PlotManager* plotMan;
 

	
 
private slots:
 
    void onPortToggled(bool open);
 
    void onPortError(QSerialPort::SerialPortError error);
src/plotmanager.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 "plot.h"
 

	
 
#include "plotmanager.h"
 

	
 
#include <QtDebug>
 

	
 
PlotManager::PlotManager(QWidget* plotArea, QObject *parent) :
 
    QObject(parent),
 
    _plotArea(plotArea)
 
{
 
    // initalize layout and single widget
 
    isMulti = false;
 
    scrollArea = NULL;
 
    setupLayout(isMulti);
 
    addPlotWidget();
 

	
 
    // test code
 
    addCurve("test", new FrameBuffer(100));
 
    addCurve("test", new FrameBuffer(100));
 
    addCurve("test", new FrameBuffer(100));
 

	
 
    setMulti(true);
 
    setMulti(false);
 
}
 

	
 
PlotManager::~PlotManager()
 
{
 
    while (curves.size())
 
    {
 
        delete curves.takeLast();
 
    }
 

	
 
    // remove all widgets
 
    while (plotWidgets.size())
 
    {
 
        delete plotWidgets.takeLast();
 
    }
 

	
 
    if (scrollArea != NULL) delete scrollArea;
 
}
 

	
 
void PlotManager::setMulti(bool enabled)
 
{
 
    if (enabled == isMulti) return;
 

	
 
    isMulti = enabled;
 

	
 
    // detach all curves
 
    for (auto curve : curves)
 
    {
 
        curve->detach();
 
    }
 

	
 
    // remove all widgets
 
    while (plotWidgets.size())
 
    {
 
        delete plotWidgets.takeLast();
 
    }
 

	
 
    // setup new layout
 
    setupLayout(isMulti);
 

	
 
    if (isMulti)
 
    {
 
        // add new widgets and attach
 
        for (auto curve : curves)
 
        {
 
            curve->attach(addPlotWidget());
 
        }
 
    }
 
    else
 
    {
 
        // add a single widget
 
        auto plot = addPlotWidget();
 

	
 
        // attach all curves
 
        for (auto curve : curves)
 
        {
 
            curve->attach(plot);
 
        }
 
    }
 
}
 

	
 
void PlotManager::setupLayout(bool multiPlot)
 
{
 
    // delete previous layout if it exists
 
    if (_plotArea->layout() != 0)
 
    {
 
        delete _plotArea->layout();
 
    }
 

	
 
    if (multiPlot)
 
    {
 
        // setup a scroll area
 
        scrollArea = new QScrollArea();
 
        auto scrolledPlotArea = new QWidget(scrollArea);
 
        scrollArea->setWidget(scrolledPlotArea);
 
        scrollArea->setWidgetResizable(true);
 

	
 
        _plotArea->setLayout(new QVBoxLayout());
 
        _plotArea->layout()->addWidget(scrollArea);
 

	
 
        layout = new QVBoxLayout(scrolledPlotArea);
 
    }
 
    else
 
    {
 
        // delete scrollArea left from multi layout
 
        if (scrollArea != NULL) delete scrollArea;
 

	
 
        layout = new QVBoxLayout(_plotArea);
 
    }
 

	
 
    layout->setSpacing(1);
 
}
 

	
 
Plot* PlotManager::addPlotWidget()
 
{
 
    auto plot = new Plot();
 
    plotWidgets.append(plot);
 
    layout->addWidget(plot);
 
    return plot;
 
}
 

	
 
void PlotManager::addCurve(QString title, FrameBuffer* buffer)
 
{
 
    Plot* plot;
 

	
 
    if (isMulti)
 
    {
 
        // create a new plot widget
 
        plot = addPlotWidget();
 
    }
 
    else
 
    {
 
        plot = plotWidgets[0];
 
    }
 

	
 
    // create the curve
 
    QwtPlotCurve* curve = new QwtPlotCurve(title);
 
    curves.append(curve);
 

	
 
    curve->setSamples(new FrameBufferSeries(buffer));
 
    unsigned index = curves.size()-1;
 
    curve->setPen(Plot::makeColor(index));
 

	
 
    curve->attach(plot);
 
}
src/plotmanager.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 PLOTMANAGER_H
 
#define PLOTMANAGER_H
 

	
 
#include <QObject>
 
#include <QWidget>
 
#include <QScrollArea>
 
#include <QVBoxLayout>
 
#include <QList>
 

	
 
#include <qwt_plot_curve.h>
 
#include "plot.h"
 
#include "framebufferseries.h"
 

	
 
class PlotManager : public QObject
 
{
 
    Q_OBJECT
 

	
 
public:
 
    explicit PlotManager(QWidget* plotArea, QObject *parent = 0);
 
    ~PlotManager();
 

	
 
    /// Add a new curve with title and buffer. A color is
 
    /// automatically chosen for curve.
 
    void addCurve(QString title, FrameBuffer* buffer);
 

	
 
    /// Set the displayed title for a curve
 
    void setTitle(unsigned index, QString title);
 

	
 
    /// Removes curves from the end
 
    void removeCurves(unsigned number);
 

	
 
signals:
 

	
 
public slots:
 
    /// Enable/Disable multiple plot display
 
    void setMulti(bool enabled);
 

	
 
private:
 
    bool isMulti;
 
    QWidget* _plotArea;
 
    QVBoxLayout* layout; ///< layout of the `plotArea`
 
    QScrollArea* scrollArea;
 
    QList<QwtPlotCurve*> curves;
 
    QList<Plot*> plotWidgets;
 

	
 
    void setupLayout(bool multiPlot);
 
    Plot* addPlotWidget(); ///< inserts a new plot widget to the current layout
 
};
 

	
 
#endif // PLOTMANAGER_H
0 comments (0 inline, 0 general)