KD Chart 2  [rev.2.5.1]
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Pages
KDChartNormalPlotter_p.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2001-2013 Klaralvdalens Datakonsult AB. All rights reserved.
3 **
4 ** This file is part of the KD Chart library.
5 **
6 ** Licensees holding valid commercial KD Chart licenses may use this file in
7 ** accordance with the KD Chart Commercial License Agreement provided with
8 ** the Software.
9 **
10 **
11 ** This file may be distributed and/or modified under the terms of the
12 ** GNU General Public License version 2 and version 3 as published by the
13 ** Free Software Foundation and appearing in the file LICENSE.GPL.txt included.
14 **
15 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17 **
18 ** Contact info@kdab.com if any conditions of this licensing are not
19 ** clear to you.
20 **
21 **********************************************************************/
22 
23 #include "KDChartNormalPlotter_p.h"
24 #include "KDChartPlotter.h"
25 #include "PaintingHelpers_p.h"
26 
27 #include <limits>
28 
29 using namespace KDChart;
30 using namespace std;
31 
32 NormalPlotter::NormalPlotter( Plotter* d )
33  : PlotterType( d )
34 {
35 }
36 
37 Plotter::PlotType NormalPlotter::type() const
38 {
39  return Plotter::Normal;
40 }
41 
42 const QPair< QPointF, QPointF > NormalPlotter::calculateDataBoundaries() const
43 {
44  if ( diagram()->useDataCompression() != Plotter::NONE )
45  return plotterCompressor().dataBoundaries();
46  else
47  return compressor().dataBoundaries();
48 }
49 
50 static bool isFinite( const QPointF &point )
51 {
52  return !ISINF( point.x() ) && !ISNAN( point.x() ) && !ISINF( point.y() ) && !ISNAN( point.y() );
53 }
54 
55 void NormalPlotter::paint( PaintContext* ctx )
56 {
57  reverseMapper().clear();
58 
59  Q_ASSERT( dynamic_cast< CartesianCoordinatePlane* >( ctx->coordinatePlane() ) );
60  const CartesianCoordinatePlane* const plane = static_cast< CartesianCoordinatePlane* >( ctx->coordinatePlane() );
61  const int colCount = compressor().modelDataColumns();
62  const int rowCount = compressor().modelDataRows();
63 
64  LabelPaintCache lpc;
65 
66  if ( diagram()->useDataCompression() != Plotter::NONE )
67  {
68  for ( int dataset = 0; dataset < plotterCompressor().datasetCount(); ++dataset )
69  {
70  LineAttributesInfoList lineList;
71  LineAttributes laPreviousCell;
73  for ( PlotterDiagramCompressor::Iterator it = plotterCompressor().begin( dataset ); it != plotterCompressor().end( dataset ); ++ it )
74  {
75  const PlotterDiagramCompressor::DataPoint point = *it;
76 
77  const QModelIndex sourceIndex = attributesModel()->mapToSource( point.index );
78  LineAttributes laCell = diagram()->lineAttributes( sourceIndex );
80 
81  if ( ISNAN( point.key ) || ISNAN( point.value ) )
82  {
83  switch ( policy )
84  {
85  case LineAttributes::MissingValuesAreBridged: // we just bridge both values
86  continue;
87  case LineAttributes::MissingValuesShownAsZero: // fall-through since that attribute makes no sense for the plotter
88  case LineAttributes::MissingValuesHideSegments: // fall-through since they're just hidden
89  default:
91  continue;
92  }
93  }
94 
95  // area corners, a + b are the line ends:
96  const QPointF a( plane->translate( QPointF( lastPoint.key, lastPoint.value ) ) );
97  const QPointF b( plane->translate( QPointF( point.key, point.value ) ) );
98  if ( a.toPoint() == b.toPoint() )
99  continue;
100 
101  const QPointF c( plane->translate( QPointF( lastPoint.key, 0.0 ) ) );
102  const QPointF d( plane->translate( QPointF( point.key, 0.0 ) ) );
103 
104  // add the pieces to painting if this is not hidden:
105  if ( !point.hidden /*&& !ISNAN( lastPoint.key ) && !ISNAN( lastPoint.value ) */) {
106  // add data point labels:
107  const PositionPoints pts = PositionPoints( b, a, d, c );
108  // if necessary, add the area to the area list:
109  QList<QPolygonF> areas;
110  if ( laCell.displayArea() ) {
111  QPolygonF polygon;
112  polygon << a << b << d << c;
113  areas << polygon;
114  }
115  m_private->addLabel( &lpc, sourceIndex, 0, pts, Position::NorthWest,
116  Position::NorthWest, point.value );
117  if ( !ISNAN( lastPoint.key ) && !ISNAN( lastPoint.value ) )
118  {
119  PaintingHelpers::paintAreas( m_private, ctx,
120  attributesModel()->mapToSource( lastPoint.index ),
121  areas, laCell.transparency() );
122  lineList.append( LineAttributesInfo( sourceIndex, a, b ) );
123  }
124  }
125 
126  // wrap it up:
127  laPreviousCell = laCell;
128  lastPoint = point;
129  }
130  PaintingHelpers::paintElements( m_private, ctx, lpc, lineList );
131  }
132  }
133  else
134  {
135  if ( colCount == 0 || rowCount == 0 )
136  return;
137  for ( int column = 0; column < colCount; ++column )
138  {
139  LineAttributesInfoList lineList;
140  LineAttributes laPreviousCell;
141  CartesianDiagramDataCompressor::CachePosition previousCellPosition;
142  CartesianDiagramDataCompressor::DataPoint lastPoint;
143 
144  for ( int row = 0; row < rowCount; ++row )
145  {
146  const CartesianDiagramDataCompressor::CachePosition position( row, column );
147  const CartesianDiagramDataCompressor::DataPoint point = compressor().data( position );
148 
149  const QModelIndex sourceIndex = attributesModel()->mapToSource( point.index );
150  LineAttributes laCell = diagram()->lineAttributes( sourceIndex );
152 
153  if ( ISNAN( point.key ) || ISNAN( point.value ) )
154  {
155  switch ( policy )
156  {
157  case LineAttributes::MissingValuesAreBridged: // we just bridge both values
158  continue;
159  case LineAttributes::MissingValuesShownAsZero: // fall-through since that attribute makes no sense for the plotter
160  case LineAttributes::MissingValuesHideSegments: // fall-through since they're just hidden
161  default:
162  previousCellPosition = CartesianDiagramDataCompressor::CachePosition();
163  lastPoint = CartesianDiagramDataCompressor::DataPoint();
164  continue;
165  }
166  }
167 
168  // area corners, a + b are the line ends:
169  const QPointF a( plane->translate( QPointF( lastPoint.key, lastPoint.value ) ) );
170  const QPointF b( plane->translate( QPointF( point.key, point.value ) ) );
171  if ( a.toPoint() == b.toPoint() || !isFinite( a ) || !isFinite( b ) ) {
172  lastPoint = point;
173  continue;
174  }
175 
176  const QPointF c( plane->translate( QPointF( lastPoint.key, 0.0 ) ) );
177  const QPointF d( plane->translate( QPointF( point.key, 0.0 ) ) );
178 
179  // add the pieces to painting if this is not hidden:
180  if ( !point.hidden ) {
181  // add data point labels:
182  const PositionPoints pts = PositionPoints( b, a, d, c );
183  // if necessary, add the area to the area list:
184  QList<QPolygonF> areas;
185  if ( laCell.displayArea() ) {
186  QPolygonF polygon;
187  polygon << a << b << d << c;
188  areas << polygon;
189  }
190  m_private->addLabel( &lpc, sourceIndex, 0, pts, Position::NorthWest,
191  Position::NorthWest, point.value );
192  PaintingHelpers::paintAreas( m_private, ctx,
193  attributesModel()->mapToSource( lastPoint.index ),
194  areas, laCell.transparency() );
195  lineList.append( LineAttributesInfo( sourceIndex, a, b ) );
196  }
197 
198  // wrap it up:
199  previousCellPosition = position;
200  laPreviousCell = laCell;
201  lastPoint = point;
202  }
203  PaintingHelpers::paintElements( m_private, ctx, lpc, lineList );
204  }
205  }
206 }

Klarälvdalens Datakonsult AB (KDAB)
Qt-related services and products
http://www.kdab.com/
http://www.kdab.com/products/kd-chart/