diff --git a/src/plotmanager.cpp b/src/plotmanager.cpp --- a/src/plotmanager.cpp +++ b/src/plotmanager.cpp @@ -1,5 +1,5 @@ /* - Copyright © 2016 Hasan Yavuz Özderya + Copyright © 2017 Hasan Yavuz Özderya This file is part of serialplot. @@ -18,13 +18,14 @@ */ #include +#include "qwt_symbol.h" #include "plot.h" #include "plotmanager.h" #include "utils.h" #include "setting_defines.h" -PlotManager::PlotManager(QWidget* plotArea, QObject *parent) : +PlotManager::PlotManager(QWidget* plotArea, ChannelInfoModel* infoModel, QObject *parent) : QObject(parent), _plotArea(plotArea), showGridAction("&Grid", this), @@ -38,6 +39,7 @@ PlotManager::PlotManager(QWidget* plotAr _yMin = 0; _yMax = 1; isDemoShown = false; + _infoModel = infoModel; // initalize layout and single widget isMulti = false; @@ -85,6 +87,21 @@ PlotManager::PlotManager(QWidget* plotAr this, &PlotManager::showLegend); connect(&showMultiAction, SELECT::OVERLOAD_OF(&QAction::triggered), this, &PlotManager::setMulti); + + // 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() @@ -103,6 +120,46 @@ PlotManager::~PlotManager() if (scrollArea != NULL) delete scrollArea; } +void PlotManager::onChannelInfoChanged(const QModelIndex &topLeft, + const QModelIndex &bottomRight, + const QVector &roles) +{ + int start = topLeft.row(); + int end = bottomRight.row(); + + for (int ci = start; ci <= end; ci++) + { + QString name = topLeft.sibling(ci, ChannelInfoModel::COLUMN_NAME).data(Qt::EditRole).toString(); + QColor color = topLeft.sibling(ci, ChannelInfoModel::COLUMN_NAME).data(Qt::ForegroundRole).value(); + bool visible = topLeft.sibling(ci, ChannelInfoModel::COLUMN_VISIBILITY).data(Qt::CheckStateRole).toBool(); + + curves[ci]->setTitle(name); + curves[ci]->setPen(color); + curves[ci]->setVisible(visible); + curves[ci]->setItemAttribute(QwtPlotItem::Legend, visible); + + // replot only updated widgets + if (isMulti) + { + plotWidgets[ci]->updateSymbols(); // required for color change + plotWidgets[ci]->updateLegend(curves[ci]); + plotWidgets[ci]->setVisible(visible); + if (visible) + { + plotWidgets[ci]->replot(); + } + } + } + + // replot single widget + if (!isMulti) + { + plotWidgets[0]->updateSymbols(); + plotWidgets[0]->updateLegend(); + replot(); + } +} + void PlotManager::setMulti(bool enabled) { if (enabled == isMulti) return; @@ -219,7 +276,8 @@ void PlotManager::_addCurve(QwtPlotCurve curves.append(curve); unsigned index = curves.size()-1; - curve->setPen(Plot::makeColor(index)); + auto color = _infoModel->color(index); + curve->setPen(color); // create the plot for the curve if we are on multi display Plot* plot;