qwt_scale_map.h 4 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
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#ifndef QWT_SCALE_MAP_H
#define QWT_SCALE_MAP_H

#include "qwt_global.h"
#include "qwt_math.h"

/*!
   \brief Operations for linear or logarithmic (base 10) transformations
*/
class QWT_EXPORT QwtScaleTransformation
{
public:
22
    enum Type {
pixhawk's avatar
pixhawk committed
23 24 25 26 27 28 29 30 31 32
        Linear,
        Log10,

        Other
    };

    QwtScaleTransformation(Type type);
    virtual ~QwtScaleTransformation();

    virtual double xForm(double x, double s1, double s2,
33
                         double p1, double p2) const;
pixhawk's avatar
pixhawk committed
34
    virtual double invXForm(double x, double s1, double s2,
35 36 37 38 39
                            double p1, double p2) const;

    inline Type type() const {
        return d_type;
    }
pixhawk's avatar
pixhawk committed
40 41 42 43 44 45 46 47 48 49 50 51 52

    virtual QwtScaleTransformation *copy() const;

private:
    QwtScaleTransformation();
    QwtScaleTransformation &operator=( const QwtScaleTransformation);

    const Type d_type;
};

/*!
   \brief A scale map

53
   QwtScaleMap offers transformations from a scale
pixhawk's avatar
pixhawk committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
   into a paint interval and vice versa.
*/
class QWT_EXPORT QwtScaleMap
{
public:
    QwtScaleMap();
    QwtScaleMap(const QwtScaleMap&);

    ~QwtScaleMap();

    QwtScaleMap &operator=(const QwtScaleMap &);

    void setTransformation(QwtScaleTransformation * );
    const QwtScaleTransformation *transformation() const;

    void setPaintInterval(int p1, int p2);
    void setPaintXInterval(double p1, double p2);
    void setScaleInterval(double s1, double s2);

    int transform(double x) const;
    double invTransform(double i) const;

    double xTransform(double x) const;

    inline double p1() const;
    inline double p2() const;

    inline double s1() const;
    inline double s2() const;

    inline double pDist() const;
    inline double sDist() const;

    QT_STATIC_CONST double LogMin;
    QT_STATIC_CONST double LogMax;

private:
91
    void newFactor();
pixhawk's avatar
pixhawk committed
92 93 94 95 96 97 98 99 100 101 102 103

    double d_s1, d_s2;     // scale interval boundaries
    double d_p1, d_p2;     // paint device interval boundaries

    double d_cnv;       // conversion factor

    QwtScaleTransformation *d_transformation;
};

/*!
    \return First border of the scale interval
*/
104
inline double QwtScaleMap::s1() const
pixhawk's avatar
pixhawk committed
105 106 107 108 109 110 111
{
    return d_s1;
}

/*!
    \return Second border of the scale interval
*/
112
inline double QwtScaleMap::s2() const
pixhawk's avatar
pixhawk committed
113 114 115 116 117 118 119
{
    return d_s2;
}

/*!
    \return First border of the paint interval
*/
120
inline double QwtScaleMap::p1() const
pixhawk's avatar
pixhawk committed
121 122 123 124 125 126 127
{
    return d_p1;
}

/*!
    \return Second border of the paint interval
*/
128
inline double QwtScaleMap::p2() const
pixhawk's avatar
pixhawk committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
{
    return d_p2;
}

inline double QwtScaleMap::pDist() const
{
    return qwtAbs(d_p2 - d_p1);
}

inline double QwtScaleMap::sDist() const
{
    return qwtAbs(d_s2 - d_s1);
}

/*!
144
  Transform a point related to the scale interval into an point
pixhawk's avatar
pixhawk committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
  related to the interval of the paint device
*/
inline double QwtScaleMap::xTransform(double s) const
{
    // try to inline code from QwtScaleTransformation

    if ( d_transformation->type() == QwtScaleTransformation::Linear )
        return d_p1 + (s - d_s1) * d_cnv;

    if ( d_transformation->type() == QwtScaleTransformation::Log10 )
        return d_p1 + log(s / d_s1) * d_cnv;

    return d_transformation->xForm(s, d_s1, d_s2, d_p1, d_p2 );
}

/*!
  \brief Transform an paint device value into a value in the
         interval of the scale.
*/
inline double QwtScaleMap::invTransform(double p) const
{
    return d_transformation->invXForm(p, d_p1, d_p2, d_s1, d_s2 );
}

/*!
170
  Transform a point related to the scale interval into an point
pixhawk's avatar
pixhawk committed
171 172 173 174 175 176 177 178 179 180 181
  related to the interval of the paint device and round it to
  an integer. (In Qt <= 3.x paint devices are integer based. )

  \sa QwtScaleMap::xTransform
*/
inline int QwtScaleMap::transform(double s) const
{
    return qRound(xTransform(s));
}

#endif