File indexing completed on 2024-05-19 04:42:22

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, 2020 Chris Rizzitello <rizzitello@kde.org>
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QChartView>
0010 #include <QWidget>
0011 #include <QtCharts>
0012 
0013 #include "atcorewidgets_export.h"
0014 
0015 /**
0016  * @brief PlotWidget Show a graph of the temperature over time.
0017  */
0018 class ATCOREWIDGETS_EXPORT PlotWidget : public QWidget
0019 {
0020     Q_OBJECT
0021 
0022 public:
0023     explicit PlotWidget(QWidget *parent = nullptr);
0024     ~PlotWidget() = default;
0025 
0026     /**
0027      * @brief Create a new plot
0028      * @param name p_name: plot name
0029      */
0030     void addPlot(const QString &name);
0031 
0032     /**
0033      * @brief Delete plot with name
0034      * @param name p_name: name
0035      */
0036     void removePlot(const QString &name);
0037 
0038     /**
0039      * @brief Append point to plot
0040      * @param name p_name: plot name
0041      * @param value p_value: value
0042      */
0043     void appendPoint(const QString &name, float value);
0044 
0045     /**
0046      * @brief plots
0047      * @return List of all named plots
0048      */
0049     QStringList plots();
0050 
0051     /**
0052      * @brief set The Maximum Number of points per series the plot widget stores
0053      * @param newMax: new maximum Number (default:120)
0054      */
0055     void setMaximumPoints(const int newMax);
0056 
0057     /**
0058      * @brief set the maximum temperature shown on the plot
0059      * @param maxTemp : number greater then 0
0060      */
0061     void setMaximumTemperature(const uint maxTemp);
0062 
0063 private:
0064     class plot
0065     {
0066     public:
0067         explicit plot()
0068             : _series(new QLineSeries())
0069         {
0070         }
0071 
0072         explicit plot(const plot &other)
0073            : _series(other.serie())
0074         {
0075             setName(other.name());
0076         }
0077 
0078         ~plot()
0079         {
0080             delete _series;// Series will be deleted with chart
0081         }
0082 
0083         void pushPoint(float value)
0084         {
0085             QDateTime now = QDateTime::currentDateTime();
0086             _series->append(now.toMSecsSinceEpoch(), value);
0087         }
0088 
0089         void setName(const QString &name)
0090         {
0091             _name = name;
0092             _series->setName(_name);
0093 
0094             // Add 3 initial points to plot
0095             QDateTime now = QDateTime::currentDateTime();
0096             _series->append(now.toMSecsSinceEpoch() - 2 * 60e3, 0.0);
0097             _series->append(now.toMSecsSinceEpoch() - 60e3, 0.0);
0098             _series->append(now.toMSecsSinceEpoch(), 0.0);
0099         }
0100 
0101         QLineSeries *serie() const
0102         {
0103             return _series;
0104         }
0105 
0106         QString name() const
0107         {
0108             return _name;
0109         }
0110 
0111     private:
0112         QLineSeries *_series;
0113         QString _name;
0114     };
0115     /**
0116      * @brief Update plot list, need to run after ALL plots added
0117      */
0118     void update();
0119 
0120     QChartView *_chart;
0121     QDateTimeAxis *_axisX;
0122     QValueAxis *_axisY;
0123     QMap<QString, plot*> _plots;
0124     int m_maximumPoints;
0125 };