diff --git a/src/widgets/plotwidget.h b/src/widgets/plotwidget.h --- a/src/widgets/plotwidget.h +++ b/src/widgets/plotwidget.h @@ -41,13 +41,13 @@ * @brief Create a new plot * @param name p_name: plot name */ - void newPlot(const QString &name); + void addPlot(const QString &name); /** * @brief Delete plot with name * @param name p_name: name */ - void deletePlot(const QString &name); + void removePlot(const QString &name); /** * @brief Append point to plot @@ -57,9 +57,23 @@ void appendPoint(const QString &name, float value); /** - * @brief Update plot list, need to run after ALL plots added + * @brief plots + * @return List of all named plots */ - void update(); + QStringList plots(); + + /** + * @brief set The Maximum Number of points per series the plot widget stores + * @param newMax: new maximum Number (default:24) + */ + void setMaximumPoints(const uint newMax); + + /** + * @brief set the maximum temperature shown on the plot + * @param maxTemp : number greater then 0 + */ + void setMaximumTemperature(const uint maxTemp); + private: class plot { @@ -105,10 +119,14 @@ QLineSeries *_series; QString _name; }; + /** + * @brief Update plot list, need to run after ALL plots added + */ + void update(); + QChartView *_chart; QDateTimeAxis *_axisX; QValueAxis *_axisY; - - QHash _name2Index; - QVector _plots; + QMap _plots; + int m_maximumPoints; }; diff --git a/src/widgets/plotwidget.cpp b/src/widgets/plotwidget.cpp --- a/src/widgets/plotwidget.cpp +++ b/src/widgets/plotwidget.cpp @@ -23,14 +23,12 @@ #include PlotWidget::PlotWidget(QWidget *parent) : - QWidget(parent), - _chart(new QChartView()), - _axisX(new QDateTimeAxis()), - _axisY(new QValueAxis()) + QWidget(parent) + , _chart(new QChartView(this)) + , _axisX(new QDateTimeAxis(this)) + , _axisY(new QValueAxis(this)) + , m_maximumPoints(24) { - QHBoxLayout *mainLayout = new QHBoxLayout; - mainLayout->addWidget(_chart); - setLayout(mainLayout); _axisX->setTickCount(3); _axisX->setFormat(QStringLiteral("hh:mm:ss")); @@ -46,41 +44,56 @@ _chart->chart()->setTheme(QChart::ChartThemeDark); } - newPlot(tr("Actual Bed")); - newPlot(tr("Target Bed")); - // The extruder need to be added after some signal emitted (ExtruderCountChanged) - newPlot(tr("Actual Ext.1")); - newPlot(tr("Target Ext.1")); - + QHBoxLayout *mainLayout = new QHBoxLayout; + mainLayout->addWidget(_chart); + setLayout(mainLayout); } -void PlotWidget::newPlot(const QString &name) +void PlotWidget::addPlot(const QString &name) { - _name2Index[name] = _plots.size(); plot _newPlot; _newPlot.setName(name); _chart->chart()->addSeries(_newPlot.serie()); _newPlot.serie()->attachAxis(_axisY); _newPlot.serie()->attachAxis(_axisX); - _plots.append(_newPlot); + _plots.insert(name, _newPlot); } -void PlotWidget::deletePlot(const QString &name) +void PlotWidget::removePlot(const QString &name) { - _plots.remove(_name2Index[name]); - _name2Index.remove(name); + _chart->chart()->removeSeries(_plots[name].serie()); + _plots.remove(name); } void PlotWidget::appendPoint(const QString &name, float value) { - _plots[_name2Index[name]].pushPoint(value); + if (_plots[name].serie()->count() > m_maximumPoints) { + _plots[name].serie()->remove(0); + } + _plots[name].pushPoint(value); + update(); } void PlotWidget::update() { _chart->chart()->axisX()->setRange(QDateTime::currentDateTime().addSecs(-120), QDateTime::currentDateTime()); } +QStringList PlotWidget::plots() +{ + return _plots.keys(); +} + +void PlotWidget::setMaximumPoints(const uint newMax) +{ + m_maximumPoints = newMax; +} + +void PlotWidget::setMaximumTemperature(const uint maxTemp) +{ + _chart->chart()->axisY()->setRange(0, maxTemp); +} + PlotWidget::~PlotWidget() { } diff --git a/testclient/mainwindow.cpp b/testclient/mainwindow.cpp --- a/testclient/mainwindow.cpp +++ b/testclient/mainwindow.cpp @@ -52,29 +52,7 @@ connect(core, &AtCore::stateChanged, this, &MainWindow::printerStateChanged); connect(core, &AtCore::portsChanged, this, &MainWindow::locateSerialPort); - connect(core, &AtCore::sdCardFileListChanged, sdWidget, &SdWidget::updateFilelist); - - connect(&core->temperature(), &Temperature::bedTemperatureChanged, [ this ](float temp) { - checkTemperature(0x00, 0, temp); - plotWidget->appendPoint(tr("Actual Bed"), temp); - plotWidget->update(); - }); - connect(&core->temperature(), &Temperature::bedTargetTemperatureChanged, [ this ](float temp) { - checkTemperature(0x01, 0, temp); - plotWidget->appendPoint(tr("Target Bed"), temp); - plotWidget->update(); - }); - connect(&core->temperature(), &Temperature::extruderTemperatureChanged, [ this ](float temp) { - checkTemperature(0x02, 0, temp); - plotWidget->appendPoint(tr("Actual Ext.1"), temp); - plotWidget->update(); - }); - connect(&core->temperature(), &Temperature::extruderTargetTemperatureChanged, [ this ](float temp) { - checkTemperature(0x03, 0, temp); - plotWidget->appendPoint(tr("Target Ext.1"), temp); - plotWidget->update(); - }); } void MainWindow::initMenu() @@ -192,6 +170,31 @@ void MainWindow::makeTempTimelineDock() { plotWidget = new PlotWidget; + //make and connect our plots in the widget. + plotWidget->addPlot(tr("Actual Bed")); + connect(&core->temperature(), &Temperature::bedTemperatureChanged, [ this ](float temp) { + checkTemperature(0x00, 0, temp); + plotWidget->appendPoint(tr("Actual Bed"), temp); + }); + + plotWidget->addPlot(tr("Target Bed")); + connect(&core->temperature(), &Temperature::bedTargetTemperatureChanged, [ this ](float temp) { + checkTemperature(0x01, 0, temp); + plotWidget->appendPoint(tr("Target Bed"), temp); + }); + + plotWidget->addPlot(tr("Actual Ext.1")); + connect(&core->temperature(), &Temperature::extruderTemperatureChanged, [ this ](float temp) { + checkTemperature(0x02, 0, temp); + plotWidget->appendPoint(tr("Actual Ext.1"), temp); + }); + + plotWidget->addPlot(tr("Target Ext.1")); + connect(&core->temperature(), &Temperature::extruderTargetTemperatureChanged, [ this ](float temp) { + checkTemperature(0x03, 0, temp); + plotWidget->appendPoint(tr("Target Ext.1"), temp); + }); + tempTimelineDock = new QDockWidget(tr("Temperature Timeline"), this); tempTimelineDock->setWidget(plotWidget); menuView->insertAction(nullptr, tempTimelineDock->toggleViewAction());