1 /*
2     SPDX-FileCopyrightText: 2007 Krzysztof Kundzicz <athantor@gmail.com>
3     SPDX-License-Identifier: GPL-2.0-or-later
4 */
5 
6 #ifndef ChartDrawer_H_
7 #define ChartDrawer_H_
8 
9 #include <QPaintEvent>
10 #include <QPen>
11 #include <QString>
12 #include <QUuid>
13 
14 #include <cstdint>
15 #include <memory>
16 
17 #include <ChartDrawerData.h>
18 
19 namespace kt
20 {
21 /**
22 \brief Base class for chart widgets used by plugin
23 \author Krzysztof Kundzicz <athantor@gmail.com>
24 */
25 
26 class ChartDrawer
27 {
28 public:
29     /// Mode of setting maximum on the chart's OY axis
30     enum MaxMode {
31         MM_Top, ///< Max ever achieved
32         MM_Exact, ///< Max visible on chart
33     };
34 
35     /// Type used as values on charts
36     typedef qreal wgtunit_t;
37     /// Type holding chart's data values
38     typedef std::vector<ChartDrawerData> val_t;
39 
40     /** \brief Constructor
41      */
42     ChartDrawer();
43     /// Destructor
44     virtual ~ChartDrawer();
45 
46     /** \brief Get maximum on OX
47     \return Max on OX
48     */
49     wgtunit_t getXMax() const;
50     /** \brief Get maximum on OY
51     \return Max on OY
52     */
53     wgtunit_t getYMax() const;
54     /** \brief Get name of a unit used shown chart
55     \return Name
56     */
getUnitName()57     QString getUnitName() const
58     {
59         return pmUnitName;
60     }
61     /** \brief Get UUID of the dataset
62     \param idx Index of the dataset
63     \return UUID
64     */
65     virtual QUuid getUuid(const size_t idx) const = 0;
66 
67     /** \brief Gets the begin of dataset as const_iterator
68     \return Beginning of the set
69     */
70     virtual val_t::const_iterator begin() const;
71     /** \brief Gets the end of dataset as const_iterator
72     \return End of the set
73     */
74     virtual val_t::const_iterator end() const;
75 
76     /** \brief QWidget's paintEvent
77     \param pPevt Event
78 
79     Implement here the drawing of the chart.
80     */
81     virtual void paintEvent(QPaintEvent *pPevt) = 0;
82 
83     /** \brief Amount of dataset shown on chart
84     \return Datasets count
85     */
86     size_t dataSetCount() const;
87     //---------------
88     /** \brief Adds value to the dataset
89     \param idx Index of the dataset
90     \param val Value that's being added
91     \param upd Update chart after adding?
92     */
93     virtual void addValue(const size_t idx, const wgtunit_t val, const bool upd = false) = 0;
94     /** \brief Add new dataset
95     \param Cdd Dataset that's being added
96     */
97     virtual void addDataSet(ChartDrawerData Cdd) = 0;
98     /** \brief Inserts dataset before idx
99     \param idx Index of the dataset before which new set is boing to be added
100     \param Cdd New dataset
101     */
102     virtual void insertDataSet(const size_t idx, ChartDrawerData Cdd) = 0;
103     /** \brief Removes dataset
104     \param idx Index of the dataset to remove
105     */
106     virtual void removeDataSet(const size_t idx) = 0;
107     /** \brief Zeroes dataset
108     \param idx Index of the dataset to zero
109     */
110     virtual void zero(const size_t idx) = 0;
111     /// Zeroes all datasets in the chart
112     virtual void zeroAll() = 0;
113     /** \brief Sets maximum mode
114     \param mm Mode
115     */
116     virtual void setMaxMode(const MaxMode mm) = 0;
117     /** \brief Sets name of an unit shown on the chart
118     \param rN Unit's name
119     */
120     virtual void setUnitName(const QString &rN) = 0;
121     /** \brief Sets pen of the dataset
122     \param idx Index of the dataset which pen is beeng changed
123     \param rP New pen
124     */
125     virtual void setPen(const size_t idx, const QPen &rP) = 0;
126     /** \brief Sets UUID of the dataset
127     \param idx Index of the dataset which pen is beeng changed
128     \param rQ New UUID
129     */
130     virtual void setUuid(const size_t idx, const QUuid &rQ) = 0;
131     /** \brief Finds given UUID in the set
132     \param rQ UUID to find
133     \return Index of the set
134     \retval -1 UUID not found
135     \retval >0 Found index
136     */
137     virtual int16_t findUuidInSet(const QUuid &rQ) const = 0;
138     /** \brief Enable antialiasing
139     \param aa Enable?
140     */
141     virtual void enableAntiAlias(bool aa) = 0;
142 
143     /** \brief Enable background grid
144     \param bg Enable?
145     */
146     virtual void enableBackgroundGrid(bool bg) = 0;
147 
148     /** \brief Sets maximum on OX axis
149     \param x Maximum
150     */
151     virtual void setXMax(const wgtunit_t x) = 0;
152     /** \brief Sets maximum on OY axis
153     \param y Maximum
154     */
155     virtual void setYMax(const wgtunit_t y) = 0;
156 
157     /// Automagically finds maximum from data in sets and sets it on OY scale
158     virtual void findSetMax() = 0;
159 
160     /// Function generating legend string
161     virtual QString makeLegendString() = 0;
162 
163     /** \brief Function settng a legend
164     \param rL Legend's text
165     */
166     virtual void setLegend(const QString &rL) = 0;
167     /// Updates the widget
168     virtual void update() = 0;
169 
170     /** \brief Shows context menu @ point
171     \param rP Point where to show menu
172     */
173     virtual void showContextMenu(const QPoint &rP) = 0;
174     /** \brief Renders chart to image
175     \note This function will show modal KFileDialog
176     */
177     virtual void renderToImage() = 0;
178 
179 protected:
180     /// Pointer to chart's data container
181     val_t pmVals;
182     /// Pointer to a name of the unit used on chart
183     QString pmUnitName;
184     /// Current maximum mode
185     MaxMode mCurrMaxMode;
186     /// Current maximum on OX
187     wgtunit_t mXMax;
188     /// Current maximum on OY
189     wgtunit_t mYMax;
190     /// Use antialiasing?
191     bool mAntiAlias;
192     /// Draw bgd grid?
193     bool mBgdGrid;
194 };
195 
196 } // ns end
197 
198 #endif
199