diff --git a/src/plotmanager.cpp b/src/plotmanager.cpp --- a/src/plotmanager.cpp +++ b/src/plotmanager.cpp @@ -28,9 +28,57 @@ #include "setting_defines.h" PlotManager::PlotManager(QWidget* plotArea, PlotMenu* menu, - ChannelInfoModel* infoModel, QObject* parent) : + const Stream* stream, QObject* parent) : QObject(parent) { + construct(plotArea, menu); + _stream = stream; + if (_stream == NULL) return; + + // connect to ChannelInfoModel + infoModel = _stream->infoModel(); + connect(infoModel, &QAbstractItemModel::dataChanged, + this, &PlotManager::onChannelInfoChanged); + connect(infoModel, &QAbstractItemModel::modelReset, + [this]() + { + onChannelInfoChanged(infoModel->index(0, 0), // start + infoModel->index(infoModel->rowCount()-1, 0), // end + {}); // roles ignored + }); + + connect(stream, &Stream::numChannelsChanged, this, &PlotManager::onNumChannelsChanged); + connect(stream, &Stream::dataAdded, this, &PlotManager::replot); + + // add initial curves if any? + for (unsigned int i = 0; i < stream->numChannels(); i++) + { + addCurve(stream->channel(i)->name(), stream->channel(i)->yData()); + } + +} + +PlotManager::PlotManager(QWidget* plotArea, PlotMenu* menu, + Snapshot* snapshot, QObject *parent) : + QObject(parent) +{ + construct(plotArea, menu); + + setNumOfSamples(snapshot->numSamples()); + setPlotWidth(snapshot->numSamples()); + infoModel = snapshot->infoModel(); + + for (unsigned ci = 0; ci < snapshot->numChannels(); ci++) + { + addCurve(snapshot->channelName(ci), snapshot->yData[ci]); + } + + connect(infoModel, &QAbstractItemModel::dataChanged, + this, &PlotManager::onChannelInfoChanged); +} + +void PlotManager::construct(QWidget* plotArea, PlotMenu* menu) +{ _menu = menu; _plotArea = plotArea; _autoScaled = true; @@ -38,7 +86,6 @@ PlotManager::PlotManager(QWidget* plotAr _yMax = 1; _xAxisAsIndex = true; isDemoShown = false; - _infoModel = infoModel; _numOfSamples = 1; _plotWidth = 1; showSymbols = Plot::ShowSymbolsAuto; @@ -72,21 +119,6 @@ PlotManager::PlotManager(QWidget* plotAr darkBackground(menu->darkBackgroundAction.isChecked()); showLegend(menu->showLegendAction.isChecked()); setMulti(menu->showMultiAction.isChecked()); - - // connect to channel info model - if (_infoModel != NULL) // TODO: remove when snapshots have infomodel - { - connect(_infoModel, &QAbstractItemModel::dataChanged, - this, &PlotManager::onChannelInfoChanged); - - connect(_infoModel, &QAbstractItemModel::modelReset, - [this]() - { - onChannelInfoChanged(_infoModel->index(0, 0), // start - _infoModel->index(_infoModel->rowCount()-1, 0), // end - {}); // roles ignored - }); - } } PlotManager::~PlotManager() @@ -106,6 +138,27 @@ PlotManager::~PlotManager() if (emptyPlot != NULL) delete emptyPlot; } +void PlotManager::onNumChannelsChanged(unsigned value) +{ + unsigned int oldNum = numOfCurves(); + unsigned numOfChannels = value; + + if (numOfChannels > oldNum) + { + // add new channels + for (unsigned int i = oldNum; i < numOfChannels; i++) + { + addCurve(_stream->channel(i)->name(), _stream->channel(i)->yData()); + } + } + else if(numOfChannels < oldNum) + { + removeCurves(oldNum - numOfChannels); + } + + replot(); +} + void PlotManager::onChannelInfoChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector &roles) @@ -279,7 +332,7 @@ Plot* PlotManager::addPlotWidget() return plot; } -void PlotManager::addCurve(QString title, FrameBuffer* buffer) +void PlotManager::addCurve(QString title, const FrameBuffer* buffer) { auto curve = new QwtPlotCurve(title); auto series = new FrameBufferSeries(buffer); @@ -288,20 +341,13 @@ void PlotManager::addCurve(QString title _addCurve(curve); } -void PlotManager::addCurve(QString title, QVector data) -{ - auto curve = new QwtPlotCurve(title); - curve->setSamples(data); - _addCurve(curve); -} - void PlotManager::_addCurve(QwtPlotCurve* curve) { // store and init the curve curves.append(curve); unsigned index = curves.size()-1; - auto color = _infoModel->color(index); + auto color = infoModel->color(index); curve->setPen(color); // create the plot for the curve if we are on multi display @@ -437,7 +483,6 @@ void PlotManager::setXAxis(bool asIndex, _xMax = xMax; for (auto curve : curves) { - // TODO: what happens when addCurve(QVector) is used? FrameBufferSeries* series = static_cast(curve->data()); series->setXAxis(asIndex, xMin, xMax); }