KChart

KChartPolarGrid.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 "KChartPolarGrid.h"
21 
22 #include "KChartPaintContext.h"
23 #include "KChartPolarDiagram.h"
24 #include "KChartPieDiagram.h"
25 #include "KChartPrintingParameters.h"
26 #include "KChartMath_p.h"
27 
28 #include <QPainter>
29 
30 using namespace KChart;
31 
32 
33 DataDimensionsList PolarGrid::calculateGrid(
34  const DataDimensionsList& rawDataDimensions ) const
35 {
36  qDebug("Calling PolarGrid::calculateGrid()");
37  Q_ASSERT_X( rawDataDimensions.count() == 2, "PolarGrid::calculateGrid",
38  "calculateGrid() expects a list with exactly two entries." );
39  Q_ASSERT_X( dynamic_cast< PolarCoordinatePlane* >( mPlane ), "PolarGrid::calculateGrid",
40  "PaintContext::calculatePlane() called, but no polar plane set." );
41 
43 
44  //FIXME(khz): do the real calculation
45 
46  l = rawDataDimensions;
47 
48  return l;
49 }
50 
51 
53 {
54 // if ( d->coordinateTransformations.size () <= 0 ) return;
55 
56  const QBrush backupBrush( context->painter()->brush() );
57  context->painter()->setBrush( QBrush() );
58  PolarCoordinatePlane* plane = dynamic_cast<PolarCoordinatePlane*>(context->coordinatePlane());
59  Q_ASSERT_X ( plane, "PolarGrid::drawGrid",
60  "Bad function call: PaintContext::coodinatePlane() NOT a polar plane." );
61 
62  const GridAttributes gridAttrsCircular( plane->gridAttributes( true ) );
63  const GridAttributes gridAttrsSagittal( plane->gridAttributes( false ) );
64 
65  //qDebug() << "OK:";
66  if ( !gridAttrsCircular.isGridVisible() && !gridAttrsSagittal.isGridVisible() ) return;
67  //qDebug() << "A";
68 
69  // FIXME: we paint the rulers to the settings of the first diagram for now:
70  AbstractPolarDiagram* dgr = dynamic_cast<AbstractPolarDiagram*> (plane->diagrams().first() );
71  Q_ASSERT ( dgr ); // only polar diagrams are allowed here
72 
73 
74  // Do not draw a grid for pie diagrams
75  if ( dynamic_cast<PieDiagram*> (plane->diagrams().first() ) ) return;
76 
77 
78  context->painter()->setPen ( PrintingParameters::scalePen( QColor ( Qt::lightGray ) ) );
79  const qreal min = dgr->dataBoundaries().first.y();
80  QPointF origin = plane->translate( QPointF( min, 0 ) ) + context->rectangle().topLeft();
81  //qDebug() << "origin" << origin;
82 
83  const qreal r = qAbs( min ) + dgr->dataBoundaries().second.y(); // use the full extents
84 
85  if ( gridAttrsSagittal.isGridVisible() ) {
86  const int numberOfSpokes = ( int ) ( 360 / plane->angleUnit() );
87  for ( int i = 0; i < numberOfSpokes ; ++i ) {
88  context->painter()->drawLine( origin, plane->translate( QPointF( r - qAbs( min ), i ) ) + context->rectangle().topLeft() );
89  }
90  }
91 
92  if ( gridAttrsCircular.isGridVisible() )
93  {
94  const qreal startPos = plane->startPosition();
95  plane->setStartPosition( 0.0 );
96  const int numberOfGridRings = ( int )dgr->numberOfGridRings();
97  for ( int j = 0; j < numberOfGridRings; ++j ) {
98  const qreal rad = min - ( ( j + 1) * r / numberOfGridRings );
99 
100  if ( rad == 0 )
101  continue;
102 
103  QRectF rect;
104  QPointF topLeftPoint;
105  QPointF bottomRightPoint;
106 
107  topLeftPoint = plane->translate( QPointF( rad, 0 ) );
108  topLeftPoint.setX( plane->translate( QPointF( rad, 90 / plane->angleUnit() ) ).x() );
109  bottomRightPoint = plane->translate( QPointF( rad, 180 / plane->angleUnit() ) );
110  bottomRightPoint.setX( plane->translate( QPointF( rad, 270 / plane->angleUnit() ) ).x() );
111 
112  rect.setTopLeft( topLeftPoint + context->rectangle().topLeft() );
113  rect.setBottomRight( bottomRightPoint + context->rectangle().topLeft() );
114 
115  context->painter()->drawEllipse( rect );
116  }
117  plane->setStartPosition( startPos );
118  }
119  context->painter()->setBrush( backupBrush );
120 }
const QPair< QPointF, QPointF > dataBoundaries() const
Return the bottom left and top right data point, that the diagram will display (unless the grid adjus...
void drawLine(const QLineF &line)
void drawGrid(PaintContext *context) override
Doing the actual drawing.
int count(const T &value) const const
qreal x() const const
Stores information about painting diagrams.
void setPen(const QColor &color)
void drawEllipse(const QRectF &rectangle)
QPointF topLeft() const const
void setBrush(const QBrush &brush)
const QPointF translate(const QPointF &diagramPoint) const override
Translate the given point in value space coordinates to a position in pixel space.
T & first()
const QBrush & brush() const const
void setTopLeft(const QPointF &position)
qreal startPosition() const
Retrieve the rotation of the coordinate plane.
A set of attributes controlling the appearance of grids.
void setX(qreal x)
const GridAttributes gridAttributes(bool circular) const
Base class for diagrams based on a polar coordinate system.
lightGray
Global namespace.
void setBottomRight(const QPointF &position)
void setStartPosition(qreal degrees)
Specify the rotation of the coordinate plane.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Wed Jan 20 2021 22:39:31 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.