Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

# qwt_sldbase.cpp

    00001 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
    00002  * Qwt Widget Library
    00003  * Copyright (C) 1997   Josef Wilgen
    00004  * Copyright (C) 2002   Uwe Rathmann
    00005  *
    00006  * This library is free software; you can redistribute it and/or
    00007  * modify it under the terms of the Qwt License, Version 1.0
    00008  *****************************************************************************/
    00009
    00010 #include <qevent.h>
    00011 #include "qwt_sldbase.h"
    00012 #include "qwt_math.h"
    00013
    00014 #ifndef WHEEL_DELTA
    00015 #define WHEEL_DELTA 120
    00016 #endif
    00017
    00019 QwtSliderBase::QwtSliderBase(Qt::Orientation orient,
    00020         QWidget *parent, const char *name, Qt::WFlags f):
    00021     QWidget(parent, name, f),
    00022     d_scrollMode(ScrNone),
    00023     d_orientation(orient),
    00024     d_readOnly(FALSE)
    00025 {
    00026     d_tmrID = 0;
    00027     d_updTime = 150;
    00028     d_mass = 0.0;
    00029     d_tracking = TRUE;
    00030     d_mouseOffset = 0.0;
    00031     setRange(0.0,100.0, 1.0);
    00032     setValue(0.0);
    00033
    00034     setFocusPolicy(QWidget::TabFocus);
    00035 }
    00036
    00038 QwtSliderBase::~QwtSliderBase()
    00039 {
    00040     if(d_tmrID)
    00041         killTimer(d_tmrID);
    00042 }
    00043
    00053 void QwtSliderBase::setReadOnly(bool readOnly)
    00054 {
    00055     d_readOnly = readOnly;
    00056     update();
    00057 }
    00058
    00066 bool QwtSliderBase::isReadOnly() const
    00067 {
    00068     return d_readOnly;
    00069 }
    00070
    00076 void QwtSliderBase::setOrientation(Orientation o)
    00077 {
    00078     d_orientation = o;
    00079 }
    00080
    00085 Qt::Orientation QwtSliderBase::orientation() const
    00086 {
    00087     return d_orientation;
    00088 }
    00089
    00091
    00092 void QwtSliderBase::stopMoving()
    00093 {
    00094     if(d_tmrID)
    00095     {
    00096         killTimer(d_tmrID);
    00097         d_tmrID = 0;
    00098     }
    00099 }
    00100
    00106 void QwtSliderBase::setUpdateTime(int t)
    00107 {
    00108     if (t < 50)
    00109         t = 50;
    00110     d_updTime = t;
    00111 }
    00112
    00113
    00115 void QwtSliderBase::mousePressEvent(QMouseEvent *e)
    00116 {
    00117     if ( isReadOnly() )
    00118     {
    00119 #if QT_VERSION >= 300
    00120         e->ignore();
    00121 #endif
    00122         return;
    00123     }
    00124     if ( !isValid() )
    00125         return;
    00126
    00127     const QPoint &p = e->pos();
    00128
    00129     d_timerTick = 0;
    00130
    00131     getScrollMode(p, d_scrollMode, d_direction);
    00132     stopMoving();
    00133
    00134     switch(d_scrollMode)
    00135     {
    00136         case ScrPage:
    00137         case ScrTimer:
    00138             d_mouseOffset = 0;
    00139             d_tmrID = startTimer(qwtMax(250, 2 * d_updTime));
    00140             break;
    00141
    00142         case ScrMouse:
    00143             d_time.start();
    00144             d_speed = 0;
    00145             d_mouseOffset = getValue(p) - value();
    00146             emit sliderPressed();
    00147             break;
    00148
    00149         default:
    00150             d_mouseOffset = 0;
    00151             d_direction = 0;
    00152             break;
    00153     }
    00154 }
    00155
    00156
    00158 void QwtSliderBase::buttonReleased()
    00159 {
    00160     if ((!d_tracking) || (value() != prevValue()))
    00161         emit valueChanged(value());
    00162 }
    00163
    00164
    00166 void QwtSliderBase::mouseReleaseEvent(QMouseEvent *e)
    00167 {
    00168     if ( isReadOnly() )
    00169     {
    00170 #if QT_VERSION >= 300
    00171         e->ignore();
    00172 #endif
    00173         return;
    00174     }
    00175     if ( !isValid() )
    00176         return;
    00177
    00178     const double inc = step();
    00179
    00180     switch(d_scrollMode)
    00181     {
    00182         case ScrMouse:
    00183         {
    00184             setPosition(e->pos());
    00185             d_direction = 0;
    00186             d_mouseOffset = 0;
    00187             if (d_mass > 0.0)
    00188             {
    00189                 const int ms = d_time.elapsed();
    00190                 if ((fabs(d_speed) >  0.0) && (ms < 50))
    00191                     d_tmrID = startTimer(d_updTime);
    00192             }
    00193             else
    00194             {
    00195                 d_scrollMode = ScrNone;
    00196                 buttonReleased();
    00197             }
    00198             emit sliderReleased();
    00199
    00200             break;
    00201         }
    00202
    00203         case ScrDirect:
    00204         {
    00205             setPosition(e->pos());
    00206             d_direction = 0;
    00207             d_mouseOffset = 0;
    00208             d_scrollMode = ScrNone;
    00209             buttonReleased();
    00210             break;
    00211         }
    00212
    00213         case ScrPage:
    00214         {
    00215             stopMoving();
    00216             if (!d_timerTick)
    00217                 QwtDblRange::incPages(d_direction);
    00218             d_timerTick = 0;
    00219             buttonReleased();
    00220             d_scrollMode = ScrNone;
    00221             break;
    00222         }
    00223
    00224         case ScrTimer:
    00225         {
    00226             stopMoving();
    00227             if (!d_timerTick)
    00228                 QwtDblRange::fitValue(value() + double(d_direction) * inc);
    00229             d_timerTick = 0;
    00230             buttonReleased();
    00231             d_scrollMode = ScrNone;
    00232             break;
    00233         }
    00234
    00235         default:
    00236         {
    00237             d_scrollMode = ScrNone;
    00238             buttonReleased();
    00239         }
    00240     }
    00241 }
    00242
    00243
    00248 void QwtSliderBase::setPosition(const QPoint &p)
    00249 {
    00250     QwtDblRange::fitValue(getValue(p) - d_mouseOffset);
    00251 }
    00252
    00253
    00268 void QwtSliderBase::setTracking(bool enable)
    00269 {
    00270     d_tracking = enable;
    00271 }
    00272
    00274 void QwtSliderBase::mouseMoveEvent(QMouseEvent *e)
    00275 {
    00276     if ( isReadOnly() )
    00277     {
    00278 #if QT_VERSION >= 300
    00279         e->ignore();
    00280 #endif
    00281         return;
    00282     }
    00283
    00284     if ( !isValid() )
    00285         return;
    00286
    00287     if (d_scrollMode == ScrMouse )
    00288     {
    00289         setPosition(e->pos());
    00290         if (d_mass > 0.0)
    00291         {
    00292             double ms = double(d_time.elapsed());
    00293             if (ms < 1.0)
    00294                 ms = 1.0;
    00295             d_speed = (exactValue() - exactPrevValue()) / ms;
    00296             d_time.start();
    00297         }
    00298         if (value() != prevValue())
    00299             emit sliderMoved(value());
    00300     }
    00301 }
    00302
    00304 void QwtSliderBase::wheelEvent(QWheelEvent *e)
    00305 {
    00306     if ( isReadOnly() )
    00307     {
    00308 #if QT_VERSION >= 300
    00309         e->ignore();
    00310 #endif
    00311         return;
    00312     }
    00313
    00314     if ( !isValid() )
    00315         return;
    00316
    00317     int mode = ScrNone, direction = 0;
    00318
    00319     // Give derived classes a chance to say ScrNone
    00320     getScrollMode(e->pos(), mode, direction);
    00321     if ( mode != ScrNone )
    00322     {
    00323         const int inc = e->delta() / WHEEL_DELTA;
    00324         QwtDblRange::incPages(inc);
    00325         if (value() != prevValue())
    00326             emit sliderMoved(value());
    00327     }
    00328 }
    00329
    00341 void QwtSliderBase::keyPressEvent(QKeyEvent *e)
    00342 {
    00343     if ( isReadOnly() )
    00344     {
    00345 #if QT_VERSION >= 300
    00346         e->ignore();
    00347 #endif
    00348         return;
    00349     }
    00350
    00351     if ( !isValid() )
    00352         return;
    00353
    00354     int increment = 0;
    00355     switch ( e->key() )
    00356     {
    00357         case Qt::Key_Down:
    00358             if ( orientation() == Qt::Vertical )
    00359                 increment = -1;
    00360             break;
    00361         case Qt::Key_Up:
    00362             if ( orientation() == Qt::Vertical )
    00363                 increment = 1;
    00364             break;
    00365         case Qt::Key_Left:
    00366             if ( orientation() == Qt::Horizontal )
    00367                 increment = -1;
    00368             break;
    00369         case Qt::Key_Right:
    00370             if ( orientation() == Qt::Horizontal )
    00371                 increment = 1;
    00372             break;
    00373         default:;
    00374 #if QT_VERSION >= 300
    00375             e->ignore();
    00376 #endif
    00377     }
    00378
    00379     if ( increment != 0 )
    00380     {
    00381         QwtDblRange::incValue(increment);
    00382         if (value() != prevValue())
    00383             emit sliderMoved(value());
    00384     }
    00385 }
    00386
    00388 void QwtSliderBase::timerEvent(QTimerEvent *)
    00389 {
    00390     const double inc = step();
    00391
    00392     switch (d_scrollMode)
    00393     {
    00394         case ScrMouse:
    00395         {
    00396             if (d_mass > 0.0)
    00397             {
    00398                 d_speed *= exp( - double(d_updTime) * 0.001 / d_mass );
    00399                 const double newval =
    00400                     exactValue() + d_speed * double(d_updTime);
    00401                 QwtDblRange::fitValue(newval);
    00402                 // stop if d_speed < one step per second
    00403                 if (fabs(d_speed) < 0.001 * fabs(step()))
    00404                 {
    00405                     d_speed = 0;
    00406                     stopMoving();
    00407                     buttonReleased();
    00408                 }
    00409
    00410             }
    00411             else
    00412                stopMoving();
    00413             break;
    00414         }
    00415
    00416         case ScrPage:
    00417         {
    00418             QwtDblRange::incPages(d_direction);
    00419             if (!d_timerTick)
    00420             {
    00421                 killTimer(d_tmrID);
    00422                 d_tmrID = startTimer(d_updTime);
    00423             }
    00424             break;
    00425         }
    00426         case ScrTimer:
    00427         {
    00428             QwtDblRange::fitValue(value() +  double(d_direction) * inc);
    00429             if (!d_timerTick)
    00430             {
    00431                 killTimer(d_tmrID);
    00432                 d_tmrID = startTimer(d_updTime);
    00433             }
    00434             break;
    00435         }
    00436         default:
    00437         {
    00438             stopMoving();
    00439             break;
    00440         }
    00441     }
    00442
    00443     d_timerTick = 1;
    00444 }
    00445
    00446
    00455 void QwtSliderBase::valueChange()
    00456 {
    00457     if (d_tracking)
    00458        emit valueChanged(value());
    00459 }
    00460
    00478 void QwtSliderBase::setMass(double val)
    00479 {
    00480     if (val < 0.001)
    00481        d_mass = 0.0;
    00482     else if (val > 100.0)
    00483        d_mass = 100.0;
    00484     else
    00485        d_mass = val;
    00486 }
    00487
    00492 double QwtSliderBase::mass() const
    00493 {
    00494     return d_mass;
    00495 }
    00496
    00497
    00506 void QwtSliderBase::setValue(double val)
    00507 {
    00508     if (d_scrollMode == ScrMouse)
    00509         stopMoving();
    00510     QwtDblRange::setValue(val);
    00511 }
    00512
    00513
    00519 void QwtSliderBase::fitValue(double val)
    00520 {
    00521     if (d_scrollMode == ScrMouse)
    00522         stopMoving();
    00523     QwtDblRange::fitValue(val);
    00524 }
    00525
    00526
    00531 void QwtSliderBase::incValue(int steps)
    00532 {
    00533     if (d_scrollMode == ScrMouse)
    00534         stopMoving();
    00535     QwtDblRange::incValue(steps);
    00536 }