qwt_double_range.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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_double_range.h"
  10. #include "qwt_math.h"
  11. #if QT_VERSION < 0x040601
  12. #define qFabs(x) ::fabs(x)
  13. #endif
  14. static double MinRelStep = 1.0e-10;
  15. static double DefaultRelStep = 1.0e-2;
  16. static double MinEps = 1.0e-10;
  17. /*!
  18. The range is initialized to [0.0, 100.0], the
  19. step size to 1.0, and the value to 0.0.
  20. */
  21. QwtDoubleRange::QwtDoubleRange():
  22. d_minValue( 0.0 ),
  23. d_maxValue( 0.0 ),
  24. d_step( 1.0 ),
  25. d_pageSize( 1 ),
  26. d_isValid( false ),
  27. d_value( 0.0 ),
  28. d_exactValue( 0.0 ),
  29. d_exactPrevValue( 0.0 ),
  30. d_prevValue( 0.0 ),
  31. d_periodic( false )
  32. {
  33. }
  34. //! Destroys the QwtDoubleRange
  35. QwtDoubleRange::~QwtDoubleRange()
  36. {
  37. }
  38. //! Set the value to be valid/invalid
  39. void QwtDoubleRange::setValid( bool isValid )
  40. {
  41. if ( isValid != d_isValid )
  42. {
  43. d_isValid = isValid;
  44. valueChange();
  45. }
  46. }
  47. //! Indicates if the value is valid
  48. bool QwtDoubleRange::isValid() const
  49. {
  50. return d_isValid;
  51. }
  52. /*!
  53. \brief No docs
  54. Description
  55. \param x ???
  56. \param align
  57. \todo Documentation
  58. */
  59. void QwtDoubleRange::setNewValue( double x, bool align )
  60. {
  61. double vmin, vmax;
  62. d_prevValue = d_value;
  63. vmin = qMin( d_minValue, d_maxValue );
  64. vmax = qMax( d_minValue, d_maxValue );
  65. //
  66. // Range check
  67. //
  68. if ( x < vmin )
  69. {
  70. if ( ( d_periodic ) && ( vmin != vmax ) )
  71. d_value = x + qCeil( ( vmin - x ) / ( vmax - vmin ) )
  72. * ( vmax - vmin );
  73. else
  74. d_value = vmin;
  75. }
  76. else if ( x > vmax )
  77. {
  78. if ( ( d_periodic ) && ( vmin != vmax ) )
  79. d_value = x - qCeil( ( x - vmax ) / ( vmax - vmin ) )
  80. * ( vmax - vmin );
  81. else
  82. d_value = vmax;
  83. }
  84. else
  85. d_value = x;
  86. d_exactPrevValue = d_exactValue;
  87. d_exactValue = d_value;
  88. // align to grid
  89. if ( align )
  90. {
  91. if ( d_step != 0.0 )
  92. {
  93. d_value = d_minValue +
  94. qRound( ( d_value - d_minValue ) / d_step ) * d_step;
  95. }
  96. else
  97. d_value = d_minValue;
  98. // correct rounding error at the border
  99. if ( qFabs( d_value - d_maxValue ) < MinEps * qAbs( d_step ) )
  100. d_value = d_maxValue;
  101. // correct rounding error if value = 0
  102. if ( qFabs( d_value ) < MinEps * qAbs( d_step ) )
  103. d_value = 0.0;
  104. }
  105. if ( !d_isValid || d_prevValue != d_value )
  106. {
  107. d_isValid = true;
  108. valueChange();
  109. }
  110. }
  111. /*!
  112. \brief Adjust the value to the closest point in the step raster.
  113. \param x value
  114. \warning The value is clipped when it lies outside the range.
  115. When the range is QwtDoubleRange::periodic, it will
  116. be mapped to a point in the interval such that
  117. \verbatim new value := x + n * (max. value - min. value)\endverbatim
  118. with an integer number n.
  119. */
  120. void QwtDoubleRange::fitValue( double x )
  121. {
  122. setNewValue( x, true );
  123. }
  124. /*!
  125. \brief Set a new value without adjusting to the step raster
  126. \param x new value
  127. \warning The value is clipped when it lies outside the range.
  128. When the range is QwtDoubleRange::periodic, it will
  129. be mapped to a point in the interval such that
  130. \verbatim new value := x + n * (max. value - min. value)\endverbatim
  131. with an integer number n.
  132. */
  133. void QwtDoubleRange::setValue( double x )
  134. {
  135. setNewValue( x, false );
  136. }
  137. /*!
  138. \brief Specify range and step size
  139. \param vmin lower boundary of the interval
  140. \param vmax higher boundary of the interval
  141. \param vstep step width
  142. \param pageSize page size in steps
  143. \warning
  144. \li A change of the range changes the value if it lies outside the
  145. new range. The current value
  146. will *not* be adjusted to the new step raster.
  147. \li vmax < vmin is allowed.
  148. \li If the step size is left out or set to zero, it will be
  149. set to 1/100 of the interval length.
  150. \li If the step size has an absurd value, it will be corrected
  151. to a better one.
  152. */
  153. void QwtDoubleRange::setRange( double vmin, double vmax, double vstep, int pageSize )
  154. {
  155. bool rchg = ( ( d_maxValue != vmax ) || ( d_minValue != vmin ) );
  156. if ( rchg )
  157. {
  158. d_minValue = vmin;
  159. d_maxValue = vmax;
  160. }
  161. //
  162. // look if the step width has an acceptable
  163. // value or otherwise change it.
  164. //
  165. setStep( vstep );
  166. //
  167. // limit page size
  168. //
  169. d_pageSize = qwtLim( pageSize, 0,
  170. int( qAbs( ( d_maxValue - d_minValue ) / d_step ) ) );
  171. // If the value lies out of the range, it
  172. // will be changed. Note that it will not be adjusted to
  173. // the new step width.
  174. setNewValue( d_value, false );
  175. // call notifier after the step width has been
  176. // adjusted.
  177. if ( rchg )
  178. rangeChange();
  179. }
  180. /*!
  181. \brief Change the step raster
  182. \param vstep new step width
  183. \warning The value will \e not be adjusted to the new step raster.
  184. */
  185. void QwtDoubleRange::setStep( double vstep )
  186. {
  187. double intv = d_maxValue - d_minValue;
  188. double newStep;
  189. if ( vstep == 0.0 )
  190. newStep = intv * DefaultRelStep;
  191. else
  192. {
  193. if ( ( intv > 0.0 && vstep < 0.0 ) || ( intv < 0.0 && vstep > 0.0 ) )
  194. newStep = -vstep;
  195. else
  196. newStep = vstep;
  197. if ( qFabs( newStep ) < qFabs( MinRelStep * intv ) )
  198. newStep = MinRelStep * intv;
  199. }
  200. if ( newStep != d_step )
  201. {
  202. d_step = newStep;
  203. stepChange();
  204. }
  205. }
  206. /*!
  207. \brief Make the range periodic
  208. When the range is periodic, the value will be set to a point
  209. inside the interval such that
  210. \verbatim point = value + n * width \endverbatim
  211. if the user tries to set a new value which is outside the range.
  212. If the range is nonperiodic (the default), values outside the
  213. range will be clipped.
  214. \param tf true for a periodic range
  215. */
  216. void QwtDoubleRange::setPeriodic( bool tf )
  217. {
  218. d_periodic = tf;
  219. }
  220. /*!
  221. \brief Increment the value by a specified number of steps
  222. \param nSteps Number of steps to increment
  223. \warning As a result of this operation, the new value will always be
  224. adjusted to the step raster.
  225. */
  226. void QwtDoubleRange::incValue( int nSteps )
  227. {
  228. if ( isValid() )
  229. setNewValue( d_value + double( nSteps ) * d_step, true );
  230. }
  231. /*!
  232. \brief Increment the value by a specified number of pages
  233. \param nPages Number of pages to increment.
  234. A negative number decrements the value.
  235. \warning The Page size is specified in the constructor.
  236. */
  237. void QwtDoubleRange::incPages( int nPages )
  238. {
  239. if ( isValid() )
  240. setNewValue( d_value + double( nPages ) * double( d_pageSize ) * d_step, true );
  241. }
  242. /*!
  243. \brief Notify a change of value
  244. This virtual function is called whenever the value changes.
  245. The default implementation does nothing.
  246. */
  247. void QwtDoubleRange::valueChange()
  248. {
  249. }
  250. /*!
  251. \brief Notify a change of the range
  252. This virtual function is called whenever the range changes.
  253. The default implementation does nothing.
  254. */
  255. void QwtDoubleRange::rangeChange()
  256. {
  257. }
  258. /*!
  259. \brief Notify a change of the step size
  260. This virtual function is called whenever the step size changes.
  261. The default implementation does nothing.
  262. */
  263. void QwtDoubleRange::stepChange()
  264. {
  265. }
  266. /*!
  267. \return the step size
  268. \sa setStep(), setRange()
  269. */
  270. double QwtDoubleRange::step() const
  271. {
  272. return qAbs( d_step );
  273. }
  274. /*!
  275. \brief Returns the value of the second border of the range
  276. maxValue returns the value which has been specified
  277. as the second parameter in QwtDoubleRange::setRange.
  278. \sa setRange()
  279. */
  280. double QwtDoubleRange::maxValue() const
  281. {
  282. return d_maxValue;
  283. }
  284. /*!
  285. \brief Returns the value at the first border of the range
  286. minValue returns the value which has been specified
  287. as the first parameter in setRange().
  288. \sa setRange()
  289. */
  290. double QwtDoubleRange::minValue() const
  291. {
  292. return d_minValue;
  293. }
  294. /*!
  295. \brief Returns true if the range is periodic
  296. \sa setPeriodic()
  297. */
  298. bool QwtDoubleRange::periodic() const
  299. {
  300. return d_periodic;
  301. }
  302. //! Returns the page size in steps.
  303. int QwtDoubleRange::pageSize() const
  304. {
  305. return d_pageSize;
  306. }
  307. //! Returns the current value.
  308. double QwtDoubleRange::value() const
  309. {
  310. return d_value;
  311. }
  312. /*!
  313. \brief Returns the exact value
  314. The exact value is the value which QwtDoubleRange::value would return
  315. if the value were not adjusted to the step raster. It differs from
  316. the current value only if QwtDoubleRange::fitValue or
  317. QwtDoubleRange::incValue have been used before. This function
  318. is intended for internal use in derived classes.
  319. */
  320. double QwtDoubleRange::exactValue() const
  321. {
  322. return d_exactValue;
  323. }
  324. //! Returns the exact previous value
  325. double QwtDoubleRange::exactPrevValue() const
  326. {
  327. return d_exactPrevValue;
  328. }
  329. //! Returns the previous value
  330. double QwtDoubleRange::prevValue() const
  331. {
  332. return d_prevValue;
  333. }