qwt_abstract_scale.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. #include "qwt_abstract_scale.h"
  10. #include "qwt_scale_engine.h"
  11. #include "qwt_scale_draw.h"
  12. #include "qwt_scale_div.h"
  13. #include "qwt_scale_map.h"
  14. #include "qwt_interval.h"
  15. class QwtAbstractScale::PrivateData
  16. {
  17. public:
  18. PrivateData():
  19. maxMajor( 5 ),
  20. maxMinor( 3 ),
  21. stepSize( 0.0 ),
  22. autoScale( true )
  23. {
  24. scaleEngine = new QwtLinearScaleEngine;
  25. scaleDraw = new QwtScaleDraw();
  26. }
  27. ~PrivateData()
  28. {
  29. delete scaleEngine;
  30. delete scaleDraw;
  31. }
  32. QwtScaleEngine *scaleEngine;
  33. QwtAbstractScaleDraw *scaleDraw;
  34. int maxMajor;
  35. int maxMinor;
  36. double stepSize;
  37. bool autoScale;
  38. };
  39. /*!
  40. Constructor
  41. Creates a default QwtScaleDraw and a QwtLinearScaleEngine.
  42. Autoscaling is enabled, and the stepSize is initialized by 0.0.
  43. */
  44. QwtAbstractScale::QwtAbstractScale()
  45. {
  46. d_data = new PrivateData;
  47. rescale( 0.0, 100.0 );
  48. }
  49. //! Destructor
  50. QwtAbstractScale::~QwtAbstractScale()
  51. {
  52. delete d_data;
  53. }
  54. /*!
  55. \brief Specify a scale.
  56. Disable autoscaling and define a scale by an interval and a step size
  57. \param vmin lower limit of the scale interval
  58. \param vmax upper limit of the scale interval
  59. \param stepSize major step size
  60. \sa setAutoScale()
  61. */
  62. void QwtAbstractScale::setScale( double vmin, double vmax, double stepSize )
  63. {
  64. d_data->autoScale = false;
  65. d_data->stepSize = stepSize;
  66. rescale( vmin, vmax, stepSize );
  67. }
  68. /*!
  69. \brief Specify a scale.
  70. Disable autoscaling and define a scale by an interval and a step size
  71. \param interval Interval
  72. \param stepSize major step size
  73. \sa setAutoScale()
  74. */
  75. void QwtAbstractScale::setScale( const QwtInterval &interval, double stepSize )
  76. {
  77. setScale( interval.minValue(), interval.maxValue(), stepSize );
  78. }
  79. /*!
  80. \brief Specify a scale.
  81. Disable autoscaling and define a scale by a scale division
  82. \param scaleDiv Scale division
  83. \sa setAutoScale()
  84. */
  85. void QwtAbstractScale::setScale( const QwtScaleDiv &scaleDiv )
  86. {
  87. d_data->autoScale = false;
  88. if ( scaleDiv != d_data->scaleDraw->scaleDiv() )
  89. {
  90. d_data->scaleDraw->setScaleDiv( scaleDiv );
  91. scaleChange();
  92. }
  93. }
  94. /*!
  95. Recalculate the scale division and update the scale draw.
  96. \param vmin Lower limit of the scale interval
  97. \param vmax Upper limit of the scale interval
  98. \param stepSize Major step size
  99. \sa scaleChange()
  100. */
  101. void QwtAbstractScale::rescale( double vmin, double vmax, double stepSize )
  102. {
  103. const QwtScaleDiv scaleDiv = d_data->scaleEngine->divideScale(
  104. vmin, vmax, d_data->maxMajor, d_data->maxMinor, stepSize );
  105. if ( scaleDiv != d_data->scaleDraw->scaleDiv() )
  106. {
  107. d_data->scaleDraw->setTransformation(
  108. d_data->scaleEngine->transformation() );
  109. d_data->scaleDraw->setScaleDiv( scaleDiv );
  110. scaleChange();
  111. }
  112. }
  113. /*!
  114. \brief Advise the widget to control the scale range internally.
  115. Autoscaling is on by default.
  116. \sa setScale(), autoScale()
  117. */
  118. void QwtAbstractScale::setAutoScale()
  119. {
  120. if ( !d_data->autoScale )
  121. {
  122. d_data->autoScale = true;
  123. scaleChange();
  124. }
  125. }
  126. /*!
  127. \return \c true if autoscaling is enabled
  128. */
  129. bool QwtAbstractScale::autoScale() const
  130. {
  131. return d_data->autoScale;
  132. }
  133. /*!
  134. \brief Set the maximum number of major tick intervals.
  135. The scale's major ticks are calculated automatically such that
  136. the number of major intervals does not exceed ticks.
  137. The default value is 5.
  138. \param ticks maximal number of major ticks.
  139. \sa QwtAbstractScaleDraw
  140. */
  141. void QwtAbstractScale::setScaleMaxMajor( int ticks )
  142. {
  143. if ( ticks != d_data->maxMajor )
  144. {
  145. d_data->maxMajor = ticks;
  146. updateScaleDraw();
  147. }
  148. }
  149. /*!
  150. \brief Set the maximum number of minor tick intervals
  151. The scale's minor ticks are calculated automatically such that
  152. the number of minor intervals does not exceed ticks.
  153. The default value is 3.
  154. \param ticks
  155. \sa QwtAbstractScaleDraw
  156. */
  157. void QwtAbstractScale::setScaleMaxMinor( int ticks )
  158. {
  159. if ( ticks != d_data->maxMinor )
  160. {
  161. d_data->maxMinor = ticks;
  162. updateScaleDraw();
  163. }
  164. }
  165. /*!
  166. \return Max. number of minor tick intervals
  167. The default value is 3.
  168. */
  169. int QwtAbstractScale::scaleMaxMinor() const
  170. {
  171. return d_data->maxMinor;
  172. }
  173. /*!
  174. \return Max. number of major tick intervals
  175. The default value is 5.
  176. */
  177. int QwtAbstractScale::scaleMaxMajor() const
  178. {
  179. return d_data->maxMajor;
  180. }
  181. /*!
  182. \brief Set a scale draw
  183. scaleDraw has to be created with new and will be deleted in
  184. ~QwtAbstractScale or the next call of setAbstractScaleDraw.
  185. */
  186. void QwtAbstractScale::setAbstractScaleDraw( QwtAbstractScaleDraw *scaleDraw )
  187. {
  188. if ( scaleDraw == NULL || scaleDraw == d_data->scaleDraw )
  189. return;
  190. if ( d_data->scaleDraw != NULL )
  191. scaleDraw->setScaleDiv( d_data->scaleDraw->scaleDiv() );
  192. delete d_data->scaleDraw;
  193. d_data->scaleDraw = scaleDraw;
  194. }
  195. /*!
  196. \return Scale draw
  197. \sa setAbstractScaleDraw()
  198. */
  199. QwtAbstractScaleDraw *QwtAbstractScale::abstractScaleDraw()
  200. {
  201. return d_data->scaleDraw;
  202. }
  203. /*!
  204. \return Scale draw
  205. \sa setAbstractScaleDraw()
  206. */
  207. const QwtAbstractScaleDraw *QwtAbstractScale::abstractScaleDraw() const
  208. {
  209. return d_data->scaleDraw;
  210. }
  211. void QwtAbstractScale::updateScaleDraw()
  212. {
  213. rescale( d_data->scaleDraw->scaleDiv().lowerBound(),
  214. d_data->scaleDraw->scaleDiv().upperBound(), d_data->stepSize );
  215. }
  216. /*!
  217. \brief Set a scale engine
  218. The scale engine is responsible for calculating the scale division,
  219. and in case of auto scaling how to align the scale.
  220. scaleEngine has to be created with new and will be deleted in
  221. ~QwtAbstractScale or the next call of setScaleEngine.
  222. */
  223. void QwtAbstractScale::setScaleEngine( QwtScaleEngine *scaleEngine )
  224. {
  225. if ( scaleEngine != NULL && scaleEngine != d_data->scaleEngine )
  226. {
  227. delete d_data->scaleEngine;
  228. d_data->scaleEngine = scaleEngine;
  229. }
  230. }
  231. /*!
  232. \return Scale engine
  233. \sa setScaleEngine()
  234. */
  235. const QwtScaleEngine *QwtAbstractScale::scaleEngine() const
  236. {
  237. return d_data->scaleEngine;
  238. }
  239. /*!
  240. \return Scale engine
  241. \sa setScaleEngine()
  242. */
  243. QwtScaleEngine *QwtAbstractScale::scaleEngine()
  244. {
  245. return d_data->scaleEngine;
  246. }
  247. /*!
  248. \brief Notify changed scale
  249. Dummy empty implementation, intended to be overloaded by derived classes
  250. */
  251. void QwtAbstractScale::scaleChange()
  252. {
  253. }
  254. /*!
  255. \return abstractScaleDraw()->scaleMap()
  256. */
  257. const QwtScaleMap &QwtAbstractScale::scaleMap() const
  258. {
  259. return d_data->scaleDraw->scaleMap();
  260. }