/
qwt_polar_curve.cpp
596 lines (500 loc) · 14.4 KB
/
qwt_polar_curve.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
* QwtPolar Widget Library
* Copyright (C) 2008 Uwe Rathmann
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the Qwt License, Version 1.0
*****************************************************************************/
#include "qwt_polar_curve.h"
#include "qwt_polar.h"
#include <qwt_painter.h>
#include <qwt_scale_map.h>
#include <qwt_math.h>
#include <qwt_symbol.h>
#include <qwt_legend.h>
#include <qwt_curve_fitter.h>
#include <qwt_clipper.h>
#include <qpainter.h>
static inline bool qwtInsidePole( const QwtScaleMap &map, double radius )
{
return map.isInverting() ? ( radius > map.s1() ) : ( radius < map.s1() );
}
static int qwtVerifyRange( int size, int &i1, int &i2 )
{
if ( size < 1 )
return 0;
i1 = qBound( 0, i1, size - 1 );
i2 = qBound( 0, i2, size - 1 );
if ( i1 > i2 )
qSwap( i1, i2 );
return ( i2 - i1 + 1 );
}
class QwtPolarCurve::PrivateData
{
public:
PrivateData():
style( QwtPolarCurve::Lines ),
curveFitter( NULL ),
legendAttributes( 0 )
{
symbol = new QwtSymbol();
pen = QPen( Qt::black );
}
~PrivateData()
{
delete symbol;
delete curveFitter;
}
QwtPolarCurve::CurveStyle style;
const QwtSymbol *symbol;
QPen pen;
QwtCurveFitter *curveFitter;
QwtPolarCurve::LegendAttributes legendAttributes;
};
//! Constructor
QwtPolarCurve::QwtPolarCurve():
QwtPolarItem( QwtText() )
{
init();
}
/*!
Constructor
\param title title of the curve
*/
QwtPolarCurve::QwtPolarCurve( const QwtText &title ):
QwtPolarItem( title )
{
init();
}
/*!
Constructor
\param title title of the curve
*/
QwtPolarCurve::QwtPolarCurve( const QString &title ):
QwtPolarItem( QwtText( title ) )
{
init();
}
//! Destructor
QwtPolarCurve::~QwtPolarCurve()
{
delete d_series;
delete d_data;
}
//! Initialize data members
void QwtPolarCurve::init()
{
d_data = new PrivateData;
d_series = NULL;
setItemAttribute( QwtPolarItem::AutoScale );
setItemAttribute( QwtPolarItem::Legend );
setZ( 20.0 );
setRenderHint( RenderAntialiased, true );
}
//! \return QwtPolarCurve::Rtti_PolarCurve
int QwtPolarCurve::rtti() const
{
return QwtPolarItem::Rtti_PolarCurve;
}
/*!
Specify an attribute how to draw the legend identifier
\param attribute Attribute
\param on On/Off
/sa LegendAttribute, testLegendAttribute()
*/
void QwtPolarCurve::setLegendAttribute( LegendAttribute attribute, bool on )
{
if ( on )
d_data->legendAttributes |= attribute;
else
d_data->legendAttributes &= ~attribute;
}
/*!
\brief Test if a lefend attribute is enables
\param attribute Legend attribute
\return True if attribute is enabled
\sa LegendAttribute, setLegendAttribute()
*/
bool QwtPolarCurve::testLegendAttribute( LegendAttribute attribute ) const
{
return ( d_data->legendAttributes & attribute );
}
/*!
Set the curve's drawing style
\param style Curve style
\sa CurveStyle, style()
*/
void QwtPolarCurve::setStyle( CurveStyle style )
{
if ( style != d_data->style )
{
d_data->style = style;
itemChanged();
}
}
/*!
\return Current style
\sa CurveStyle, setStyle()
*/
QwtPolarCurve::CurveStyle QwtPolarCurve::style() const
{
return d_data->style;
}
/*!
\brief Assign a symbol
\param symbol Symbol
\sa symbol()
*/
void QwtPolarCurve::setSymbol( QwtSymbol *symbol )
{
if ( symbol != d_data->symbol )
{
delete d_data->symbol;
d_data->symbol = symbol;
itemChanged();
}
}
/*!
\return The current symbol
\sa setSymbol()
*/
const QwtSymbol *QwtPolarCurve::symbol() const
{
return d_data->symbol;
}
/*!
\brief Assign a pen
\param pen New pen
\sa pen()
*/
void QwtPolarCurve::setPen( const QPen &pen )
{
if ( pen != d_data->pen )
{
d_data->pen = pen;
itemChanged();
}
}
/*!
\return Pen used to draw the lines
\sa setPen()
*/
const QPen& QwtPolarCurve::pen() const
{
return d_data->pen;
}
/*!
Initialize data with a pointer to QwtSeriesData<QwtPointPolar>.
The x-values of the data object represent the azimuth,
the y-value respresent the radius.
\param data Data
*/
void QwtPolarCurve::setData( QwtSeriesData<QwtPointPolar> *data )
{
if ( d_series != data )
{
delete d_series;
d_series = data;
itemChanged();
}
}
/*!
\brief Insert a curve fitter
\param curveFitter Curve fitter
A curve fitter interpolates the curve points. F.e QwtPolarFitter
adds equidistant points so that the connection gets rounded instead
of having straight lines. If curveFitter is NULL fitting is disabled.
\sa curveFitter()
*/
void QwtPolarCurve::setCurveFitter( QwtCurveFitter *curveFitter )
{
if ( curveFitter != d_data->curveFitter )
{
delete d_data->curveFitter;
d_data->curveFitter = curveFitter;
itemChanged();
}
}
/*!
\return The curve fitter
\sa setCurveFitter()
*/
QwtCurveFitter *QwtPolarCurve::curveFitter() const
{
return d_data->curveFitter;
}
/*!
Draw the curve
\param painter Painter
\param azimuthMap Maps azimuth values to values related to 0.0, M_2PI
\param radialMap Maps radius values into painter coordinates.
\param pole Position of the pole in painter coordinates
\param radius Radius of the complete plot area in painter coordinates
\param canvasRect Contents rect of the canvas in painter coordinates
*/
void QwtPolarCurve::draw( QPainter *painter,
const QwtScaleMap &azimuthMap, const QwtScaleMap &radialMap,
const QPointF &pole, double radius,
const QRectF &canvasRect ) const
{
Q_UNUSED( radius );
Q_UNUSED( canvasRect );
draw( painter, azimuthMap, radialMap, pole, 0, -1 );
}
/*!
\brief Draw an interval of the curve
\param painter Painter
\param azimuthMap Maps azimuth values to values related to 0.0, M_2PI
\param radialMap Maps radius values into painter coordinates.
\param pole Position of the pole in painter coordinates
\param from index of the first point to be painted
\param to index of the last point to be painted. If to < 0 the
curve will be painted to its last point.
\sa drawCurve(), drawSymbols(),
*/
void QwtPolarCurve::draw( QPainter *painter,
const QwtScaleMap &azimuthMap, const QwtScaleMap &radialMap,
const QPointF &pole, int from, int to ) const
{
if ( !painter || dataSize() <= 0 )
return;
if ( to < 0 )
to = dataSize() - 1;
if ( qwtVerifyRange( dataSize(), from, to ) > 0 )
{
painter->save();
painter->setPen( d_data->pen );
drawCurve( painter, d_data->style,
azimuthMap, radialMap, pole, from, to );
painter->restore();
if ( d_data->symbol->style() != QwtSymbol::NoSymbol )
{
painter->save();
drawSymbols( painter, *d_data->symbol,
azimuthMap, radialMap, pole, from, to );
painter->restore();
}
}
}
/*!
Draw the line part (without symbols) of a curve interval.
\param painter Painter
\param style Curve style, see QwtPolarCurve::CurveStyle
\param azimuthMap Maps azimuth values to values related to 0.0, M_2PI
\param radialMap Maps radius values into painter coordinates.
\param pole Position of the pole in painter coordinates
\param from index of the first point to be painted
\param to index of the last point to be painted.
\sa draw(), drawLines()
*/
void QwtPolarCurve::drawCurve( QPainter *painter, int style,
const QwtScaleMap &azimuthMap, const QwtScaleMap &radialMap,
const QPointF &pole, int from, int to ) const
{
switch ( style )
{
case Lines:
drawLines( painter, azimuthMap, radialMap, pole, from, to );
break;
case NoCurve:
default:
break;
}
}
/*!
Draw lines
\param painter Painter
\param azimuthMap Maps azimuth values to values related to 0.0, M_2PI
\param radialMap Maps radius values into painter coordinates.
\param pole Position of the pole in painter coordinates
\param from index of the first point to be painted
\param to index of the last point to be painted.
\sa draw(), drawLines(), setCurveFitter()
*/
void QwtPolarCurve::drawLines( QPainter *painter,
const QwtScaleMap &azimuthMap, const QwtScaleMap &radialMap,
const QPointF &pole, int from, int to ) const
{
int size = to - from + 1;
if ( size <= 0 )
return;
QPolygonF polyline;
if ( d_data->curveFitter )
{
QPolygonF points( size );
for ( int j = from; j <= to; j++ )
{
const QwtPointPolar point = sample( j );
points[j - from] = QPointF( point.azimuth(), point.radius() );
}
points = d_data->curveFitter->fitCurve( points );
polyline.resize( points.size() );
QPointF *polylineData = polyline.data();
QPointF *pointsData = points.data();
for ( int i = 0; i < points.size(); i++ )
{
const QwtPointPolar point( pointsData[i].x(), pointsData[i].y() );
double r = radialMap.transform( point.radius() );
const double a = azimuthMap.transform( point.azimuth() );
polylineData[i] = qwtPolar2Pos( pole, r, a );
}
}
else
{
polyline.resize( size );
QPointF *polylineData = polyline.data();
for ( int i = from; i <= to; i++ )
{
QwtPointPolar point = sample( i );
if ( !qwtInsidePole( radialMap, point.radius() ) )
{
double r = radialMap.transform( point.radius() );
const double a = azimuthMap.transform( point.azimuth() );
polylineData[i - from] = qwtPolar2Pos( pole, r, a );
}
else
{
polylineData[i - from] = pole;
}
}
}
QRectF clipRect;
if ( painter->hasClipping() )
clipRect = painter->clipRegion().boundingRect();
else
{
clipRect = painter->window();
if ( !clipRect.isEmpty() )
clipRect = painter->transform().inverted().mapRect( clipRect );
}
if ( !clipRect.isEmpty() )
{
double off = qCeil( qMax( qreal( 1.0 ), painter->pen().widthF() ) );
clipRect = clipRect.toRect().adjusted( -off, -off, off, off );
polyline = QwtClipper::clipPolygonF( clipRect, polyline );
}
QwtPainter::drawPolyline( painter, polyline );
painter->drawPolyline( polyline );
}
/*!
Draw symbols
\param painter Painter
\param symbol Curve symbol
\param azimuthMap Maps azimuth values to values related to 0.0, M_2PI
\param radialMap Maps radius values into painter coordinates.
\param pole Position of the pole in painter coordinates
\param from index of the first point to be painted
\param to index of the last point to be painted.
\sa setSymbol(), draw(), drawCurve()
*/
void QwtPolarCurve::drawSymbols( QPainter *painter, const QwtSymbol &symbol,
const QwtScaleMap &azimuthMap, const QwtScaleMap &radialMap,
const QPointF &pole, int from, int to ) const
{
painter->setBrush( symbol.brush() );
painter->setPen( symbol.pen() );
const int chunkSize = 500;
for ( int i = from; i <= to; i += chunkSize )
{
const int n = qMin( chunkSize, to - i + 1 );
QPolygonF points;
for ( int j = 0; j < n; j++ )
{
const QwtPointPolar point = sample( i + j );
if ( !qwtInsidePole( radialMap, point.radius() ) )
{
const double r = radialMap.transform( point.radius() );
const double a = azimuthMap.transform( point.azimuth() );
points += qwtPolar2Pos( pole, r, a );
}
else
{
points += pole;
}
}
if ( points.size() > 0 )
symbol.drawSymbols( painter, points );
}
}
/*!
\return Number of points
\sa setData()
*/
size_t QwtPolarCurve::dataSize() const
{
return d_series->size();
}
/*!
\return Icon representing the curve on the legend
\param index Index of the legend entry
( ignored as there is only one )
\param size Icon size
\sa QwtPolarItem::setLegendIconSize(), QwtPolarItem::legendData()
*/
QwtGraphic QwtPolarCurve::legendIcon( int index,
const QSizeF &size ) const
{
Q_UNUSED( index );
if ( size.isEmpty() )
return QwtGraphic();
QwtGraphic graphic;
graphic.setDefaultSize( size );
graphic.setRenderHint( QwtGraphic::RenderPensUnscaled, true );
QPainter painter( &graphic );
painter.setRenderHint( QPainter::Antialiasing,
testRenderHint( QwtPolarItem::RenderAntialiased ) );
if ( d_data->legendAttributes == 0 )
{
QBrush brush;
if ( style() != QwtPolarCurve::NoCurve )
{
brush = QBrush( pen().color() );
}
else if ( d_data->symbol &&
( d_data->symbol->style() != QwtSymbol::NoSymbol ) )
{
brush = QBrush( d_data->symbol->pen().color() );
}
if ( brush.style() != Qt::NoBrush )
{
QRectF r( 0, 0, size.width(), size.height() );
painter.fillRect( r, brush );
}
}
if ( d_data->legendAttributes & QwtPolarCurve::LegendShowLine )
{
if ( pen() != Qt::NoPen )
{
QPen pn = pen();
pn.setCapStyle( Qt::FlatCap );
painter.setPen( pn );
const double y = 0.5 * size.height();
QwtPainter::drawLine( &painter, 0.0, y, size.width(), y );
}
}
if ( d_data->legendAttributes & QwtPolarCurve::LegendShowSymbol )
{
if ( d_data->symbol )
{
QRectF r( 0, 0, size.width(), size.height() );
d_data->symbol->drawSymbol( &painter, r );
}
}
return graphic;
}
/*!
Interval, that is necessary to display the item
This interval can be useful for operations like clipping or autoscaling
\param scaleId Scale index
\return bounding interval
\sa QwtData::boundingRect()
*/
QwtInterval QwtPolarCurve::boundingInterval( int scaleId ) const
{
const QRectF boundingRect = d_series->boundingRect();
if ( scaleId == QwtPolar::ScaleAzimuth )
return QwtInterval( boundingRect.left(), boundingRect.right() );
if ( scaleId == QwtPolar::ScaleRadius )
return QwtInterval( boundingRect.top(), boundingRect.bottom() );
return QwtInterval();
}