dynamicSlider.cpp

Go to the documentation of this file.
00001 //==============================================
00002 //  copyright            : (C) 2003-2005 by Will Stokes
00003 //==============================================
00004 //  This program is free software; you can redistribute it
00005 //  and/or modify it under the terms of the GNU General
00006 //  Public License as published by the Free Software
00007 //  Foundation; either version 2 of the License, or
00008 //  (at your option) any later version.
00009 //==============================================
00010 
00011 #include <qapplication.h>
00012 #include <qtooltip.h>
00013 
00014 #include "dynamicSlider.h"
00015 
00016 //==========================================
00017 DynamicSlider::DynamicSlider( Orientation orientation, QWidget* parent,
00018                               const char* name) : QSlider(orientation, parent, name)
00019 {
00020   //determine the parent screen the tooltip will be displayed in and create tooltip
00021   int scr = QApplication::desktop()->screenNumber( this );                                
00022   tooltip = new SliderToolTip( QApplication::desktop()->screen( scr ), this);
00023   updateTooltipLabel();
00024 
00025   //make sure tooltip label is updated when the slider value changes
00026   connect( this, SIGNAL( valueChanged(int) ),
00027            this, SLOT( updateTooltipLabel() ) );
00028 }
00029 //==========================================
00030 DynamicSlider::~DynamicSlider()
00031 {
00032   delete tooltip;
00033   tooltip = NULL;
00034 }
00035 //==========================================
00036 void DynamicSlider::setZeroString( QString val )
00037 {
00038   zeroString = val;  
00039   updateTooltipLabel();
00040 }
00041 //==========================================
00042 void DynamicSlider::setPrefix( QString val )
00043 {
00044   prefix1 = val;
00045   prefix2 = QString( NULL );
00046   updateTooltipLabel();
00047 }
00048 //==========================================
00049 void DynamicSlider::setPrefixes( QString v1, QString v2 )
00050 {
00051   prefix1 = v1;
00052   prefix2 = v2;
00053   updateTooltipLabel();
00054 }
00055 //==========================================
00056 void DynamicSlider::setSuffix( QString val )
00057 {
00058   suffix1 = val;
00059   suffix2 = QString( NULL );
00060   updateTooltipLabel();
00061 }
00062 //==========================================
00063 void DynamicSlider::setSuffixes( QString v1, QString v2 )
00064 {
00065   suffix1 = v1;
00066   suffix2 = v2;
00067   updateTooltipLabel();
00068 }
00069 //==========================================
00070 QString DynamicSlider::mapValToString()
00071 {
00072   //the default behavior is to simply use the slider value directly
00073   if( orientation() == Qt::Vertical )  
00074     return QString("%1").arg( -value() );
00075   else
00076     return QString("%1").arg(value());
00077 }
00078 //==========================================
00079 void DynamicSlider::updateTooltipLabel()
00080 {
00081   //determine string that will be used for tooltip
00082   QString tipString;
00083   
00084   //if the value is zero and a zero string has been provided used that
00085   if( value() == 0 && !zeroString.isNull() )
00086   {
00087     tipString = zeroString;
00088   }
00089   //otherwise construct a tip string using provided prefixes, suffixes, and the current slider value
00090   else
00091   { 
00092     //determine prefix and suffix that will be used to construct tooltip string
00093     QString p, s;
00094     if( value() > 0 || prefix2.isNull() ) p = prefix1;
00095     else                                  p = prefix2;
00096 
00097     if( value() > 0 || suffix2.isNull() ) s = suffix1;
00098     else                                  s = suffix2;
00099     
00100     //construct tipstring
00101     tipString = QString("%1%2%3").arg(p).arg(mapValToString()).arg(s);
00102     
00103   }
00104 
00105   //update tooltip
00106   tooltip->setText( tipString );
00107   tooltip->adjustSize();
00108   if( tooltip->isShown() ) qApp->processEvents();
00109 }
00110 //==========================================
00111 void DynamicSlider::mouseMoveEvent(QMouseEvent* e)
00112 {
00113   //cache the mouse position since the tooltip will need this information when updating itself
00114   cachedMousePos = e->pos();
00115   QSlider::mouseMoveEvent(e);
00116   emit mouseHasMoved();
00117 }
00118 //==========================================
00119 QPoint DynamicSlider::getMousePos() { return cachedMousePos; }
00120 //==========================================
00121 SliderToolTip::SliderToolTip( QWidget* parent, DynamicSlider* slider) 
00122 : QLabel( parent, "toolTipTip",
00123           WStyle_StaysOnTop | WStyle_Customize | 
00124           WStyle_NoBorder | WStyle_Tool | WX11BypassWM )
00125 {
00126   //store slider handle                             
00127   this->slider = slider;
00128   
00129   //setup lable to use standard black writing on a light yellow background so it
00130   //looks like a normal tooltip
00131   setPaletteForegroundColor( QColor("Black") );
00132   setPaletteBackgroundColor( QColor("LightYellow") );
00133 
00134   //use default system tooltip font
00135   setFont( QToolTip::font() );
00136   
00137   //setup the otherparmslike a frame etc so it looks like a normal tooltip
00138   setMargin(1);
00139   setAutoMask( FALSE );
00140   setFrameStyle( QFrame::Plain | QFrame::Box );
00141   setLineWidth( 1 );
00142   setAlignment( AlignAuto | AlignTop );
00143   setIndent(0);
00144   polish();
00145   adjustSize();
00146   
00147   //show the tooltip when the user presses the slider
00148   connect( slider, SIGNAL( sliderPressed() ),  this, SLOT( showTooltip() ) );
00149   
00150   //move tooltip to follow the slider handle
00151   setMouseTracking(true);
00152   connect( slider, SIGNAL( mouseHasMoved() ),  this, SLOT( update() ) );
00153   
00154   //hide tooltip when users releases the slider
00155   connect( slider, SIGNAL( sliderReleased() ), this, SLOT( hideTooltip() ) );
00156 }
00157 //==========================================
00158 void SliderToolTip::showTooltip()
00159 {
00160   //make sure label is up-to-date
00161   update();
00162   show();
00163 }
00164 //==========================================
00165 void SliderToolTip::hideTooltip() { hide(); }
00166 //==========================================
00167 void SliderToolTip::update()
00168 {
00169   //margin well provide betweent the slider and the tooltip
00170   const int TOOLTIP_MARGIN = 4;
00171   
00172   //fetch slider handle rect
00173   QRect sliderRect = slider->sliderRect();
00174   
00175   //determine location tooltip will be shown
00176   QPoint tooltipTopLeft;
00177   if( slider->orientation() == Qt::Horizontal )
00178   {
00179     tooltipTopLeft = QPoint( sliderRect.right() + TOOLTIP_MARGIN,
00180                              slider->getMousePos().y() >= sliderRect.top() ?
00181                              sliderRect.top() - TOOLTIP_MARGIN - height() :
00182                              sliderRect.bottom() + TOOLTIP_MARGIN );
00183   }
00184   else
00185   {
00186     tooltipTopLeft = QPoint( slider->getMousePos().x() >= sliderRect.right() ?
00187                              sliderRect.left() - TOOLTIP_MARGIN - width() :
00188                              sliderRect.right() + TOOLTIP_MARGIN,
00189                              (sliderRect.top() + sliderRect.bottom())/2 - height()/2 );
00190   }
00191   
00192   //map tooltip position from slider widget to screen coordinates
00193   tooltipTopLeft = slider->mapToGlobal( tooltipTopLeft );
00194   
00195   //position tooltip
00196   move( tooltipTopLeft );
00197   if( isShown() ) qApp->processEvents();
00198 }
00199 //==========================================

Generated on Wed Jan 24 05:38:28 2007 for AlbumShaper by  doxygen 1.5.1