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

#include <qevent.h>
#include <qdatetime.h>
#include "qwt_abstract_slider.h"
#include "qwt_math.h"

#ifndef WHEEL_DELTA
#define WHEEL_DELTA 120
#endif

class QwtAbstractSlider::PrivateData
{
public:
    PrivateData():
        scrollMode(ScrNone),
        mouseOffset(0.0),
        tracking(true),
        tmrID(0),
        updTime(150),
        mass(0.0),
29
        readOnly(false) {
pixhawk's avatar
pixhawk committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    }

    int scrollMode;
    double mouseOffset;
    int direction;
    int tracking;

    int tmrID;
    int updTime;
    int timerTick;
    QTime time;
    double speed;
    double mass;
    Qt::Orientation orientation;
    bool readOnly;
};

47
/*!
pixhawk's avatar
pixhawk committed
48 49 50 51 52 53
   \brief Constructor

   \param orientation Orientation
   \param parent Parent widget
*/
QwtAbstractSlider::QwtAbstractSlider(
54
    Qt::Orientation orientation, QWidget *parent):
pixhawk's avatar
pixhawk committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68
    QWidget(parent, NULL)
{
    d_data = new QwtAbstractSlider::PrivateData;
    d_data->orientation = orientation;

#if QT_VERSION >= 0x040000
    using namespace Qt;
#endif
    setFocusPolicy(TabFocus);
}

//! Destructor
QwtAbstractSlider::~QwtAbstractSlider()
{
69
    if(d_data->tmrID)
pixhawk's avatar
pixhawk committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
        killTimer(d_data->tmrID);

    delete d_data;
}

/*!
  En/Disable read only mode

  In read only mode the slider can't be controlled by mouse
  or keyboard.

  \param readOnly Enables in case of true
  \sa isReadOnly()
*/
void QwtAbstractSlider::setReadOnly(bool readOnly)
{
    d_data->readOnly = readOnly;
    update();
}

/*!
  In read only mode the slider can't be controlled by mouse
  or keyboard.

  \return true if read only
  \sa setReadOnly()
*/
bool QwtAbstractSlider::isReadOnly() const
{
    return d_data->readOnly;
}

/*!
  \brief Set the orientation.
  \param o Orientation. Allowed values are
           Qt::Horizontal and Qt::Vertical.
*/
void QwtAbstractSlider::setOrientation(Qt::Orientation o)
{
    d_data->orientation = o;
}

112
/*!
pixhawk's avatar
pixhawk committed
113 114 115 116 117 118 119 120 121 122
  \return Orientation
  \sa setOrientation()
*/
Qt::Orientation QwtAbstractSlider::orientation() const
{
    return d_data->orientation;
}

//! Stop updating if automatic scrolling is active

123
void QwtAbstractSlider::stopMoving()
pixhawk's avatar
pixhawk committed
124
{
125
    if(d_data->tmrID) {
pixhawk's avatar
pixhawk committed
126 127 128 129 130 131 132 133 134 135
        killTimer(d_data->tmrID);
        d_data->tmrID = 0;
    }
}

/*!
  \brief Specify the update interval for automatic scrolling
  \param t update interval in milliseconds
  \sa getScrollMode()
*/
136
void QwtAbstractSlider::setUpdateTime(int t)
pixhawk's avatar
pixhawk committed
137
{
138
    if (t < 50)
pixhawk's avatar
pixhawk committed
139 140 141 142 143 144
        t = 50;
    d_data->updTime = t;
}


//! Mouse press event handler
145
void QwtAbstractSlider::mousePressEvent(QMouseEvent *e)
pixhawk's avatar
pixhawk committed
146
{
147
    if ( isReadOnly() ) {
pixhawk's avatar
pixhawk committed
148 149 150 151 152 153 154 155 156 157 158 159
        e->ignore();
        return;
    }
    if ( !isValid() )
        return;

    const QPoint &p = e->pos();

    d_data->timerTick = 0;

    getScrollMode(p, d_data->scrollMode, d_data->direction);
    stopMoving();
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

    switch(d_data->scrollMode) {
    case ScrPage:
    case ScrTimer:
        d_data->mouseOffset = 0;
        d_data->tmrID = startTimer(qwtMax(250, 2 * d_data->updTime));
        break;

    case ScrMouse:
        d_data->time.start();
        d_data->speed = 0;
        d_data->mouseOffset = getValue(p) - value();
        emit sliderPressed();
        break;

    default:
        d_data->mouseOffset = 0;
        d_data->direction = 0;
        break;
pixhawk's avatar
pixhawk committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
    }
}


//! Emits a valueChanged() signal if necessary
void QwtAbstractSlider::buttonReleased()
{
    if ((!d_data->tracking) || (value() != prevValue()))
        emit valueChanged(value());
}


//! Mouse Release Event handler
void QwtAbstractSlider::mouseReleaseEvent(QMouseEvent *e)
{
194
    if ( isReadOnly() ) {
pixhawk's avatar
pixhawk committed
195 196 197 198 199 200 201 202
        e->ignore();
        return;
    }
    if ( !isValid() )
        return;

    const double inc = step();

203 204 205 206 207 208 209 210 211 212
    switch(d_data->scrollMode) {
    case ScrMouse: {
        setPosition(e->pos());
        d_data->direction = 0;
        d_data->mouseOffset = 0;
        if (d_data->mass > 0.0) {
            const int ms = d_data->time.elapsed();
            if ((fabs(d_data->speed) >  0.0) && (ms < 50))
                d_data->tmrID = startTimer(d_data->updTime);
        } else {
pixhawk's avatar
pixhawk committed
213 214 215
            d_data->scrollMode = ScrNone;
            buttonReleased();
        }
216
        emit sliderReleased();
pixhawk's avatar
pixhawk committed
217

218 219
        break;
    }
pixhawk's avatar
pixhawk committed
220

221 222 223 224 225 226 227 228
    case ScrDirect: {
        setPosition(e->pos());
        d_data->direction = 0;
        d_data->mouseOffset = 0;
        d_data->scrollMode = ScrNone;
        buttonReleased();
        break;
    }
pixhawk's avatar
pixhawk committed
229

230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
    case ScrPage: {
        stopMoving();
        if (!d_data->timerTick)
            QwtDoubleRange::incPages(d_data->direction);
        d_data->timerTick = 0;
        buttonReleased();
        d_data->scrollMode = ScrNone;
        break;
    }

    case ScrTimer: {
        stopMoving();
        if (!d_data->timerTick)
            QwtDoubleRange::fitValue(value() + double(d_data->direction) * inc);
        d_data->timerTick = 0;
        buttonReleased();
        d_data->scrollMode = ScrNone;
        break;
    }

    default: {
        d_data->scrollMode = ScrNone;
        buttonReleased();
    }
pixhawk's avatar
pixhawk committed
254 255 256 257 258 259 260 261
    }
}


/*!
  Move the slider to a specified point, adjust the value
  and emit signals if necessary.
*/
262
void QwtAbstractSlider::setPosition(const QPoint &p)
pixhawk's avatar
pixhawk committed
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
{
    QwtDoubleRange::fitValue(getValue(p) - d_data->mouseOffset);
}


/*!
  \brief Enables or disables tracking.

  If tracking is enabled, the slider emits a
  valueChanged() signal whenever its value
  changes (the default behaviour). If tracking
  is disabled, the value changed() signal will only
  be emitted if:<ul>
  <li>the user releases the mouse
      button and the value has changed or
  <li>at the end of automatic scrolling.</ul>
  Tracking is enabled by default.
  \param enable \c true (enable) or \c false (disable) tracking.
*/
void QwtAbstractSlider::setTracking(bool enable)
{
    d_data->tracking = enable;
}

287
/*!
pixhawk's avatar
pixhawk committed
288 289 290 291 292
   Mouse Move Event handler
   \param e Mouse event
*/
void QwtAbstractSlider::mouseMoveEvent(QMouseEvent *e)
{
293
    if ( isReadOnly() ) {
pixhawk's avatar
pixhawk committed
294 295 296 297 298 299 300
        e->ignore();
        return;
    }

    if ( !isValid() )
        return;

301
    if (d_data->scrollMode == ScrMouse ) {
pixhawk's avatar
pixhawk committed
302
        setPosition(e->pos());
303
        if (d_data->mass > 0.0) {
pixhawk's avatar
pixhawk committed
304
            double ms = double(d_data->time.elapsed());
305
            if (ms < 1.0)
pixhawk's avatar
pixhawk committed
306 307 308 309 310 311 312 313 314
                ms = 1.0;
            d_data->speed = (exactValue() - exactPrevValue()) / ms;
            d_data->time.start();
        }
        if (value() != prevValue())
            emit sliderMoved(value());
    }
}

315
/*!
pixhawk's avatar
pixhawk committed
316 317 318 319 320
   Wheel Event handler
   \param e Whell event
*/
void QwtAbstractSlider::wheelEvent(QWheelEvent *e)
{
321
    if ( isReadOnly() ) {
pixhawk's avatar
pixhawk committed
322 323 324 325 326 327 328 329 330 331 332
        e->ignore();
        return;
    }

    if ( !isValid() )
        return;

    int mode = ScrNone, direction = 0;

    // Give derived classes a chance to say ScrNone
    getScrollMode(e->pos(), mode, direction);
333
    if ( mode != ScrNone ) {
pixhawk's avatar
pixhawk committed
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
        const int inc = e->delta() / WHEEL_DELTA;
        QwtDoubleRange::incPages(inc);
        if (value() != prevValue())
            emit sliderMoved(value());
    }
}

/*!
  Handles key events

  - Key_Down, KeyLeft\n
    Decrement by 1
  - Key_Up, Key_Right\n
    Increment by 1

  \param e Key event
  \sa isReadOnly()
*/
void QwtAbstractSlider::keyPressEvent(QKeyEvent *e)
{
354
    if ( isReadOnly() ) {
pixhawk's avatar
pixhawk committed
355 356 357 358 359 360 361 362
        e->ignore();
        return;
    }

    if ( !isValid() )
        return;

    int increment = 0;
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
    switch ( e->key() ) {
    case Qt::Key_Down:
        if ( orientation() == Qt::Vertical )
            increment = -1;
        break;
    case Qt::Key_Up:
        if ( orientation() == Qt::Vertical )
            increment = 1;
        break;
    case Qt::Key_Left:
        if ( orientation() == Qt::Horizontal )
            increment = -1;
        break;
    case Qt::Key_Right:
        if ( orientation() == Qt::Horizontal )
            increment = 1;
        break;
    default:
        ;
        e->ignore();
pixhawk's avatar
pixhawk committed
383 384
    }

385
    if ( increment != 0 ) {
pixhawk's avatar
pixhawk committed
386 387 388 389 390 391
        QwtDoubleRange::incValue(increment);
        if (value() != prevValue())
            emit sliderMoved(value());
    }
}

392
/*!
pixhawk's avatar
pixhawk committed
393 394 395 396 397 398 399
   Qt timer event
   \param e Timer event
*/
void QwtAbstractSlider::timerEvent(QTimerEvent *)
{
    const double inc = step();

400 401 402 403 404 405 406 407 408 409 410 411
    switch (d_data->scrollMode) {
    case ScrMouse: {
        if (d_data->mass > 0.0) {
            d_data->speed *= exp( - double(d_data->updTime) * 0.001 / d_data->mass );
            const double newval =
                exactValue() + d_data->speed * double(d_data->updTime);
            QwtDoubleRange::fitValue(newval);
            // stop if d_data->speed < one step per second
            if (fabs(d_data->speed) < 0.001 * fabs(step())) {
                d_data->speed = 0;
                stopMoving();
                buttonReleased();
pixhawk's avatar
pixhawk committed
412 413
            }

414
        } else
pixhawk's avatar
pixhawk committed
415
            stopMoving();
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
        break;
    }

    case ScrPage: {
        QwtDoubleRange::incPages(d_data->direction);
        if (!d_data->timerTick) {
            killTimer(d_data->tmrID);
            d_data->tmrID = startTimer(d_data->updTime);
        }
        break;
    }
    case ScrTimer: {
        QwtDoubleRange::fitValue(value() +  double(d_data->direction) * inc);
        if (!d_data->timerTick) {
            killTimer(d_data->tmrID);
            d_data->tmrID = startTimer(d_data->updTime);
pixhawk's avatar
pixhawk committed
432
        }
433 434 435 436 437 438
        break;
    }
    default: {
        stopMoving();
        break;
    }
pixhawk's avatar
pixhawk committed
439 440 441 442 443 444 445 446 447 448 449 450 451 452
    }

    d_data->timerTick = 1;
}


/*!
  Notify change of value

  This function can be reimplemented by derived classes
  in order to keep track of changes, i.e. repaint the widget.
  The default implementation emits a valueChanged() signal
  if tracking is enabled.
*/
453
void QwtAbstractSlider::valueChange()
pixhawk's avatar
pixhawk committed
454 455
{
    if (d_data->tracking)
456
        emit valueChanged(value());
pixhawk's avatar
pixhawk committed
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
}

/*!
  \brief Set the slider's mass for flywheel effect.

  If the slider's mass is greater then 0, it will continue
  to move after the mouse button has been released. Its speed
  decreases with time at a rate depending on the slider's mass.
  A large mass means that it will continue to move for a
  long time.

  Derived widgets may overload this function to make it public.

  \param val New mass in kg

  \bug If the mass is smaller than 1g, it is set to zero.
       The maximal mass is limited to 100kg.
  \sa mass()
*/
void QwtAbstractSlider::setMass(double val)
{
    if (val < 0.001)
479
        d_data->mass = 0.0;
pixhawk's avatar
pixhawk committed
480
    else if (val > 100.0)
481
        d_data->mass = 100.0;
pixhawk's avatar
pixhawk committed
482
    else
483
        d_data->mass = val;
pixhawk's avatar
pixhawk committed
484 485 486 487 488 489 490
}

/*!
    \return mass
    \sa setMass()
*/
double QwtAbstractSlider::mass() const
491 492
{
    return d_data->mass;
pixhawk's avatar
pixhawk committed
493 494 495 496 497 498 499 500 501 502 503 504 505
}


/*!
  \brief Move the slider to a specified value

  This function can be used to move the slider to a value
  which is not an integer multiple of the step size.
  \param val new value
  \sa fitValue()
*/
void QwtAbstractSlider::setValue(double val)
{
506
    if (d_data->scrollMode == ScrMouse)
pixhawk's avatar
pixhawk committed
507 508 509 510 511 512 513 514 515 516 517 518 519 520
        stopMoving();
    QwtDoubleRange::setValue(val);
}


/*!
  \brief Set the slider's value to the nearest integer multiple
         of the step size.

   \param valeu Value
   \sa setValue(), incValue()
*/
void QwtAbstractSlider::fitValue(double value)
{
521
    if (d_data->scrollMode == ScrMouse)
pixhawk's avatar
pixhawk committed
522 523 524 525 526 527 528 529 530 531 532
        stopMoving();
    QwtDoubleRange::fitValue(value);
}

/*!
  \brief Increment the value by a specified number of steps
  \param steps number of steps
  \sa setValue()
*/
void QwtAbstractSlider::incValue(int steps)
{
533
    if (d_data->scrollMode == ScrMouse)
pixhawk's avatar
pixhawk committed
534 535 536 537 538 539 540
        stopMoving();
    QwtDoubleRange::incValue(steps);
}

void QwtAbstractSlider::setMouseOffset(double offset)
{
    d_data->mouseOffset = offset;
541
}
pixhawk's avatar
pixhawk committed
542 543 544 545 546 547 548 549 550 551

double QwtAbstractSlider::mouseOffset() const
{
    return d_data->mouseOffset;
}

int QwtAbstractSlider::scrollMode() const
{
    return d_data->scrollMode;
}