diff --git a/src/widgets/plotwidget.cpp b/src/widgets/plotwidget.cpp index aa2306f..5001643 100644 --- a/src/widgets/plotwidget.cpp +++ b/src/widgets/plotwidget.cpp @@ -1,99 +1,99 @@ /* Atelier KDE Printer Host for 3D Printing Copyright (C) <2016> Author: Patrick José Pereira - patrickjp@kde.org Tomaz Canabrava Lays Rodrigues Chris Rizzitello This program 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. This program 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 this program. If not, see . */ #include "plotwidget.h" #include PlotWidget::PlotWidget(QWidget *parent) : QWidget(parent) , _chart(new QChartView(this)) , _axisX(new QDateTimeAxis(this)) , _axisY(new QValueAxis(this)) , m_maximumPoints(120) { _axisX->setTickCount(3); _axisX->setFormat(QStringLiteral("hh:mm:ss")); _axisY->setLabelFormat(QStringLiteral("%d")); _axisY->setTitleText(tr("Temp.")); _chart->chart()->addAxis(_axisY, Qt::AlignLeft); _chart->chart()->addAxis(_axisX, Qt::AlignBottom); _chart->chart()->axisY()->setRange(0, 3e2); _chart->chart()->axisX()->setRange(QDateTime::currentDateTime().addSecs(-120), QDateTime::currentDateTime()); _chart->setRenderHint(QPainter::Antialiasing); if (palette().text().color().value() >= QColor(Qt::lightGray).value()) { _chart->chart()->setTheme(QChart::ChartThemeDark); } QHBoxLayout *mainLayout = new QHBoxLayout; mainLayout->addWidget(_chart); setLayout(mainLayout); } void PlotWidget::addPlot(const QString &name) { plot _newPlot; _newPlot.setName(name); _chart->chart()->addSeries(_newPlot.serie()); _newPlot.serie()->attachAxis(_axisY); _newPlot.serie()->attachAxis(_axisX); _plots.insert(name, _newPlot); } void PlotWidget::removePlot(const QString &name) { _chart->chart()->removeSeries(_plots[name].serie()); _plots.remove(name); } void PlotWidget::appendPoint(const QString &name, float 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) +void PlotWidget::setMaximumPoints(const int newMax) { - m_maximumPoints = newMax; + m_maximumPoints = std::max(newMax, 0); } void PlotWidget::setMaximumTemperature(const uint maxTemp) { _chart->chart()->axisY()->setRange(0, maxTemp); } PlotWidget::~PlotWidget() { } diff --git a/src/widgets/plotwidget.h b/src/widgets/plotwidget.h index 3ef9ee0..b7d634c 100644 --- a/src/widgets/plotwidget.h +++ b/src/widgets/plotwidget.h @@ -1,132 +1,132 @@ /* Atelier KDE Printer Host for 3D Printing Copyright (C) <2016> Author: Patrick José Pereira - patrickjp@kde.org Tomaz Canabrava Lays Rodrigues Chris Rizzitello This program 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. This program 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 this program. If not, see . */ #pragma once #include #include #include #include "atcorewidgets_export.h" /** * @brief PlotWidget Show a graph of the temperature over time. */ class ATCOREWIDGETS_EXPORT PlotWidget : public QWidget { Q_OBJECT public: explicit PlotWidget(QWidget *parent = nullptr); ~PlotWidget(); /** * @brief Create a new plot * @param name p_name: plot name */ void addPlot(const QString &name); /** * @brief Delete plot with name * @param name p_name: name */ void removePlot(const QString &name); /** * @brief Append point to plot * @param name p_name: plot name * @param value p_value: value */ void appendPoint(const QString &name, float value); /** * @brief plots * @return List of all named plots */ QStringList plots(); /** * @brief set The Maximum Number of points per series the plot widget stores * @param newMax: new maximum Number (default:120) */ - void setMaximumPoints(const uint newMax); + void setMaximumPoints(const int newMax); /** * @brief set the maximum temperature shown on the plot * @param maxTemp : number greater then 0 */ void setMaximumTemperature(const uint maxTemp); private: class plot { public: explicit plot() : _series(new QLineSeries()) { } ~plot() { // Series will be deleted with chart } void pushPoint(float value) { QDateTime now = QDateTime::currentDateTime(); _series->append(now.toMSecsSinceEpoch(), value); } void setName(const QString &name) { _name = name; _series->setName(_name); //Add 3 initial points to plot QDateTime now = QDateTime::currentDateTime(); _series->append(now.toMSecsSinceEpoch() - 2 * 60e3, 0.0); _series->append(now.toMSecsSinceEpoch() - 60e3, 0.0); _series->append(now.toMSecsSinceEpoch(), 0.0); } QLineSeries *serie() const { return _series; } QString name() const { return _name; } private: QLineSeries *_series; QString _name; }; /** * @brief Update plot list, need to run after ALL plots added */ void update(); QChartView *_chart; QDateTimeAxis *_axisX; QValueAxis *_axisY; QMap _plots; int m_maximumPoints; };