qwt_scale_div.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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_scale_div.h"
  10. #include "qwt_math.h"
  11. #include "qwt_interval.h"
  12. #include <qalgorithms.h>
  13. //! Construct an invalid QwtScaleDiv instance.
  14. QwtScaleDiv::QwtScaleDiv():
  15. d_lowerBound( 0.0 ),
  16. d_upperBound( 0.0 ),
  17. d_isValid( false )
  18. {
  19. }
  20. /*!
  21. Construct QwtScaleDiv instance.
  22. \param interval Interval
  23. \param ticks List of major, medium and minor ticks
  24. */
  25. QwtScaleDiv::QwtScaleDiv( const QwtInterval &interval,
  26. QList<double> ticks[NTickTypes] ):
  27. d_lowerBound( interval.minValue() ),
  28. d_upperBound( interval.maxValue() ),
  29. d_isValid( true )
  30. {
  31. for ( int i = 0; i < NTickTypes; i++ )
  32. d_ticks[i] = ticks[i];
  33. }
  34. /*!
  35. Construct QwtScaleDiv instance.
  36. \param lowerBound First interval limit
  37. \param upperBound Second interval limit
  38. \param ticks List of major, medium and minor ticks
  39. */
  40. QwtScaleDiv::QwtScaleDiv(
  41. double lowerBound, double upperBound,
  42. QList<double> ticks[NTickTypes] ):
  43. d_lowerBound( lowerBound ),
  44. d_upperBound( upperBound ),
  45. d_isValid( true )
  46. {
  47. for ( int i = 0; i < NTickTypes; i++ )
  48. d_ticks[i] = ticks[i];
  49. }
  50. /*!
  51. Change the interval
  52. \param interval Interval
  53. */
  54. void QwtScaleDiv::setInterval( const QwtInterval &interval )
  55. {
  56. setInterval( interval.minValue(), interval.maxValue() );
  57. }
  58. /*!
  59. \brief Equality operator
  60. \return true if this instance is equal to other
  61. */
  62. bool QwtScaleDiv::operator==( const QwtScaleDiv &other ) const
  63. {
  64. if ( d_lowerBound != other.d_lowerBound ||
  65. d_upperBound != other.d_upperBound ||
  66. d_isValid != other.d_isValid )
  67. {
  68. return false;
  69. }
  70. for ( int i = 0; i < NTickTypes; i++ )
  71. {
  72. if ( d_ticks[i] != other.d_ticks[i] )
  73. return false;
  74. }
  75. return true;
  76. }
  77. /*!
  78. \brief Inequality
  79. \return true if this instance is not equal to s
  80. */
  81. bool QwtScaleDiv::operator!=( const QwtScaleDiv &s ) const
  82. {
  83. return ( !( *this == s ) );
  84. }
  85. //! Invalidate the scale division
  86. void QwtScaleDiv::invalidate()
  87. {
  88. d_isValid = false;
  89. // detach arrays
  90. for ( int i = 0; i < NTickTypes; i++ )
  91. d_ticks[i].clear();
  92. d_lowerBound = d_upperBound = 0;
  93. }
  94. //! Check if the scale division is valid
  95. bool QwtScaleDiv::isValid() const
  96. {
  97. return d_isValid;
  98. }
  99. /*!
  100. Return if a value is between lowerBound() and upperBound()
  101. \param value Value
  102. \return true/false
  103. */
  104. bool QwtScaleDiv::contains( double value ) const
  105. {
  106. if ( !d_isValid )
  107. return false;
  108. const double min = qMin( d_lowerBound, d_upperBound );
  109. const double max = qMax( d_lowerBound, d_upperBound );
  110. return value >= min && value <= max;
  111. }
  112. //! Invert the scale divison
  113. void QwtScaleDiv::invert()
  114. {
  115. qSwap( d_lowerBound, d_upperBound );
  116. for ( int i = 0; i < NTickTypes; i++ )
  117. {
  118. QList<double>& ticks = d_ticks[i];
  119. const int size = ticks.count();
  120. const int size2 = size / 2;
  121. for ( int i = 0; i < size2; i++ )
  122. qSwap( ticks[i], ticks[size - 1 - i] );
  123. }
  124. }
  125. /*!
  126. Assign ticks
  127. \param type MinorTick, MediumTick or MajorTick
  128. \param ticks Values of the tick positions
  129. */
  130. void QwtScaleDiv::setTicks( int type, const QList<double> &ticks )
  131. {
  132. if ( type >= 0 || type < NTickTypes )
  133. d_ticks[type] = ticks;
  134. }
  135. /*!
  136. Return a list of ticks
  137. \param type MinorTick, MediumTick or MajorTick
  138. */
  139. const QList<double> &QwtScaleDiv::ticks( int type ) const
  140. {
  141. if ( type >= 0 || type < NTickTypes )
  142. return d_ticks[type];
  143. static QList<double> noTicks;
  144. return noTicks;
  145. }