浏览代码

BUG:ctkDoubleRangeSlider::setValues was firing signals with a wrong max val

int ctkDoubleRangeSlider, when calling setMinimumValue(...), the new max
value was not set yet, but setMinimumValue() was firing the signal
valuesChanged(newmin, oldMax)... which could be fatal for objects that
listen to it.
Julien Finet 15 年之前
父节点
当前提交
ffff10262b
共有 1 个文件被更改,包括 43 次插入3 次删除
  1. 43 3
      Libs/Widgets/ctkDoubleRangeSlider.cpp

+ 43 - 3
Libs/Widgets/ctkDoubleRangeSlider.cpp

@@ -315,6 +315,7 @@ void ctkDoubleRangeSlider::setMinimumValue(double newMinValue)
     // similar to the old value.
     if (qAbs(newMinValue - oldValue) > (d->SingleStep * 0.000000001))
       {
+      emit this->valuesChanged(newMinValue, this->maximumValue());
       emit this->minimumValueChanged(newMinValue);
       }
     }
@@ -352,16 +353,55 @@ void ctkDoubleRangeSlider::setMaximumValue(double newMaxValue)
     // similar to the old value.
     if (qAbs(newMaxValue - oldValue) > (d->SingleStep * 0.000000001))
       {
+      emit this->valuesChanged(this->minimumValue(), newMaxValue);
       emit this->maximumValueChanged(newMaxValue);
       }
     }
 }
 
 // --------------------------------------------------------------------------
-void ctkDoubleRangeSlider::setValues(double newMinValue, double newMaxValue)
+void ctkDoubleRangeSlider::setValues(double newMinVal, double newMaxVal)
 {
-  this->setMinimumValue(qMin(newMinValue, newMaxValue));
-  this->setMaximumValue(qMax(newMinValue, newMaxValue));
+  CTK_D(ctkDoubleRangeSlider);
+  // We can't call setMinimumValue() and setMaximumValue() as they would
+  // generate an inconsistent state. when minimumValueChanged() is fired the
+  // new max value wouldn't be updated yet.
+  double newMinValue = qMin(newMinVal, newMaxVal);
+  double newMaxValue = qMax(newMinVal, newMaxVal);
+  d->updateMinOffset(newMinValue);
+  d->updateMaxOffset(newMaxValue);
+  int newMinIntValue = d->toInt(newMinValue);
+  int newMaxIntValue = d->toInt(newMaxValue);
+  if (newMinIntValue != d->Slider->minimumValue() ||
+      newMaxIntValue != d->Slider->maximumValue())
+    {
+    // d->Slider will emit a maximumValueChanged signal that is connected to
+    // ctkDoubleSlider::onValueChanged
+    d->Slider->setValues(newMinIntValue, newMaxIntValue);
+    }
+  else
+    {
+    double oldMinValue = d->MinValue;
+    double oldMaxValue = d->MaxValue;
+    d->MinValue = newMinValue;
+    d->MaxValue = newMaxValue;
+    // don't emit a valuechanged signal if the new value is quite
+    // similar to the old value.
+    bool minChanged = qAbs(newMinValue - oldMinValue) > (d->SingleStep * 0.000000001);
+    bool maxChanged = qAbs(newMaxValue - oldMaxValue) > (d->SingleStep * 0.000000001);
+    if (minChanged || maxChanged)
+      {
+      emit this->valuesChanged(newMinValue, newMaxValue);
+      if (minChanged)
+        {
+        emit this->minimumValueChanged(newMinValue);
+        }
+      if (maxChanged)
+        {
+        emit this->maximumValueChanged(newMaxValue);
+        }
+      }
+    }
 }
 
 // --------------------------------------------------------------------------