diff --git a/testclient/widgets/plotwidget.h b/testclient/widgets/plotwidget.h --- a/testclient/widgets/plotwidget.h +++ b/testclient/widgets/plotwidget.h @@ -1,14 +1,14 @@ /* Atelier KDE Printer Host for 3D Printing Copyright (C) <2016> - Author: Patrick José Pereira - patrickelectric@gmail.com + 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 any later version accepted by the membership of - KDE e.V. (or its successor approved by the membership of KDE - e.V.), which shall act as a proxy defined in Section 14 of - version 3 of the license. + 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 @@ -31,47 +31,41 @@ public: explicit PlotWidget(QWidget *parent = nullptr); - ~PlotWidget() override; + ~PlotWidget(); /** * @brief Create a new plot * * @param name p_name: plot name */ - void newPlot(QString name); + void newPlot(const QString &name); /** * @brief Delete plot with name * * @param name p_name: name */ - void deletePlot(QString name); + void deletePlot(const QString &name); /** * @brief Append point to plot * * @param name p_name: plot name * @param value p_value: value */ - void appendPoint(QString name, float value); + void appendPoint(const QString &name, float value); /** * @brief Update plot list, need to run after ALL plots added * */ void update(); private: - - QChartView *_chart; - - QHash _name2Index; - class plot { public: - explicit plot() + explicit plot() : _series(new QLineSeries()) { - _series = new QLineSeries(); }; ~plot() @@ -85,7 +79,7 @@ _series->append(now.toMSecsSinceEpoch(), value); } - void setName(QString name) + void setName(const QString &name) { _name = name; _series->setName(_name); @@ -97,20 +91,24 @@ _series->append(now.toMSecsSinceEpoch(), 0.0); } - QLineSeries *serie() + QLineSeries *serie() const { return _series; } - QString name() + QString name() const { return _name; } private: QLineSeries *_series; QString _name; }; + QChartView *_chart; + QDateTimeAxis *_axisX; + QValueAxis *_axisY; + QHash _name2Index; QVector _plots; }; diff --git a/testclient/widgets/plotwidget.cpp b/testclient/widgets/plotwidget.cpp --- a/testclient/widgets/plotwidget.cpp +++ b/testclient/widgets/plotwidget.cpp @@ -1,14 +1,14 @@ /* Atelier KDE Printer Host for 3D Printing Copyright (C) <2016> - Author: Patrick José Pereira - patrickelectric@gmail.com + 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 any later version accepted by the membership of - KDE e.V. (or its successor approved by the membership of KDE - e.V.), which shall act as a proxy defined in Section 14 of - version 3 of the license. + 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 @@ -19,82 +19,67 @@ along with this program. If not, see . */ -#include - #include "plotwidget.h" +#include +#include PlotWidget::PlotWidget(QWidget *parent) : - QWidget(parent) + QWidget(parent), + _chart(new QChartView()), + _axisX(new QDateTimeAxis()), + _axisY(new QValueAxis()) { - _chart = new QChartView; + QHBoxLayout *mainLayout = new QHBoxLayout; + mainLayout->addWidget(_chart); + setLayout(mainLayout); + + _axisX->setTickCount(3); + _axisX->setFormat(QStringLiteral("hh:mm:ss")); + _axisY->setLabelFormat(QStringLiteral("%d")); + _axisY->setTitleText(i18n("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); - this->setLayout(mainLayout); - newPlot(tr("Actual Bed")); - newPlot(tr("Target Bed")); + newPlot(i18n("Actual Bed")); + newPlot(i18n("Target Bed")); // The extruder need to be added after some signal emitted (ExtruderCountChanged) - newPlot(tr("Actual Ext.1")); - newPlot(tr("Target Ext.1")); - update(); + newPlot(i18n("Actual Ext.1")); + newPlot(i18n("Target Ext.1")); + } -void PlotWidget::newPlot(QString name) +void PlotWidget::newPlot(const QString &name) { - // Create a new plot _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); } -void PlotWidget::deletePlot(QString name) +void PlotWidget::deletePlot(const QString &name) { _plots.remove(_name2Index[name]); - _name2Index[name] = -1; + _name2Index.remove(name); } -void PlotWidget::appendPoint(QString name, float value) +void PlotWidget::appendPoint(const QString &name, float value) { _plots[_name2Index[name]].pushPoint(value); } void PlotWidget::update() { - static bool firstTimeCheck = true; - static QDateTimeAxis *axisX = new QDateTimeAxis; - static QValueAxis *axisY = new QValueAxis; - - // After already executed, update time axis - if (firstTimeCheck == false) { - _chart->chart()->axisX()->setRange(QDateTime::currentDateTime().addSecs(-120), QDateTime::currentDateTime()); - return; - } - - firstTimeCheck = false; - - 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()); - - for (auto plot : _plots) { - _chart->chart()->addSeries(plot.serie()); - plot.serie()->attachAxis(axisY); - plot.serie()->attachAxis(axisX); - } - - _chart->setRenderHint(QPainter::Antialiasing); } PlotWidget::~PlotWidget()