KChart

KChartAbstractGrid.cpp
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 #include "KChartAbstractGrid.h"
21 #include "KChartPaintContext.h"
22 
23 #include "KChartMath_p.h"
24 
25 #include <qglobal.h>
26 
27 
28 using namespace KChart;
29 using namespace std;
30 
31 
32 static qreal _trunc( qreal v )
33 {
34  return (( v > 0.0 ) ? floor( v ) : ceil( v ));
35 }
36 
37 
38 AbstractGrid::AbstractGrid ()
39  : mPlane( nullptr )
40 {
41  //this bloc left empty intentionally
42 }
43 
44 AbstractGrid::~AbstractGrid()
45 {
46  //this bloc left empty intentionally
47 }
48 
50 {
51  mCachedRawDataDimensions.clear();
52 }
53 
55 {
56  if ( plane ) {
57  const DataDimensionsList rawDataDimensions( plane->getDataDimensionsList() );
58  // ### this could be dangerous becaus calculateGrid() looks at some data we are not checking
59  // for changes here.
60  if ( mCachedRawDataDimensions.empty() || ( rawDataDimensions != mCachedRawDataDimensions ) ) {
61  mCachedRawDataDimensions = rawDataDimensions;
62  mPlane = plane;
63  mDataDimensions = calculateGrid( rawDataDimensions );
64  }
65  }
66  return mDataDimensions;
67 }
68 
70 {
71  return isBoundariesValid( qMakePair( r.topLeft(), r.bottomRight() ) );
72 }
73 
75 {
76  return isValueValid( b.first.x() ) && isValueValid( b.first.y() ) &&
77  isValueValid( b.second.x() ) && isValueValid( b.second.y() );
78 }
79 
81 {
82  for (int i = 0; i < l.size(); ++i)
83  if ( ! isValueValid( l.at(i).start ) || ! isValueValid( l.at(i).end ) )
84  return false;
85  return true;
86 }
87 
88 bool AbstractGrid::isValueValid(const qreal& r )
89 {
90  return !(ISNAN(r) || ISINF(r));
91 }
92 
94  qreal& start, qreal& end,
95  qreal stepWidth,
96  bool adjustLower, bool adjustUpper )
97 {
98  const qreal startAdjust = ( start >= 0.0 ) ? 0.0 : -1.0;
99  const qreal endAdjust = ( end >= 0.0 ) ? 1.0 : 0.0;
100  if ( adjustLower && !qFuzzyIsNull( fmod( start, stepWidth ) ) )
101  start = stepWidth * (_trunc( start / stepWidth ) + startAdjust);
102  if ( adjustUpper && !qFuzzyIsNull( fmod( end, stepWidth ) ) )
103  end = stepWidth * (_trunc( end / stepWidth ) + endAdjust);
104 }
105 
107  const DataDimension& dim,
108  bool adjustLower, bool adjustUpper )
109 {
110  DataDimension result( dim );
111  adjustLowerUpperRange(
112  result.start, result.end,
113  result.stepWidth,
114  adjustLower, adjustUpper );
115  return result;
116 }
Helper class for one dimension of data, e.g.
void setNeedRecalculate()
Causes grid to be recalculated upon the next call of updateData().
static bool isBoundariesValid(const QRectF &r)
Checks whether both coordinates of r are valid according to isValueValid.
static const DataDimension adjustedLowerUpperRange(const DataDimension &dim, bool adjustLower, bool adjustUpper)
Adjusts dim so that dim.start and/or dim.end are a multiple of dim.stepWidth.
const T & at(int i) const const
int size() const const
QPointF topLeft() const const
Base class common for all coordinate planes, CartesianCoordinatePlane, PolarCoordinatePlane, TernaryCoordinatePlane.
DataDimensionsList updateData(AbstractCoordinatePlane *plane)
Returns the cached result of data calculation.
static void adjustLowerUpperRange(qreal &start, qreal &end, qreal stepWidth, bool adjustLower, bool adjustUpper)
Adjusts start and/or end so that they are a multiple of stepWidth.
static bool isValueValid(const qreal &r)
Checks if r is neither NaN nor infinity.
QPointF bottomRight() const const
Global namespace.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Tue Sep 29 2020 22:42:40 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.