1 /*
2     SPDX-FileCopyrightText: 2007 Krzysztof Kundzicz <athantor@gmail.com>
3     SPDX-License-Identifier: GPL-2.0-or-later
4 */
5 
6 #ifndef PlainChartDrawer_H_
7 #define PlainChartDrawer_H_
8 
9 #include <QBrush>
10 #include <QFont>
11 #include <QFrame>
12 #include <QImage>
13 #include <QMenu>
14 #include <QPainter>
15 #include <QPalette>
16 #include <QPen>
17 #include <QString>
18 #include <QStyleOption>
19 #include <QWidget>
20 
21 #include <memory>
22 #include <utility>
23 
24 #include <ChartDrawer.h>
25 #include <ChartDrawerData.h>
26 
27 namespace kt
28 {
29 /** \brief Basic chart drawer widget
30 \author Krzysztof Kundzicz <athantor@gmail.com>
31 */
32 
33 class PlainChartDrawer : public QFrame, public ChartDrawer
34 {
35     Q_OBJECT
36 private:
37     /// Pointer to context menu
38     QMenu *pmCtxMenu;
39 
40     /// Height of the chart area ( \b not widget height!)
41     inline wgtunit_t height() const;
42     /// Width of the chart area ( \b not widget width!)
43     inline wgtunit_t width() const;
44 
45     /// Translates coords
46     inline wgtunit_t TY(const wgtunit_t) const;
47 
48     /** \brief Translates chart X coord to screen coord
49     \param xcc X chart coord
50     \return Screen X coord
51     */
52     inline wgtunit_t FindXScreenCoords(const wgtunit_t xcc) const;
53     /** \brief Translates chart Y coord to screen coord
54     \param ycc Y chart coord
55     \return Screen Y coord
56     */
57     inline wgtunit_t FindYScreenCoords(const wgtunit_t ycc) const;
58 
59     /// Makes a context menu for widget
60     void MakeCtxMenu();
61 
62     /** \brief Draws chart's scale
63     \param rPnt Painter object
64     */
65     void DrawScale(QPainter &rPnt);
66     /** \brief Draws chart's frame
67     \param rPnt Painter object
68     */
69     void DrawFrame(QPainter &rPnt);
70     /** \brief Draws chart
71     \param rPnt Painter object
72     */
73     void DrawChart(QPainter &rPnt);
74 
75     /** \brief Draws chart's lines
76     \param rPnt Painter object
77     \param rCdd Dataset to draw
78     */
79     void DrawChartLine(QPainter &rPnt, const ChartDrawerData &rCdd);
80     /** \brief Draws current values of the sets
81     \param rPnt Painter object
82     \param rCdd Dataset to draw
83     \param idx Set's index
84     */
85     void DrawCurrentValue(QPainter &rPnt, const ChartDrawerData &rCdd, size_t idx);
86     /** \brief Marks maximum values
87     \param rPnt Painter object
88     \param rCdd Dataset to draw
89     \param idx Set's index
90     */
91     void DrawMaximum(QPainter &rPnt, const ChartDrawerData &rCdd, size_t idx);
92 
93 public:
94     /** \brief Constructor
95     \param p Parent
96     */
97     PlainChartDrawer(QWidget *p = nullptr);
98     /// Destructor
99     ~PlainChartDrawer() override;
100 
101     /** \brief Widget's paint event
102     \param pPevt Event
103     */
104     void paintEvent(QPaintEvent *pPevt) override;
105 
106 public Q_SLOTS:
107     void showContextMenu(const QPoint &rP) override;
108     void renderToImage() override;
109 
110     void addValue(const size_t idx, const wgtunit_t val, const bool upd = false) override;
111     void addDataSet(ChartDrawerData Cdd) override;
112     void insertDataSet(const size_t idx, ChartDrawerData Cdd) override;
113     void removeDataSet(const size_t idx) override;
114     void zero(const size_t idx) override;
115     void zeroAll() override;
setUnitName(const QString & rN)116     void setUnitName(const QString &rN) override
117     {
118         pmUnitName = rN;
119     }
120     void setPen(const size_t idx, const QPen &rP) override;
121     void setXMax(const wgtunit_t x) override;
122     void setYMax(const wgtunit_t y) override;
123     void findSetMax() override;
124     void setUuid(const size_t idx, const QUuid &rQ) override;
125     int16_t findUuidInSet(const QUuid &rQ) const override;
126     void setMaxMode(const MaxMode mm) override;
127     QUuid getUuid(const size_t idx) const override;
128     QString makeLegendString() override;
129     void setLegend(const QString &rL) override;
130     void update() override;
131 
132     void enableAntiAlias(bool aa) override;
133     void enableBackgroundGrid(bool bg) override;
134 
135 Q_SIGNALS:
136     void Zeroed(ChartDrawer *);
137 };
138 
139 } // ns end
140 
141 #endif
142