23 #include "PaintingHelpers_p.h"
28 #include "KDChartAbstractDiagram_p.h"
31 #include "KDChartLineDiagram_p.h"
34 #include "KDChartPainterSaver_p.h"
41 namespace PaintingHelpers {
53 return QPointF( point.x() * cos( yrad ) + tdAttributes.
depth() * sin( yrad ),
54 point.y() * cos( xrad ) - tdAttributes.
depth() * sin( xrad ) );
57 QPainterPath
fitPoints(
const QPolygonF &points, qreal tension, SplineDirection splineDirection )
60 path.moveTo( points.at( 0 ) );
61 const int count = points.size();
68 dataAtLambda(
const QPolygonF &points,
int count)
73 const QPolygonF &points;
76 QPointF operator() (
int i)
const
78 return i < 0 || i >= count ? QPointF(NAN, NAN) : points.at( i );
82 dataAtLambda dataAt(points, count);
84 for (
int i = 1; i < count; ++i) {
85 addSplineChunkTo( path, tension, dataAt( i - 2 ), points.at( i - 1 ), points.at( i ), dataAt( i + 1 ), splineDirection );
93 ctx->
painter()->setBrush( brush );
95 QPen( pen.color(), pen.width(), pen.style(), Qt::FlatCap, Qt::MiterJoin ) ) );
96 ctx->
painter()->drawPolyline( points );
99 void paintSpline(
PaintContext* ctx,
const QBrush& brush,
const QPen& pen,
const QPolygonF& points, qreal tension, SplineDirection splineDirection )
101 if (points.size() < 3) {
106 ctx->
painter()->setBrush( brush );
107 ctx->
painter()->setBrush( QBrush() );
109 QPen( pen.color(), pen.width(), pen.style(), Qt::FlatCap, Qt::MiterJoin ) ) );
118 const QPointF topLeft =
project( from, tdAttributes );
119 const QPointF topRight =
project ( to, tdAttributes );
120 const QPolygonF segment = QPolygonF() << from << topLeft << topRight << to;
122 QBrush indexBrush( diagram->
brush( index ) );
123 indexBrush = tdAttributes.
threeDBrush( indexBrush, QRectF(topLeft, topRight) );
125 const PainterSaver painterSaver( ctx->
painter() );
128 ctx->
painter()->setBrush( indexBrush );
131 reverseMapper->
addPolygon( index.row(), index.column(), segment );
132 ctx->
painter()->drawPolygon( segment );
144 gridDimensions.at( 0 ).end :
145 gridDimensions.at( 0 ).start,
147 gridDimensions.at( 1 ).end :
148 gridDimensions.at( 1 ).start ) ) );
151 gridDimensions.at( 0 ).start :
152 gridDimensions.at( 0 ).end,
154 gridDimensions.at( 1 ).start :
155 gridDimensions.at( 1 ).end ) ) );
156 const QPointF markerPoint = at;
160 startPoint = QPointF( bottomLeft.x(), at.y() );
162 startPoint = QPointF( at.x(), topRight.y() );
167 endPoint = QPointF( at.x(), bottomLeft.y() );
169 endPoint = QPointF( topRight.x(), at.y() );
173 const QRectF ellipseMarker = QRectF( at.x() - markerSize.width() / 2,
174 at.y() - markerSize.height() / 2,
175 markerSize.width(), markerSize.height() );
177 QPointF startMarker[3];
179 startMarker[0] = startPoint + QPointF( 0, markerSize.height() / 2 );
180 startMarker[1] = startPoint + QPointF( markerSize.width() / 2, 0 );
181 startMarker[2] = startPoint - QPointF( 0, markerSize.height() / 2 );
183 startMarker[0] = startPoint + QPointF( 0, markerSize.height() / 2 );
184 startMarker[1] = startPoint + QPointF( markerSize.width() / 2, 0 );
185 startMarker[2] = startPoint - QPointF( markerSize.width() / 2, 0 );
188 QPointF endMarker[3];
191 endMarker[0] = endPoint + QPointF( markerSize.width() / 2, 0 );
192 endMarker[1] = endPoint - QPointF( 0, markerSize.height() / 2 );
193 endMarker[2] = endPoint - QPointF( markerSize.width() / 2, 0 );
195 endMarker[0] = endPoint + QPointF( 0, markerSize.width() / 2 );
196 endMarker[1] = endPoint - QPointF( 0, markerSize.height() / 2 );
197 endMarker[2] = endPoint - QPointF( markerSize.width() / 2, 0 );
200 QPointF topLeft = startPoint;
201 QPointF bottomRightOffset = endPoint - topLeft;
202 QSizeF size( bottomRightOffset.x(), bottomRightOffset.y() );
203 QRectF area( topLeft, size );
205 PainterSaver painterSaver( ctx->
painter() );
207 ctx->
painter()->setBrush( QBrush() );
208 ctx->
painter()->drawLine( markerPoint, startPoint );
209 ctx->
painter()->drawLine( markerPoint, endPoint );
215 ctx->
painter()->drawEllipse( ellipseMarker );
219 ctx->
painter()->drawPolygon( startMarker, 3 );
220 ctx->
painter()->drawPolygon( endMarker, 3 );
227 if (
Plotter *plotter = qobject_cast< Plotter* >( diagram ) ) {
228 return plotter->threeDLineAttributes( index );
229 }
else if (
LineDiagram *lineDiagram = qobject_cast< LineDiagram* >( diagram ) ) {
230 return lineDiagram->threeDLineAttributes( index );
238 if (
Plotter *plotter = qobject_cast< Plotter* >( diagram ) ) {
239 return plotter->valueTrackerAttributes( index );
240 }
else if (
LineDiagram *lineDiagram = qobject_cast< LineDiagram* >( diagram ) ) {
241 return lineDiagram->valueTrackerAttributes( index );
247 void paintObject ( AbstractDiagram::Private *diagramPrivate,
PaintContext* ctx,
const QBrush& brush,
const QPen& pen,
const QPolygonF& points )
250 SplineDirection splineDirection = NormalSplineDirection;
252 if ( LineDiagram::Private* lineDiagram =
dynamic_cast<LineDiagram::Private*
>( diagramPrivate ) ) {
253 tension = lineDiagram->tension;
259 if ( qFuzzyIsNull(tension) ) {
262 paintSpline( ctx, brush, pen, points, tension, splineDirection );
267 const LabelPaintCache& lpc,
const LineAttributesInfoList& lineList )
271 const PainterSaver painterSaver( ctx->
painter() );
277 KDAB_FOREACH (
const LineAttributesInfo& lineInfo, lineList ) {
278 const QModelIndex& index = lineInfo.index;
283 lineInfo.nextValue, td, &diagramPrivate->reverseMapper );
285 const QBrush brush( diagram->
brush( index ) );
286 const QPen pen( diagram->
pen( index ) );
292 if (pen.style() != Qt::NoPen)
293 diagramPrivate->reverseMapper.addLine( lineInfo.index.row(), lineInfo.index.column(),
294 lineInfo.value, lineInfo.nextValue );
296 if ( points.count() && points.last() == lineInfo.value && curBrush == brush && curPen == pen ) {
300 if ( points.count() ) {
301 paintObject( diagramPrivate, ctx, curBrush, curPen, points );
306 points << lineInfo.value;
308 points << lineInfo.nextValue;
311 if ( points.count() ) {
313 paintObject( diagramPrivate, ctx, curBrush, curPen, points );
316 KDAB_FOREACH (
const LineAttributesInfo& lineInfo, lineList ) {
324 diagramPrivate->paintDataValueTextsAndMarkers( ctx, lpc,
true );
332 for (
int i = 0; i < areas.count(); ++i )
334 const QPolygonF& p = areas[ i ];
335 path.addPolygon( p );
336 diagramPrivate->reverseMapper.addPolygon( index.row(), index.column(), p );
341 QBrush trans = diagram->
brush( index );
343 trans = threeDAttrs.
threeDBrush( trans, path.boundingRect() );
345 QColor transColor = trans.color();
346 transColor.setAlpha( opacity );
347 trans.setColor(transColor);
348 QPen indexPen = diagram->
pen(index);
349 indexPen.setBrush( trans );
350 const PainterSaver painterSaver( ctx->
painter() );
354 ctx->
painter()->setBrush( trans );
356 ctx->
painter()->drawPath( path );
364 for (
int i = 0; i < areas.count(); ++i )
370 QBrush trans = diagram->
brush( index );
371 QColor transColor = trans.color();
372 transColor.setAlpha( opacity );
373 trans.setColor( transColor );
374 QPen indexPen = diagram->
pen( index );
375 indexPen.setBrush( trans );
376 const PainterSaver painterSaver( ctx->
painter() );
380 ctx->
painter()->setBrush( trans );
382 ctx->
painter()->drawPath( path );