Main Page   Class Hierarchy   Compound List   File List   Compound Members   Related Pages  

gapezoomableslider.h

00001 //Code in this file was primarily written by Randal Leistikow randal@ccrma.stanford.edu
00002 
00003 #ifndef _GAPE_ZOOMABLE_SLIDER_H
00004 #define _GAPE_ZOOMABLE_SLIDER_H
00005 
00006 #include <qslider.h>
00007 #include <qevent.h>
00008 
00009 // names of keys are in Qt's qnamespace.h
00010 #define ZOOMABLESLIDER_DEFAULT_KEY             Qt::Key_Shift
00011 #define ZOOMABLESLIDER_DEFAULT_ZOOM_PERCENT    5.0
00012 #define ZOOMABLESLIDER_DEFAULT_ARROW_PERCENT   0.05
00013 
00039 class GapeZoomableSlider : public QSlider
00040 {
00041   Q_OBJECT
00042 
00043   // private data
00044   int    d_minValue, d_maxValue, d_key;
00045   double d_minMaxScalar, d_arrowScalar;
00046 
00047 
00048  public:
00049   GapeZoomableSlider ( int minValue, int maxValue,
00050           int pageStep, int value,
00051           Orientation orientation,
00052           QWidget * parent,
00053           const char * name=0,
00054           int zoomKey = ZOOMABLESLIDER_DEFAULT_KEY,
00055           double zoomPercent  = ZOOMABLESLIDER_DEFAULT_ZOOM_PERCENT,
00056           double arrowPercent = ZOOMABLESLIDER_DEFAULT_ARROW_PERCENT )
00057    : QSlider( minValue, maxValue, pageStep, value, orientation, parent, name ),
00058      d_minValue( minValue ),
00059      d_maxValue( maxValue ),
00060      d_key( zoomKey ),
00061      d_minMaxScalar( zoomPercent / 100.0 ),
00062      d_arrowScalar( arrowPercent / 100.0 )
00063 
00064    {
00065       setFocusPolicy( QWidget::StrongFocus );
00066    }
00067 
00068 
00069   ~GapeZoomableSlider() {}
00070 
00071 
00072   void keyPressEvent( QKeyEvent *e )
00073   {
00074     // for zoom
00075     if( e->key() == d_key )  zoomIn();
00076 
00077     // for arrow keys
00078     if( (e->key() == Qt::Key_Down || e->key() == Qt::Key_Right) &&
00079     value() < d_maxValue )  arrowIncrease();
00080 
00081     if( (e->key() == Qt::Key_Up || e->key() == Qt::Key_Left) &&
00082     value() > d_minValue )  arrowDecrease();
00083 
00084   }
00085 
00086 
00087   void keyReleaseEvent( QKeyEvent *e )
00088   {
00089     if(e->key() == d_key)  zoomOut();
00090   }
00091 
00092 
00093   void focusOutEvent( QFocusEvent *e )
00094   {
00095       zoomOut();
00096   }
00097 
00098 
00099   private:
00100     void zoomIn()
00101     {
00102     int minMaxOffset = (int)( value() * d_minMaxScalar );
00103     int possibleMax = value() + minMaxOffset;
00104     int possibleMin = value() - minMaxOffset;
00105 
00106     setMaxValue( (possibleMax > d_maxValue) ? d_maxValue : possibleMax );
00107     setMinValue( (possibleMin < d_minValue) ? d_minValue : possibleMin );
00108     }
00109 
00110     void zoomOut()
00111     {
00112     setMaxValue( d_maxValue );
00113     setMinValue( d_minValue );
00114     }
00115 
00116 
00117     void arrowIncrease()
00118     {
00119       int possibleNewValue = value() + (int)(value() * d_arrowScalar);
00120       setValue( (possibleNewValue >= d_maxValue) ?
00121         d_maxValue : possibleNewValue );
00122       emit sliderMoved( value() );
00123     }
00124 
00125     void arrowDecrease()
00126     {
00127       int possibleNewValue = value() - (int)(value() * d_arrowScalar);
00128       setValue( (possibleNewValue <= d_minValue) ?
00129         d_minValue : possibleNewValue );
00130       emit sliderMoved( value() );
00131     }
00132 
00133 
00134 };
00135 
00136 
00137 #endif

Generated at Thu Jun 21 13:28:50 2001 for GAPE by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001