qwt_plot_axis.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.h"
11 #include "qwt_math.h"
12 #include "qwt_scale_widget.h"
13 #include "qwt_scale_div.h"
14 #include "qwt_scale_engine.h"
15 
17 {
18 public:
19  bool isEnabled;
21 
22  double minValue;
23  double maxValue;
24  double stepSize;
25 
26  int maxMajor;
27  int maxMinor;
28 
29  bool isValid;
30 
34 };
35 
38 {
39  int axisId;
40 
41  for ( axisId = 0; axisId < axisCnt; axisId++ )
42  d_axisData[axisId] = new AxisData;
43 
52 
53  d_axisData[yLeft]->scaleWidget->setObjectName( "QwtPlotAxisYLeft" );
54  d_axisData[yRight]->scaleWidget->setObjectName( "QwtPlotAxisYRight" );
55  d_axisData[xTop]->scaleWidget->setObjectName( "QwtPlotAxisXTop" );
56  d_axisData[xBottom]->scaleWidget->setObjectName( "QwtPlotAxisXBottom" );
57 
58 #if 1
59  // better find the font sizes from the application font
60  QFont fscl( fontInfo().family(), 10 );
61  QFont fttl( fontInfo().family(), 12, QFont::Bold );
62 #endif
63 
64  for ( axisId = 0; axisId < axisCnt; axisId++ )
65  {
66  AxisData &d = *d_axisData[axisId];
67 
69 
72 
73  d.scaleWidget->setFont( fscl );
74  d.scaleWidget->setMargin( 2 );
75 
76  QwtText text = d.scaleWidget->title();
77  text.setFont( fttl );
78  d.scaleWidget->setTitle( text );
79 
80  d.doAutoScale = true;
81 
82  d.minValue = 0.0;
83  d.maxValue = 1000.0;
84  d.stepSize = 0.0;
85 
86  d.maxMinor = 5;
87  d.maxMajor = 8;
88 
89 
90  d.isValid = false;
91  }
92 
93  d_axisData[yLeft]->isEnabled = true;
94  d_axisData[yRight]->isEnabled = false;
95  d_axisData[xBottom]->isEnabled = true;
96  d_axisData[xTop]->isEnabled = false;
97 }
98 
100 {
101  for ( int axisId = 0; axisId < axisCnt; axisId++ )
102  {
103  delete d_axisData[axisId]->scaleEngine;
104  delete d_axisData[axisId];
105  d_axisData[axisId] = NULL;
106  }
107 }
108 
113 const QwtScaleWidget *QwtPlot::axisWidget( int axisId ) const
114 {
115  if ( axisValid( axisId ) )
116  return d_axisData[axisId]->scaleWidget;
117 
118  return NULL;
119 }
120 
126 {
127  if ( axisValid( axisId ) )
128  return d_axisData[axisId]->scaleWidget;
129 
130  return NULL;
131 }
132 
142 {
143  if ( axisValid( axisId ) && scaleEngine != NULL )
144  {
145  AxisData &d = *d_axisData[axisId];
146 
147  delete d.scaleEngine;
149 
151  scaleEngine->transformation() );
152 
153  d.isValid = false;
154 
155  autoRefresh();
156  }
157 }
158 
164 {
165  if ( axisValid( axisId ) )
166  return d_axisData[axisId]->scaleEngine;
167  else
168  return NULL;
169 }
170 
175 const QwtScaleEngine *QwtPlot::axisScaleEngine( int axisId ) const
176 {
177  if ( axisValid( axisId ) )
178  return d_axisData[axisId]->scaleEngine;
179  else
180  return NULL;
181 }
186 bool QwtPlot::axisAutoScale( int axisId ) const
187 {
188  if ( axisValid( axisId ) )
189  return d_axisData[axisId]->doAutoScale;
190  else
191  return false;
192 }
193 
198 bool QwtPlot::axisEnabled( int axisId ) const
199 {
200  if ( axisValid( axisId ) )
201  return d_axisData[axisId]->isEnabled;
202  else
203  return false;
204 }
205 
210 QFont QwtPlot::axisFont( int axisId ) const
211 {
212  if ( axisValid( axisId ) )
213  return axisWidget( axisId )->font();
214  else
215  return QFont();
216 
217 }
218 
224 int QwtPlot::axisMaxMajor( int axisId ) const
225 {
226  if ( axisValid( axisId ) )
227  return d_axisData[axisId]->maxMajor;
228  else
229  return 0;
230 }
231 
237 int QwtPlot::axisMaxMinor( int axisId ) const
238 {
239  if ( axisValid( axisId ) )
240  return d_axisData[axisId]->maxMinor;
241  else
242  return 0;
243 }
244 
256 const QwtScaleDiv &QwtPlot::axisScaleDiv( int axisId ) const
257 {
258  return d_axisData[axisId]->scaleDiv;
259 }
260 
267 const QwtScaleDraw *QwtPlot::axisScaleDraw( int axisId ) const
268 {
269  if ( !axisValid( axisId ) )
270  return NULL;
271 
272  return axisWidget( axisId )->scaleDraw();
273 }
274 
282 {
283  if ( !axisValid( axisId ) )
284  return NULL;
285 
286  return axisWidget( axisId )->scaleDraw();
287 }
288 
299 double QwtPlot::axisStepSize( int axisId ) const
300 {
301  if ( !axisValid( axisId ) )
302  return 0;
303 
304  return d_axisData[axisId]->stepSize;
305 }
306 
318 {
319  if ( !axisValid( axisId ) )
320  return QwtInterval();
321 
322  return d_axisData[axisId]->scaleDiv.interval();
323 }
324 
329 QwtText QwtPlot::axisTitle( int axisId ) const
330 {
331  if ( axisValid( axisId ) )
332  return axisWidget( axisId )->title();
333  else
334  return QwtText();
335 }
336 
350 void QwtPlot::enableAxis( int axisId, bool tf )
351 {
352  if ( axisValid( axisId ) && tf != d_axisData[axisId]->isEnabled )
353  {
354  d_axisData[axisId]->isEnabled = tf;
355  updateLayout();
356  }
357 }
358 
371 double QwtPlot::invTransform( int axisId, int pos ) const
372 {
373  if ( axisValid( axisId ) )
374  return( canvasMap( axisId ).invTransform( pos ) );
375  else
376  return 0.0;
377 }
378 
379 
388 double QwtPlot::transform( int axisId, double value ) const
389 {
390  if ( axisValid( axisId ) )
391  return( canvasMap( axisId ).transform( value ) );
392  else
393  return 0.0;
394 }
395 
404 void QwtPlot::setAxisFont( int axisId, const QFont &font )
405 {
406  if ( axisValid( axisId ) )
407  axisWidget( axisId )->setFont( font );
408 }
409 
423 void QwtPlot::setAxisAutoScale( int axisId, bool on )
424 {
425  if ( axisValid( axisId ) && ( d_axisData[axisId]->doAutoScale != on ) )
426  {
427  d_axisData[axisId]->doAutoScale = on;
428  autoRefresh();
429  }
430 }
431 
447 void QwtPlot::setAxisScale( int axisId, double min, double max, double stepSize )
448 {
449  if ( axisValid( axisId ) )
450  {
451  AxisData &d = *d_axisData[axisId];
452 
453  d.doAutoScale = false;
454  d.isValid = false;
455 
456  d.minValue = min;
457  d.maxValue = max;
458  d.stepSize = stepSize;
459 
460  autoRefresh();
461  }
462 }
463 
477 {
478  if ( axisValid( axisId ) )
479  {
480  AxisData &d = *d_axisData[axisId];
481 
482  d.doAutoScale = false;
483  d.scaleDiv = scaleDiv;
484  d.isValid = true;
485 
486  autoRefresh();
487  }
488 }
489 
506 void QwtPlot::setAxisScaleDraw( int axisId, QwtScaleDraw *scaleDraw )
507 {
508  if ( axisValid( axisId ) )
509  {
510  axisWidget( axisId )->setScaleDraw( scaleDraw );
511  autoRefresh();
512  }
513 }
514 
523 void QwtPlot::setAxisLabelAlignment( int axisId, Qt::Alignment alignment )
524 {
525  if ( axisValid( axisId ) )
526  axisWidget( axisId )->setLabelAlignment( alignment );
527 }
528 
538 void QwtPlot::setAxisLabelRotation( int axisId, double rotation )
539 {
540  if ( axisValid( axisId ) )
541  axisWidget( axisId )->setLabelRotation( rotation );
542 }
543 
552 void QwtPlot::setAxisMaxMinor( int axisId, int maxMinor )
553 {
554  if ( axisValid( axisId ) )
555  {
556  maxMinor = qBound( 0, maxMinor, 100 );
557 
558  AxisData &d = *d_axisData[axisId];
559  if ( maxMinor != d.maxMinor )
560  {
561  d.maxMinor = maxMinor;
562  d.isValid = false;
563  autoRefresh();
564  }
565  }
566 }
567 
576 void QwtPlot::setAxisMaxMajor( int axisId, int maxMajor )
577 {
578  if ( axisValid( axisId ) )
579  {
580  maxMajor = qBound( 1, maxMajor, 10000 );
581 
582  AxisData &d = *d_axisData[axisId];
583  if ( maxMajor != d.maxMajor )
584  {
585  d.maxMajor = maxMajor;
586  d.isValid = false;
587  autoRefresh();
588  }
589  }
590 }
591 
598 void QwtPlot::setAxisTitle( int axisId, const QString &title )
599 {
600  if ( axisValid( axisId ) )
601  axisWidget( axisId )->setTitle( title );
602 }
603 
610 void QwtPlot::setAxisTitle( int axisId, const QwtText &title )
611 {
612  if ( axisValid( axisId ) )
613  axisWidget( axisId )->setTitle( title );
614 }
615 
641 {
642  // Find bounding interval of the item data
643  // for all axes, where autoscaling is enabled
644 
645  QwtInterval intv[axisCnt];
646 
647  const QwtPlotItemList& itmList = itemList();
648 
650  for ( it = itmList.begin(); it != itmList.end(); ++it )
651  {
652  const QwtPlotItem *item = *it;
653 
655  continue;
656 
657  if ( !item->isVisible() )
658  continue;
659 
660  if ( axisAutoScale( item->xAxis() ) || axisAutoScale( item->yAxis() ) )
661  {
662  const QRectF rect = item->boundingRect();
663 
664  if ( rect.width() >= 0.0 )
665  intv[item->xAxis()] |= QwtInterval( rect.left(), rect.right() );
666 
667  if ( rect.height() >= 0.0 )
668  intv[item->yAxis()] |= QwtInterval( rect.top(), rect.bottom() );
669  }
670  }
671 
672  // Adjust scales
673 
674  for ( int axisId = 0; axisId < axisCnt; axisId++ )
675  {
676  AxisData &d = *d_axisData[axisId];
677 
678  double minValue = d.minValue;
679  double maxValue = d.maxValue;
680  double stepSize = d.stepSize;
681 
682  if ( d.doAutoScale && intv[axisId].isValid() )
683  {
684  d.isValid = false;
685 
686  minValue = intv[axisId].minValue();
687  maxValue = intv[axisId].maxValue();
688 
690  minValue, maxValue, stepSize );
691  }
692  if ( !d.isValid )
693  {
695  minValue, maxValue,
696  d.maxMajor, d.maxMinor, stepSize );
697  d.isValid = true;
698  }
699 
701  scaleWidget->setScaleDiv( d.scaleDiv );
702 
703  int startDist, endDist;
704  scaleWidget->getBorderDistHint( startDist, endDist );
705  scaleWidget->setBorderDist( startDist, endDist );
706  }
707 
708  for ( it = itmList.begin(); it != itmList.end(); ++it )
709  {
710  QwtPlotItem *item = *it;
712  {
713  item->updateScaleDiv( axisScaleDiv( item->xAxis() ),
714  axisScaleDiv( item->yAxis() ) );
715  }
716  }
717 }
718 
d
const QwtScaleDiv & axisScaleDiv(int axisId) const
Return the scale division of a specified axis.
QFont axisFont(int axisId) const
X axis above the canvas.
Definition: qwt_plot.h:105
void setFont(const QFont &)
Definition: qwt_text.cpp:296
void setAxisLabelRotation(int axisId, double rotation)
bool isVisible() const
void setAxisFont(int axisId, const QFont &f)
Change the font of an axis.
A class representing an interval.
Definition: qwt_interval.h:26
Number of axes.
Definition: qwt_plot.h:108
int xAxis() const
Return xAxis.
double minValue() const
Definition: qwt_interval.h:193
The scale is above.
void setAxisMaxMajor(int axisId, int maxMajor)
QList< QwtPlotItem * > QwtPlotItemList
See QT 4.x assistant documentation for QList.
Definition: qwt_plot_dict.h:20
void setAxisScaleDraw(int axisId, QwtScaleDraw *)
Set a scale draw.
const QwtPlotItemList & itemList() const
A QwtPlotItemList of all attached plot items.
void deleteAxesData()
void autoRefresh()
Replots the plot if autoReplot() is true.
Definition: qwt_plot.cpp:293
Y axis right of the canvas.
Definition: qwt_plot.h:99
void setLabelAlignment(Qt::Alignment)
Change the alignment for the labels.
A class representing a scale division.
Definition: qwt_scale_div.h:36
double invTransform(int axisId, int pos) const
bool axisEnabled(int axisId) const
QwtTransform * transformation() const
void setMargin(int)
Specify the margin to the colorBar/base line.
const QwtScaleDraw * scaleDraw() const
void setLabelRotation(double rotation)
Change the rotation for the labels. See QwtScaleDraw::setLabelRotation().
double maxValue() const
Definition: qwt_interval.h:199
Y axis left of the canvas.
Definition: qwt_plot.h:96
void setAxisScaleDiv(int axisId, const QwtScaleDiv &)
Disable autoscaling and specify a fixed scale for a selected axis.
QList< QwtPlotItem * >::ConstIterator QwtPlotItemIterator
Definition: qwt_plot_dict.h:21
static bool axisValid(int axisId)
Definition: qwt_plot.cpp:886
const QwtScaleWidget * axisWidget(int axisId) const
void setAxisLabelAlignment(int axisId, Qt::Alignment)
bool testItemAttribute(ItemAttribute) const
void setAxisTitle(int axisId, const QString &)
Change the title of a specified axis.
void setBorderDist(int start, int end)
The scale is left.
virtual void updateLayout()
Adjust plot content to its current size.
Definition: qwt_plot.cpp:578
void setAxisAutoScale(int axisId, bool on=true)
Enable autoscaling for a specified axis.
void setTitle(const QString &title)
void enableAxis(int axisId, bool tf=true)
Enable or disable a specified axis.
A Widget which contains a scale.
void setScaleDiv(const QwtScaleDiv &sd)
Assign a scale division.
int axisMaxMinor(int axisId) const
void initAxesData()
Initialize axes.
bool isValid() const
Definition: qwt_interval.h:211
A class representing a text.
Definition: qwt_text.h:51
virtual QwtScaleMap canvasMap(int axisId) const
Definition: qwt_plot.cpp:790
double axisStepSize(int axisId) const
Return the step size parameter that has been set in setAxisScale.
bool axisAutoScale(int axisId) const
int yAxis() const
Return yAxis.
QwtScaleDiv scaleDiv
void setAxisScaleEngine(int axisId, QwtScaleEngine *)
QwtScaleEngine * scaleEngine
bool testItemInterest(ItemInterest) const
QwtText title() const
Definition: qwt_plot.cpp:355
const QwtScaleDraw * axisScaleDraw(int axisId) const
Return the scale draw of a specified axis.
AxisData * d_axisData[axisCnt]
Definition: qwt_plot.h:305
double transform(int axisId, double value) const
Transform a value into a coordinate in the plotting region.
The scale is right.
virtual void autoScale(int maxNumSteps, double &x1, double &x2, double &stepSize) const =0
int axisMaxMajor(int axisId) const
void getBorderDistHint(int &start, int &end) const
Calculate a hint for the border distances.
QwtInterval interval() const
QwtScaleEngine * axisScaleEngine(int axisId)
virtual void updateScaleDiv(const QwtScaleDiv &, const QwtScaleDiv &)
Update the item to changes of the axes scale division.
QwtText axisTitle(int axisId) const
A class for drawing scales.
A scale engine for linear scales.
Base class for items on the plot canvas.
Definition: qwt_plot_item.h:64
void setTransformation(QwtTransform *)
virtual QwtScaleDiv divideScale(double x1, double x2, int maxMajorSteps, int maxMinorSteps, double stepSize=0.0) const =0
Calculate a scale division.
QwtText title() const
void setScaleDraw(QwtScaleDraw *)
QwtScaleWidget * scaleWidget
void setAxisScale(int axisId, double min, double max, double step=0)
Disable autoscaling and specify a fixed scale for a selected axis.
QwtInterval axisInterval(int axisId) const
Return the current interval of the specified axis.
The scale is below.
void updateAxes()
Rebuild the axes scales.
void setAxisMaxMinor(int axisId, int maxMinor)
X axis below the canvas.
Definition: qwt_plot.h:102
Base class for scale engines.
virtual QRectF boundingRect() const


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