Browse Source

Add ctkRangeSlider::handleToolTip

Julien Finet 13 years ago
parent
commit
32dd74e75a
2 changed files with 76 additions and 2 deletions
  1. 62 1
      Libs/Widgets/ctkRangeSlider.cpp
  2. 14 1
      Libs/Widgets/ctkRangeSlider.h

+ 62 - 1
Libs/Widgets/ctkRangeSlider.cpp

@@ -26,6 +26,7 @@
 #include <QApplication>
 #include <QStylePainter>
 #include <QStyle>
+#include <QToolTip>
 
 // CTK includes
 #include "ctkRangeSlider.h"
@@ -84,12 +85,14 @@ public:
   
   /// Original width between the 2 bounds before any moves
   int m_SubclassWidth;
-  
+
   ctkRangeSliderPrivate::Handles m_SelectedHandles;
 
   /// When symmetricMoves is true, moving a handle will move the other handle
   /// symmetrically, otherwise the handles are independent.
   bool m_SymmetricMoves;
+
+  QString m_HandleToolTip;
 };
 
 // --------------------------------------------------------------------------
@@ -781,3 +784,61 @@ void ctkRangeSlider::initMaximumSliderStyleOption(QStyleOptionSlider* option) co
 {
   this->initStyleOption(option);
 }
+
+// --------------------------------------------------------------------------
+QString ctkRangeSlider::handleToolTip()const
+{
+  Q_D(const ctkRangeSlider);
+  return d->m_HandleToolTip;
+}
+
+// --------------------------------------------------------------------------
+void ctkRangeSlider::setHandleToolTip(const QString& toolTip)
+{
+  Q_D(ctkRangeSlider);
+  d->m_HandleToolTip = toolTip;
+}
+
+// --------------------------------------------------------------------------
+bool ctkRangeSlider::event(QEvent* event)
+{
+  Q_D(ctkRangeSlider);
+  switch(event->type())
+    {
+    case QEvent::ToolTip:
+      {
+      QHelpEvent* helpEvent = static_cast<QHelpEvent*>(event);
+      QStyleOptionSlider opt;
+      // Test the MinimumHandle
+      opt.sliderPosition = d->m_MinimumPosition;
+      opt.sliderValue = d->m_MinimumValue;
+      this->initStyleOption(&opt);
+      QStyle::SubControl hoveredControl =
+        this->style()->hitTestComplexControl(
+          QStyle::CC_Slider, &opt, helpEvent->pos(), this);
+      if (!d->m_HandleToolTip.isEmpty() &&
+          hoveredControl == QStyle::SC_SliderHandle)
+        {
+        QToolTip::showText(helpEvent->globalPos(), d->m_HandleToolTip.arg(this->minimumValue()));
+        event->accept();
+        return true;
+        }
+      // Test the MaximumHandle
+      opt.sliderPosition = d->m_MaximumPosition;
+      opt.sliderValue = d->m_MaximumValue;
+      this->initStyleOption(&opt);
+      hoveredControl = this->style()->hitTestComplexControl(
+        QStyle::CC_Slider, &opt, helpEvent->pos(), this);
+      if (!d->m_HandleToolTip.isEmpty() &&
+          hoveredControl == QStyle::SC_SliderHandle)
+        {
+        QToolTip::showText(helpEvent->globalPos(), d->m_HandleToolTip.arg(this->maximumValue()));
+        event->accept();
+        return true;
+        }
+      }
+    default:
+      break;
+    }
+  return this->Superclass::event(event);
+}

+ 14 - 1
Libs/Widgets/ctkRangeSlider.h

@@ -54,6 +54,7 @@ class CTK_WIDGETS_EXPORT ctkRangeSlider : public QSlider
   Q_PROPERTY(int minimumPosition READ minimumPosition WRITE setMinimumPosition)
   Q_PROPERTY(int maximumPosition READ maximumPosition WRITE setMaximumPosition)
   Q_PROPERTY(bool symmetricMoves READ symmetricMoves WRITE setSymmetricMoves)
+  Q_PROPERTY(QString handleToolTip READ handleToolTip WRITE setHandleToolTip)
 
 public:
   // Superclass typedef
@@ -101,7 +102,15 @@ public:
   /// symmetrically, otherwise the handles are independent. False by default
   bool symmetricMoves()const; 
   void setSymmetricMoves(bool symmetry);
-  
+
+  ///
+  /// Controls the text to display for the handle tooltip. It is in addition
+  /// to the widget tooltip.
+  /// "%1" is replaced by the current value of the slider.
+  /// Empty string (by default) means no tooltip.
+  QString handleToolTip()const;
+  void setHandleToolTip(const QString& toolTip);
+
 signals:
   ///
   /// This signal is emitted when the slider minimum value has changed, 
@@ -182,6 +191,10 @@ protected:
   virtual void initMinimumSliderStyleOption(QStyleOptionSlider* option) const;
   virtual void initMaximumSliderStyleOption(QStyleOptionSlider* option) const;
 
+  // Description:
+  // Reimplemented for the tooltips
+  virtual bool event(QEvent* event);
+
 protected:
   QScopedPointer<ctkRangeSliderPrivate> d_ptr;