# HG changeset patch # User Hasan Yavuz ÖZDERYA # Date 2016-08-21 16:43:07 # Node ID 5a4e6dd9042e9bcb6b28c1322f9c5bebd7dbc3dd # Parent ebfc27478fc5c5553fdf662eabbaa8518b397d58 plot manager implementation for multi plot display (not integrated yet) diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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} diff --git a/serialplot.pro b/serialplot.pro --- a/serialplot.pro +++ b/serialplot.pro @@ -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 \ diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -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"); diff --git a/src/mainwindow.h b/src/mainwindow.h --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -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); diff --git a/src/plotmanager.cpp b/src/plotmanager.cpp new file mode 100644 --- /dev/null +++ b/src/plotmanager.cpp @@ -0,0 +1,166 @@ +/* + 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); +} diff --git a/src/plotmanager.h b/src/plotmanager.h new file mode 100644 --- /dev/null +++ b/src/plotmanager.h @@ -0,0 +1,69 @@ +/* + 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 . +*/ + +#ifndef PLOTMANAGER_H +#define PLOTMANAGER_H + +#include +#include +#include +#include +#include + +#include +#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 curves; + QList plotWidgets; + + void setupLayout(bool multiPlot); + Plot* addPlotWidget(); ///< inserts a new plot widget to the current layout +}; + +#endif // PLOTMANAGER_H