瀏覽代碼

Fix slider unit tests with tiny single steps

Such single steps should not be allowed as they would prevent the original
double value from being retrieved with an integer.
Julien Finet 11 年之前
父節點
當前提交
4b4e82098b

+ 16 - 0
Libs/Widgets/ctkDoubleRangeSlider.cpp

@@ -467,6 +467,12 @@ double ctkDoubleRangeSlider::singleStep()const
 void ctkDoubleRangeSlider::setSingleStep(double newStep)
 {
   Q_D(ctkDoubleRangeSlider);
+  if (!this->isValidStep(newStep))
+    {
+    qWarning() << "Single step " << newStep << "is out of bounds.";
+    return;
+    }
+
   d->SingleStep = newStep;
   // The following can fire A LOT of signals that shouldn't be 
   // fired.
@@ -488,6 +494,16 @@ void ctkDoubleRangeSlider::setSingleStep(double newStep)
 }
 
 // --------------------------------------------------------------------------
+bool ctkDoubleRangeSlider::isValidStep(double step)const
+{
+  const double minStep( qMax(this->maximum() / std::numeric_limits<int>::max(),
+                                   std::numeric_limits<double>::epsilon()) );
+  const double maxStep( qMin(this->maximum() - this->minimum(),
+                                   static_cast<double>(std::numeric_limits<int>::max())) );
+  return step > minStep && step < maxStep;
+}
+
+// --------------------------------------------------------------------------
 double ctkDoubleRangeSlider::tickInterval()const
 {
   Q_D(const ctkDoubleRangeSlider);

+ 9 - 1
Libs/Widgets/ctkDoubleRangeSlider.h

@@ -71,9 +71,17 @@ public:
   /// This property holds the single step.
   /// The smaller of two natural steps that an abstract sliders provides and
   /// typically corresponds to the user pressing an arrow key
+  /// \sa isValidStep()
   void setSingleStep(double ss);
   double singleStep()const;
-  
+
+  /// This utility function checks whether singleStep is
+  /// valid or not. To be valid, single step should not be too
+  /// small or too large. The singleStep property is used to convert
+  /// the slider value from int to double, therefore the boundary.
+  /// \sa singleStep
+  bool isValidStep(double singleStep)const;
+
   /// 
   /// This property holds the interval between tickmarks.
   /// This is a value interval, not a pixel interval. If it is 0, the slider

+ 15 - 0
Libs/Widgets/ctkDoubleSlider.cpp

@@ -286,6 +286,11 @@ double ctkDoubleSlider::singleStep()const
 void ctkDoubleSlider::setSingleStep(double newStep)
 {
   Q_D(ctkDoubleSlider);
+  if (!this->isValidStep(newStep))
+    {
+    qWarning() << "Single step " << newStep << "is out of bounds.";
+    return;
+    }
   d->SingleStep = newStep;
   d->updateOffset(d->Value);
   // update the new values of the QSlider
@@ -298,6 +303,16 @@ void ctkDoubleSlider::setSingleStep(double newStep)
 }
 
 // --------------------------------------------------------------------------
+bool ctkDoubleSlider::isValidStep(double step)const
+{
+  const double minStep( qMax(this->maximum() / std::numeric_limits<int>::max(),
+                                   std::numeric_limits<double>::epsilon()) );
+  const double maxStep( qMin(this->maximum() - this->minimum(),
+                                   static_cast<double>(std::numeric_limits<int>::max())) );
+  return step > minStep && step < maxStep;
+}
+
+// --------------------------------------------------------------------------
 double ctkDoubleSlider::pageStep()const
 {
   Q_D(const ctkDoubleSlider);

+ 8 - 0
Libs/Widgets/ctkDoubleSlider.h

@@ -103,9 +103,17 @@ public:
   /// The smaller of two natural steps that an abstract sliders provides and
   /// typically corresponds to the user pressing an arrow key
   /// Default value is 1.
+  /// \sa isValidStep
   void setSingleStep(double step);
   double singleStep()const;
 
+  /// This utility function checks whether singleStep is
+  /// valid or not. To be valid, single step should not be too
+  /// small or too large. The singleStep property is used to convert
+  /// the slider value from int to double, therefore the boundary.
+  /// \sa singleStep
+  bool isValidStep(double singleStep)const;
+
   /// 
   /// This property holds the page step.
   /// The larger of two natural steps that an abstract slider provides and

+ 2 - 10
Libs/Widgets/ctkRangeWidget.cpp

@@ -596,21 +596,13 @@ double ctkRangeWidget::singleStep()const
 void ctkRangeWidget::setSingleStep(double step)
 {
   Q_D(ctkRangeWidget);
-  const double minSingleStep( qMax(this->maximum() / std::numeric_limits<int>::max(),
-                                   std::numeric_limits<double>::epsilon()) );
-  const double maxSingleStep( qMin(this->maximum() - this->minimum(),
-                                   static_cast<double>(std::numeric_limits<int>::max())) );
-  if (step < minSingleStep || step > maxSingleStep)
+  if (!d->Slider->isValidStep(step))
     {
-    qWarning() << "ctkRangeWidget single step is out of bounds";
+    qWarning() << "Single step" << step << "is out of bounds.";
     return;
     }
-  qDebug() << "setSingleStep" << step;
-  qDebug() << "min slider: " << d->Slider->minimumValue() << "max slider" << d->Slider->maximumValue();
-  qDebug() << "min spinbox: " << d->MinimumSpinBox->value() << "max spinBox" << d->MaximumSpinBox->value();
   d->MinimumSpinBox->setSingleStep(step);
   d->MaximumSpinBox->setSingleStep(step);
-  qDebug() << "min single step: " << d->MinimumSpinBox->singleStep();
   d->Slider->setSingleStep(d->MinimumSpinBox->singleStep());
   Q_ASSERT(d->equal(d->Slider->singleStep(), d->MinimumSpinBox->singleStep()));
   Q_ASSERT(d->equal(d->Slider->singleStep(), d->MaximumSpinBox->singleStep()));

+ 7 - 2
Libs/Widgets/ctkSliderWidget.cpp

@@ -399,10 +399,15 @@ double ctkSliderWidget::singleStep()const
 }
 
 // --------------------------------------------------------------------------
-void ctkSliderWidget::setSingleStep(double step)
+void ctkSliderWidget::setSingleStep(double newStep)
 {
   Q_D(ctkSliderWidget);
-  d->SpinBox->setSingleStep(step);
+  if (!d->Slider->isValidStep(newStep))
+    {
+    qWarning() << "Single step " << newStep << "is out of bounds.";
+    return;
+    }
+  d->SpinBox->setSingleStep(newStep);
   d->Slider->setSingleStep(d->SpinBox->singleStep());
   Q_ASSERT(d->equal(d->SpinBox->minimum(),d->Slider->minimum()));
   Q_ASSERT(d->equal(d->SpinBox->value(),d->Slider->value()));