| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #pragma once |
| 5 | |
| 6 | #include "graphitemprofilepart.h" |
| 7 | #include <QList> |
| 8 | #include <QObject> |
| 9 | #include <QPointF> |
| 10 | #include <QQuickItem> |
| 11 | #include <QString> |
| 12 | #include <QtCharts/QAbstractAxis> |
| 13 | #include <QtCharts/QChart> |
| 14 | #include <QtCharts/QLineSeries> |
| 15 | #include <string> |
| 16 | #include <string_view> |
| 17 | |
| 18 | QT_USE_NAMESPACE |
| 19 | |
| 20 | class GraphItem |
| 21 | : public QQuickItem |
| 22 | , public GraphItemProfilePart::Importer |
| 23 | , public GraphItemProfilePart::Exporter |
| 24 | { |
| 25 | Q_OBJECT |
| 26 | Q_PROPERTY(QString name READ name NOTIFY nameChanged) |
| 27 | Q_PROPERTY(qreal value READ value NOTIFY valueChanged) |
| 28 | Q_PROPERTY(bool active READ active WRITE active NOTIFY activeChanged) |
| 29 | Q_PROPERTY(bool ignored READ ignored WRITE ignored NOTIFY ignoredChanged) |
| 30 | Q_PROPERTY(QString unit READ unit) |
| 31 | Q_PROPERTY(QString color READ color) |
| 32 | |
| 33 | public: |
| 34 | GraphItem(std::string_view name, std::string_view unit); |
| 35 | |
| 36 | QString const &name() const; |
| 37 | QString const &unit() const; |
| 38 | QString color() const; |
| 39 | qreal value() const; |
| 40 | |
| 41 | void initialRange(qreal min, qreal max); |
| 42 | void updateGraph(qreal value); |
| 43 | |
| 44 | std::optional<std::reference_wrapper<Importable::Importer>> |
| 45 | provideImporter(Item const &i) final override; |
| 46 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 47 | provideExporter(Item const &i) final override; |
| 48 | |
| 49 | bool provideActive() const final override; |
| 50 | std::string const &provideColor() const final override; |
| 51 | |
| 52 | void takeActive(bool active) override; |
| 53 | |
| 54 | void takeColor(std::string const &color) override; |
| 55 | |
| 56 | virtual void init(Exportable const *e) = 0; |
| 57 | |
| 58 | public slots: |
| 59 | void configure(QAbstractSeries *series, QAbstractAxis *xAxis, |
| 60 | QAbstractAxis *yAxis); |
| 61 | void updateYAxisRange(qreal min, qreal max); |
| 62 | void refreshSeriePoints(); |
| 63 | |
| 64 | virtual void update() = 0; |
| 65 | |
| 66 | signals: |
| 67 | void settingsChanged(); |
| 68 | void nameChanged(); |
| 69 | void valueChanged(qreal value); |
| 70 | void activeChanged(bool active); |
| 71 | void colorChanged(QString const &color); |
| 72 | void ignoredChanged(bool ignored); |
| 73 | void yAxisRangeChanged(qreal min, qreal max); |
| 74 | |
| 75 | private: |
| 76 | bool active() const; |
| 77 | void active(bool active); |
| 78 | bool ignored() const; |
| 79 | void ignored(bool ignored); |
| 80 | void updateYAxis(qreal value); |
| 81 | void restartXPoints(); |
| 82 | |
| 83 | static unsigned short const PointsCount; |
| 84 | |
| 85 | QString const name_; |
| 86 | QString const unit_; |
| 87 | |
| 88 | std::string color_{"white"}; |
| 89 | bool active_{true}; |
| 90 | bool ignored_{false}; |
| 91 | |
| 92 | qreal value_{0}; |
| 93 | |
| 94 | QList<QPointF> points_; |
| 95 | |
| 96 | QLineSeries *series_{nullptr}; |
| 97 | QAbstractAxis *xAxis_; |
| 98 | QAbstractAxis *yAxis_; |
| 99 | |
| 100 | qreal yMin_; |
| 101 | qreal yMax_; |
| 102 | }; |