/*
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 .
*/
#include "plot.h"
#include "plotmanager.h"
#include
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);
plot->replot();
}
void PlotManager::removeCurves(unsigned number)
{
for (unsigned i = 0; i < number; i++)
{
if (!curves.isEmpty())
{
delete curves.takeLast();
if (isMulti) // delete corresponding widget as well
{
delete plotWidgets.takeLast();
}
}
}
}
unsigned PlotManager::numOfCurves()
{
return curves.size();
}
void PlotManager::setTitle(unsigned index, QString title)
{
curves[index]->setTitle(title);
plotWidget(index)->replot();
}
Plot* PlotManager::plotWidget(unsigned curveIndex)
{
if (isMulti)
{
return plotWidgets[curveIndex];
}
else
{
return plotWidgets[0];
}
}