qwt_double_range.cpp 8.84 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
5
 *
pixhawk's avatar
pixhawk committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#include <cfloat>
#include "qwt_double_range.h"
#include "qwt_math.h"

static double MinRelStep = 1.0e-10;
static double DefaultRelStep = 1.0e-2;
static double MinEps = 1.0e-10;

/*!
  The range is initialized to [0.0, 100.0], the
  step size to 1.0, and the value to 0.0.
*/
QwtDoubleRange::QwtDoubleRange():
    d_minValue(0.0),
    d_maxValue(0.0),
    d_step(1.0),
    d_pageSize(1),
    d_isValid(false),
    d_value(0.0),
    d_exactValue(0.0),
    d_exactPrevValue(0.0),
    d_prevValue(0.0),
    d_periodic(false)
{
}

//! Destroys the QwtDoubleRange
QwtDoubleRange::~QwtDoubleRange()
{
}

//! Set the value to be valid/invalid
void QwtDoubleRange::setValid(bool isValid)
{
44
    if ( isValid != d_isValid ) {
pixhawk's avatar
pixhawk committed
45 46 47 48 49 50 51 52 53 54 55 56 57
        d_isValid = isValid;
        valueChange();
    }
}

//! Indicates if the value is valid
bool QwtDoubleRange::isValid() const
{
    return d_isValid;
}

/*!
  \brief No docs
58

pixhawk's avatar
pixhawk committed
59 60 61 62 63 64 65 66
  Description
  \param x ???
  \param align
  \todo Documentation
*/
void QwtDoubleRange::setNewValue(double x, bool align)
{
    double vmin,vmax;
67

pixhawk's avatar
pixhawk committed
68 69 70 71 72
    d_prevValue = d_value;

    vmin = qwtMin(d_minValue, d_maxValue);
    vmax = qwtMax(d_minValue, d_maxValue);

73
    //
pixhawk's avatar
pixhawk committed
74 75
    // Range check
    //
76
    if (x < vmin) {
pixhawk's avatar
pixhawk committed
77
        if ((d_periodic) && (vmin != vmax))
78 79
            d_value = x + ::ceil( (vmin - x) / (vmax - vmin ) )
                      * (vmax - vmin);
pixhawk's avatar
pixhawk committed
80
        else
81 82
            d_value = vmin;
    } else if (x > vmax) {
pixhawk's avatar
pixhawk committed
83
        if ((d_periodic) && (vmin != vmax))
84 85
            d_value = x - ::ceil( ( x - vmax) / (vmax - vmin ))
                      * (vmax - vmin);
pixhawk's avatar
pixhawk committed
86
        else
87 88 89
            d_value = vmax;
    } else
        d_value = x;
pixhawk's avatar
pixhawk committed
90 91 92

    d_exactPrevValue = d_exactValue;
    d_exactValue = d_value;
93

pixhawk's avatar
pixhawk committed
94
    // align to grid
95 96 97 98 99
    if (align) {
        if (d_step != 0.0) {
            d_value = d_minValue +
                      qwtRound((d_value - d_minValue) / d_step) * d_step;
        } else
pixhawk's avatar
pixhawk committed
100
            d_value = d_minValue;
101

pixhawk's avatar
pixhawk committed
102 103 104 105 106 107 108 109 110
        // correct rounding error at the border
        if (fabs(d_value - d_maxValue) < MinEps * qwtAbs(d_step))
            d_value = d_maxValue;

        // correct rounding error if value = 0
        if (::fabs(d_value) < MinEps * qwtAbs(d_step))
            d_value = 0.0;
    }

111
    if (!d_isValid || d_prevValue != d_value) {
pixhawk's avatar
pixhawk committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
        d_isValid = true;
        valueChange();
    }
}

/*!
  \brief  Adjust the value to the closest point in the step raster.
  \param x value
  \warning The value is clipped when it lies outside the range.
  When the range is QwtDoubleRange::periodic, it will
  be mapped to a point in the interval such that
  \verbatim new value := x + n * (max. value - min. value)\endverbatim
  with an integer number n.
*/
void QwtDoubleRange::fitValue(double x)
{
    setNewValue(x, true);
}


/*!
  \brief Set a new value without adjusting to the step raster
  \param x new value
  \warning The value is clipped when it lies outside the range.
  When the range is QwtDoubleRange::periodic, it will
  be mapped to a point in the interval such that
  \verbatim new value := x + n * (max. value - min. value)\endverbatim
  with an integer number n.
*/
void QwtDoubleRange::setValue(double x)
{
    setNewValue(x, false);
}

/*!
  \brief Specify  range and step size

  \param vmin   lower boundary of the interval
  \param vmax   higher boundary of the interval
  \param vstep  step width
  \param pageSize  page size in steps
  \warning
  \li A change of the range changes the value if it lies outside the
      new range. The current value
      will *not* be adjusted to the new step raster.
  \li vmax < vmin is allowed.
  \li If the step size is left out or set to zero, it will be
      set to 1/100 of the interval length.
  \li If the step size has an absurd value, it will be corrected
      to a better one.
*/
void QwtDoubleRange::setRange(double vmin, double vmax, double vstep, int pageSize)
{
    bool rchg = ((d_maxValue != vmax) || (d_minValue != vmin));
166 167

    if (rchg) {
pixhawk's avatar
pixhawk committed
168 169 170
        d_minValue = vmin;
        d_maxValue = vmax;
    }
171

pixhawk's avatar
pixhawk committed
172
    //
173
    // look if the step width has an acceptable
pixhawk's avatar
pixhawk committed
174 175 176 177 178 179 180
    // value or otherwise change it.
    //
    setStep(vstep);

    //
    // limit page size
    //
181 182 183 184 185 186
    d_pageSize = qwtLim(pageSize,0,
                        int(qwtAbs((d_maxValue - d_minValue) / d_step)));

    //
    // If the value lies out of the range, it
    // will be changed. Note that it will not be adjusted to
pixhawk's avatar
pixhawk committed
187 188
    // the new step width.
    setNewValue(d_value, false);
189 190

    // call notifier after the step width has been
pixhawk's avatar
pixhawk committed
191 192
    // adjusted.
    if (rchg)
193
        rangeChange();
pixhawk's avatar
pixhawk committed
194 195 196
}

/*!
197
  \brief Change the step raster
pixhawk's avatar
pixhawk committed
198 199 200 201 202 203
  \param vstep new step width
  \warning The value will \e not be adjusted to the new step raster.
*/
void QwtDoubleRange::setStep(double vstep)
{
    double intv = d_maxValue - d_minValue;
204

pixhawk's avatar
pixhawk committed
205 206
    double newStep;
    if (vstep == 0.0)
207 208
        newStep = intv * DefaultRelStep;
    else {
pixhawk's avatar
pixhawk committed
209
        if (((intv > 0) && (vstep < 0)) || ((intv < 0) && (vstep > 0)))
210
            newStep = -vstep;
pixhawk's avatar
pixhawk committed
211
        else
212 213
            newStep = vstep;

pixhawk's avatar
pixhawk committed
214
        if ( fabs(newStep) < fabs(MinRelStep * intv) )
215
            newStep = MinRelStep * intv;
pixhawk's avatar
pixhawk committed
216
    }
217 218

    if (newStep != d_step) {
pixhawk's avatar
pixhawk committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
        d_step = newStep;
        stepChange();
    }
}


/*!
  \brief Make the range periodic

  When the range is periodic, the value will be set to a point
  inside the interval such that

  \verbatim point = value + n * width \endverbatim

  if the user tries to set a new value which is outside the range.
  If the range is nonperiodic (the default), values outside the
  range will be clipped.

  \param tf true for a periodic range
*/
void QwtDoubleRange::setPeriodic(bool tf)
{
    d_periodic = tf;
}

/*!
  \brief Increment the value by a specified number of steps
  \param nSteps Number of steps to increment
  \warning As a result of this operation, the new value will always be
       adjusted to the step raster.
*/
void QwtDoubleRange::incValue(int nSteps)
{
    if ( isValid() )
        setNewValue(d_value + double(nSteps) * d_step, true);
}

/*!
  \brief Increment the value by a specified number of pages
  \param nPages Number of pages to increment.
        A negative number decrements the value.
  \warning The Page size is specified in the constructor.
*/
void QwtDoubleRange::incPages(int nPages)
{
    if ( isValid() )
        setNewValue(d_value + double(nPages) * double(d_pageSize) * d_step, true);
}

/*!
  \brief Notify a change of value

  This virtual function is called whenever the value changes.
  The default implementation does nothing.
*/
void QwtDoubleRange::valueChange()
{
}


/*!
  \brief Notify a change of the range

  This virtual function is called whenever the range changes.
  The default implementation does nothing.
*/
void QwtDoubleRange::rangeChange()
{
}


/*!
  \brief Notify a change of the step size

  This virtual function is called whenever the step size changes.
  The default implementation does nothing.
*/
void QwtDoubleRange::stepChange()
{
}

/*!
  \return the step size
  \sa QwtDoubleRange::setStep, QwtDoubleRange::setRange
*/
double QwtDoubleRange::step() const
{
    return qwtAbs(d_step);
}

/*!
  \brief Returns the value of the second border of the range

  maxValue returns the value which has been specified
  as the second parameter in  QwtDoubleRange::setRange.
314

pixhawk's avatar
pixhawk committed
315
  \sa QwtDoubleRange::setRange()
316
*/
pixhawk's avatar
pixhawk committed
317
double QwtDoubleRange::maxValue() const
318
{
pixhawk's avatar
pixhawk committed
319
    return d_maxValue;
320 321
}

pixhawk's avatar
pixhawk committed
322 323
/*!
  \brief Returns the value at the first border of the range
324

pixhawk's avatar
pixhawk committed
325 326
  minValue returns the value which has been specified
  as the first parameter in  setRange().
327

pixhawk's avatar
pixhawk committed
328 329
  \sa QwtDoubleRange::setRange()
*/
330
double QwtDoubleRange::minValue() const
pixhawk's avatar
pixhawk committed
331
{
332 333
    return d_minValue;
}
pixhawk's avatar
pixhawk committed
334 335 336 337 338

/*!
  \brief Returns true if the range is periodic
  \sa QwtDoubleRange::setPeriodic()
*/
339 340 341
bool QwtDoubleRange::periodic() const
{
    return d_periodic;
pixhawk's avatar
pixhawk committed
342 343 344
}

//! Returns the page size in steps.
345 346 347
int QwtDoubleRange::pageSize() const
{
    return d_pageSize;
pixhawk's avatar
pixhawk committed
348 349 350
}

//! Returns the current value.
351 352 353
double QwtDoubleRange::value() const
{
    return d_value;
pixhawk's avatar
pixhawk committed
354 355 356 357 358 359 360 361 362 363 364
}

/*!
  \brief Returns the exact value

  The exact value is the value which QwtDoubleRange::value would return
  if the value were not adjusted to the step raster. It differs from
  the current value only if QwtDoubleRange::fitValue or
  QwtDoubleRange::incValue have been used before. This function
  is intended for internal use in derived classes.
*/
365 366 367
double QwtDoubleRange::exactValue() const
{
    return d_exactValue;
pixhawk's avatar
pixhawk committed
368 369 370
}

//! Returns the exact previous value
371 372 373
double QwtDoubleRange::exactPrevValue() const
{
    return d_exactPrevValue;
pixhawk's avatar
pixhawk committed
374 375 376
}

//! Returns the previous value
377 378 379
double QwtDoubleRange::prevValue() const
{
    return d_prevValue;
pixhawk's avatar
pixhawk committed
380
}