Qwt User's Guide: qwt_plot_marker.cpp Source File

Qwt User's Guide 6.2.0

qwt_plot_marker.cpp
1 /******************************************************************************
2 * Qwt Widget Library
3 * Copyright (C) 1997 Josef Wilgen
4 * Copyright (C) 2002 Uwe Rathmann
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the Qwt License, Version 1.0
8 *****************************************************************************/
9
10 #include "qwt_plot_marker.h"
11 #include "qwt_painter.h"
12 #include "qwt_scale_map.h"
13 #include "qwt_symbol.h"
14 #include "qwt_text.h"
15 #include "qwt_graphic.h"
16 #include "qwt_math.h"
17
18 #include <qpainter.h>
19
20 class QwtPlotMarker::PrivateData
21 {
22 public:
23 PrivateData()
24 : labelAlignment( Qt::AlignCenter )
25 , labelOrientation( Qt::Horizontal )
26 , spacing( 2 )
27 , symbol( NULL )
28 , style( QwtPlotMarker::NoLine )
29 , xValue( 0.0 )
30 , yValue( 0.0 )
31 {
32 }
33
34 ~PrivateData()
35 {
36 delete symbol;
37 }
38
39 QwtText label;
40 Qt::Alignment labelAlignment;
41 Qt::Orientation labelOrientation;
42 int spacing;
43
44 QPen pen;
45 const QwtSymbol* symbol;
46 LineStyle style;
47
48 double xValue;
49 double yValue;
50 };
51
53 QwtPlotMarker::QwtPlotMarker()
54 {
55 m_data = new PrivateData;
56 setZ( 30.0 );
57 }
58
60 QwtPlotMarker::QwtPlotMarker( const QString& title )
61 : QwtPlotItem( QwtText( title ) )
62 {
63 m_data = new PrivateData;
64 setZ( 30.0 );
65 }
66
68 QwtPlotMarker::QwtPlotMarker( const QwtText& title )
69 : QwtPlotItem( title )
70 {
71 m_data = new PrivateData;
72 setZ( 30.0 );
73 }
74
76 QwtPlotMarker::~QwtPlotMarker()
77 {
78 delete m_data;
79 }
80
82 int QwtPlotMarker::rtti() const
83 {
84 return QwtPlotItem::Rtti_PlotMarker;
85 }
86
88 QPointF QwtPlotMarker::value() const
89 {
90 return QPointF( m_data->xValue, m_data->yValue );
91 }
92
94 double QwtPlotMarker::xValue() const
95 {
96 return m_data->xValue;
97 }
98
100 double QwtPlotMarker::yValue() const
101 {
102 return m_data->yValue;
103 }
104
106 void QwtPlotMarker::setValue( const QPointF& pos )
107 {
108 setValue( pos.x(), pos.y() );
109 }
110
112 void QwtPlotMarker::setValue( double x, double y )
113 {
114 if ( x != m_data->xValue || y != m_data->yValue )
115 {
116 m_data->xValue = x;
117 m_data->yValue = y;
118 itemChanged();
119 }
120 }
121
123 void QwtPlotMarker::setXValue( double x )
124 {
125 setValue( x, m_data->yValue );
126 }
127
129 void QwtPlotMarker::setYValue( double y )
130 {
131 setValue( m_data->xValue, y );
132 }
133
142 void QwtPlotMarker::draw( QPainter* painter,
143 const QwtScaleMap& xMap, const QwtScaleMap& yMap,
144 const QRectF& canvasRect ) const
145 {
146 const QPointF pos( xMap.transform( m_data->xValue ),
147 yMap.transform( m_data->yValue ) );
148
149 drawLines( painter, canvasRect, pos );
150 drawSymbol( painter, canvasRect, pos );
151 drawLabel( painter, canvasRect, pos );
152 }
153
163 void QwtPlotMarker::drawLines( QPainter* painter,
164 const QRectF& canvasRect, const QPointF& pos ) const
165 {
166 if ( m_data->style == NoLine )
167 return;
168
169 const bool doAlign = QwtPainter::roundingAlignment( painter );
170
171 painter->setPen( m_data->pen );
172 if ( m_data->style == QwtPlotMarker::HLine ||
173 m_data->style == QwtPlotMarker::Cross )
174 {
175 double y = pos.y();
176 if ( doAlign )
177 y = qRound( y );
178
179 QwtPainter::drawLine( painter, canvasRect.left(),
180 y, canvasRect.right() - 1.0, y );
181 }
182 if ( m_data->style == QwtPlotMarker::VLine ||
183 m_data->style == QwtPlotMarker::Cross )
184 {
185 double x = pos.x();
186 if ( doAlign )
187 x = qRound( x );
188
189 QwtPainter::drawLine( painter, x,
190 canvasRect.top(), x, canvasRect.bottom() - 1.0 );
191 }
192 }
193
203 void QwtPlotMarker::drawSymbol( QPainter* painter,
204 const QRectF& canvasRect, const QPointF& pos ) const
205 {
206 if ( m_data->symbol == NULL )
207 return;
208
209 const QwtSymbol& symbol = *m_data->symbol;
210
211 if ( symbol.style() != QwtSymbol::NoSymbol )
212 {
213 const QSizeF sz = symbol.size();
214
215 const QRectF clipRect = canvasRect.adjusted(
216 -sz.width(), -sz.height(), sz.width(), sz.height() );
217
218 if ( clipRect.contains( pos ) )
219 symbol.drawSymbol( painter, pos );
220 }
221 }
222
232 void QwtPlotMarker::drawLabel( QPainter* painter,
233 const QRectF& canvasRect, const QPointF& pos ) const
234 {
235 if ( m_data->label.isEmpty() )
236 return;
237
238 Qt::Alignment align = m_data->labelAlignment;
239 QPointF alignPos = pos;
240
241 QSizeF symbolOff( 0, 0 );
242
243 switch ( m_data->style )
244 {
245 case QwtPlotMarker::VLine:
246 {
247 // In VLine-style the y-position is pointless and
248 // the alignment flags are relative to the canvas
249
250 if ( m_data->labelAlignment & Qt::AlignTop )
251 {
252 alignPos.setY( canvasRect.top() );
253 align &= ~Qt::AlignTop;
254 align |= Qt::AlignBottom;
255 }
256 else if ( m_data->labelAlignment & Qt::AlignBottom )
257 {
258 // In HLine-style the x-position is pointless and
259 // the alignment flags are relative to the canvas
260
261 alignPos.setY( canvasRect.bottom() - 1 );
262 align &= ~Qt::AlignBottom;
263 align |= Qt::AlignTop;
264 }
265 else
266 {
267 alignPos.setY( canvasRect.center().y() );
268 }
269 break;
270 }
271 case QwtPlotMarker::HLine:
272 {
273 if ( m_data->labelAlignment & Qt::AlignLeft )
274 {
275 alignPos.setX( canvasRect.left() );
276 align &= ~Qt::AlignLeft;
277 align |= Qt::AlignRight;
278 }
279 else if ( m_data->labelAlignment & Qt::AlignRight )
280 {
281 alignPos.setX( canvasRect.right() - 1 );
282 align &= ~Qt::AlignRight;
283 align |= Qt::AlignLeft;
284 }
285 else
286 {
287 alignPos.setX( canvasRect.center().x() );
288 }
289 break;
290 }
291 default:
292 {
293 if ( m_data->symbol &&
294 ( m_data->symbol->style() != QwtSymbol::NoSymbol ) )
295 {
296 symbolOff = m_data->symbol->size() + QSizeF( 1, 1 );
297 symbolOff /= 2;
298 }
299 }
300 }
301
302 qreal pw2 = m_data->pen.widthF() / 2.0;
303 if ( pw2 == 0.0 )
304 pw2 = 0.5;
305
306 const int spacing = m_data->spacing;
307
308 const qreal xOff = qwtMaxF( pw2, symbolOff.width() );
309 const qreal yOff = qwtMaxF( pw2, symbolOff.height() );
310
311 const QSizeF textSize = m_data->label.textSize( painter->font() );
312
313 if ( align & Qt::AlignLeft )
314 {
315 alignPos.rx() -= xOff + spacing;
316 if ( m_data->labelOrientation == Qt::Vertical )
317 alignPos.rx() -= textSize.height();
318 else
319 alignPos.rx() -= textSize.width();
320 }
321 else if ( align & Qt::AlignRight )
322 {
323 alignPos.rx() += xOff + spacing;
324 }
325 else
326 {
327 if ( m_data->labelOrientation == Qt::Vertical )
328 alignPos.rx() -= textSize.height() / 2;
329 else
330 alignPos.rx() -= textSize.width() / 2;
331 }
332
333 if ( align & Qt::AlignTop )
334 {
335 alignPos.ry() -= yOff + spacing;
336 if ( m_data->labelOrientation != Qt::Vertical )
337 alignPos.ry() -= textSize.height();
338 }
339 else if ( align & Qt::AlignBottom )
340 {
341 alignPos.ry() += yOff + spacing;
342 if ( m_data->labelOrientation == Qt::Vertical )
343 alignPos.ry() += textSize.width();
344 }
345 else
346 {
347 if ( m_data->labelOrientation == Qt::Vertical )
348 alignPos.ry() += textSize.width() / 2;
349 else
350 alignPos.ry() -= textSize.height() / 2;
351 }
352
353 painter->translate( alignPos.x(), alignPos.y() );
354 if ( m_data->labelOrientation == Qt::Vertical )
355 painter->rotate( -90.0 );
356
357 const QRectF textRect( 0, 0, textSize.width(), textSize.height() );
358 m_data->label.draw( painter, textRect );
359 }
360
366 void QwtPlotMarker::setLineStyle( LineStyle style )
367 {
368 if ( style != m_data->style )
369 {
370 m_data->style = style;
371
372 legendChanged();
373 itemChanged();
374 }
375 }
376
381 QwtPlotMarker::LineStyle QwtPlotMarker::lineStyle() const
382 {
383 return m_data->style;
384 }
385
391 void QwtPlotMarker::setSymbol( const QwtSymbol* symbol )
392 {
393 if ( symbol != m_data->symbol )
394 {
395 delete m_data->symbol;
396 m_data->symbol = symbol;
397
398 if ( symbol )
399 setLegendIconSize( symbol->boundingRect().size() );
400
401 legendChanged();
402 itemChanged();
403 }
404 }
405
410 const QwtSymbol* QwtPlotMarker::symbol() const
411 {
412 return m_data->symbol;
413 }
414
420 void QwtPlotMarker::setLabel( const QwtText& label )
421 {
422 if ( label != m_data->label )
423 {
424 m_data->label = label;
425 itemChanged();
426 }
427 }
428
433 QwtText QwtPlotMarker::label() const
434 {
435 return m_data->label;
436 }
437
452 void QwtPlotMarker::setLabelAlignment( Qt::Alignment align )
453 {
454 if ( align != m_data->labelAlignment )
455 {
456 m_data->labelAlignment = align;
457 itemChanged();
458 }
459 }
460
465 Qt::Alignment QwtPlotMarker::labelAlignment() const
466 {
467 return m_data->labelAlignment;
468 }
469
480 void QwtPlotMarker::setLabelOrientation( Qt::Orientation orientation )
481 {
482 if ( orientation != m_data->labelOrientation )
483 {
484 m_data->labelOrientation = orientation;
485 itemChanged();
486 }
487 }
488
493 Qt::Orientation QwtPlotMarker::labelOrientation() const
494 {
495 return m_data->labelOrientation;
496 }
497
507 void QwtPlotMarker::setSpacing( int spacing )
508 {
509 if ( spacing < 0 )
510 spacing = 0;
511
512 if ( spacing == m_data->spacing )
513 return;
514
515 m_data->spacing = spacing;
516 itemChanged();
517 }
518
523 int QwtPlotMarker::spacing() const
524 {
525 return m_data->spacing;
526 }
527
541 void QwtPlotMarker::setLinePen( const QColor& color, qreal width, Qt::PenStyle style )
542 {
543 setLinePen( QPen( color, width, style ) );
544 }
545
552 void QwtPlotMarker::setLinePen( const QPen& pen )
553 {
554 if ( pen != m_data->pen )
555 {
556 m_data->pen = pen;
557
558 legendChanged();
559 itemChanged();
560 }
561 }
562
567 const QPen& QwtPlotMarker::linePen() const
568 {
569 return m_data->pen;
570 }
571
572 QRectF QwtPlotMarker::boundingRect() const
573 {
574 // width/height of -1 does not affect the autoscale calculation
575
576 switch (m_data->style)
577 {
578 case QwtPlotMarker::HLine:
579 return QRectF( m_data->xValue, m_data->yValue, -1.0, 0.0 );
580
581 case QwtPlotMarker::VLine:
582 return QRectF( m_data->xValue, m_data->yValue, 0.0, -1.0 );
583
584 default:
585 return QRectF( m_data->xValue, m_data->yValue, 0.0, 0.0 );
586 }
587 }
588
598 QwtGraphic QwtPlotMarker::legendIcon( int index, const QSizeF& size ) const
599 {
600 Q_UNUSED( index );
601
602 if ( size.isEmpty() )
603 return QwtGraphic();
604
605 QwtGraphic icon;
606 icon.setDefaultSize( size );
607 icon.setRenderHint( QwtGraphic::RenderPensUnscaled, true );
608
609 QPainter painter( &icon );
610 painter.setRenderHint( QPainter::Antialiasing,
611 testRenderHint( QwtPlotItem::RenderAntialiased ) );
612
613 if ( m_data->style != QwtPlotMarker::NoLine )
614 {
615 painter.setPen( m_data->pen );
616
617 if ( m_data->style == QwtPlotMarker::HLine ||
618 m_data->style == QwtPlotMarker::Cross )
619 {
620 const double y = 0.5 * size.height();
621
622 QwtPainter::drawLine( &painter,
623 0.0, y, size.width(), y );
624 }
625
626 if ( m_data->style == QwtPlotMarker::VLine ||
627 m_data->style == QwtPlotMarker::Cross )
628 {
629 const double x = 0.5 * size.width();
630
631 QwtPainter::drawLine( &painter,
632 x, 0.0, x, size.height() );
633 }
634 }
635
636 if ( m_data->symbol )
637 {
638 const QRect r( 0.0, 0.0, size.width(), size.height() );
639 m_data->symbol->drawSymbol( &painter, r );
640 }
641
642 return icon;
643 }
644
QwtGraphic
A paint device for scalable graphics.
Definition: qwt_graphic.h:76
QwtGraphic::RenderPensUnscaled
@ RenderPensUnscaled
Definition: qwt_graphic.h:96
QwtGraphic::setRenderHint
void setRenderHint(RenderHint, bool on=true)
Definition: qwt_graphic.cpp:443
QwtGraphic::setDefaultSize
void setDefaultSize(const QSizeF &)
Set a default size.
Definition: qwt_graphic.cpp:553
QwtPainter::roundingAlignment
static bool roundingAlignment()
Definition: qwt_painter.h:183
QwtPainter::drawLine
static void drawLine(QPainter *, qreal x1, qreal y1, qreal x2, qreal y2)
Wrapper for QPainter::drawLine()
Definition: qwt_painter.h:154
QwtPlotItem
Base class for items on the plot canvas.
Definition: qwt_plot_item.h:67
QwtPlotItem::setLegendIconSize
void setLegendIconSize(const QSize &)
Definition: qwt_plot_item.cpp:373
QwtPlotItem::legendChanged
virtual void legendChanged()
Definition: qwt_plot_item.cpp:491
QwtPlotItem::setZ
void setZ(double z)
Set the z value.
Definition: qwt_plot_item.cpp:165
QwtPlotItem::Rtti_PlotMarker
@ Rtti_PlotMarker
For QwtPlotMarker.
Definition: qwt_plot_item.h:90
QwtPlotItem::RenderAntialiased
@ RenderAntialiased
Enable antialiasing.
Definition: qwt_plot_item.h:206
QwtPlotItem::testRenderHint
bool testRenderHint(RenderHint) const
Definition: qwt_plot_item.cpp:332
QwtPlotItem::itemChanged
virtual void itemChanged()
Definition: qwt_plot_item.cpp:481
QwtPlotMarker
A class for drawing markers.
Definition: qwt_plot_marker.h:46
QwtPlotMarker::setXValue
void setXValue(double)
Set X Value.
Definition: qwt_plot_marker.cpp:123
QwtPlotMarker::setLineStyle
void setLineStyle(LineStyle)
Set the line style.
Definition: qwt_plot_marker.cpp:366
QwtPlotMarker::lineStyle
LineStyle lineStyle() const
Definition: qwt_plot_marker.cpp:381
QwtPlotMarker::LineStyle
LineStyle
Definition: qwt_plot_marker.h:54
QwtPlotMarker::HLine
@ HLine
A horizontal line.
Definition: qwt_plot_marker.h:59
QwtPlotMarker::Cross
@ Cross
A crosshair.
Definition: qwt_plot_marker.h:65
QwtPlotMarker::NoLine
@ NoLine
No line.
Definition: qwt_plot_marker.h:56
QwtPlotMarker::VLine
@ VLine
A vertical line.
Definition: qwt_plot_marker.h:62
QwtPlotMarker::setLabelOrientation
void setLabelOrientation(Qt::Orientation)
Set the orientation of the label.
Definition: qwt_plot_marker.cpp:480
QwtPlotMarker::drawLines
virtual void drawLines(QPainter *, const QRectF &, const QPointF &) const
Definition: qwt_plot_marker.cpp:163
QwtPlotMarker::linePen
const QPen & linePen() const
Definition: qwt_plot_marker.cpp:567
QwtPlotMarker::label
QwtText label() const
Definition: qwt_plot_marker.cpp:433
QwtPlotMarker::setYValue
void setYValue(double)
Set Y Value.
Definition: qwt_plot_marker.cpp:129
QwtPlotMarker::spacing
int spacing() const
Definition: qwt_plot_marker.cpp:523
QwtPlotMarker::drawLabel
virtual void drawLabel(QPainter *, const QRectF &, const QPointF &) const
Definition: qwt_plot_marker.cpp:232
QwtPlotMarker::drawSymbol
virtual void drawSymbol(QPainter *, const QRectF &, const QPointF &) const
Definition: qwt_plot_marker.cpp:203
QwtPlotMarker::labelOrientation
Qt::Orientation labelOrientation() const
Definition: qwt_plot_marker.cpp:493
QwtPlotMarker::setSpacing
void setSpacing(int)
Set the spacing.
Definition: qwt_plot_marker.cpp:507
QwtPlotMarker::QwtPlotMarker
QwtPlotMarker()
Sets alignment to Qt::AlignCenter, and style to QwtPlotMarker::NoLine.
Definition: qwt_plot_marker.cpp:53
QwtPlotMarker::rtti
virtual int rtti() const override
Definition: qwt_plot_marker.cpp:82
QwtPlotMarker::~QwtPlotMarker
virtual ~QwtPlotMarker()
Destructor.
Definition: qwt_plot_marker.cpp:76
QwtPlotMarker::draw
virtual void draw(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &) const override
Definition: qwt_plot_marker.cpp:142
QwtPlotMarker::setLinePen
void setLinePen(const QColor &, qreal width=0.0, Qt::PenStyle=Qt::SolidLine)
Definition: qwt_plot_marker.cpp:541
QwtPlotMarker::yValue
double yValue() const
Return y Value.
Definition: qwt_plot_marker.cpp:100
QwtPlotMarker::setLabelAlignment
void setLabelAlignment(Qt::Alignment)
Set the alignment of the label.
Definition: qwt_plot_marker.cpp:452
QwtPlotMarker::setValue
void setValue(double, double)
Set Value.
Definition: qwt_plot_marker.cpp:112
QwtPlotMarker::setLabel
void setLabel(const QwtText &)
Set the label.
Definition: qwt_plot_marker.cpp:420
QwtPlotMarker::labelAlignment
Qt::Alignment labelAlignment() const
Definition: qwt_plot_marker.cpp:465
QwtPlotMarker::xValue
double xValue() const
Return x Value.
Definition: qwt_plot_marker.cpp:94
QwtPlotMarker::value
QPointF value() const
Return Value.
Definition: qwt_plot_marker.cpp:88
QwtPlotMarker::boundingRect
virtual QRectF boundingRect() const override
Definition: qwt_plot_marker.cpp:572
QwtPlotMarker::setSymbol
void setSymbol(const QwtSymbol *)
Assign a symbol.
Definition: qwt_plot_marker.cpp:391
QwtPlotMarker::symbol
const QwtSymbol * symbol() const
Definition: qwt_plot_marker.cpp:410
QwtPlotMarker::legendIcon
virtual QwtGraphic legendIcon(int index, const QSizeF &) const override
Definition: qwt_plot_marker.cpp:598
QwtScaleMap
A scale map.
Definition: qwt_scale_map.h:27
QwtScaleMap::transform
double transform(double s) const
Definition: qwt_scale_map.h:137
QwtSymbol
A class for drawing symbols.
Definition: qwt_symbol.h:32
QwtSymbol::style
Style style() const
Definition: qwt_symbol.cpp:1795
QwtSymbol::drawSymbol
void drawSymbol(QPainter *, const QRectF &) const
Draw the symbol into a rectangle.
Definition: qwt_symbol.cpp:1434
QwtSymbol::boundingRect
virtual QRect boundingRect() const
Definition: qwt_symbol.cpp:1637
QwtSymbol::NoSymbol
@ NoSymbol
No Style. The symbol cannot be drawn.
Definition: qwt_symbol.h:41
QwtSymbol::size
const QSize & size() const
Definition: qwt_symbol.cpp:1094
QwtText
A class representing a text.
Definition: qwt_text.h:52