瀏覽代碼

Fix ctkSliderWidget and ctkRangeRidget valueChanged signal

The issue was only when slider tracking is false.

Thanks to magro11 for the bug report & fix
Closes #444
Julien Finet 11 年之前
父節點
當前提交
97805108e4

+ 25 - 0
Libs/Widgets/Testing/Cpp/ctkSliderWidgetTest.cpp

@@ -52,6 +52,9 @@ private slots:
   /// value of the spinbox.
   void testDecimalsByShortcuts();
 
+  /// This test makes sure a valueChanged signal is fired when moving
+  /// the handle to 0 for the first time.
+  void testValueChangedWithNoTracking();
 };
 
 // ----------------------------------------------------------------------------
@@ -172,6 +175,28 @@ void ctkSliderWidgetTester::testDecimalsByShortcuts()
 }
 
 // ----------------------------------------------------------------------------
+void ctkSliderWidgetTester::testValueChangedWithNoTracking()
+{
+  ctkSliderWidget slider;
+  slider.setValue((slider.maximum() + slider.minimum()) / 2);
+
+  slider.show();
+  QTest::qWaitForWindowShown(&slider);
+
+  slider.setTracking(false);
+  QSignalSpy spy(&slider, SIGNAL(valueChanged(double)));
+
+  QPoint currentPoint = slider.slider()->rect().center();
+  QPoint nextPoint = QPoint(slider.slider()->rect().left(), currentPoint.y());
+
+  QTest::mousePress(slider.slider()->slider(), Qt::LeftButton, 0, currentPoint);
+  ctkTest::mouseMove(slider.slider()->slider(), Qt::LeftButton, 0, nextPoint);
+  QTest::mouseRelease(slider.slider()->slider(), Qt::LeftButton, 0, nextPoint);
+
+  QCOMPARE(spy.count(), 1);
+}
+
+// ----------------------------------------------------------------------------
 CTK_TEST_MAIN(ctkSliderWidgetTest)
 #include "moc_ctkSliderWidgetTest.cpp"
 

+ 7 - 0
Libs/Widgets/ctkDoubleSlider.cpp

@@ -592,6 +592,13 @@ bool ctkDoubleSlider::eventFilter(QObject* watched, QEvent* event)
   return this->Superclass::eventFilter(watched, event);
 }
 
+// --------------------------------------------------------------------------
+QSlider* ctkDoubleSlider::slider()const
+{
+  Q_D(const ctkDoubleSlider);
+  return d->Slider;
+}
+
 //----------------------------------------------------------------------------
 void ctkDoubleSlider::setValueProxy(ctkValueProxy* proxy)
 {

+ 5 - 0
Libs/Widgets/ctkDoubleSlider.h

@@ -198,6 +198,11 @@ public:
   /// Reimplemented for internal reasons (handle tooltip).
   virtual bool eventFilter(QObject*, QEvent*);
 
+  /// Return a pointer to the QSlider used internally.
+  /// Use with caution.
+  /// \sa QSlider
+  QSlider* slider()const;
+
   /// Install or remove a value proxy filter. The value proxy decouples the
   /// displayed value from the value retrieved by the value property.
   /// For example, the value proxy can allow one to display celsius in the

+ 1 - 1
Libs/Widgets/ctkRangeWidget.cpp

@@ -497,9 +497,9 @@ void ctkRangeWidget::startChanging()
     {
     return;
     }
-  d->Changing = true;
   d->MinimumValueBeforeChange = this->minimumValue();
   d->MaximumValueBeforeChange = this->maximumValue();
+  d->Changing = true;
 }
 
 // --------------------------------------------------------------------------

+ 1 - 1
Libs/Widgets/ctkSliderWidget.cpp

@@ -321,8 +321,8 @@ void ctkSliderWidget::startChanging()
     {
     return;
     }
-  d->Changing = true;
   d->ValueBeforeChange = this->value();
+  d->Changing = true;
 }
 
 // --------------------------------------------------------------------------