Ver código fonte

Discrepancy between the slider and the spinbox values

When setting the range, the value can be changed, but because the range was
valid in the spinbox and not in the slider, the value was bounded in one
case and not the other.
Julien Finet 14 anos atrás
pai
commit
65dce25633

+ 2 - 0
Libs/Widgets/Testing/Cpp/CMakeLists.txt

@@ -52,6 +52,7 @@ SET(TEST_SOURCES
   ctkSettingsTest1.cpp
   ctkSettingsDialogTest1.cpp
   ctkSliderWidgetTest1.cpp
+  ctkSliderWidgetTest2.cpp
   ctkToolTipTrapperTest1.cpp
   ctkTreeComboBoxTest1.cpp
   ctkWorkflowWidgetTest1.cpp
@@ -166,6 +167,7 @@ SIMPLE_TEST( ctkSettingsPanelTest1 )
 SIMPLE_TEST( ctkSettingsPanelTest2 )
 SIMPLE_TEST( ctkSettingsTest1 )
 SIMPLE_TEST( ctkSliderWidgetTest1 )
+SIMPLE_TEST( ctkSliderWidgetTest2 )
 SIMPLE_TEST( ctkToolTipTrapperTest1 )
 SIMPLE_TEST( ctkTreeComboBoxTest1 )
 SIMPLE_TEST( ctkWorkflowWidgetTest1 )

+ 68 - 0
Libs/Widgets/Testing/Cpp/ctkSliderWidgetTest2.cpp

@@ -0,0 +1,68 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.commontk.org/LICENSE
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+// Qt includes
+#include <QApplication>
+#include <QSignalSpy>
+#include <QTimer>
+
+// CTK includes
+#include "ctkSliderWidget.h"
+
+// STD includes
+#include <cstdlib>
+#include <iostream>
+
+//-----------------------------------------------------------------------------
+int ctkSliderWidgetTest2(int argc, char * argv [] )
+{
+  QApplication app(argc, argv);
+
+  ctkSliderWidget sliderSpinBox;
+
+  QSignalSpy spy(&sliderSpinBox, SIGNAL(valueChanged(double)));
+
+  QObject::connect(&sliderSpinBox, SIGNAL(valueChanged(double)),
+                   &sliderSpinBox, SLOT(setValue(double)));
+
+  sliderSpinBox.setMinimum(150);
+
+  if (spy.count() != 1)
+    {
+    std::cout << "ctkSliderWidget::valueChanged not fired: " << spy.count();
+    return EXIT_FAILURE;
+    }
+  spy.clear();
+  sliderSpinBox.setMaximum(1500);
+
+  if (spy.count() != 0)
+    {
+    std::cout << "ctkSliderWidget::valueChanged was fired: " << spy.count();
+    return EXIT_FAILURE;
+    }
+
+  sliderSpinBox.show();
+  if (argc < 2 || QString(argv[1]) != "-I" )
+    {
+    QTimer::singleShot(100, &app, SLOT(quit()));
+    }
+  return app.exec();
+}
+

+ 9 - 0
Libs/Widgets/ctkSliderWidget.cpp

@@ -155,7 +155,10 @@ double ctkSliderWidget::maximum()const
 void ctkSliderWidget::setMinimum(double min)
 {
   Q_D(ctkSliderWidget);
+  bool wasBlocked = d->SpinBox->blockSignals(true);
   d->SpinBox->setMinimum(min);
+  d->SpinBox->blockSignals(wasBlocked);
+
   // SpinBox can truncate min (depending on decimals).
   // use Spinbox's min to set Slider's min
   d->Slider->setMinimum(d->SpinBox->minimum());
@@ -167,7 +170,10 @@ void ctkSliderWidget::setMinimum(double min)
 void ctkSliderWidget::setMaximum(double max)
 {
   Q_D(ctkSliderWidget);
+  bool wasBlocked = d->SpinBox->blockSignals(true);
   d->SpinBox->setMaximum(max);
+  d->SpinBox->blockSignals(wasBlocked);
+
   // SpinBox can truncate max (depending on decimals).
   // use Spinbox's max to set Slider's max
   d->Slider->setMaximum(d->SpinBox->maximum());
@@ -180,7 +186,10 @@ void ctkSliderWidget::setRange(double min, double max)
 {
   Q_D(ctkSliderWidget);
   
+  bool wasBlocked = d->SpinBox->blockSignals(true);
   d->SpinBox->setRange(min, max);
+  d->SpinBox->blockSignals(wasBlocked);
+  
   // SpinBox can truncate the range (depending on decimals).
   // use Spinbox's range to set Slider's range
   d->Slider->setRange(d->SpinBox->minimum(), d->SpinBox->maximum());