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

#include "qwt_scale_engine.h"
#include "qwt_scale_draw.h"
#include "qwt_scale_div.h"
#include "qwt_scale_map.h"
#include "qwt_double_interval.h"
#include "qwt_abstract_scale.h"

class QwtAbstractScale::PrivateData
{
public:
    PrivateData():
        maxMajor(5),
        maxMinor(3),
        stepSize(0.0),
24
        autoScale(true) {
pixhawk's avatar
pixhawk committed
25 26 27 28
        scaleEngine = new QwtLinearScaleEngine;
        scaleDraw = new QwtScaleDraw();
    }

29
    ~PrivateData() {
pixhawk's avatar
pixhawk committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
        delete scaleEngine;
        delete scaleDraw;
    }

    QwtScaleEngine *scaleEngine;
    QwtAbstractScaleDraw *scaleDraw;

    int maxMajor;
    int maxMinor;
    double stepSize;

    bool autoScale;
};

/*!
  Constructor

47
  Creates a default QwtScaleDraw and a QwtLinearScaleEngine.
pixhawk's avatar
pixhawk committed
48 49
  Autoscaling is enabled, and the stepSize is initialized by 0.0.
*/
50

pixhawk's avatar
pixhawk committed
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89
QwtAbstractScale::QwtAbstractScale()
{
    d_data = new PrivateData;
    rescale(0.0, 100.0);
}

//! Destructor
QwtAbstractScale::~QwtAbstractScale()
{
    delete d_data;
}

/*!
  \brief Specify a scale.

  Disable autoscaling and define a scale by an interval and a step size

  \param vmin lower limit of the scale interval
  \param vmax upper limit of the scale interval
  \param stepSize major step size
  \sa setAutoScale()
*/
void QwtAbstractScale::setScale(double vmin, double vmax, double stepSize)
{
    d_data->autoScale = false;
    d_data->stepSize = stepSize;

    rescale(vmin, vmax, stepSize);
}

/*!
  \brief Specify a scale.

  Disable autoscaling and define a scale by an interval and a step size

  \param interval Interval
  \param stepSize major step size
  \sa setAutoScale()
*/
90 91
void QwtAbstractScale::setScale(const QwtDoubleInterval &interval,
                                double stepSize)
pixhawk's avatar
pixhawk committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
{
    setScale(interval.minValue(), interval.maxValue(), stepSize);
}


/*!
  \brief Specify a scale.

  Disable autoscaling and define a scale by a scale division

  \param scaleDiv Scale division
  \sa setAutoScale()
*/
void QwtAbstractScale::setScale(const QwtScaleDiv &scaleDiv)
{
    d_data->autoScale = false;

109
    if (scaleDiv != d_data->scaleDraw->scaleDiv()) {
pixhawk's avatar
pixhawk committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123
        d_data->scaleDraw->setScaleDiv(scaleDiv);
        scaleChange();
    }
}

/*!
  Recalculate the scale division and update the scale draw.

  \param vmin Lower limit of the scale interval
  \param vmax Upper limit of the scale interval
  \param stepSize Major step size

  \sa scaleChange()
*/
124
void QwtAbstractScale::rescale(double vmin, double vmax, double stepSize)
pixhawk's avatar
pixhawk committed
125 126
{
    const QwtScaleDiv scaleDiv = d_data->scaleEngine->divideScale(
127
                                     vmin, vmax, d_data->maxMajor, d_data->maxMinor, stepSize);
pixhawk's avatar
pixhawk committed
128

129
    if ( scaleDiv != d_data->scaleDraw->scaleDiv() ) {
pixhawk's avatar
pixhawk committed
130 131 132 133 134 135 136 137 138 139
        d_data->scaleDraw->setTransformation(
            d_data->scaleEngine->transformation());
        d_data->scaleDraw->setScaleDiv(scaleDiv);
        scaleChange();
    }
}

/*!
  \brief Advise the widget to control the scale range internally.

140
  Autoscaling is on by default.
pixhawk's avatar
pixhawk committed
141 142 143 144
  \sa setScale(), autoScale()
*/
void QwtAbstractScale::setAutoScale()
{
145
    if (!d_data->autoScale) {
pixhawk's avatar
pixhawk committed
146 147 148 149 150 151 152
        d_data->autoScale = true;
        scaleChange();
    }
}

/*!
  \return \c true if autoscaling is enabled
153
*/
pixhawk's avatar
pixhawk committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
bool QwtAbstractScale::autoScale() const
{
    return d_data->autoScale;
}

/*!
  \brief Set the maximum number of major tick intervals.

  The scale's major ticks are calculated automatically such that
  the number of major intervals does not exceed ticks.
  The default value is 5.
  \param ticks maximal number of major ticks.
  \sa QwtAbstractScaleDraw
*/
void QwtAbstractScale::setScaleMaxMajor(int ticks)
{
170
    if (ticks != d_data->maxMajor) {
pixhawk's avatar
pixhawk committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
        d_data->maxMajor = ticks;
        updateScaleDraw();
    }
}

/*!
  \brief Set the maximum number of minor tick intervals

  The scale's minor ticks are calculated automatically such that
  the number of minor intervals does not exceed ticks.
  The default value is 3.
  \param ticks
  \sa QwtAbstractScaleDraw
*/
void QwtAbstractScale::setScaleMaxMinor(int ticks)
{
187
    if ( ticks != d_data->maxMinor) {
pixhawk's avatar
pixhawk committed
188 189 190 191 192
        d_data->maxMinor = ticks;
        updateScaleDraw();
    }
}

193 194
/*!
  \return Max. number of minor tick intervals
pixhawk's avatar
pixhawk committed
195 196
  The default value is 3.
*/
197
int QwtAbstractScale::scaleMaxMinor() const
pixhawk's avatar
pixhawk committed
198 199 200 201
{
    return d_data->maxMinor;
}

202 203
/*!
  \return Max. number of major tick intervals
pixhawk's avatar
pixhawk committed
204 205
  The default value is 5.
*/
206
int QwtAbstractScale::scaleMaxMajor() const
pixhawk's avatar
pixhawk committed
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
{
    return d_data->maxMajor;
}

/*!
  \brief Set a scale draw

  scaleDraw has to be created with new and will be deleted in
  ~QwtAbstractScale or the next call of setAbstractScaleDraw.
*/
void QwtAbstractScale::setAbstractScaleDraw(QwtAbstractScaleDraw *scaleDraw)
{
    if ( scaleDraw == NULL || scaleDraw == d_data->scaleDraw )
        return;

    if ( d_data->scaleDraw != NULL )
        scaleDraw->setScaleDiv(d_data->scaleDraw->scaleDiv());

    delete d_data->scaleDraw;
    d_data->scaleDraw = scaleDraw;
227
}
pixhawk's avatar
pixhawk committed
228 229 230 231 232

/*!
    \return Scale draw
    \sa setAbstractScaleDraw()
*/
233
QwtAbstractScaleDraw *QwtAbstractScale::abstractScaleDraw()
pixhawk's avatar
pixhawk committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
{
    return d_data->scaleDraw;
}

/*!
    \return Scale draw
    \sa setAbstractScaleDraw()
*/
const QwtAbstractScaleDraw *QwtAbstractScale::abstractScaleDraw() const
{
    return d_data->scaleDraw;
}

void QwtAbstractScale::updateScaleDraw()
{
249 250
    rescale( d_data->scaleDraw->scaleDiv().lBound(),
             d_data->scaleDraw->scaleDiv().hBound(), d_data->stepSize);
pixhawk's avatar
pixhawk committed
251 252 253 254 255 256 257 258 259 260 261 262 263
}

/*!
  \brief Set a scale engine

  The scale engine is responsible for calculating the scale division,
  and in case of auto scaling how to align the scale.

  scaleEngine has to be created with new and will be deleted in
  ~QwtAbstractScale or the next call of setScaleEngine.
*/
void QwtAbstractScale::setScaleEngine(QwtScaleEngine *scaleEngine)
{
264
    if ( scaleEngine != NULL && scaleEngine != d_data->scaleEngine ) {
pixhawk's avatar
pixhawk committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
        delete d_data->scaleEngine;
        d_data->scaleEngine = scaleEngine;
    }
}

/*!
    \return Scale engine
    \sa setScaleEngine()
*/
const QwtScaleEngine *QwtAbstractScale::scaleEngine() const
{
    return d_data->scaleEngine;
}

/*!
    \return Scale engine
    \sa setScaleEngine()
*/
QwtScaleEngine *QwtAbstractScale::scaleEngine()
{
    return d_data->scaleEngine;
}

/*!
  \brief Notify changed scale

  Dummy empty implementation, intended to be overloaded by derived classes
*/
void QwtAbstractScale::scaleChange()
{
}

/*!
   \return abstractScaleDraw()->scaleMap()
*/
const QwtScaleMap &QwtAbstractScale::scaleMap() const
{
    return d_data->scaleDraw->scaleMap();
}