qwt_scale_engine.h 6.16 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
 * 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_ENGINE_H
#define QWT_SCALE_ENGINE_H

#include "qwt_global.h"
#include "qwt_scale_div.h"
Bryant's avatar
Bryant committed
15
#include "qwt_interval.h"
pixhawk's avatar
pixhawk committed
16

Bryant's avatar
Bryant committed
17
class QwtTransform;
pixhawk's avatar
pixhawk committed
18 19 20 21 22 23 24

/*!
  \brief Arithmetic including a tolerance
*/
class QWT_EXPORT QwtScaleArithmetic
{
public:
Bryant's avatar
Bryant committed
25 26
    static double ceilEps( double value, double intervalSize );
    static double floorEps( double value, double intervalSize );
pixhawk's avatar
pixhawk committed
27

Bryant's avatar
Bryant committed
28
    static double divideEps( double interval, double steps );
pixhawk's avatar
pixhawk committed
29

Bryant's avatar
Bryant committed
30 31
    static double divideInterval( double interval, 
        int numSteps, uint base );
pixhawk's avatar
pixhawk committed
32 33 34 35 36
};

/*!
  \brief Base class for scale engines.

Bryant's avatar
Bryant committed
37
  A scale engine tries to find "reasonable" ranges and step sizes
38
  for scales.
pixhawk's avatar
pixhawk committed
39 40 41

  The layout of the scale can be varied with setAttribute().

Bryant's avatar
Bryant committed
42
  Qwt offers implementations for logarithmic and linear scales. 
pixhawk's avatar
pixhawk committed
43 44 45 46 47
*/

class QWT_EXPORT QwtScaleEngine
{
public:
Bryant's avatar
Bryant committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    /*! 
       Layout attributes
       \sa setAttribute(), testAttribute(), reference(),
           lowerMargin(), upperMargin()
     */

    enum Attribute
    {
        //! No attributes
        NoAttribute = 0x00,

        //! Build a scale which includes the reference() value.
        IncludeReference = 0x01,

        //! Build a scale which is symmetric to the reference() value.
        Symmetric = 0x02,

        /*!
           The endpoints of the scale are supposed to be equal the
           outmost included values plus the specified margins 
           (see setMargins()).
           If this attribute is *not* set, the endpoints of the scale will
           be integer multiples of the step size.
         */
        Floating = 0x04,

        //! Turn the scale upside down.
        Inverted = 0x08
pixhawk's avatar
pixhawk committed
76 77
    };

Bryant's avatar
Bryant committed
78 79 80 81
    //! Layout attributes
    typedef QFlags<Attribute> Attributes;

    explicit QwtScaleEngine( uint base = 10 );
pixhawk's avatar
pixhawk committed
82 83
    virtual ~QwtScaleEngine();

Bryant's avatar
Bryant committed
84 85 86 87 88
    void setBase( uint base );
    uint base() const;

    void setAttribute( Attribute, bool on = true );
    bool testAttribute( Attribute ) const;
pixhawk's avatar
pixhawk committed
89

Bryant's avatar
Bryant committed
90 91
    void setAttributes( Attributes );
    Attributes attributes() const;
pixhawk's avatar
pixhawk committed
92

Bryant's avatar
Bryant committed
93
    void setReference( double reference );
pixhawk's avatar
pixhawk committed
94 95
    double reference() const;

Bryant's avatar
Bryant committed
96 97 98
    void setMargins( double lower, double upper );
    double lowerMargin() const;
    double upperMargin() const;
pixhawk's avatar
pixhawk committed
99 100

    /*!
101
      Align and divide an interval
pixhawk's avatar
pixhawk committed
102 103 104 105 106

      \param maxNumSteps Max. number of steps
      \param x1 First limit of the interval (In/Out)
      \param x2 Second limit of the interval (In/Out)
      \param stepSize Step size (Return value)
107
    */
Bryant's avatar
Bryant committed
108 109
    virtual void autoScale( int maxNumSteps,
        double &x1, double &x2, double &stepSize ) const = 0;
pixhawk's avatar
pixhawk committed
110 111 112 113 114 115

    /*!
      \brief Calculate a scale division

      \param x1 First interval limit
      \param x2 Second interval limit
Bryant's avatar
Bryant committed
116 117
      \param maxMajorSteps Maximum for the number of major steps
      \param maxMinorSteps Maximum number of minor steps
pixhawk's avatar
pixhawk committed
118 119
      \param stepSize Step size. If stepSize == 0.0, the scaleEngine
                   calculates one.
Bryant's avatar
Bryant committed
120 121

      \return Calculated scale division
pixhawk's avatar
pixhawk committed
122
    */
Bryant's avatar
Bryant committed
123 124 125
    virtual QwtScaleDiv divideScale( double x1, double x2,
        int maxMajorSteps, int maxMinorSteps,
        double stepSize = 0.0 ) const = 0;
pixhawk's avatar
pixhawk committed
126

Bryant's avatar
Bryant committed
127 128
    void setTransformation( QwtTransform * );
    QwtTransform *transformation() const;
pixhawk's avatar
pixhawk committed
129 130

protected:
Bryant's avatar
Bryant committed
131 132 133 134
    bool contains( const QwtInterval &, double val ) const;
    QList<double> strip( const QList<double>&, const QwtInterval & ) const;

    double divideInterval( double interval, int numSteps ) const;
pixhawk's avatar
pixhawk committed
135

Bryant's avatar
Bryant committed
136
    QwtInterval buildInterval( double v ) const;
pixhawk's avatar
pixhawk committed
137 138 139 140 141 142 143 144 145

private:
    class PrivateData;
    PrivateData *d_data;
};

/*!
  \brief A scale engine for linear scales

146
  The step size will fit into the pattern
pixhawk's avatar
pixhawk committed
147 148 149 150 151 152
  \f$\left\{ 1,2,5\right\} \cdot 10^{n}\f$, where n is an integer.
*/

class QWT_EXPORT QwtLinearScaleEngine: public QwtScaleEngine
{
public:
Bryant's avatar
Bryant committed
153 154
    QwtLinearScaleEngine( uint base = 10 );
    virtual ~QwtLinearScaleEngine();
pixhawk's avatar
pixhawk committed
155

Bryant's avatar
Bryant committed
156 157 158 159 160 161
    virtual void autoScale( int maxSteps,
        double &x1, double &x2, double &stepSize ) const;

    virtual QwtScaleDiv divideScale( double x1, double x2,
        int numMajorSteps, int numMinorSteps,
                                     double stepSize = 0.0 ) const;
pixhawk's avatar
pixhawk committed
162 163 164


protected:
Bryant's avatar
Bryant committed
165
    QwtInterval align( const QwtInterval&, double stepSize ) const;
pixhawk's avatar
pixhawk committed
166 167

    void buildTicks(
Bryant's avatar
Bryant committed
168 169
        const QwtInterval &, double stepSize, int maxMinSteps,
        QList<double> ticks[QwtScaleDiv::NTickTypes] ) const;
pixhawk's avatar
pixhawk committed
170

Bryant's avatar
Bryant committed
171 172
    QList<double> buildMajorTicks(
        const QwtInterval &interval, double stepSize ) const;
pixhawk's avatar
pixhawk committed
173

Bryant's avatar
Bryant committed
174 175 176
    void buildMinorTicks( const QList<double>& majorTicks,
        int maxMinorSteps, double stepSize,
        QList<double> &minorTicks, QList<double> &mediumTicks ) const;
pixhawk's avatar
pixhawk committed
177 178 179
};

/*!
Bryant's avatar
Bryant committed
180
  \brief A scale engine for logarithmic scales
pixhawk's avatar
pixhawk committed
181 182 183 184 185 186 187 188 189

  The step size is measured in *decades*
  and the major step size will be adjusted to fit the pattern
  \f$\left\{ 1,2,3,5\right\} \cdot 10^{n}\f$, where n is a natural number
  including zero.

  \warning the step size as well as the margins are measured in *decades*.
*/

Bryant's avatar
Bryant committed
190
class QWT_EXPORT QwtLogScaleEngine: public QwtScaleEngine
pixhawk's avatar
pixhawk committed
191 192
{
public:
Bryant's avatar
Bryant committed
193 194
    QwtLogScaleEngine( uint base = 10 );
    virtual ~QwtLogScaleEngine();
pixhawk's avatar
pixhawk committed
195

Bryant's avatar
Bryant committed
196 197
    virtual void autoScale( int maxSteps,
        double &x1, double &x2, double &stepSize ) const;
pixhawk's avatar
pixhawk committed
198

Bryant's avatar
Bryant committed
199 200 201
    virtual QwtScaleDiv divideScale( double x1, double x2,
        int numMajorSteps, int numMinorSteps,
        double stepSize = 0.0 ) const;
pixhawk's avatar
pixhawk committed
202 203

protected:
Bryant's avatar
Bryant committed
204
    QwtInterval align( const QwtInterval&, double stepSize ) const;
pixhawk's avatar
pixhawk committed
205 206

    void buildTicks(
Bryant's avatar
Bryant committed
207 208
        const QwtInterval &, double stepSize, int maxMinSteps,
        QList<double> ticks[QwtScaleDiv::NTickTypes] ) const;
pixhawk's avatar
pixhawk committed
209

Bryant's avatar
Bryant committed
210 211
    QList<double> buildMajorTicks(
        const QwtInterval &interval, double stepSize ) const;
pixhawk's avatar
pixhawk committed
212

Bryant's avatar
Bryant committed
213 214 215
    void buildMinorTicks( const QList<double>& majorTicks,
        int maxMinorSteps, double stepSize,
        QList<double> &minorTicks, QList<double> &mediumTicks ) const;
pixhawk's avatar
pixhawk committed
216 217
};

Bryant's avatar
Bryant committed
218 219
Q_DECLARE_OPERATORS_FOR_FLAGS( QwtScaleEngine::Attributes )

pixhawk's avatar
pixhawk committed
220
#endif