Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

# qwt_drange.cpp

    00001 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
    00002  * Qwt Widget Library
    00003  * Copyright (C) 1997   Josef Wilgen
    00004  * Copyright (C) 2002   Uwe Rathmann
    00005  *
    00006  * This library is free software; you can redistribute it and/or
    00007  * modify it under the terms of the Qwt License, Version 1.0
    00008  *****************************************************************************/
    00009
    00010 #include "qwt_drange.h"
    00011 #include "qwt_math.h"
    00012
    00013 static double MinRelStep = 1.0e-10;
    00014 static double DefaultRelStep = 1.0e-2;
    00015 static double MinEps = 1.0e-10;
    00016
    00021 QwtDblRange::QwtDblRange():
    00022     d_minValue(0.0),
    00023     d_maxValue(100.0),
    00024     d_step(1.0),
    00025     d_pageSize(1),
    00026     d_isValid(FALSE),
    00027     d_value(0.0),
    00028     d_exactValue(0.0),
    00029     d_exactPrevValue(0.0),
    00030     d_prevValue(0.0),
    00031     d_periodic(FALSE)
    00032 {
    00033 }
    00034
    00036 QwtDblRange::~QwtDblRange()
    00037 {
    00038 }
    00039
    00041 void QwtDblRange::setValid(bool isValid)
    00042 {
    00043     if ( isValid != d_isValid )
    00044     {
    00045         d_isValid = isValid;
    00046         valueChange();
    00047     }
    00048 }
    00049
    00051 bool QwtDblRange::isValid() const
    00052 {
    00053     return d_isValid;
    00054 }
    00055
    00064 void QwtDblRange::setNewValue(double x, int align)
    00065 {
    00066     double vmin,vmax;
    00067
    00068     d_prevValue = d_value;
    00069
    00070     vmin = qwtMin(d_minValue, d_maxValue);
    00071     vmax = qwtMax(d_minValue, d_maxValue);
    00072
    00073     //
    00074     // Range check
    00075     //
    00076     if (x < vmin)
    00077     {
    00078         if ((d_periodic) && (vmin != vmax))
    00079            d_value = x + ceil( (vmin - x) / (vmax - vmin ) )
    00080               * (vmax - vmin);
    00081         else
    00082            d_value = vmin;
    00083     }
    00084     else if (x > vmax)
    00085     {
    00086         if ((d_periodic) && (vmin != vmax))
    00087            d_value = x - ceil( ( x - vmax) / (vmax - vmin ))
    00088               * (vmax - vmin);
    00089         else
    00090            d_value = vmax;
    00091     }
    00092     else
    00093        d_value = x;
    00094
    00095     d_exactPrevValue = d_exactValue;
    00096     d_exactValue = d_value;
    00097
    00098     // align to grid
    00099     if (align)
    00100     {
    00101         if (d_step != 0.0)
    00102            d_value = d_minValue +
    00103              floor ((d_value - d_minValue) / d_step + 0.5) * d_step;
    00104         else
    00105            d_value = d_minValue;
    00106
    00107         // correct rounding error at the border
    00108         if (fabs(d_value - d_maxValue) < MinEps * qwtAbs(d_step))
    00109            d_value = d_maxValue;
    00110
    00111         // correct rounding error if value = 0
    00112         if (fabs(d_value) < MinEps * qwtAbs(d_step))
    00113            d_value = 0.0;
    00114     }
    00115
    00116     if (!d_isValid || d_prevValue != d_value)
    00117     {
    00118         d_isValid = TRUE;
    00119         valueChange();
    00120     }
    00121 }
    00122
    00132 void QwtDblRange::fitValue(double x)
    00133 {
    00134     setNewValue(x,1);
    00135 }
    00136
    00137
    00147 void QwtDblRange::setValue(double x)
    00148 {
    00149     setNewValue(x,0);
    00150 }
    00151
    00169 void QwtDblRange::setRange(double vmin, double vmax, double vstep, int pageSize)
    00170 {
    00171     int rchg = ((d_maxValue != vmax) || (d_minValue != vmin));
    00172
    00173     if (rchg)
    00174     {
    00175         d_minValue = vmin;
    00176         d_maxValue = vmax;
    00177     }
    00178
    00179     //
    00180     // look if the step width has an acceptable
    00181     // value or otherwise change it.
    00182     //
    00183     setStep(vstep);
    00184
    00185     //
    00186     // limit page size
    00187     //
    00188     d_pageSize = qwtLim(pageSize,0,
    00189         int(qwtAbs((d_maxValue - d_minValue) / d_step)));
    00190
    00191     //
    00192     // If the value lies out of the range, it
    00193     // will be changed. Note that it will not be adjusted to
    00194     // the new step width.
    00195     setNewValue(d_value,0);
    00196
    00197     // call notifier after the step width has been
    00198     // adjusted.
    00199     if (rchg)
    00200        rangeChange();
    00201 }
    00202
    00208 void QwtDblRange::setStep(double vstep)
    00209 {
    00210     double intv = d_maxValue - d_minValue;
    00211
    00212     double newStep;
    00213     if (vstep == 0.0)
    00214        newStep = intv * DefaultRelStep;
    00215     else
    00216     {
    00217         if ((intv > 0) && (vstep < 0) || (intv < 0) && (vstep > 0))
    00218            newStep = -vstep;
    00219         else
    00220            newStep = vstep;
    00221
    00222         if ( fabs(newStep) < fabs(MinRelStep * intv) )
    00223            newStep = MinRelStep * intv;
    00224     }
    00225
    00226     if (newStep != d_step)
    00227     {
    00228         d_step = newStep;
    00229         stepChange();
    00230     }
    00231 }
    00232
    00233
    00248 void QwtDblRange::setPeriodic(bool tf)
    00249 {
    00250     d_periodic = tf;
    00251 }
    00252
    00259 void QwtDblRange::incValue(int nSteps)
    00260 {
    00261     if ( isValid() )
    00262         setNewValue(d_value + double(nSteps) * d_step,1);
    00263 }
    00264
    00271 void QwtDblRange::incPages(int nPages)
    00272 {
    00273     if ( isValid() )
    00274         setNewValue(d_value + double(nPages) * double(d_pageSize) * d_step,1);
    00275 }
    00276
    00283 void QwtDblRange::valueChange()
    00284 {
    00285 }
    00286
    00287
    00294 void QwtDblRange::rangeChange()
    00295 {
    00296 }
    00297
    00298
    00305 void QwtDblRange::stepChange()
    00306 {
    00307 }
    00308
    00313 double QwtDblRange::step() const
    00314 {
    00315     return qwtAbs(d_step);
    00316 }
    00317
    00326 double QwtDblRange::maxValue() const
    00327 {
    00328     return d_maxValue;
    00329 }
    00330
    00339 double QwtDblRange::minValue() const
    00340 {
    00341     return d_minValue;
    00342 }
    00343
    00348 bool QwtDblRange::periodic() const
    00349 {
    00350     return d_periodic;
    00351 }
    00352
    00354 int QwtDblRange::pageSize() const
    00355 {
    00356     return d_pageSize;
    00357 }
    00358
    00360 double QwtDblRange::value() const
    00361 {
    00362     return d_value;
    00363 }
    00364
    00374 double QwtDblRange::exactValue() const
    00375 {
    00376     return d_exactValue;
    00377 }
    00378
    00380 double QwtDblRange::exactPrevValue() const
    00381 {
    00382     return d_exactPrevValue;
    00383 }
    00384
    00386 double QwtDblRange::prevValue() const
    00387 {
    00388     return d_prevValue;
    00389 }