qwt_plot_scaleitem.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_scaleitem.h"
11 #include "qwt_plot.h"
12 #include "qwt_scale_map.h"
13 #include "qwt_interval.h"
14 #include "qwt_text.h"
15 
16 #include <qpalette.h>
17 #include <qpainter.h>
18 
20 {
21  public:
22  PrivateData()
23  : position( 0.0 )
24  , borderDistance( -1 )
25  , scaleDivFromAxis( true )
26  , scaleDraw( new QwtScaleDraw() )
27  {
28  }
29 
30  ~PrivateData()
31  {
32  delete scaleDraw;
33  }
34 
35  QwtInterval scaleInterval( const QRectF&,
36  const QwtScaleMap&, const QwtScaleMap& ) const;
37 
38  QPalette palette;
39  QFont font;
40  double position;
41  int borderDistance;
42  bool scaleDivFromAxis;
44 };
45 
47  const QwtScaleMap& xMap, const QwtScaleMap& yMap ) const
48 {
49  QwtInterval interval;
50  if ( scaleDraw->orientation() == Qt::Horizontal )
51  {
52  interval.setMinValue( xMap.invTransform( canvasRect.left() ) );
53  interval.setMaxValue( xMap.invTransform( canvasRect.right() - 1 ) );
54  }
55  else
56  {
57  interval.setMinValue( yMap.invTransform( canvasRect.bottom() - 1 ) );
58  interval.setMaxValue( yMap.invTransform( canvasRect.top() ) );
59  }
60 
61  return interval;
62 }
63 
76  QwtScaleDraw::Alignment alignment, const double pos )
77  : QwtPlotItem( QwtText( "Scale" ) )
78 {
79  m_data = new PrivateData;
80  m_data->position = pos;
81  m_data->scaleDraw->setAlignment( alignment );
82 
84  setZ( 11.0 );
85 }
86 
89 {
90  delete m_data;
91 }
92 
95 {
97 }
98 
109 {
110  m_data->scaleDivFromAxis = false;
112 }
113 
116 {
117  return m_data->scaleDraw->scaleDiv();
118 }
119 
128 {
129  if ( on != m_data->scaleDivFromAxis )
130  {
131  m_data->scaleDivFromAxis = on;
132  if ( on )
133  {
134  const QwtPlot* plt = plot();
135  if ( plt )
136  {
137  updateScaleDiv( plt->axisScaleDiv( xAxis() ),
138  plt->axisScaleDiv( yAxis() ) );
139  itemChanged();
140  }
141  }
142  }
143 }
144 
151 {
152  return m_data->scaleDivFromAxis;
153 }
154 
159 void QwtPlotScaleItem::setPalette( const QPalette& palette )
160 {
161  if ( palette != m_data->palette )
162  {
164 
165  legendChanged();
166  itemChanged();
167  }
168 }
169 
175 {
176  return m_data->palette;
177 }
178 
183 void QwtPlotScaleItem::setFont( const QFont& font )
184 {
185  if ( font != m_data->font )
186  {
187  m_data->font = font;
188  itemChanged();
189  }
190 }
191 
197 {
198  return m_data->font;
199 }
200 
213 {
214  if ( scaleDraw == NULL )
215  return;
216 
217  if ( scaleDraw != m_data->scaleDraw )
218  delete m_data->scaleDraw;
219 
221 
222  const QwtPlot* plt = plot();
223  if ( plt )
224  {
225  updateScaleDiv( plt->axisScaleDiv( xAxis() ),
226  plt->axisScaleDiv( yAxis() ) );
227  }
228 
229  itemChanged();
230 }
231 
237 {
238  return m_data->scaleDraw;
239 }
240 
246 {
247  return m_data->scaleDraw;
248 }
249 
262 {
263  if ( m_data->position != pos )
264  {
265  m_data->position = pos;
266  m_data->borderDistance = -1;
267  itemChanged();
268  }
269 }
270 
276 {
277  return m_data->position;
278 }
279 
297 {
298  if ( distance < 0 )
299  distance = -1;
300 
301  if ( distance != m_data->borderDistance )
302  {
303  m_data->borderDistance = distance;
304  itemChanged();
305  }
306 }
307 
313 {
314  return m_data->borderDistance;
315 }
316 
334 {
336  if ( sd->alignment() != alignment )
337  {
338  sd->setAlignment( alignment );
339  itemChanged();
340  }
341 }
342 
346 void QwtPlotScaleItem::draw( QPainter* painter,
347  const QwtScaleMap& xMap, const QwtScaleMap& yMap,
348  const QRectF& canvasRect ) const
349 {
351 
352  if ( m_data->scaleDivFromAxis )
353  {
354  const QwtInterval interval =
355  m_data->scaleInterval( canvasRect, xMap, yMap );
356 
357  if ( interval != sd->scaleDiv().interval() )
358  {
359  QwtScaleDiv scaleDiv = sd->scaleDiv();
360  scaleDiv.setInterval( interval );
361  sd->setScaleDiv( scaleDiv );
362  }
363  }
364 
365  QPen pen = painter->pen();
366  pen.setStyle( Qt::SolidLine );
367  painter->setPen( pen );
368 
369  if ( sd->orientation() == Qt::Horizontal )
370  {
371  double y;
372  if ( m_data->borderDistance >= 0 )
373  {
374  if ( sd->alignment() == QwtScaleDraw::BottomScale )
375  y = canvasRect.top() + m_data->borderDistance;
376  else
377  {
378  y = canvasRect.bottom() - m_data->borderDistance;
379  }
380 
381  }
382  else
383  {
384  y = yMap.transform( m_data->position );
385  }
386 
387  if ( y < canvasRect.top() || y > canvasRect.bottom() )
388  return;
389 
390  sd->move( canvasRect.left(), y );
391  sd->setLength( canvasRect.width() - 1 );
392 
393  QwtTransform* transform = NULL;
394  if ( xMap.transformation() )
395  transform = xMap.transformation()->copy();
396 
397  sd->setTransformation( transform );
398  }
399  else // == Qt::Vertical
400  {
401  double x;
402  if ( m_data->borderDistance >= 0 )
403  {
404  if ( sd->alignment() == QwtScaleDraw::RightScale )
405  x = canvasRect.left() + m_data->borderDistance;
406  else
407  {
408  x = canvasRect.right() - m_data->borderDistance;
409  }
410  }
411  else
412  {
413  x = xMap.transform( m_data->position );
414  }
415  if ( x < canvasRect.left() || x > canvasRect.right() )
416  return;
417 
418  sd->move( x, canvasRect.top() );
419  sd->setLength( canvasRect.height() - 1 );
420 
421  QwtTransform* transform = NULL;
422  if ( yMap.transformation() )
423  transform = yMap.transformation()->copy();
424 
425  sd->setTransformation( transform );
426  }
427 
428  painter->setFont( m_data->font );
429 
430  sd->draw( painter, m_data->palette );
431 }
432 
446  const QwtScaleDiv& yScaleDiv )
447 {
449 
451  {
452  const QwtScaleDiv& scaleDiv =
453  scaleDraw->orientation() == Qt::Horizontal ? xScaleDiv : yScaleDiv;
454 
455  const QwtPlot* plt = plot();
456  if ( plt != NULL )
457  {
458  const QRectF canvasRect = plt->canvas()->contentsRect();
459 
460  const QwtInterval interval = m_data->scaleInterval(
461  canvasRect, plt->canvasMap( xAxis() ), plt->canvasMap( yAxis() ) );
462 
463  QwtScaleDiv sd = scaleDiv;
464  sd.setInterval( interval );
465 
466  if ( sd != scaleDraw->scaleDiv() )
467  {
468  // the internal label cache of QwtScaleDraw
469  // is cleared here, so better avoid pointless
470  // assignments.
471 
472  scaleDraw->setScaleDiv( sd );
473  }
474  }
475  else
476  {
478  }
479  }
480 }
QwtAbstractScaleDraw::draw
virtual void draw(QPainter *, const QPalette &) const
Draw the scale.
Definition: qwt_abstract_scale_draw.cpp:169
QwtPlotScaleItem::borderDistance
int borderDistance() const
Definition: qwt_plot_scaleitem.cpp:312
QwtScaleDraw
A class for drawing scales.
Definition: qwt_scale_draw.h:35
QwtScaleDraw::alignment
Alignment alignment() const
Definition: qwt_scale_draw.cpp:309
QwtScaleDraw::RightScale
@ RightScale
The scale is right.
Definition: qwt_scale_draw.h:54
QwtTransform::copy
virtual QwtTransform * copy() const =0
Virtualized copy operation.
QwtPlotScaleItem::font
QFont font() const
Definition: qwt_plot_scaleitem.cpp:196
QwtPlotItem::Rtti_PlotScale
@ Rtti_PlotScale
For QwtPlotScaleItem.
Definition: qwt_plot_item.h:84
QwtPlotItem::setItemInterest
void setItemInterest(ItemInterest, bool on=true)
Definition: qwt_plot_item.cpp:279
QwtAbstractScaleDraw::setTransformation
void setTransformation(QwtTransform *)
Definition: qwt_abstract_scale_draw.cpp:115
QwtPlotScaleItem::setBorderDistance
void setBorderDistance(int)
Align the scale to the canvas.
Definition: qwt_plot_scaleitem.cpp:296
QwtScaleMap::invTransform
double invTransform(double p) const
Definition: qwt_scale_map.h:154
QwtPlotScaleItem::QwtPlotScaleItem
QwtPlotScaleItem(QwtScaleDraw::Alignment=QwtScaleDraw::BottomScale, const double pos=0.0)
Constructor for scale item at the position pos.
Definition: qwt_plot_scaleitem.cpp:75
QwtPlotScaleItem::position
double position() const
Definition: qwt_plot_scaleitem.cpp:275
QwtPlotScaleItem::scaleDiv
const QwtScaleDiv & scaleDiv() const
Definition: qwt_plot_scaleitem.cpp:115
QwtPlotItem::legendChanged
virtual void legendChanged()
Definition: qwt_plot_item.cpp:491
QwtPlotScaleItem::PrivateData::borderDistance
int borderDistance
Definition: qwt_plot_scaleitem.cpp:48
QwtPlotScaleItem::setScaleDiv
void setScaleDiv(const QwtScaleDiv &)
Assign a scale division.
Definition: qwt_plot_scaleitem.cpp:108
QwtPlotScaleItem::scaleDraw
const QwtScaleDraw * scaleDraw() const
Definition: qwt_plot_scaleitem.cpp:236
QwtPlotScaleItem::PrivateData::scaleDraw
QwtScaleDraw * scaleDraw
Definition: qwt_plot_scaleitem.cpp:50
QwtAbstractScaleDraw::setScaleDiv
void setScaleDiv(const QwtScaleDiv &)
Definition: qwt_abstract_scale_draw.cpp:104
QwtPlotScaleItem::PrivateData::scaleDivFromAxis
bool scaleDivFromAxis
Definition: qwt_plot_scaleitem.cpp:49
QwtTransform
A transformation between coordinate systems.
Definition: qwt_transform.h:35
QwtPlotScaleItem::PrivateData::palette
QPalette palette
Definition: qwt_plot_scaleitem.cpp:45
QwtPlot
A 2-D plotting widget.
Definition: qwt_plot.h:78
QwtScaleDraw::orientation
Qt::Orientation orientation() const
Definition: qwt_scale_draw.cpp:337
QwtAbstractScaleDraw::scaleDiv
const QwtScaleDiv & scaleDiv() const
Definition: qwt_abstract_scale_draw.cpp:133
qwt_interval.h
qwt_plot_scaleitem.h
mqtt_test_proto.x
x
Definition: mqtt_test_proto.py:34
QwtPlotScaleItem::palette
QPalette palette() const
Definition: qwt_plot_scaleitem.cpp:174
QwtPlotScaleItem::PrivateData::position
double position
Definition: qwt_plot_scaleitem.cpp:47
QwtScaleDiv::setInterval
void setInterval(double lowerBound, double upperBound)
Definition: qwt_scale_div.cpp:91
QwtPlotScaleItem::PrivateData::scaleInterval
QwtInterval scaleInterval(const QRectF &, const QwtScaleMap &, const QwtScaleMap &) const
Definition: qwt_plot_scaleitem.cpp:46
mqtt_test_proto.y
y
Definition: mqtt_test_proto.py:35
QwtPlotItem::xAxis
QwtAxisId xAxis() const
Return xAxis.
Definition: qwt_plot_item.cpp:553
QwtPlotItem::plot
QwtPlot * plot() const
Return attached plot.
Definition: qwt_plot_item.cpp:142
QwtPlotScaleItem::~QwtPlotScaleItem
virtual ~QwtPlotScaleItem()
Destructor.
Definition: qwt_plot_scaleitem.cpp:88
QwtInterval
A class representing an interval.
Definition: qwt_interval.h:22
qwt_scale_map.h
QwtText
A class representing a text.
Definition: qwt_text.h:51
QwtPlotScaleItem::setPosition
void setPosition(double pos)
Definition: qwt_plot_scaleitem.cpp:261
QwtPlotItem::setZ
void setZ(double z)
Set the z value.
Definition: qwt_plot_item.cpp:165
QwtPlotScaleItem::PrivateData::font
QFont font
Definition: qwt_plot_scaleitem.cpp:46
QwtPlotScaleItem::rtti
virtual int rtti() const QWT_OVERRIDE
Definition: qwt_plot_scaleitem.cpp:94
QwtPlotScaleItem::setScaleDraw
void setScaleDraw(QwtScaleDraw *)
Set a scale draw.
Definition: qwt_plot_scaleitem.cpp:212
QwtPlotItem
Base class for items on the plot canvas.
Definition: qwt_plot_item.h:66
QwtScaleDraw::Alignment
Alignment
Definition: qwt_scale_draw.h:42
QwtScaleDraw::move
void move(double x, double y)
Definition: qwt_scale_draw.h:118
QwtScaleMap::transform
double transform(double s) const
Definition: qwt_scale_map.h:137
QwtPlotItem::itemChanged
virtual void itemChanged()
Definition: qwt_plot_item.cpp:481
QwtPlot::axisScaleDiv
const QwtScaleDiv & axisScaleDiv(QwtAxisId) const
Return the scale division of a specified axis.
Definition: qwt_plot_axis.cpp:283
QwtPlotScaleItem::PrivateData
Definition: qwt_plot_scaleitem.cpp:19
QwtScaleMap
A scale map.
Definition: qwt_scale_map.h:26
QwtInterval::setMinValue
void setMinValue(double)
Definition: qwt_interval.h:176
QwtPlotScaleItem::setPalette
void setPalette(const QPalette &)
Definition: qwt_plot_scaleitem.cpp:159
QwtPlotScaleItem::PrivateData::PrivateData
PrivateData()
Definition: qwt_plot_scaleitem.cpp:29
QwtPlotScaleItem::draw
virtual void draw(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect) const QWT_OVERRIDE
Draw the scale.
Definition: qwt_plot_scaleitem.cpp:346
QwtScaleDraw::BottomScale
@ BottomScale
The scale is below.
Definition: qwt_scale_draw.h:45
QwtPlotScaleItem::setScaleDivFromAxis
void setScaleDivFromAxis(bool on)
Definition: qwt_plot_scaleitem.cpp:127
QwtPlotScaleItem::m_data
PrivateData * m_data
Definition: qwt_plot_scaleitem.h:90
QwtPlotScaleItem::isScaleDivFromAxis
bool isScaleDivFromAxis() const
Definition: qwt_plot_scaleitem.cpp:150
QwtPlotScaleItem::PrivateData::~PrivateData
~PrivateData()
Definition: qwt_plot_scaleitem.cpp:37
QwtPlot::canvas
QWidget * canvas()
Definition: qwt_plot.cpp:463
QwtScaleDiv
A class representing a scale division.
Definition: qwt_scale_div.h:33
qwt_text.h
QwtScaleMap::transformation
const QwtTransform * transformation() const
Get the transformation.
Definition: qwt_scale_map.cpp:88
QwtPlotScaleItem::updateScaleDiv
virtual void updateScaleDiv(const QwtScaleDiv &, const QwtScaleDiv &) QWT_OVERRIDE
Update the item to changes of the axes scale division.
Definition: qwt_plot_scaleitem.cpp:445
QwtPlotScaleItem::setAlignment
void setAlignment(QwtScaleDraw::Alignment)
Definition: qwt_plot_scaleitem.cpp:333
QwtPlotItem::ScaleInterest
@ ScaleInterest
Definition: qwt_plot_item.h:185
QwtPlot::canvasMap
virtual QwtScaleMap canvasMap(QwtAxisId) const
Definition: qwt_plot.cpp:800
QwtPlotScaleItem::setFont
void setFont(const QFont &)
Definition: qwt_plot_scaleitem.cpp:183
QwtScaleDraw::setAlignment
void setAlignment(Alignment)
Definition: qwt_scale_draw.cpp:322
QwtPlotItem::yAxis
QwtAxisId yAxis() const
Return yAxis.
Definition: qwt_plot_item.cpp:559
QwtScaleDiv::interval
QwtInterval interval() const
Definition: qwt_scale_div.cpp:111
QwtScaleDraw::setLength
void setLength(double length)
Definition: qwt_scale_draw.cpp:732
QwtInterval::setMaxValue
void setMaxValue(double)
Definition: qwt_interval.h:186
qwt_plot.h


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:46