AlbumShaper  1.0a3
dynamicSlider.cpp
Go to the documentation of this file.
1 //==============================================
2 // copyright : (C) 2003-2005 by Will Stokes
3 //==============================================
4 // This program is free software; you can redistribute it
5 // and/or modify it under the terms of the GNU General
6 // Public License as published by the Free Software
7 // Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //==============================================
10 
11 #include <qstyleoption.h>
12 #include <qapplication.h>
13 #include <qtooltip.h>
14 //Added by qt3to4:
15 #include <QMouseEvent>
16 #include <QLabel>
17 #include <Q3Frame>
18 #include <QDesktopWidget>
19 
20 #include "dynamicSlider.h"
21 
22 //==========================================
23 DynamicSlider::DynamicSlider( Qt::Orientation orientation, QWidget* parent,
24  const char* name) : QSlider(orientation, parent, name)
25 {
26  //determine the parent screen the tooltip will be displayed in and create tooltip
27  int scr = QApplication::desktop()->screenNumber( this );
28  tooltip = new SliderToolTip( QApplication::desktop()->screen( scr ), this);
30 
31  //make sure tooltip label is updated when the slider value changes
32  connect( this, SIGNAL( valueChanged(int) ),
33  this, SLOT( updateTooltipLabel() ) );
34 }
35 //==========================================
37 {
38  delete tooltip;
39  tooltip = NULL;
40 }
41 //==========================================
42 void DynamicSlider::setZeroString( QString val )
43 {
44  zeroString = val;
46 }
47 //==========================================
48 void DynamicSlider::setPrefix( QString val )
49 {
50  prefix1 = val;
51  prefix2 = QString::null;
53 }
54 //==========================================
55 void DynamicSlider::setPrefixes( QString v1, QString v2 )
56 {
57  prefix1 = v1;
58  prefix2 = v2;
60 }
61 //==========================================
62 void DynamicSlider::setSuffix( QString val )
63 {
64  suffix1 = val;
65  suffix2 = QString::null;
67 }
68 //==========================================
69 void DynamicSlider::setSuffixes( QString v1, QString v2 )
70 {
71  suffix1 = v1;
72  suffix2 = v2;
74 }
75 //==========================================
77 {
78  //the default behavior is to simply use the slider value directly
79  if( orientation() == Qt::Vertical )
80  return QString("%1").arg( -value() );
81  else
82  return QString("%1").arg(value());
83 }
84 //==========================================
86 {
87  //determine string that will be used for tooltip
88  QString tipString;
89 
90  //if the value is zero and a zero string has been provided used that
91  if( value() == 0 && !zeroString.isNull() )
92  {
93  tipString = zeroString;
94  }
95  //otherwise construct a tip string using provided prefixes, suffixes, and the current slider value
96  else
97  {
98  //determine prefix and suffix that will be used to construct tooltip string
99  QString p, s;
100  if( value() > 0 || prefix2.isNull() ) p = prefix1;
101  else p = prefix2;
102 
103  if( value() > 0 || suffix2.isNull() ) s = suffix1;
104  else s = suffix2;
105 
106  //construct tipstring
107  tipString = QString("%1%2%3").arg(p).arg(mapValToString()).arg(s);
108 
109  }
110 
111  //update tooltip
112  tooltip->setText( tipString );
113  tooltip->adjustSize();
114  if( tooltip->isShown() ) qApp->processEvents();
115 }
116 //==========================================
117 void DynamicSlider::mouseMoveEvent(QMouseEvent* e)
118 {
119  //cache the mouse position since the tooltip will need this information when updating itself
120  cachedMousePos = e->pos();
121  QSlider::mouseMoveEvent(e);
122  emit mouseHasMoved();
123 }
124 //==========================================
126 //==========================================
128 : QLabel( parent, "toolTipTip",
129  Qt::WStyle_StaysOnTop | Qt::WStyle_Customize |
130  Qt::WStyle_NoBorder | Qt::WStyle_Tool | Qt::WX11BypassWM )
131 {
132  //store slider handle
133  this->slider = slider;
134 
135  //setup lable to use standard black writing on a light yellow background so it
136  //looks like a normal tooltip
137  setPaletteForegroundColor( QColor("Black") );
138  setPaletteBackgroundColor( QColor("LightYellow") );
139 
140  //use default system tooltip font
141  setFont( QToolTip::font() );
142 
143  //setup the otherparmslike a frame etc so it looks like a normal tooltip
144  setMargin(1);
145  //setAutoMask( FALSE ); FIXME probably needs to be replaced
146  setFrameStyle( Q3Frame::Plain | Q3Frame::Box );
147  setLineWidth( 1 );
148  setAlignment( Qt::AlignLeft | Qt::AlignTop );
149  setIndent(0);
150  polish();
151  adjustSize();
152 
153  //show the tooltip when the user presses the slider
154  connect( slider, SIGNAL( sliderPressed() ), this, SLOT( showTooltip() ) );
155 
156  //move tooltip to follow the slider handle
157  setMouseTracking(true);
158  connect( slider, SIGNAL( mouseHasMoved() ), this, SLOT( update() ) );
159 
160  //hide tooltip when users releases the slider
161  connect( slider, SIGNAL( sliderReleased() ), this, SLOT( hideTooltip() ) );
162 }
163 //==========================================
165 {
166  //make sure label is up-to-date
167  update();
168  show();
169 }
170 //==========================================
171 void SliderToolTip::hideTooltip() { hide(); }
172 //==========================================
174 {
175  //margin well provide betweent the slider and the tooltip
176  const int TOOLTIP_MARGIN = 4;
177 
178  //fetch slider handle rect
179  QStyleOption o;
180  o.initFrom(slider);
181  QRect sliderRect = style()->subControlRect(QStyle::CC_Slider, qstyleoption_cast<QStyleOptionSlider*>(&o), QStyle::SC_SliderHandle, this);
182 
183  //determine location tooltip will be shown
184  QPoint tooltipTopLeft;
185  if( slider->orientation() == Qt::Horizontal )
186  {
187  tooltipTopLeft = QPoint( sliderRect.right() + TOOLTIP_MARGIN,
188  slider->getMousePos().y() >= sliderRect.top() ?
189  sliderRect.top() - TOOLTIP_MARGIN - height() :
190  sliderRect.bottom() + TOOLTIP_MARGIN );
191  }
192  else
193  {
194  tooltipTopLeft = QPoint( slider->getMousePos().x() >= sliderRect.right() ?
195  sliderRect.left() - TOOLTIP_MARGIN - width() :
196  sliderRect.right() + TOOLTIP_MARGIN,
197  (sliderRect.top() + sliderRect.bottom())/2 - height()/2 );
198  }
199 
200  //map tooltip position from slider widget to screen coordinates
201  tooltipTopLeft = slider->mapToGlobal( tooltipTopLeft );
202 
203  //position tooltip
204  move( tooltipTopLeft );
205  if( isShown() ) qApp->processEvents();
206 }
207 //==========================================
DynamicSlider * slider
Definition: dynamicSlider.h:81
void setPrefixes(QString prefix1, QString prefix2)
set two prefix values, one for when the value is positive and one for when the value is negative...
QString suffix1
Definition: dynamicSlider.h:60
QString prefix2
Definition: dynamicSlider.h:59
void mouseHasMoved()
private class used by the DynamicSlider to show tooltips. do not use!
Definition: dynamicSlider.h:73
QString zeroString
Definition: dynamicSlider.h:57
QString suffix2
Definition: dynamicSlider.h:60
int width
Definition: blur.cpp:79
QString prefix1
Definition: dynamicSlider.h:59
SliderToolTip(QWidget *parent, DynamicSlider *slider)
QPoint getMousePos()
virtual QString mapValToString()
subclass DynamicSlider and reimplement this method to change the behavior used to display slider valu...
QPoint cachedMousePos
Definition: dynamicSlider.h:63
SliderToolTip * tooltip
Definition: dynamicSlider.h:62
A more dynamic slider that provides moving tooltips that show the slider value.
Definition: dynamicSlider.h:23
void mouseMoveEvent(QMouseEvent *e)
DynamicSlider(Qt::Orientation orientation, QWidget *parent, const char *name=0)
void setZeroString(QString val)
when set, a zero string is shown instead of the current value/prefix/suffix when the slider value is ...
void setSuffix(QString val)
set the suffix that is displayed after the current slider value
void updateTooltipLabel()
void setPrefix(QString val)
set the prefix that is displayed before the current slider value
int height
Definition: blur.cpp:79
void setSuffixes(QString suffix1, QString suffix2)
set two suffix values, one for when the value is positive and one for when the value is negative...