qwt_plot_scaleitem.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_scaleitem.h"
11 #include "qwt_plot.h"
12 #include "qwt_scale_map.h"
13 #include "qwt_interval.h"
14 #include <qpalette.h>
15 #include <qpainter.h>
16 
18 {
19 public:
21  position( 0.0 ),
22  borderDistance( -1 ),
23  scaleDivFromAxis( true ),
24  scaleDraw( new QwtScaleDraw() )
25  {
26  }
27 
29  {
30  delete scaleDraw;
31  }
32 
33  QwtInterval scaleInterval( const QRectF &,
34  const QwtScaleMap &, const QwtScaleMap & ) const;
35 
36  QPalette palette;
37  QFont font;
38  double position;
42 };
43 
45  const QwtScaleMap &xMap, const QwtScaleMap &yMap ) const
46 {
47  QwtInterval interval;
48  if ( scaleDraw->orientation() == Qt::Horizontal )
49  {
50  interval.setMinValue( xMap.invTransform( canvasRect.left() ) );
51  interval.setMaxValue( xMap.invTransform( canvasRect.right() - 1 ) );
52  }
53  else
54  {
55  interval.setMinValue( yMap.invTransform( canvasRect.bottom() - 1 ) );
56  interval.setMaxValue( yMap.invTransform( canvasRect.top() ) );
57  }
58 
59  return interval;
60 }
61 
74  QwtScaleDraw::Alignment alignment, const double pos ):
75  QwtPlotItem( QwtText( "Scale" ) )
76 {
77  d_data = new PrivateData;
78  d_data->position = pos;
79  d_data->scaleDraw->setAlignment( alignment );
80 
82  setZ( 11.0 );
83 }
84 
87 {
88  delete d_data;
89 }
90 
93 {
95 }
96 
107 {
108  d_data->scaleDivFromAxis = false;
109  d_data->scaleDraw->setScaleDiv( scaleDiv );
110 }
111 
114 {
115  return d_data->scaleDraw->scaleDiv();
116 }
117 
126 {
127  if ( on != d_data->scaleDivFromAxis )
128  {
129  d_data->scaleDivFromAxis = on;
130  if ( on )
131  {
132  const QwtPlot *plt = plot();
133  if ( plt )
134  {
135  updateScaleDiv( plt->axisScaleDiv( xAxis() ),
136  plt->axisScaleDiv( yAxis() ) );
137  itemChanged();
138  }
139  }
140  }
141 }
142 
149 {
150  return d_data->scaleDivFromAxis;
151 }
152 
157 void QwtPlotScaleItem::setPalette( const QPalette &palette )
158 {
159  if ( palette != d_data->palette )
160  {
162 
163  legendChanged();
164  itemChanged();
165  }
166 }
167 
173 {
174  return d_data->palette;
175 }
176 
181 void QwtPlotScaleItem::setFont( const QFont &font )
182 {
183  if ( font != d_data->font )
184  {
185  d_data->font = font;
186  itemChanged();
187  }
188 }
189 
195 {
196  return d_data->font;
197 }
198 
211 {
212  if ( scaleDraw == NULL )
213  return;
214 
215  if ( scaleDraw != d_data->scaleDraw )
216  delete d_data->scaleDraw;
217 
219 
220  const QwtPlot *plt = plot();
221  if ( plt )
222  {
223  updateScaleDiv( plt->axisScaleDiv( xAxis() ),
224  plt->axisScaleDiv( yAxis() ) );
225  }
226 
227  itemChanged();
228 }
229 
235 {
236  return d_data->scaleDraw;
237 }
238 
244 {
245  return d_data->scaleDraw;
246 }
247 
260 {
261  if ( d_data->position != pos )
262  {
263  d_data->position = pos;
264  d_data->borderDistance = -1;
265  itemChanged();
266  }
267 }
268 
274 {
275  return d_data->position;
276 }
277 
295 {
296  if ( distance < 0 )
297  distance = -1;
298 
299  if ( distance != d_data->borderDistance )
300  {
301  d_data->borderDistance = distance;
302  itemChanged();
303  }
304 }
305 
311 {
312  return d_data->borderDistance;
313 }
314 
332 {
334  if ( sd->alignment() != alignment )
335  {
336  sd->setAlignment( alignment );
337  itemChanged();
338  }
339 }
340 
344 void QwtPlotScaleItem::draw( QPainter *painter,
345  const QwtScaleMap &xMap, const QwtScaleMap &yMap,
346  const QRectF &canvasRect ) const
347 {
349 
350  if ( d_data->scaleDivFromAxis )
351  {
352  const QwtInterval interval =
353  d_data->scaleInterval( canvasRect, xMap, yMap );
354 
355  if ( interval != sd->scaleDiv().interval() )
356  {
357  QwtScaleDiv scaleDiv = sd->scaleDiv();
358  scaleDiv.setInterval( interval );
359  sd->setScaleDiv( scaleDiv );
360  }
361  }
362 
363  QPen pen = painter->pen();
364  pen.setStyle( Qt::SolidLine );
365  painter->setPen( pen );
366 
367  if ( sd->orientation() == Qt::Horizontal )
368  {
369  double y;
370  if ( d_data->borderDistance >= 0 )
371  {
372  if ( sd->alignment() == QwtScaleDraw::BottomScale )
373  y = canvasRect.top() + d_data->borderDistance;
374  else
375  {
376  y = canvasRect.bottom() - d_data->borderDistance;
377  }
378 
379  }
380  else
381  {
382  y = yMap.transform( d_data->position );
383  }
384 
385  if ( y < canvasRect.top() || y > canvasRect.bottom() )
386  return;
387 
388  sd->move( canvasRect.left(), y );
389  sd->setLength( canvasRect.width() - 1 );
390 
391  QwtTransform *transform = NULL;
392  if ( xMap.transformation() )
393  transform = xMap.transformation()->copy();
394 
395  sd->setTransformation( transform );
396  }
397  else // == Qt::Vertical
398  {
399  double x;
400  if ( d_data->borderDistance >= 0 )
401  {
402  if ( sd->alignment() == QwtScaleDraw::RightScale )
403  x = canvasRect.left() + d_data->borderDistance;
404  else
405  {
406  x = canvasRect.right() - d_data->borderDistance;
407  }
408  }
409  else
410  {
411  x = xMap.transform( d_data->position );
412  }
413  if ( x < canvasRect.left() || x > canvasRect.right() )
414  return;
415 
416  sd->move( x, canvasRect.top() );
417  sd->setLength( canvasRect.height() - 1 );
418 
419  QwtTransform *transform = NULL;
420  if ( yMap.transformation() )
421  transform = yMap.transformation()->copy();
422 
423  sd->setTransformation( transform );
424  }
425 
426  painter->setFont( d_data->font );
427 
428  sd->draw( painter, d_data->palette );
429 }
430 
444  const QwtScaleDiv& yScaleDiv )
445 {
447 
448  if ( d_data->scaleDivFromAxis && scaleDraw )
449  {
450  const QwtScaleDiv &scaleDiv =
451  scaleDraw->orientation() == Qt::Horizontal ? xScaleDiv : yScaleDiv;
452 
453  const QwtPlot *plt = plot();
454  if ( plt != NULL )
455  {
456  const QRectF canvasRect = plt->canvas()->contentsRect();
457 
458  const QwtInterval interval = d_data->scaleInterval(
459  canvasRect, plt->canvasMap( xAxis() ), plt->canvasMap( yAxis() ) );
460 
461  QwtScaleDiv sd = scaleDiv;
462  sd.setInterval( interval );
463 
464  if ( sd != scaleDraw->scaleDiv() )
465  {
466  // the internal label cache of QwtScaleDraw
467  // is cleared here, so better avoid pointless
468  // assignments.
469 
470  scaleDraw->setScaleDiv( sd );
471  }
472  }
473  else
474  {
475  scaleDraw->setScaleDiv( scaleDiv );
476  }
477  }
478 }
virtual void updateScaleDiv(const QwtScaleDiv &, const QwtScaleDiv &)
Update the item to changes of the axes scale division.
virtual void legendChanged()
void setPosition(double pos)
int borderDistance() const
void setScaleDivFromAxis(bool on)
const QwtScaleDiv & axisScaleDiv(int axisId) const
Return the scale division of a specified axis.
For QwtPlotScaleItem.
Definition: qwt_plot_item.h:82
void setAlignment(Alignment)
void setLength(double length)
A class representing an interval.
Definition: qwt_interval.h:26
int xAxis() const
Return xAxis.
const QwtScaleDraw * scaleDraw() const
void setScaleDraw(QwtScaleDraw *)
Set a scale draw.
void setPalette(const QPalette &)
virtual QwtTransform * copy() const =0
Virtualized copy operation.
A class representing a scale division.
Definition: qwt_scale_div.h:36
A 2-D plotting widget.
Definition: qwt_plot.h:74
QwtPlot * plot() const
Return attached plot.
TFSIMD_FORCE_INLINE const tfScalar & y() const
void setAlignment(QwtScaleDraw::Alignment)
PrivateData * d_data
bool isScaleDivFromAxis() const
void setInterval(double lowerBound, double upperBound)
virtual void draw(QPainter *p, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &rect) const
Draw the scale.
Alignment alignment() const
QPalette palette() const
const QwtScaleDiv & scaleDiv() const
A transformation between coordinate systems.
Definition: qwt_transform.h:35
void setBorderDistance(int numPixels)
Align the scale to the canvas.
A class representing a text.
Definition: qwt_text.h:51
virtual QwtScaleMap canvasMap(int axisId) const
Definition: qwt_plot.cpp:790
void setZ(double z)
Set the z value.
void setFont(const QFont &)
int yAxis() const
Return yAxis.
TFSIMD_FORCE_INLINE const tfScalar & x() const
void setScaleDiv(const QwtScaleDiv &)
Assign a scale division.
const QwtTransform * transformation() const
Get the transformation.
A scale map.
Definition: qwt_scale_map.h:30
double invTransform(double p) const
virtual void itemChanged()
Qt::Orientation orientation() const
QwtPlotScaleItem(QwtScaleDraw::Alignment=QwtScaleDraw::BottomScale, const double pos=0.0)
Constructor for scale item at the position pos.
void setMinValue(double)
Definition: qwt_interval.h:177
virtual void draw(QPainter *, const QPalette &) const
Draw the scale.
QWidget * canvas()
Definition: qwt_plot.cpp:467
QwtInterval scaleInterval(const QRectF &, const QwtScaleMap &, const QwtScaleMap &) const
virtual ~QwtPlotScaleItem()
Destructor.
The scale is right.
virtual int rtti() const
QwtInterval interval() const
void setScaleDiv(const QwtScaleDiv &s)
void setTransformation(QwtTransform *)
double position() const
const QwtScaleDiv & scaleDiv() const
A class for drawing scales.
Base class for items on the plot canvas.
Definition: qwt_plot_item.h:64
double transform(double s) const
void setItemInterest(ItemInterest, bool on=true)
void setMaxValue(double)
Definition: qwt_interval.h:187
The scale is below.
void move(double x, double y)


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