File indexing completed on 2024-05-12 15:54:13

0001 /*
0002  * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB.  All rights reserved.
0003  *
0004  * This file is part of the KD Chart library.
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU General Public License as
0008  * published by the Free Software Foundation; either version 2 of
0009  * the License, or (at your option) any later version.
0010  *
0011  * This program is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public License
0017  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
0018  */
0019 
0020 #ifndef KCHARTABSTRACTGRID_H
0021 #define KCHARTABSTRACTGRID_H
0022 
0023 #include <QPair>
0024 
0025 #include "KChartAbstractCoordinatePlane.h"
0026 #include "KChartGridAttributes.h"
0027 #include "KChartAbstractDiagram.h"
0028 #include "KChartCartesianAxis.h"
0029 
0030 namespace KChart {
0031 
0032     class PaintContext;
0033 
0034 
0035     /**
0036      * \internal
0037      *
0038      * \brief Abstract base class for grid classes: cartesian, polar, ...
0039      *
0040      * The AbstractGrid interface is the base class used by
0041      * AbstractCoordinatePlane, for calculating and for drawing
0042      * the grid lines of the plane.
0043      */
0044     class AbstractGrid
0045     {
0046     public:
0047         virtual ~AbstractGrid();
0048     protected:
0049         AbstractGrid ();
0050 
0051 
0052     public:
0053         /** \brief Returns the cached result of data calculation.
0054           *
0055           * For this, all derived classes need to implement the
0056           * pure-virtual calculateGrid() method.
0057           */
0058         DataDimensionsList updateData( AbstractCoordinatePlane* plane );
0059 
0060         /**
0061           * Doing the actual drawing.
0062           *
0063           * Every derived class must implement this.
0064           *
0065           * \note When implementing drawGrid():  Before you start drawing,
0066           * make sure to call updateData(), to get the data boundaries
0067           * recalculated.
0068           * For an example, see the implementation of CartesianGrid:drawGrid().
0069           */
0070         virtual void drawGrid( PaintContext* context ) = 0;
0071 
0072         /**
0073          * Causes grid to be recalculated upon the next call
0074          * of updateData().
0075          *
0076          * \see calculateGrid
0077          */
0078         void setNeedRecalculate();
0079 
0080         /**
0081          * Checks whether both coordinates of r are valid according
0082          * to isValueValid
0083          *
0084          * \see isValueValid
0085          */
0086         static bool isBoundariesValid(const QRectF& r );
0087 
0088         /**
0089          * Checks whether both coordinates of both points are valid
0090          * according to isValueValid
0091          *
0092          * \see isValueValid
0093          */
0094         static bool isBoundariesValid(const QPair<QPointF,QPointF>& b );
0095 
0096         /**
0097          * Checks whether all start and end properties of every
0098          * DataDimension in the list l are valid according to
0099          * isValueValid().
0100          *
0101          * \see isValueValid
0102          */
0103         static bool isBoundariesValid(const DataDimensionsList& l );
0104 
0105         /**
0106          * Checks if r is neither NaN nor infinity.
0107          */
0108         static bool isValueValid(const qreal& r );
0109 
0110         /**
0111          * Adjusts \a start and/or \a end so that they are a multiple of
0112          * \a stepWidth
0113          */
0114         static void adjustLowerUpperRange(
0115                 qreal& start, qreal& end,
0116                 qreal stepWidth,
0117                 bool adjustLower, bool adjustUpper );
0118 
0119         /**
0120          * Adjusts \a dim so that \c dim.start and/or \c dim.end are a multiple
0121          * of \c dim.stepWidth.
0122          *
0123          * \see adjustLowerUpperRange
0124          */
0125         static const DataDimension adjustedLowerUpperRange(
0126                 const DataDimension& dim,
0127                 bool adjustLower, bool adjustUpper );
0128 
0129         GridAttributes gridAttributes;
0130 
0131     protected:
0132         DataDimensionsList mDataDimensions;
0133         AbstractCoordinatePlane* mPlane;
0134 
0135     private:
0136         /**
0137           * \brief Calculates the grid start/end/step width values.
0138           *
0139           * Gets the raw data dimensions - e.g. the data model's boundaries,
0140           * together with their isCalculated flags.
0141           *
0142           * Returns the calculated start/end values for the grid, and their
0143           * respective step widths.
0144           * If at least one of the step widths is Zero, all dimensions of
0145           * the returned list are considered invalid!
0146           *
0147           * \note This function needs to be implemented by all derived classes,
0148           * like CartesianGrid, PolarGrid, ...
0149           */
0150         virtual DataDimensionsList calculateGrid( const DataDimensionsList& rawDataDimensions ) const = 0;
0151         DataDimensionsList mCachedRawDataDimensions;
0152     };
0153 
0154 }
0155 
0156 #endif