File indexing completed on 2024-05-12 04:41:11

0001 /* AtCore KDE Libary for 3D Printers
0002     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0003     SPDX-FileCopyrightText: 2017 Patrick José Pereira <patrickjp@kde.org>
0004     SPDX-FileCopyrightText: 2017-2018 Chris Rizzitello <rizzitello@kde.org>
0005 */
0006 
0007 #include "plotwidget.h"
0008 #include <QChart>
0009 #include <QChartView>
0010 
0011 PlotWidget::PlotWidget(QWidget *parent)
0012     : QWidget(parent)
0013     , _chart(new QChartView(this))
0014     , _axisX(new QDateTimeAxis(this))
0015     , _axisY(new QValueAxis(this))
0016     , m_maximumPoints(120)
0017 {
0018     _axisX->setTickCount(3);
0019     _axisX->setFormat(QStringLiteral("hh:mm:ss"));
0020     _axisY->setLabelFormat(QStringLiteral("%d"));
0021     _axisY->setTitleText(tr("Temp."));
0022     _axisY->setRange(0, 3e2);
0023 
0024     _axisX->setRange(QDateTime::currentDateTime().addSecs(-120), QDateTime::currentDateTime());
0025 
0026     _chart->chart()->addAxis(_axisY, Qt::AlignLeft);
0027     _chart->chart()->addAxis(_axisX, Qt::AlignBottom);
0028 
0029     _chart->setRenderHint(QPainter::Antialiasing);
0030     if (palette().text().color().value() >= QColor(Qt::lightGray).value()) {
0031         _chart->chart()->setTheme(QChart::ChartThemeDark);
0032     }
0033 
0034     auto mainLayout = new QHBoxLayout;
0035     mainLayout->addWidget(_chart);
0036     setLayout(mainLayout);
0037 }
0038 
0039 void PlotWidget::addPlot(const QString &name)
0040 {
0041     plot _newPlot;
0042     _newPlot.setName(name);
0043     _chart->chart()->addSeries(_newPlot.serie());
0044     _newPlot.serie()->attachAxis(_axisY);
0045     _newPlot.serie()->attachAxis(_axisX);
0046     _plots.insert(name, &_newPlot);
0047 }
0048 
0049 void PlotWidget::removePlot(const QString &name)
0050 {
0051     QChart *temp = _chart->chart();
0052     temp->removeSeries(_plots.value(name)->serie());
0053     _plots.remove(name);
0054 }
0055 
0056 void PlotWidget::appendPoint(const QString &name, float value)
0057 {
0058     if (_plots.value(name)->serie()->count() > m_maximumPoints) {
0059         _plots.value(name)->serie()->remove(0);
0060     }
0061     _plots.value(name)->pushPoint(value);
0062     update();
0063 }
0064 
0065 void PlotWidget::update()
0066 {
0067     _axisX->setRange(QDateTime::currentDateTime().addSecs(-120), QDateTime::currentDateTime());
0068 }
0069 
0070 QStringList PlotWidget::plots()
0071 {
0072     return _plots.keys();
0073 }
0074 
0075 void PlotWidget::setMaximumPoints(const int newMax)
0076 {
0077     m_maximumPoints = std::max(newMax, 0);
0078 }
0079 
0080 void PlotWidget::setMaximumTemperature(const uint maxTemp)
0081 {
0082     _axisY->setRange(0, maxTemp);
0083 }