File indexing completed on 2024-05-12 04:20:30

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 #include "KChartAbstractGrid.h"
0010 #include "KChartPaintContext.h"
0011 
0012 #include "KChartMath_p.h"
0013 
0014 #include <qglobal.h>
0015 
0016 
0017 using namespace KChart;
0018 using namespace std;
0019 
0020 
0021 static qreal _trunc( qreal v )
0022 {
0023     return (( v > 0.0 ) ? floor( v ) : ceil( v ));
0024 }
0025 
0026 
0027 AbstractGrid::AbstractGrid ()
0028     : mPlane( nullptr )
0029 {
0030     // this block left empty intentionally
0031 }
0032 
0033 AbstractGrid::~AbstractGrid()
0034 {
0035     // this block left empty intentionally
0036 }
0037 
0038 void AbstractGrid::setNeedRecalculate()
0039 {
0040     mCachedRawDataDimensions.clear();
0041 }
0042 
0043 DataDimensionsList AbstractGrid::updateData( AbstractCoordinatePlane* plane )
0044 {
0045     if ( plane ) {
0046         const DataDimensionsList rawDataDimensions( plane->getDataDimensionsList() );
0047         // ### this could be dangerous because calculateGrid() looks at some data we are not checking
0048         //     for changes here.
0049         if ( mCachedRawDataDimensions.empty() || ( rawDataDimensions != mCachedRawDataDimensions ) ) {
0050             mCachedRawDataDimensions = rawDataDimensions;
0051             mPlane = plane;
0052             mDataDimensions = calculateGrid( rawDataDimensions );
0053         }
0054     }
0055     return mDataDimensions;
0056 }
0057 
0058 bool AbstractGrid::isBoundariesValid(const QRectF& r )
0059 {
0060     return isBoundariesValid( qMakePair( r.topLeft(), r.bottomRight() ) );
0061 }
0062 
0063 bool AbstractGrid::isBoundariesValid(const QPair<QPointF,QPointF>& b )
0064 {
0065   return isValueValid( b.first.x() )  && isValueValid( b.first.y() ) &&
0066          isValueValid( b.second.x() ) && isValueValid( b.second.y() );
0067 }
0068 
0069 bool AbstractGrid::isBoundariesValid(const DataDimensionsList& l )
0070 {
0071     for (int i = 0; i < l.size(); ++i)
0072         if ( ! isValueValid( l.at(i).start ) || ! isValueValid( l.at(i).end ) )
0073             return false;
0074     return true;
0075 }
0076 
0077 bool AbstractGrid::isValueValid(const qreal& r )
0078 {
0079   return !(ISNAN(r) || ISINF(r));
0080 }
0081 
0082 void AbstractGrid::adjustLowerUpperRange(
0083         qreal& start, qreal& end,
0084         qreal stepWidth,
0085         bool adjustLower, bool adjustUpper )
0086 {
0087     const qreal startAdjust = ( start >= 0.0 ) ? 0.0 : -1.0;
0088     const qreal endAdjust   = ( end   >= 0.0 ) ? 1.0 :  0.0;
0089     if ( adjustLower && !qFuzzyIsNull( fmod( start, stepWidth ) ) )
0090         start = stepWidth * (_trunc( start / stepWidth ) + startAdjust);
0091     if ( adjustUpper && !qFuzzyIsNull( fmod( end, stepWidth ) ) )
0092         end = stepWidth * (_trunc( end / stepWidth ) + endAdjust);
0093 }
0094 
0095 const DataDimension AbstractGrid::adjustedLowerUpperRange(
0096         const DataDimension& dim,
0097         bool adjustLower, bool adjustUpper )
0098 {
0099     DataDimension result( dim );
0100     adjustLowerUpperRange(
0101             result.start, result.end,
0102             result.stepWidth,
0103             adjustLower, adjustUpper );
0104     return result;
0105 }