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_math.h"
16 #include <qpainter.h>
17 
19 {
20 public:
22  labelAlignment( Qt::AlignCenter ),
23  labelOrientation( Qt::Horizontal ),
24  spacing( 2 ),
25  symbol( NULL ),
27  xValue( 0.0 ),
28  yValue( 0.0 )
29  {
30  }
31 
33  {
34  delete symbol;
35  }
36 
38  Qt::Alignment labelAlignment;
39  Qt::Orientation labelOrientation;
40  int spacing;
41 
42  QPen pen;
43  const QwtSymbol *symbol;
45 
46  double xValue;
47  double yValue;
48 };
49 
52  QwtPlotItem( QwtText( title ) )
53 {
54  d_data = new PrivateData;
55  setZ( 30.0 );
56 }
57 
60  QwtPlotItem( title )
61 {
62  d_data = new PrivateData;
63  setZ( 30.0 );
64 }
65 
68 {
69  delete d_data;
70 }
71 
74 {
76 }
77 
79 QPointF QwtPlotMarker::value() const
80 {
81  return QPointF( d_data->xValue, d_data->yValue );
82 }
83 
85 double QwtPlotMarker::xValue() const
86 {
87  return d_data->xValue;
88 }
89 
91 double QwtPlotMarker::yValue() const
92 {
93  return d_data->yValue;
94 }
95 
97 void QwtPlotMarker::setValue( const QPointF& pos )
98 {
99  setValue( pos.x(), pos.y() );
100 }
101 
103 void QwtPlotMarker::setValue( double x, double y )
104 {
105  if ( x != d_data->xValue || y != d_data->yValue )
106  {
107  d_data->xValue = x;
108  d_data->yValue = y;
109  itemChanged();
110  }
111 }
112 
114 void QwtPlotMarker::setXValue( double x )
115 {
116  setValue( x, d_data->yValue );
117 }
118 
120 void QwtPlotMarker::setYValue( double y )
121 {
122  setValue( d_data->xValue, y );
123 }
124 
133 void QwtPlotMarker::draw( QPainter *painter,
134  const QwtScaleMap &xMap, const QwtScaleMap &yMap,
135  const QRectF &canvasRect ) const
136 {
137  const QPointF pos( xMap.transform( d_data->xValue ),
138  yMap.transform( d_data->yValue ) );
139 
140  drawLines( painter, canvasRect, pos );
141  drawSymbol( painter, canvasRect, pos );
142  drawLabel( painter, canvasRect, pos );
143 }
144 
154 void QwtPlotMarker::drawLines( QPainter *painter,
155  const QRectF &canvasRect, const QPointF &pos ) const
156 {
157  if ( d_data->style == NoLine )
158  return;
159 
160  const bool doAlign = QwtPainter::roundingAlignment( painter );
161 
162  painter->setPen( d_data->pen );
163  if ( d_data->style == QwtPlotMarker::HLine ||
165  {
166  double y = pos.y();
167  if ( doAlign )
168  y = qRound( y );
169 
170  QwtPainter::drawLine( painter, canvasRect.left(),
171  y, canvasRect.right() - 1.0, y );
172  }
173  if ( d_data->style == QwtPlotMarker::VLine ||
175  {
176  double x = pos.x();
177  if ( doAlign )
178  x = qRound( x );
179 
180  QwtPainter::drawLine( painter, x,
181  canvasRect.top(), x, canvasRect.bottom() - 1.0 );
182  }
183 }
184 
194 void QwtPlotMarker::drawSymbol( QPainter *painter,
195  const QRectF &canvasRect, const QPointF &pos ) const
196 {
197  if ( d_data->symbol == NULL )
198  return;
199 
200  const QwtSymbol &symbol = *d_data->symbol;
201 
202  if ( symbol.style() != QwtSymbol::NoSymbol )
203  {
204  const QSizeF sz = symbol.size();
205 
206  const QRectF clipRect = canvasRect.adjusted(
207  -sz.width(), -sz.height(), sz.width(), sz.height() );
208 
209  if ( clipRect.contains( pos ) )
210  symbol.drawSymbol( painter, pos );
211  }
212 }
213 
223 void QwtPlotMarker::drawLabel( QPainter *painter,
224  const QRectF &canvasRect, const QPointF &pos ) const
225 {
226  if ( d_data->label.isEmpty() )
227  return;
228 
229  Qt::Alignment align = d_data->labelAlignment;
230  QPointF alignPos = pos;
231 
232  QSizeF symbolOff( 0, 0 );
233 
234  switch ( d_data->style )
235  {
237  {
238  // In VLine-style the y-position is pointless and
239  // the alignment flags are relative to the canvas
240 
241  if ( d_data->labelAlignment & Qt::AlignTop )
242  {
243  alignPos.setY( canvasRect.top() );
244  align &= ~Qt::AlignTop;
245  align |= Qt::AlignBottom;
246  }
247  else if ( d_data->labelAlignment & Qt::AlignBottom )
248  {
249  // In HLine-style the x-position is pointless and
250  // the alignment flags are relative to the canvas
251 
252  alignPos.setY( canvasRect.bottom() - 1 );
253  align &= ~Qt::AlignBottom;
254  align |= Qt::AlignTop;
255  }
256  else
257  {
258  alignPos.setY( canvasRect.center().y() );
259  }
260  break;
261  }
263  {
264  if ( d_data->labelAlignment & Qt::AlignLeft )
265  {
266  alignPos.setX( canvasRect.left() );
267  align &= ~Qt::AlignLeft;
268  align |= Qt::AlignRight;
269  }
270  else if ( d_data->labelAlignment & Qt::AlignRight )
271  {
272  alignPos.setX( canvasRect.right() - 1 );
273  align &= ~Qt::AlignRight;
274  align |= Qt::AlignLeft;
275  }
276  else
277  {
278  alignPos.setX( canvasRect.center().x() );
279  }
280  break;
281  }
282  default:
283  {
284  if ( d_data->symbol &&
286  {
287  symbolOff = d_data->symbol->size() + QSizeF( 1, 1 );
288  symbolOff /= 2;
289  }
290  }
291  }
292 
293  qreal pw2 = d_data->pen.widthF() / 2.0;
294  if ( pw2 == 0.0 )
295  pw2 = 0.5;
296 
297  const int spacing = d_data->spacing;
298 
299  const qreal xOff = qMax( pw2, symbolOff.width() );
300  const qreal yOff = qMax( pw2, symbolOff.height() );
301 
302  const QSizeF textSize = d_data->label.textSize( painter->font() );
303 
304  if ( align & Qt::AlignLeft )
305  {
306  alignPos.rx() -= xOff + spacing;
307  if ( d_data->labelOrientation == Qt::Vertical )
308  alignPos.rx() -= textSize.height();
309  else
310  alignPos.rx() -= textSize.width();
311  }
312  else if ( align & Qt::AlignRight )
313  {
314  alignPos.rx() += xOff + spacing;
315  }
316  else
317  {
318  if ( d_data->labelOrientation == Qt::Vertical )
319  alignPos.rx() -= textSize.height() / 2;
320  else
321  alignPos.rx() -= textSize.width() / 2;
322  }
323 
324  if ( align & Qt::AlignTop )
325  {
326  alignPos.ry() -= yOff + spacing;
327  if ( d_data->labelOrientation != Qt::Vertical )
328  alignPos.ry() -= textSize.height();
329  }
330  else if ( align & Qt::AlignBottom )
331  {
332  alignPos.ry() += yOff + spacing;
333  if ( d_data->labelOrientation == Qt::Vertical )
334  alignPos.ry() += textSize.width();
335  }
336  else
337  {
338  if ( d_data->labelOrientation == Qt::Vertical )
339  alignPos.ry() += textSize.width() / 2;
340  else
341  alignPos.ry() -= textSize.height() / 2;
342  }
343 
344  painter->translate( alignPos.x(), alignPos.y() );
345  if ( d_data->labelOrientation == Qt::Vertical )
346  painter->rotate( -90.0 );
347 
348  const QRectF textRect( 0, 0, textSize.width(), textSize.height() );
349  d_data->label.draw( painter, textRect );
350 }
351 
358 {
359  if ( style != d_data->style )
360  {
361  d_data->style = style;
362 
363  legendChanged();
364  itemChanged();
365  }
366 }
367 
373 {
374  return d_data->style;
375 }
376 
383 {
384  if ( symbol != d_data->symbol )
385  {
386  delete d_data->symbol;
387  d_data->symbol = symbol;
388 
389  if ( symbol )
390  setLegendIconSize( symbol->boundingRect().size() );
391 
392  legendChanged();
393  itemChanged();
394  }
395 }
396 
402 {
403  return d_data->symbol;
404 }
405 
412 {
413  if ( label != d_data->label )
414  {
415  d_data->label = label;
416  itemChanged();
417  }
418 }
419 
425 {
426  return d_data->label;
427 }
428 
443 void QwtPlotMarker::setLabelAlignment( Qt::Alignment align )
444 {
445  if ( align != d_data->labelAlignment )
446  {
447  d_data->labelAlignment = align;
448  itemChanged();
449  }
450 }
451 
456 Qt::Alignment QwtPlotMarker::labelAlignment() const
457 {
458  return d_data->labelAlignment;
459 }
460 
471 void QwtPlotMarker::setLabelOrientation( Qt::Orientation orientation )
472 {
473  if ( orientation != d_data->labelOrientation )
474  {
475  d_data->labelOrientation = orientation;
476  itemChanged();
477  }
478 }
479 
484 Qt::Orientation QwtPlotMarker::labelOrientation() const
485 {
486  return d_data->labelOrientation;
487 }
488 
499 {
500  if ( spacing < 0 )
501  spacing = 0;
502 
503  if ( spacing == d_data->spacing )
504  return;
505 
507  itemChanged();
508 }
509 
515 {
516  return d_data->spacing;
517 }
518 
532 void QwtPlotMarker::setLinePen( const QColor &color, qreal width, Qt::PenStyle style )
533 {
534  setLinePen( QPen( color, width, style ) );
535 }
536 
543 void QwtPlotMarker::setLinePen( const QPen &pen )
544 {
545  if ( pen != d_data->pen )
546  {
547  d_data->pen = pen;
548 
549  legendChanged();
550  itemChanged();
551  }
552 }
553 
558 const QPen &QwtPlotMarker::linePen() const
559 {
560  return d_data->pen;
561 }
562 
564 {
565  return QRectF( d_data->xValue, d_data->yValue, 0.0, 0.0 );
566 }
567 
578  const QSizeF &size ) const
579 {
580  Q_UNUSED( index );
581 
582  if ( size.isEmpty() )
583  return QwtGraphic();
584 
585  QwtGraphic icon;
586  icon.setDefaultSize( size );
588 
589  QPainter painter( &icon );
590  painter.setRenderHint( QPainter::Antialiasing,
592 
594  {
595  painter.setPen( d_data->pen );
596 
597  if ( d_data->style == QwtPlotMarker::HLine ||
599  {
600  const double y = 0.5 * size.height();
601 
602  QwtPainter::drawLine( &painter,
603  0.0, y, size.width(), y );
604  }
605 
606  if ( d_data->style == QwtPlotMarker::VLine ||
608  {
609  const double x = 0.5 * size.width();
610 
611  QwtPainter::drawLine( &painter,
612  x, 0.0, x, size.height() );
613  }
614  }
615 
616  if ( d_data->symbol )
617  {
618  const QRect r( 0.0, 0.0, size.width(), size.height() );
619  d_data->symbol->drawSymbol( &painter, r );
620  }
621 
622  return icon;
623 }
624 
Qt::Alignment labelAlignment() const
virtual void legendChanged()
virtual QRect boundingRect() const
Style style() const
LineStyle lineStyle() const
static void drawLine(QPainter *, double x1, double y1, double x2, double y2)
Wrapper for QPainter::drawLine()
Definition: qwt_painter.h:147
PrivateData * d_data
Enable antialiasing.
virtual void draw(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &) const
virtual int rtti() const
int spacing() const
void setValue(double, double)
Set Value.
virtual void drawLines(QPainter *, const QRectF &, const QPointF &) const
void setSymbol(const QwtSymbol *)
Assign a symbol.
void draw(QPainter *painter, const QRectF &rect) const
Definition: qwt_text.cpp:560
Qt::Orientation labelOrientation
A class for drawing symbols.
Definition: qwt_symbol.h:30
TFSIMD_FORCE_INLINE const tfScalar & y() const
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)
CONSTEXPR_F fields align(second_tag, fields f) noexcept
double yValue() const
Return y Value.
QSizeF textSize(const QFont &=QFont()) const
Definition: qwt_text.cpp:526
const QSize & size() const
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.h:213
void setXValue(double)
Set X Value.
A class representing a text.
Definition: qwt_text.h:51
virtual QRectF boundingRect() const
void setZ(double z)
Set the z value.
A paint device for scalable graphics.
Definition: qwt_graphic.h:74
virtual QwtGraphic legendIcon(int index, const QSizeF &) const
TFSIMD_FORCE_INLINE const tfScalar & x() const
A horizontal line.
A scale map.
Definition: qwt_scale_map.h:30
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:88
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
void setLineStyle(LineStyle st)
Set the line style.
Base class for items on the plot canvas.
Definition: qwt_plot_item.h:64
void setLegendIconSize(const QSize &)
double transform(double s) const
QwtPlotMarker(const QString &title=QString())
Sets alignment to Qt::AlignCenter, and style to QwtPlotMarker::NoLine.
void drawSymbol(QPainter *, const QRectF &) const
Draw the symbol into a rectangle.
static bool roundingAlignment()
Definition: qwt_painter.h:176
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:40


plotjuggler
Author(s): Davide Faconti
autogenerated on Sat Jul 6 2019 03:44:17