qwt_plot_marker.cpp
Go to the documentation of this file.
1 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
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:
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 
35  {
36  delete symbol;
37  }
38 
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 {
55  d_data = new PrivateData;
56  setZ( 30.0 );
57 }
58 
61  QwtPlotItem( QwtText( title ) )
62 {
63  d_data = new PrivateData;
64  setZ( 30.0 );
65 }
66 
69  QwtPlotItem( title )
70 {
71  d_data = new PrivateData;
72  setZ( 30.0 );
73 }
74 
77 {
78  delete d_data;
79 }
80 
83 {
85 }
86 
88 QPointF QwtPlotMarker::value() const
89 {
90  return QPointF( d_data->xValue, d_data->yValue );
91 }
92 
94 double QwtPlotMarker::xValue() const
95 {
96  return d_data->xValue;
97 }
98 
100 double QwtPlotMarker::yValue() const
101 {
102  return d_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 != d_data->xValue || y != d_data->yValue )
115  {
116  d_data->xValue = x;
117  d_data->yValue = y;
118  itemChanged();
119  }
120 }
121 
123 void QwtPlotMarker::setXValue( double x )
124 {
125  setValue( x, d_data->yValue );
126 }
127 
129 void QwtPlotMarker::setYValue( double y )
130 {
131  setValue( d_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( d_data->xValue ),
147  yMap.transform( d_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 ( d_data->style == NoLine )
167  return;
168 
169  const bool doAlign = QwtPainter::roundingAlignment( painter );
170 
171  painter->setPen( d_data->pen );
172  if ( d_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 ( d_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 ( d_data->symbol == NULL )
207  return;
208 
209  const QwtSymbol &symbol = *d_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 ( d_data->label.isEmpty() )
236  return;
237 
238  Qt::Alignment align = d_data->labelAlignment;
239  QPointF alignPos = pos;
240 
241  QSizeF symbolOff( 0, 0 );
242 
243  switch ( d_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 ( d_data->labelAlignment & Qt::AlignTop )
251  {
252  alignPos.setY( canvasRect.top() );
253  align &= ~Qt::AlignTop;
254  align |= Qt::AlignBottom;
255  }
256  else if ( d_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 ( d_data->labelAlignment & Qt::AlignLeft )
274  {
275  alignPos.setX( canvasRect.left() );
276  align &= ~Qt::AlignLeft;
277  align |= Qt::AlignRight;
278  }
279  else if ( d_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 ( d_data->symbol &&
295  {
296  symbolOff = d_data->symbol->size() + QSizeF( 1, 1 );
297  symbolOff /= 2;
298  }
299  }
300  }
301 
302  qreal pw2 = d_data->pen.widthF() / 2.0;
303  if ( pw2 == 0.0 )
304  pw2 = 0.5;
305 
306  const int spacing = d_data->spacing;
307 
308  const qreal xOff = qwtMaxF( pw2, symbolOff.width() );
309  const qreal yOff = qwtMaxF( pw2, symbolOff.height() );
310 
311  const QSizeF textSize = d_data->label.textSize( painter->font() );
312 
313  if ( align & Qt::AlignLeft )
314  {
315  alignPos.rx() -= xOff + spacing;
316  if ( d_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 ( d_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 ( d_data->labelOrientation != Qt::Vertical )
337  alignPos.ry() -= textSize.height();
338  }
339  else if ( align & Qt::AlignBottom )
340  {
341  alignPos.ry() += yOff + spacing;
342  if ( d_data->labelOrientation == Qt::Vertical )
343  alignPos.ry() += textSize.width();
344  }
345  else
346  {
347  if ( d_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 ( d_data->labelOrientation == Qt::Vertical )
355  painter->rotate( -90.0 );
356 
357  const QRectF textRect( 0, 0, textSize.width(), textSize.height() );
358  d_data->label.draw( painter, textRect );
359 }
360 
367 {
368  if ( style != d_data->style )
369  {
370  d_data->style = style;
371 
372  legendChanged();
373  itemChanged();
374  }
375 }
376 
382 {
383  return d_data->style;
384 }
385 
392 {
393  if ( symbol != d_data->symbol )
394  {
395  delete d_data->symbol;
396  d_data->symbol = symbol;
397 
398  if ( symbol )
399  setLegendIconSize( symbol->boundingRect().size() );
400 
401  legendChanged();
402  itemChanged();
403  }
404 }
405 
411 {
412  return d_data->symbol;
413 }
414 
421 {
422  if ( label != d_data->label )
423  {
424  d_data->label = label;
425  itemChanged();
426  }
427 }
428 
434 {
435  return d_data->label;
436 }
437 
453 {
454  if ( align != d_data->labelAlignment )
455  {
457  itemChanged();
458  }
459 }
460 
465 Qt::Alignment QwtPlotMarker::labelAlignment() const
466 {
467  return d_data->labelAlignment;
468 }
469 
480 void QwtPlotMarker::setLabelOrientation( Qt::Orientation orientation )
481 {
482  if ( orientation != d_data->labelOrientation )
483  {
484  d_data->labelOrientation = orientation;
485  itemChanged();
486  }
487 }
488 
493 Qt::Orientation QwtPlotMarker::labelOrientation() const
494 {
495  return d_data->labelOrientation;
496 }
497 
508 {
509  if ( spacing < 0 )
510  spacing = 0;
511 
512  if ( spacing == d_data->spacing )
513  return;
514 
516  itemChanged();
517 }
518 
524 {
525  return d_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 != d_data->pen )
555  {
556  d_data->pen = pen;
557 
558  legendChanged();
559  itemChanged();
560  }
561 }
562 
567 const QPen &QwtPlotMarker::linePen() const
568 {
569  return d_data->pen;
570 }
571 
573 {
574  // width/height of -1 does not affect the autoscale calculation
575 
576  switch (d_data->style)
577  {
579  return QRectF( d_data->xValue, d_data->yValue, -1.0, 0.0 );
580 
582  return QRectF( d_data->xValue, d_data->yValue, 0.0, -1.0 );
583 
584  default :
585  return QRectF( d_data->xValue, d_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( d_data->pen );
616 
617  if ( d_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 ( d_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 ( d_data->symbol )
637  {
638  const QRect r( 0.0, 0.0, size.width(), size.height() );
639  d_data->symbol->drawSymbol( &painter, r );
640  }
641 
642  return icon;
643 }
644 
Qt::Alignment labelAlignment() const
void setLineStyle(LineStyle)
Set the line style.
virtual void legendChanged()
virtual QRect boundingRect() const
Style style() const
Definition: format.h:1173
LineStyle lineStyle() const
PrivateData * d_data
Enable antialiasing.
int spacing() const
QSizeF textSize() const
Definition: qwt_text.cpp:547
void setValue(double, double)
Set Value.
static void drawLine(QPainter *, qreal x1, qreal y1, qreal x2, qreal y2)
Wrapper for QPainter::drawLine()
Definition: qwt_painter.h:152
QWT_CONSTEXPR float qwtMaxF(float a, float b)
Definition: qwt_math.h:123
virtual void drawLines(QPainter *, const QRectF &, const QPointF &) const
void setSymbol(const QwtSymbol *)
Assign a symbol.
virtual QwtGraphic legendIcon(int index, const QSizeF &) const QWT_OVERRIDE
void draw(QPainter *painter, const QRectF &rect) const
Definition: qwt_text.cpp:592
Qt::Orientation labelOrientation
A class for drawing symbols.
Definition: qwt_symbol.h:31
virtual int rtti() const QWT_OVERRIDE
void setRenderHint(RenderHint, bool on=true)
void setDefaultSize(const QSizeF &)
Set a default size.
void setLinePen(const QColor &, qreal width=0.0, Qt::PenStyle=Qt::SolidLine)
double yValue() const
Return y Value.
const QSize & size() const
virtual QRectF boundingRect() const QWT_OVERRIDE
void * align(std::size_t alignment, std::size_t size, void *&ptr, std::size_t &space, std::size_t &required_space)
Definition: sol.hpp:9768
bool testRenderHint(RenderHint) const
double xValue() const
Return x Value.
void setSpacing(int)
Set the spacing.
const QwtSymbol * symbol() const
bool isEmpty() const
Definition: qwt_text.cpp:716
void setXValue(double)
Set X Value.
A class representing a text.
Definition: qwt_text.h:51
void setZ(double z)
Set the z value.
A paint device for scalable graphics.
Definition: qwt_graphic.h:75
A horizontal line.
A scale map.
Definition: qwt_scale_map.h:26
QwtPlotMarker()
Sets alignment to Qt::AlignCenter, and style to QwtPlotMarker::NoLine.
virtual void itemChanged()
virtual void drawSymbol(QPainter *, const QRectF &, const QPointF &) const
void setLabel(const QwtText &)
Set the label.
virtual void drawLabel(QPainter *, const QRectF &, const QPointF &) const
Qt::Orientation labelOrientation() const
For QwtPlotMarker.
Definition: qwt_plot_item.h:89
QPointF value() const
Return Value.
virtual ~QwtPlotMarker()
Destructor.
void setLabelAlignment(Qt::Alignment)
Set the alignment of the label.
void setYValue(double)
Set Y Value.
void setLabelOrientation(Qt::Orientation)
Set the orientation of the label.
QwtText label() const
Base class for items on the plot canvas.
Definition: qwt_plot_item.h:65
void setLegendIconSize(const QSize &)
double transform(double s) const
void drawSymbol(QPainter *, const QRectF &) const
Draw the symbol into a rectangle.
static bool roundingAlignment()
Definition: qwt_painter.h:181
virtual void draw(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &) const QWT_OVERRIDE
const QPen & linePen() const
A class for drawing markers.
const QwtText & title() const
A vertical line.
No Style. The symbol cannot be drawn.
Definition: qwt_symbol.h:41


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:48:10