1 /*
2  * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB.  All rights reserved.
3  *
4  * This file is part of the KD Chart library.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef KCHARTABSTRACTGRID_H
21 #define KCHARTABSTRACTGRID_H
22 
23 #include <QPair>
24 
25 #include "KChartAbstractCoordinatePlane.h"
26 #include "KChartGridAttributes.h"
27 #include "KChartAbstractDiagram.h"
28 #include "KChartCartesianAxis.h"
29 
30 namespace KChart {
31 
32     class PaintContext;
33 
34 
35     /**
36      * \internal
37      *
38      * \brief Abstract base class for grid classes: cartesian, polar, ...
39      *
40      * The AbstractGrid interface is the base class used by
41      * AbstractCoordinatePlane, for calculating and for drawing
42      * the grid lines of the plane.
43      */
44     class AbstractGrid
45     {
46     public:
47         virtual ~AbstractGrid();
48     protected:
49         AbstractGrid ();
50 
51 
52     public:
53         /** \brief Returns the cached result of data calculation.
54           *
55           * For this, all derived classes need to implement the
56           * pure-virtual calculateGrid() method.
57           */
58         DataDimensionsList updateData( AbstractCoordinatePlane* plane );
59 
60         /**
61           * Doing the actual drawing.
62           *
63           * Every derived class must implement this.
64           *
65           * \note When implementing drawGrid():  Before you start drawing,
66           * make sure to call updateData(), to get the data boundaries
67           * recalculated.
68           * For an example, see the implementation of CartesianGrid:drawGrid().
69           */
70         virtual void drawGrid( PaintContext* context ) = 0;
71 
72         /**
73          * Causes grid to be recalculated upon the next call
74          * of updateData().
75          *
76          * \see calculateGrid
77          */
78         void setNeedRecalculate();
79 
80         /**
81          * Checks whether both coordinates of r are valid according
82          * to isValueValid
83          *
84          * \see isValueValid
85          */
86         static bool isBoundariesValid(const QRectF& r );
87 
88         /**
89          * Checks whether both coordinates of both points are valid
90          * according to isValueValid
91          *
92          * \see isValueValid
93          */
94         static bool isBoundariesValid(const QPair<QPointF,QPointF>& b );
95 
96         /**
97          * Checks whether all start and end properties of every
98          * DataDimension in the list l are valid according to
99          * isValueValid().
100          *
101          * \see isValueValid
102          */
103         static bool isBoundariesValid(const DataDimensionsList& l );
104 
105         /**
106          * Checks if r is neither NaN nor infinity.
107          */
108         static bool isValueValid(const qreal& r );
109 
110         /**
111          * Adjusts \a start and/or \a end so that they are a multiple of
112          * \a stepWidth
113          */
114         static void adjustLowerUpperRange(
115                 qreal& start, qreal& end,
116                 qreal stepWidth,
117                 bool adjustLower, bool adjustUpper );
118 
119         /**
120          * Adjusts \a dim so that \c dim.start and/or \c dim.end are a multiple
121          * of \c dim.stepWidth.
122          *
123          * \see adjustLowerUpperRange
124          */
125         static const DataDimension adjustedLowerUpperRange(
126                 const DataDimension& dim,
127                 bool adjustLower, bool adjustUpper );
128 
129         GridAttributes gridAttributes;
130 
131     protected:
132         DataDimensionsList mDataDimensions;
133         AbstractCoordinatePlane* mPlane;
134 
135     private:
136         /**
137           * \brief Calculates the grid start/end/step width values.
138           *
139           * Gets the raw data dimensions - e.g. the data model's boundaries,
140           * together with their isCalculated flags.
141           *
142           * Returns the calculated start/end values for the grid, and their
143           * respective step widths.
144           * If at least one of the step widths is Zero, all dimensions of
145           * the returned list are considered invalid!
146           *
147           * \note This function needs to be implemented by all derived classes,
148           * like CartesianGrid, PolarGrid, ...
149           */
150         virtual DataDimensionsList calculateGrid( const DataDimensionsList& rawDataDimensions ) const = 0;
151         DataDimensionsList mCachedRawDataDimensions;
152     };
153 
154 }
155 
156 #endif
157