Forráskód Böngészése

Merge branch 'slider-handle-tooltip'

* slider-handle-tooltip:
  Add ctkDoubleSlider::handleToolTip
Julien Finet 13 éve
szülő
commit
93c5fbac1c
2 módosított fájl, 76 hozzáadás és 2 törlés
  1. 64 2
      Libs/Widgets/ctkDoubleSlider.cpp
  2. 12 0
      Libs/Widgets/ctkDoubleSlider.h

+ 64 - 2
Libs/Widgets/ctkDoubleSlider.cpp

@@ -21,6 +21,10 @@
 // QT includes
 #include <QDebug>
 #include <QHBoxLayout>
+#include <QHelpEvent>
+#include <QStyle>
+#include <QStyleOptionSlider>
+#include <QToolTip>
 
 // CTK includes
 #include "ctkDoubleSlider.h"
@@ -29,6 +33,19 @@
 #include <limits>
 
 //-----------------------------------------------------------------------------
+class ctkSlider: public QSlider
+{
+public:
+  ctkSlider(QWidget* parent);
+  using QSlider::initStyleOption;
+};
+
+//-----------------------------------------------------------------------------
+ctkSlider::ctkSlider(QWidget* parent): QSlider(parent)
+{
+}
+
+//-----------------------------------------------------------------------------
 class ctkDoubleSliderPrivate
 {
   Q_DECLARE_PUBLIC(ctkDoubleSlider);
@@ -42,7 +59,8 @@ public:
   void init();
   void updateOffset(double value);
 
-  QSlider*    Slider;
+  ctkSlider*    Slider;
+  QString       HandleToolTip;
   double      Minimum;
   double      Maximum;
   bool        SettingRange;
@@ -71,7 +89,8 @@ ctkDoubleSliderPrivate::ctkDoubleSliderPrivate(ctkDoubleSlider& object)
 void ctkDoubleSliderPrivate::init()
 {
   Q_Q(ctkDoubleSlider);
-  this->Slider = new QSlider(q);
+  this->Slider = new ctkSlider(q);
+  this->Slider->installEventFilter(q);
   QHBoxLayout* l = new QHBoxLayout(q);
   l->addWidget(this->Slider);
   l->setContentsMargins(0,0,0,0);
@@ -354,6 +373,20 @@ void ctkDoubleSlider::setOrientation(Qt::Orientation newOrientation)
 }
 
 // --------------------------------------------------------------------------
+QString ctkDoubleSlider::handleToolTip()const
+{
+  Q_D(const ctkDoubleSlider);
+  return d->HandleToolTip;
+}
+
+// --------------------------------------------------------------------------
+void ctkDoubleSlider::setHandleToolTip(const QString& toolTip)
+{
+  Q_D(ctkDoubleSlider);
+  d->HandleToolTip = toolTip;
+}
+
+// --------------------------------------------------------------------------
 void ctkDoubleSlider::onValueChanged(int newValue)
 {
   Q_D(ctkDoubleSlider);
@@ -388,4 +421,33 @@ void ctkDoubleSlider::onRangeChanged(int min, int max)
     }
 }
 
+// --------------------------------------------------------------------------
+bool ctkDoubleSlider::eventFilter(QObject* watched, QEvent* event)
+{
+  Q_D(ctkDoubleSlider);
+  if (watched == d->Slider)
+    {
+    switch(event->type())
+      {
+      case QEvent::ToolTip:
+        {
+        QHelpEvent* helpEvent = static_cast<QHelpEvent*>(event);
+        QStyleOptionSlider opt;
+        d->Slider->initStyleOption(&opt);
+        QStyle::SubControl hoveredControl =
+          d->Slider->style()->hitTestComplexControl(
+            QStyle::CC_Slider, &opt, helpEvent->pos(), this);
+        if (!d->HandleToolTip.isEmpty() &&
+            hoveredControl == QStyle::SC_SliderHandle)
+          {
+          QToolTip::showText(helpEvent->globalPos(), d->HandleToolTip.arg(this->value()));
+          event->accept();
+          return true;
+          }
+        }
+      }
+    }
+  return this->Superclass::eventFilter(watched, event);
+}
+
 

+ 12 - 0
Libs/Widgets/ctkDoubleSlider.h

@@ -51,6 +51,7 @@ class CTK_WIDGETS_EXPORT ctkDoubleSlider : public QWidget
   Q_PROPERTY(double tickInterval READ tickInterval WRITE setTickInterval)
   Q_PROPERTY(bool tracking READ hasTracking WRITE setTracking)
   Q_PROPERTY(Qt::Orientation orientation READ orientation WRITE setOrientation)
+  Q_PROPERTY(QString handleToolTip READ handleToolTip WRITE setHandleToolTip)
 
 public:
   /// Superclass typedef
@@ -143,6 +144,17 @@ public:
   /// The orientation must be Qt::Vertical (the default) or Qt::Horizontal.
   Qt::Orientation orientation()const;
 
+  ///
+  /// 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);
+
+  /// Reimplemented for internal reasons (handle tooltip).
+  virtual bool eventFilter(QObject*, QEvent*);
+
 public slots:
   /// 
   /// This property holds the slider's current value.