File indexing completed on 2024-06-09 04:18:27

0001 /*
0002  * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
0003  *
0004  * This file is part of the KD Chart library.
0005  *
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #ifndef KCHARTCARTESIANGRID_H
0010 #define KCHARTCARTESIANGRID_H
0011 
0012 #include "KChartCartesianCoordinatePlane.h"
0013 #include "KChartAbstractGrid.h"
0014 
0015 namespace KChart {
0016 
0017     class PaintContext;
0018     class CartesianCoordinatePlane;
0019 
0020     /**
0021      * \internal
0022      *
0023      * \brief Class for the grid in a cartesian plane.
0024      *
0025      * The CartesianGrid interface is used
0026      * for calculating and for drawing
0027      * the horizontal grid lines, and the vertical grid lines
0028      * of a cartesian coordinate plane.
0029      */
0030     class CartesianGrid : public AbstractGrid
0031     {
0032     public:
0033         CartesianGrid();
0034         ~CartesianGrid() override;
0035 
0036         int minimalSteps() const;
0037         void setMinimalSteps(int minsteps);
0038 
0039         int maximalSteps() const;
0040         void setMaximalSteps(int maxsteps);
0041 
0042         void drawGrid( PaintContext* context ) override;
0043 
0044     private:
0045         int m_minsteps;
0046         int m_maxsteps;
0047         
0048         DataDimensionsList calculateGrid(
0049             const DataDimensionsList& rawDataDimensions ) const override;
0050 
0051         /**
0052          * Helper function called by calculateGrid() to calculate the grid of one dimension.
0053          *
0054          * Classes derived from CartesianGrid can overwrite calculateGridXY() if they need
0055          * a special way of calculating the start or end or step width of their grid lines.
0056          * 
0057          * \param adjustLower If true, the function adjusts the start value
0058          * so it matches the position of a grid line, if false the start value is
0059          * the raw data dimension start value.
0060          * \param adjustUpper If true, the function adjusts the end value
0061          * so it matches the position of a grid line, if false the end value is
0062          * the raw data dimension end value.
0063          */
0064         virtual DataDimension calculateGridXY(
0065             const DataDimension& rawDataDimension,
0066             Qt::Orientation orientation,
0067             bool adjustLower, bool adjustUpper ) const;
0068 
0069         /**
0070           * Helper function called by calculateGridXY().
0071           *
0072           * Classes derived from CartesianGrid can overwrite calculateStepWidth() if they need
0073           * a way of calculating the step width, based upon given start/end values
0074           * for their horizontal or vertical grid lines which is different from the default
0075           * implementation.
0076           *
0077           * \note The CartesianGrid class tries to keep the displayed range as close to
0078           * the raw data range as possible, so in most cases there should be no reason
0079           * to change the default implementation: Using
0080           * KChart::GridAttributes::setGridGranularitySequence() should be sufficient.
0081           *
0082           * \param start The raw start value of the data range.
0083           * \param end The raw end value of the data range.
0084           * \param granularities The list of allowed granularities.
0085           * \param adjustLower If true, the function adjusts the start value
0086           * so it matches the position of a grid line, if false the start value is
0087           * left as it is, in any case the value is adjusted for internal calculation only.
0088           * \param adjustUpper If true, the function adjusts the end value
0089           * so it matches the position of a grid line, if false the end value is
0090           * left as it is, in any case the value is adjusted for internal calculation only.
0091           *
0092           * \returns stepWidth: One of the values from the granularities
0093           * list, optionally multiplied by a positive (or negative, resp.)
0094           * power of ten. subStepWidth: The matching width for sub-grid lines.
0095           */
0096         virtual void calculateStepWidth(
0097             qreal start, qreal end,
0098             const QList<qreal>& granularities,
0099             Qt::Orientation orientation,
0100             qreal& stepWidth, qreal& subStepWidth,
0101             bool adjustLower, bool adjustUpper ) const;
0102     };
0103 
0104 }
0105 
0106 #endif