qwt_scale_engine.h 5.77 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 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
 * 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"
#include "qwt_double_interval.h"

class QwtScaleTransformation;

/*!
  \brief Arithmetic including a tolerance
*/
class QWT_EXPORT QwtScaleArithmetic
{
public:
    static int compareEps(
        double value1, double value2, double intervalSize);

    static double ceilEps(double value, double intervalSize);
    static double floorEps(double value, double intervalSize);

    static double divideEps(double interval, double steps);

    static double ceil125(double x);
    static double floor125(double x);
};

/*!
  \brief Base class for scale engines.

  A scale engine trys to find "reasonable" ranges and step sizes
41
  for scales.
pixhawk's avatar
pixhawk committed
42 43 44

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

45 46
  Qwt offers implementations for logarithmic (log10)
  and linear scales. Contributions for other types of scale engines
pixhawk's avatar
pixhawk committed
47 48 49 50 51 52 53
  (date/time, log2 ... ) are welcome.
*/

class QWT_EXPORT QwtScaleEngine
{
public:
    //! see QwtScaleEngine::setAttribute, testAttribute
54
    enum Attribute {
pixhawk's avatar
pixhawk committed
55
        NoAttribute = 0,
56 57
        IncludeReference = 1,
        Symmetric = 2,
pixhawk's avatar
pixhawk committed
58
        Floating = 4,
59
        Inverted = 8
pixhawk's avatar
pixhawk committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    };

    explicit QwtScaleEngine();
    virtual ~QwtScaleEngine();

    void setAttribute(Attribute, bool on = true);
    bool testAttribute(Attribute) const;

    void setAttributes(int);
    int attributes() const;

    void setReference(double reference);
    double reference() const;

    void setMargins(double m1, double m2);
    double loMargin() const;
    double hiMargin() const;

    /*!
79
      Align and divide an interval
pixhawk's avatar
pixhawk committed
80 81 82 83 84

      \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)
85 86 87
    */
    virtual void autoScale(int maxNumSteps,
                           double &x1, double &x2, double &stepSize) const = 0;
pixhawk's avatar
pixhawk committed
88 89 90 91 92 93 94 95 96 97 98 99

    /*!
      \brief Calculate a scale division

      \param x1 First interval limit
      \param x2 Second interval limit
      \param maxMajSteps Maximum for the number of major steps
      \param maxMinSteps Maximum number of minor steps
      \param stepSize Step size. If stepSize == 0.0, the scaleEngine
                   calculates one.
    */
    virtual QwtScaleDiv divideScale(double x1, double x2,
100 101
                                    int maxMajSteps, int maxMinSteps,
                                    double stepSize = 0.0) const = 0;
pixhawk's avatar
pixhawk committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

    //! \return a transformation
    virtual QwtScaleTransformation *transformation() const = 0;

protected:
    bool contains(const QwtDoubleInterval &, double val) const;
    QwtValueList strip(const QwtValueList&, const QwtDoubleInterval &) const;
    double divideInterval(double interval, int numSteps) const;

    QwtDoubleInterval buildInterval(double v) const;

private:
    class PrivateData;
    PrivateData *d_data;
};

/*!
  \brief A scale engine for linear scales

121
  The step size will fit into the pattern
pixhawk's avatar
pixhawk committed
122 123 124 125 126 127
  \f$\left\{ 1,2,5\right\} \cdot 10^{n}\f$, where n is an integer.
*/

class QWT_EXPORT QwtLinearScaleEngine: public QwtScaleEngine
{
public:
128 129
    virtual void autoScale(int maxSteps,
                           double &x1, double &x2, double &stepSize) const;
pixhawk's avatar
pixhawk committed
130 131

    virtual QwtScaleDiv divideScale(double x1, double x2,
132 133
                                    int numMajorSteps, int numMinorSteps,
                                    double stepSize = 0.0) const;
pixhawk's avatar
pixhawk committed
134 135 136 137 138

    virtual QwtScaleTransformation *transformation() const;

protected:
    QwtDoubleInterval align(const QwtDoubleInterval&,
139
                            double stepSize) const;
pixhawk's avatar
pixhawk committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

private:
    void buildTicks(
        const QwtDoubleInterval &, double stepSize, int maxMinSteps,
        QwtValueList ticks[QwtScaleDiv::NTickTypes]) const;

    void buildMinorTicks(
        const QwtValueList& majorTicks,
        int maxMinMark, double step,
        QwtValueList &, QwtValueList &) const;

    QwtValueList buildMajorTicks(
        const QwtDoubleInterval &interval, double stepSize) const;
};

/*!
  \brief A scale engine for logarithmic (base 10) scales

  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*.
*/

class QWT_EXPORT QwtLog10ScaleEngine: public QwtScaleEngine
{
public:
169 170
    virtual void autoScale(int maxSteps,
                           double &x1, double &x2, double &stepSize) const;
pixhawk's avatar
pixhawk committed
171 172

    virtual QwtScaleDiv divideScale(double x1, double x2,
173 174
                                    int numMajorSteps, int numMinorSteps,
                                    double stepSize = 0.0) const;
pixhawk's avatar
pixhawk committed
175 176 177 178 179 180 181 182 183

    virtual QwtScaleTransformation *transformation() const;

protected:
    QwtDoubleInterval log10(const QwtDoubleInterval&) const;
    QwtDoubleInterval pow10(const QwtDoubleInterval&) const;

private:
    QwtDoubleInterval align(const QwtDoubleInterval&,
184
                            double stepSize) const;
pixhawk's avatar
pixhawk committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198

    void buildTicks(
        const QwtDoubleInterval &, double stepSize, int maxMinSteps,
        QwtValueList ticks[QwtScaleDiv::NTickTypes]) const;

    QwtValueList buildMinorTicks(
        const QwtValueList& majorTicks,
        int maxMinMark, double step) const;

    QwtValueList buildMajorTicks(
        const QwtDoubleInterval &interval, double stepSize) const;
};

#endif