qwt_plot_marker.cpp
Go to the documentation of this file.
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 
21 {
22  public:
23  PrivateData()
24  : labelAlignment( Qt::AlignCenter )
25  , labelOrientation( Qt::Horizontal )
26  , spacing( 2 )
27  , symbol( NULL )
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;
47 
48  double xValue;
49  double yValue;
50 };
51 
54 {
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 
69  : QwtPlotItem( title )
70 {
71  m_data = new PrivateData;
72  setZ( 30.0 );
73 }
74 
77 {
78  delete m_data;
79 }
80 
83 {
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 
124 {
125  setValue( x, m_data->yValue );
126 }
127 
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 ||
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 ||
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  {
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 &&
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 
372  legendChanged();
373  itemChanged();
374  }
375 }
376 
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 )
400 
401  legendChanged();
402  itemChanged();
403  }
404 }
405 
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 
434 {
435  return m_data->label;
436 }
437 
453 {
454  if ( align != m_data->labelAlignment )
455  {
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 
516  itemChanged();
517 }
518 
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 
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 
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 );
608 
609  QPainter painter( &icon );
610  painter.setRenderHint( QPainter::Antialiasing,
612 
614  {
615  painter.setPen( m_data->pen );
616 
617  if ( m_data->style == QwtPlotMarker::HLine ||
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 ||
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 
QwtSymbol::size
const QSize & size() const
Definition: qwt_symbol.cpp:1094
color
color
Definition: color.h:16
QwtPlotMarker::symbol
const QwtSymbol * symbol() const
Definition: qwt_plot_marker.cpp:410
qwt_graphic.h
align
Definition: core.h:2016
QwtPlotMarker::yValue
double yValue() const
Return y Value.
Definition: qwt_plot_marker.cpp:100
QwtPlotMarker::spacing
int spacing() const
Definition: qwt_plot_marker.cpp:523
QwtPlotMarker::PrivateData::yValue
double yValue
Definition: qwt_plot_marker.cpp:56
QwtPlotItem::legendChanged
virtual void legendChanged()
Definition: qwt_plot_item.cpp:491
QwtGraphic
A paint device for scalable graphics.
Definition: qwt_graphic.h:75
QwtPlotMarker::HLine
@ HLine
A horizontal line.
Definition: qwt_plot_marker.h:59
QwtGraphic::setRenderHint
void setRenderHint(RenderHint, bool on=true)
Definition: qwt_graphic.cpp:443
QwtPlotMarker::PrivateData::spacing
int spacing
Definition: qwt_plot_marker.cpp:49
QwtPlotMarker::Cross
@ Cross
A crosshair.
Definition: qwt_plot_marker.h:65
QwtPlotMarker::NoLine
@ NoLine
No line.
Definition: qwt_plot_marker.h:56
QwtSymbol::boundingRect
virtual QRect boundingRect() const
Definition: qwt_symbol.cpp:1637
mqtt_test_proto.x
x
Definition: mqtt_test_proto.py:34
QwtPlotMarker::PrivateData::pen
QPen pen
Definition: qwt_plot_marker.cpp:51
QwtPlotMarker::rtti
virtual int rtti() const QWT_OVERRIDE
Definition: qwt_plot_marker.cpp:82
qwt_math.h
QwtPlotMarker::drawSymbol
virtual void drawSymbol(QPainter *, const QRectF &, const QPointF &) const
Definition: qwt_plot_marker.cpp:203
QwtPlotMarker::linePen
const QPen & linePen() const
Definition: qwt_plot_marker.cpp:567
QwtPainter::roundingAlignment
static bool roundingAlignment()
Definition: qwt_painter.h:183
QwtPlotMarker::value
QPointF value() const
Return Value.
Definition: qwt_plot_marker.cpp:88
qwt_symbol.h
mqtt_test_proto.y
y
Definition: mqtt_test_proto.py:35
QwtPlotMarker::setSymbol
void setSymbol(const QwtSymbol *)
Assign a symbol.
Definition: qwt_plot_marker.cpp:391
QwtPlotMarker::PrivateData::~PrivateData
~PrivateData()
Definition: qwt_plot_marker.cpp:41
QwtSymbol::NoSymbol
@ NoSymbol
No Style. The symbol cannot be drawn.
Definition: qwt_symbol.h:41
QwtPlotMarker::PrivateData
Definition: qwt_plot_marker.cpp:20
QwtPlotMarker::setXValue
void setXValue(double)
Set X Value.
Definition: qwt_plot_marker.cpp:123
QwtPlotMarker::legendIcon
virtual QwtGraphic legendIcon(int index, const QSizeF &) const QWT_OVERRIDE
Definition: qwt_plot_marker.cpp:598
nonstd::span_lite::size
span_constexpr std::size_t size(span< T, Extent > const &spn)
Definition: span.hpp:1554
QwtText::textSize
QSizeF textSize() const
Definition: qwt_text.cpp:570
QwtPlotMarker::PrivateData::labelAlignment
Qt::Alignment labelAlignment
Definition: qwt_plot_marker.cpp:47
QwtPlotMarker::label
QwtText label() const
Definition: qwt_plot_marker.cpp:433
QwtGraphic::RenderPensUnscaled
@ RenderPensUnscaled
Definition: qwt_graphic.h:96
QwtPlotMarker::VLine
@ VLine
A vertical line.
Definition: qwt_plot_marker.h:62
qwt_scale_map.h
QwtText
A class representing a text.
Definition: qwt_text.h:51
QwtSymbol
A class for drawing symbols.
Definition: qwt_symbol.h:31
QwtPlotMarker::PrivateData::PrivateData
PrivateData()
Definition: qwt_plot_marker.cpp:30
QwtPlotMarker::labelOrientation
Qt::Orientation labelOrientation() const
Definition: qwt_plot_marker.cpp:493
qwtMaxF
QWT_CONSTEXPR float qwtMaxF(float a, float b)
Definition: qwt_math.h:127
QwtPlotMarker::lineStyle
LineStyle lineStyle() const
Definition: qwt_plot_marker.cpp:381
QwtSymbol::drawSymbol
void drawSymbol(QPainter *, const QRectF &) const
Draw the symbol into a rectangle.
Definition: qwt_symbol.cpp:1434
QwtPlotItem::setZ
void setZ(double z)
Set the z value.
Definition: qwt_plot_item.cpp:165
QwtPainter::drawLine
static void drawLine(QPainter *, qreal x1, qreal y1, qreal x2, qreal y2)
Wrapper for QPainter::drawLine()
Definition: qwt_painter.h:154
QwtPlotMarker::setLinePen
void setLinePen(const QColor &, qreal width=0.0, Qt::PenStyle=Qt::SolidLine)
Definition: qwt_plot_marker.cpp:541
QwtPlotItem
Base class for items on the plot canvas.
Definition: qwt_plot_item.h:66
QwtPlotMarker::setSpacing
void setSpacing(int)
Set the spacing.
Definition: qwt_plot_marker.cpp:507
QwtPlotMarker::boundingRect
virtual QRectF boundingRect() const QWT_OVERRIDE
Definition: qwt_plot_marker.cpp:572
QwtPlotMarker::PrivateData::symbol
const QwtSymbol * symbol
Definition: qwt_plot_marker.cpp:52
QwtPlotMarker::m_data
PrivateData * m_data
Definition: qwt_plot_marker.h:128
QwtScaleMap::transform
double transform(double s) const
Definition: qwt_scale_map.h:137
QwtPlotItem::itemChanged
virtual void itemChanged()
Definition: qwt_plot_item.cpp:481
QwtPlotMarker::PrivateData::xValue
double xValue
Definition: qwt_plot_marker.cpp:55
QwtScaleMap
A scale map.
Definition: qwt_scale_map.h:26
QwtPlotMarker::~QwtPlotMarker
virtual ~QwtPlotMarker()
Destructor.
Definition: qwt_plot_marker.cpp:76
QwtPlotMarker::setLabel
void setLabel(const QwtText &)
Set the label.
Definition: qwt_plot_marker.cpp:420
QwtPlotMarker::PrivateData::label
QwtText label
Definition: qwt_plot_marker.cpp:46
QwtPlotMarker::QwtPlotMarker
QwtPlotMarker()
Sets alignment to Qt::AlignCenter, and style to QwtPlotMarker::NoLine.
Definition: qwt_plot_marker.cpp:53
qwt_painter.h
sol::detail::align
constexpr std::uintptr_t align(std::size_t alignment, std::uintptr_t ptr, std::size_t &space)
Definition: sol.hpp:10963
QwtPlotMarker::xValue
double xValue() const
Return x Value.
Definition: qwt_plot_marker.cpp:94
QwtPlotMarker::setYValue
void setYValue(double)
Set Y Value.
Definition: qwt_plot_marker.cpp:129
QwtPlotItem::Rtti_PlotMarker
@ Rtti_PlotMarker
For QwtPlotMarker.
Definition: qwt_plot_item.h:90
QwtText::isEmpty
bool isEmpty() const
Definition: qwt_text.cpp:739
QwtPlotMarker::setLabelOrientation
void setLabelOrientation(Qt::Orientation)
Set the orientation of the label.
Definition: qwt_plot_marker.cpp:480
QwtPlotMarker::LineStyle
LineStyle
Definition: qwt_plot_marker.h:53
QwtPlotMarker::PrivateData::labelOrientation
Qt::Orientation labelOrientation
Definition: qwt_plot_marker.cpp:48
QwtPlotMarker::draw
virtual void draw(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &) const QWT_OVERRIDE
Definition: qwt_plot_marker.cpp:142
QwtText::draw
void draw(QPainter *painter, const QRectF &rect) const
Definition: qwt_text.cpp:615
QwtPlotMarker::drawLabel
virtual void drawLabel(QPainter *, const QRectF &, const QPointF &) const
Definition: qwt_plot_marker.cpp:232
QwtSymbol::style
Style style() const
Definition: qwt_symbol.cpp:1795
QwtPlotItem::testRenderHint
bool testRenderHint(RenderHint) const
Definition: qwt_plot_item.cpp:332
qwt_text.h
QwtPlotMarker::drawLines
virtual void drawLines(QPainter *, const QRectF &, const QPointF &) const
Definition: qwt_plot_marker.cpp:163
QwtPlotMarker::PrivateData::style
LineStyle style
Definition: qwt_plot_marker.cpp:53
QwtPlotMarker::setLabelAlignment
void setLabelAlignment(Qt::Alignment)
Set the alignment of the label.
Definition: qwt_plot_marker.cpp:452
QwtPlotItem::setLegendIconSize
void setLegendIconSize(const QSize &)
Definition: qwt_plot_item.cpp:373
QwtPlotItem::RenderAntialiased
@ RenderAntialiased
Enable antialiasing.
Definition: qwt_plot_item.h:206
QwtGraphic::setDefaultSize
void setDefaultSize(const QSizeF &)
Set a default size.
Definition: qwt_graphic.cpp:553
QwtPlotMarker::labelAlignment
Qt::Alignment labelAlignment() const
Definition: qwt_plot_marker.cpp:465
QwtPlotMarker::setLineStyle
void setLineStyle(LineStyle)
Set the line style.
Definition: qwt_plot_marker.cpp:366
qwt_plot_marker.h
QwtPlotMarker::setValue
void setValue(double, double)
Set Value.
Definition: qwt_plot_marker.cpp:112


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Aug 11 2024 02:24:24