~ubuntu-branches/ubuntu/oneiric/qwt/oneiric-proposed

« back to all changes in this revision

Viewing changes to qwt/src/qwt_double_range.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2011-06-10 11:22:47 UTC
  • mfrom: (1.1.6 upstream) (2.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20110610112247-0i1019vvmzaq6p86
Tags: 6.0.0-1
* New upstream release (Closes: #624107):
  - drop Qt3 support. (Closes: #604379, #626868)
* Register documentation with doc-base. (Closes: #626567)
* Drop patches:
  - 01_makefiles.diff
  - 02_add_missing_warnings.diff
  - 03_qwt_branch_pull_r544.diff
* Add qwt_install_paths.patch to fix the hardcoded installation paths.
* Update debian/control:
  - drop libqt3-mt-dev build dependency.
  - bump Standards-Version to 3.9.2 (no changes).
  - drop Qt3 related packages.
  - due to bump soname (and as we dropper Qt3 support):
    - libqwt5-qt4-dev -> libqwt-dev
    - libqwt5-qt4 -> libqwt6
    - libqwt5-doc -> libqwt-doc
* Update debian/copyright file.
* Update debian/rules: drop Qt3 packages support.

Show diffs side-by-side

added added

removed removed

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