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 #ifndef QWT_SCALE_MAP_H
11 #define QWT_SCALE_MAP_H
12 
13 #include "qwt_global.h"
14 #include "qwt_math.h"
15 
16 /*!
17    \brief Operations for linear or logarithmic (base 10) transformations
18 */
19 class QWT_EXPORT QwtScaleTransformation
20 {
21 public:
22     enum Type
23     {
24         Linear,
25         Log10,
26 
27         Other
28     };
29 
30     QwtScaleTransformation(Type type);
31     virtual ~QwtScaleTransformation();
32 
33     virtual double xForm(double x, double s1, double s2,
34         double p1, double p2) const;
35     virtual double invXForm(double x, double p1, double p2,
36         double s1, double s2) const;
37 
38     Type type() const;
39 
40     virtual QwtScaleTransformation *copy() const;
41 
42 private:
43     QwtScaleTransformation();
44     QwtScaleTransformation &operator=( const QwtScaleTransformation);
45 
46     const Type d_type;
47 };
48 
49 //! \return Transformation type
type()50 inline QwtScaleTransformation::Type QwtScaleTransformation::type() const
51 {
52     return d_type;
53 }
54 
55 /*!
56    \brief A scale map
57 
58    QwtScaleMap offers transformations from a scale
59    into a paint interval and vice versa.
60 */
61 class QWT_EXPORT QwtScaleMap
62 {
63 public:
64     QwtScaleMap();
65     QwtScaleMap(const QwtScaleMap&);
66 
67     ~QwtScaleMap();
68 
69     QwtScaleMap &operator=(const QwtScaleMap &);
70 
71     void setTransformation(QwtScaleTransformation * );
72     const QwtScaleTransformation *transformation() const;
73 
74     void setPaintInterval(int p1, int p2);
75     void setPaintXInterval(double p1, double p2);
76     void setScaleInterval(double s1, double s2);
77 
78     int transform(double x) const;
79     double invTransform(double i) const;
80 
81     double xTransform(double x) const;
82 
83     double p1() const;
84     double p2() const;
85 
86     double s1() const;
87     double s2() const;
88 
89     double pDist() const;
90     double sDist() const;
91 
92     static const double LogMin;
93     static const double LogMax;
94 
95 private:
96     void newFactor();
97 
98     double d_s1, d_s2;     // scale interval boundaries
99     double d_p1, d_p2;     // paint device interval boundaries
100 
101     double d_cnv;       // conversion factor
102 
103     QwtScaleTransformation *d_transformation;
104 };
105 
106 /*!
107     \return First border of the scale interval
108 */
s1()109 inline double QwtScaleMap::s1() const
110 {
111     return d_s1;
112 }
113 
114 /*!
115     \return Second border of the scale interval
116 */
s2()117 inline double QwtScaleMap::s2() const
118 {
119     return d_s2;
120 }
121 
122 /*!
123     \return First border of the paint interval
124 */
p1()125 inline double QwtScaleMap::p1() const
126 {
127     return d_p1;
128 }
129 
130 /*!
131     \return Second border of the paint interval
132 */
p2()133 inline double QwtScaleMap::p2() const
134 {
135     return d_p2;
136 }
137 
138 /*!
139     \return qwtAbs(p2() - p1())
140 */
pDist()141 inline double QwtScaleMap::pDist() const
142 {
143     return qwtAbs(d_p2 - d_p1);
144 }
145 
146 /*!
147     \return qwtAbs(s2() - s1())
148 */
sDist()149 inline double QwtScaleMap::sDist() const
150 {
151     return qwtAbs(d_s2 - d_s1);
152 }
153 
154 /*!
155   Transform a point related to the scale interval into an point
156   related to the interval of the paint device
157 
158   \param s Value relative to the coordinates of the scale
159 */
xTransform(double s)160 inline double QwtScaleMap::xTransform(double s) const
161 {
162     // try to inline code from QwtScaleTransformation
163 
164     if ( d_transformation->type() == QwtScaleTransformation::Linear )
165         return d_p1 + (s - d_s1) * d_cnv;
166 
167     if ( d_transformation->type() == QwtScaleTransformation::Log10 )
168         return d_p1 + log(s / d_s1) * d_cnv;
169 
170     return d_transformation->xForm(s, d_s1, d_s2, d_p1, d_p2 );
171 }
172 
173 /*!
174   Transform an paint device value into a value in the
175   interval of the scale.
176 
177   \param p Value relative to the coordinates of the paint device
178   \sa transform()
179 */
invTransform(double p)180 inline double QwtScaleMap::invTransform(double p) const
181 {
182     return d_transformation->invXForm(p, d_p1, d_p2, d_s1, d_s2 );
183 }
184 
185 /*!
186   Transform a point related to the scale interval into an point
187   related to the interval of the paint device and round it to
188   an integer. (In Qt <= 3.x paint devices are integer based. )
189 
190   \param s Value relative to the coordinates of the scale
191   \sa xTransform()
192 */
transform(double s)193 inline int QwtScaleMap::transform(double s) const
194 {
195     return qRound(xTransform(s));
196 }
197 
198 #endif
199