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