File indexing completed on 2024-06-16 04:09:14
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 "KChartPolarGrid.h" 0010 0011 #include "KChartPaintContext.h" 0012 #include "KChartPolarDiagram.h" 0013 #include "KChartPieDiagram.h" 0014 #include "KChartPrintingParameters.h" 0015 #include "KChartMath_p.h" 0016 0017 #include <QPainter> 0018 0019 using namespace KChart; 0020 0021 0022 DataDimensionsList PolarGrid::calculateGrid( 0023 const DataDimensionsList& rawDataDimensions ) const 0024 { 0025 qDebug("Calling PolarGrid::calculateGrid()"); 0026 Q_ASSERT_X( rawDataDimensions.count() == 2, "PolarGrid::calculateGrid", 0027 "calculateGrid() expects a list with exactly two entries." ); 0028 Q_ASSERT_X( dynamic_cast< PolarCoordinatePlane* >( mPlane ), "PolarGrid::calculateGrid", 0029 "PaintContext::calculatePlane() called, but no polar plane set." ); 0030 0031 DataDimensionsList l; 0032 0033 //FIXME(khz): do the real calculation 0034 0035 l = rawDataDimensions; 0036 0037 return l; 0038 } 0039 0040 0041 void PolarGrid::drawGrid( PaintContext* context ) 0042 { 0043 // if ( d->coordinateTransformations.size () <= 0 ) return; 0044 0045 const QBrush backupBrush( context->painter()->brush() ); 0046 context->painter()->setBrush( QBrush() ); 0047 PolarCoordinatePlane* plane = dynamic_cast<PolarCoordinatePlane*>(context->coordinatePlane()); 0048 Q_ASSERT_X ( plane, "PolarGrid::drawGrid", 0049 "Bad function call: PaintContext::coodinatePlane() NOT a polar plane." ); 0050 0051 const GridAttributes gridAttrsCircular( plane->gridAttributes( true ) ); 0052 const GridAttributes gridAttrsSagittal( plane->gridAttributes( false ) ); 0053 0054 //qDebug() << "OK:"; 0055 if ( !gridAttrsCircular.isGridVisible() && !gridAttrsSagittal.isGridVisible() ) return; 0056 //qDebug() << "A"; 0057 0058 // FIXME: we paint the rulers to the settings of the first diagram for now: 0059 AbstractPolarDiagram* dgr = dynamic_cast<AbstractPolarDiagram*> (plane->diagrams().first() ); 0060 Q_ASSERT ( dgr ); // only polar diagrams are allowed here 0061 0062 0063 // Do not draw a grid for pie diagrams 0064 if ( dynamic_cast<PieDiagram*> (plane->diagrams().first() ) ) return; 0065 0066 0067 context->painter()->setPen ( PrintingParameters::scalePen( QColor ( Qt::lightGray ) ) ); 0068 const qreal min = dgr->dataBoundaries().first.y(); 0069 QPointF origin = plane->translate( QPointF( min, 0 ) ) + context->rectangle().topLeft(); 0070 //qDebug() << "origin" << origin; 0071 0072 const qreal r = qAbs( min ) + dgr->dataBoundaries().second.y(); // use the full extents 0073 0074 if ( gridAttrsSagittal.isGridVisible() ) { 0075 const int numberOfSpokes = ( int ) ( 360 / plane->angleUnit() ); 0076 for ( int i = 0; i < numberOfSpokes ; ++i ) { 0077 context->painter()->drawLine( origin, plane->translate( QPointF( r - qAbs( min ), i ) ) + context->rectangle().topLeft() ); 0078 } 0079 } 0080 0081 if ( gridAttrsCircular.isGridVisible() ) 0082 { 0083 const qreal startPos = plane->startPosition(); 0084 plane->setStartPosition( 0.0 ); 0085 const int numberOfGridRings = ( int )dgr->numberOfGridRings(); 0086 for ( int j = 0; j < numberOfGridRings; ++j ) { 0087 const qreal rad = min - ( ( j + 1) * r / numberOfGridRings ); 0088 0089 if ( rad == 0 ) 0090 continue; 0091 0092 QRectF rect; 0093 QPointF topLeftPoint; 0094 QPointF bottomRightPoint; 0095 0096 topLeftPoint = plane->translate( QPointF( rad, 0 ) ); 0097 topLeftPoint.setX( plane->translate( QPointF( rad, 90 / plane->angleUnit() ) ).x() ); 0098 bottomRightPoint = plane->translate( QPointF( rad, 180 / plane->angleUnit() ) ); 0099 bottomRightPoint.setX( plane->translate( QPointF( rad, 270 / plane->angleUnit() ) ).x() ); 0100 0101 rect.setTopLeft( topLeftPoint + context->rectangle().topLeft() ); 0102 rect.setBottomRight( bottomRightPoint + context->rectangle().topLeft() ); 0103 0104 context->painter()->drawEllipse( rect ); 0105 } 0106 plane->setStartPosition( startPos ); 0107 } 0108 context->painter()->setBrush( backupBrush ); 0109 }