Qwt User's Guide 6.3.0
Loading...
Searching...
No Matches
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
20class 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
54{
55 m_data = new PrivateData;
56 setZ( 30.0 );
57}
58
60QwtPlotMarker::QwtPlotMarker( const QString& title )
61 : QwtPlotItem( QwtText( title ) )
62{
63 m_data = new PrivateData;
64 setZ( 30.0 );
65}
66
69 : QwtPlotItem( title )
70{
71 m_data = new PrivateData;
72 setZ( 30.0 );
73}
74
77{
78 delete m_data;
79}
80
86
88QPointF QwtPlotMarker::value() const
89{
90 return QPointF( m_data->xValue, m_data->yValue );
91}
92
95{
96 return m_data->xValue;
97}
98
101{
102 return m_data->yValue;
103}
104
106void QwtPlotMarker::setValue( const QPointF& pos )
107{
108 setValue( pos.x(), pos.y() );
109}
110
112void 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
124{
125 setValue( x, m_data->yValue );
126}
127
130{
131 setValue( m_data->xValue, y );
132}
133
142void 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
163void 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
203void 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
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
232void 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 {
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 }
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
367{
368 if ( style != m_data->style )
369 {
370 m_data->style = style;
371
373 itemChanged();
374 }
375}
376
382{
383 return m_data->style;
384}
385
392{
393 if ( symbol != m_data->symbol )
394 {
395 delete m_data->symbol;
396 m_data->symbol = symbol;
397
398 if ( symbol )
400
402 itemChanged();
403 }
404}
405
411{
412 return m_data->symbol;
413}
414
421{
422 if ( label != m_data->label )
423 {
424 m_data->label = label;
425 itemChanged();
426 }
427}
428
434{
435 return m_data->label;
436}
437
452void QwtPlotMarker::setLabelAlignment( Qt::Alignment align )
453{
454 if ( align != m_data->labelAlignment )
455 {
456 m_data->labelAlignment = align;
457 itemChanged();
458 }
459}
460
465Qt::Alignment QwtPlotMarker::labelAlignment() const
466{
467 return m_data->labelAlignment;
468}
469
480void QwtPlotMarker::setLabelOrientation( Qt::Orientation orientation )
481{
482 if ( orientation != m_data->labelOrientation )
483 {
484 m_data->labelOrientation = orientation;
485 itemChanged();
486 }
487}
488
493Qt::Orientation QwtPlotMarker::labelOrientation() const
494{
495 return m_data->labelOrientation;
496}
497
507void 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
524{
525 return m_data->spacing;
526}
527
541void QwtPlotMarker::setLinePen( const QColor& color, qreal width, Qt::PenStyle style )
542{
543 setLinePen( QPen( color, width, style ) );
544}
545
552void QwtPlotMarker::setLinePen( const QPen& pen )
553{
554 if ( pen != m_data->pen )
555 {
556 m_data->pen = pen;
557
559 itemChanged();
560 }
561}
562
567const QPen& QwtPlotMarker::linePen() const
568{
569 return m_data->pen;
570}
571
573{
574 // width/height of -1 does not affect the autoscale calculation
575
576 switch (m_data->style)
577 {
579 return QRectF( m_data->xValue, m_data->yValue, -1.0, 0.0 );
580
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
598QwtGraphic 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 );
608
609 QPainter painter( &icon );
610 painter.setRenderHint( QPainter::Antialiasing,
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
A paint device for scalable graphics.
Definition qwt_graphic.h:76
@ RenderPensUnscaled
Definition qwt_graphic.h:96
void setRenderHint(RenderHint, bool on=true)
void setDefaultSize(const QSizeF &)
Set a default size.
static bool roundingAlignment()
static void drawLine(QPainter *, qreal x1, qreal y1, qreal x2, qreal y2)
Wrapper for QPainter::drawLine()
Base class for items on the plot canvas.
void setLegendIconSize(const QSize &)
virtual void legendChanged()
void setZ(double z)
Set the z value.
@ Rtti_PlotMarker
For QwtPlotMarker.
@ RenderAntialiased
Enable antialiasing.
bool testRenderHint(RenderHint) const
virtual void itemChanged()
A class for drawing markers.
void setXValue(double)
Set X Value.
void setLineStyle(LineStyle)
Set the line style.
LineStyle lineStyle() const
@ HLine
A horizontal line.
@ Cross
A crosshair.
@ VLine
A vertical line.
void setLabelOrientation(Qt::Orientation)
Set the orientation of the label.
virtual void drawLines(QPainter *, const QRectF &, const QPointF &) const
const QPen & linePen() const
QwtText label() const
void setYValue(double)
Set Y Value.
int spacing() const
virtual void drawLabel(QPainter *, const QRectF &, const QPointF &) const
virtual void drawSymbol(QPainter *, const QRectF &, const QPointF &) const
Qt::Orientation labelOrientation() const
void setSpacing(int)
Set the spacing.
QwtPlotMarker()
Sets alignment to Qt::AlignCenter, and style to QwtPlotMarker::NoLine.
virtual int rtti() const override
virtual ~QwtPlotMarker()
Destructor.
virtual void draw(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &) const override
void setLinePen(const QColor &, qreal width=0.0, Qt::PenStyle=Qt::SolidLine)
double yValue() const
Return y Value.
void setLabelAlignment(Qt::Alignment)
Set the alignment of the label.
void setValue(double, double)
Set Value.
void setLabel(const QwtText &)
Set the label.
Qt::Alignment labelAlignment() const
double xValue() const
Return x Value.
QPointF value() const
Return Value.
virtual QRectF boundingRect() const override
void setSymbol(const QwtSymbol *)
Assign a symbol.
const QwtSymbol * symbol() const
virtual QwtGraphic legendIcon(int index, const QSizeF &) const override
A scale map.
double transform(double s) const
A class for drawing symbols.
Definition qwt_symbol.h:32
Style style() const
void drawSymbol(QPainter *, const QRectF &) const
Draw the symbol into a rectangle.
virtual QRect boundingRect() const
@ NoSymbol
No Style. The symbol cannot be drawn.
Definition qwt_symbol.h:41
const QSize & size() const
A class representing a text.
Definition qwt_text.h:52
QSizeF textSize() const
Definition qwt_text.cpp:570
bool isEmpty() const
Definition qwt_text.cpp:739
void draw(QPainter *painter, const QRectF &rect) const
Definition qwt_text.cpp:615