Pārlūkot izejas kodu

ctkRange: don't fire useless rangeChanged signal

when the range is not actually changed (new range is equal to old range)
Julien Finet 14 gadi atpakaļ
vecāks
revīzija
2eb1b1c70f

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

@@ -19,6 +19,7 @@ CREATE_TEST_SOURCELIST(Tests ${KIT}CppTests.cxx
   ctkCoordinatesWidgetTest1.cpp
   ctkDirectoryButtonTest1.cpp
   ctkDoubleRangeSliderTest1.cpp
+  ctkDoubleRangeSliderTest2.cpp
   ctkDoubleSliderTest1.cpp
   ctkDynamicSpacerTest1.cpp
   ctkDynamicSpacerTest2.cpp
@@ -98,6 +99,7 @@ SIMPLE_TEST( ctkConsoleWidgetTest1 )
 SIMPLE_TEST( ctkCoordinatesWidgetTest1 )
 SIMPLE_TEST( ctkDirectoryButtonTest1 )
 SIMPLE_TEST( ctkDoubleRangeSliderTest1 )
+SIMPLE_TEST( ctkDoubleRangeSliderTest2 )
 SIMPLE_TEST( ctkDoubleSliderTest1 )
 SIMPLE_TEST( ctkDynamicSpacerTest1 )
 SIMPLE_TEST( ctkDynamicSpacerTest2 )

+ 97 - 0
Libs/Widgets/Testing/Cpp/ctkDoubleRangeSliderTest2.cpp

@@ -0,0 +1,97 @@
+/*=========================================================================
+
+  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 "ctkDoubleRangeSlider.h"
+
+// STD includes
+#include <cstdlib>
+#include <iostream>
+
+
+//-----------------------------------------------------------------------------
+int ctkDoubleRangeSliderTest2(int argc, char * argv [] )
+{
+  QApplication app(argc, argv);
+
+  ctkDoubleRangeSlider slider(Qt::Horizontal);
+
+  QSignalSpy spyRange(&slider, SIGNAL(rangeChanged(double, double)));
+
+  slider.setRange(200., 499.01);
+  
+  if (spyRange.count() != 1 ||
+      spyRange.first().at(0).toDouble() != 200. ||
+      spyRange.first().at(1).toDouble() != 499.01)
+    {
+    std::cerr << "ctkDoubleRangeSlider::setRange() failed: "
+              << spyRange.count() << " min: "
+              << (spyRange.count() ? spyRange.first().at(0).toDouble() : -1.)
+              << " max: "
+              << (spyRange.count() ? spyRange.first().at(1).toDouble() : -1.)
+              << std::endl;
+    return EXIT_FAILURE; 
+    }
+  spyRange.clear();
+  // set again and rangeChanged shouldn't be called 
+  slider.setRange(200., 499.01);
+  slider.setMinimum(200.);
+  slider.setMaximum(499.01);
+
+  if (spyRange.count() != 0)
+    {
+    std::cerr << "ctkDoubleRangeSlider::setRange() fired useless signal: "
+              << spyRange.count() << " min: "
+              << spyRange.first().at(0).toDouble() << " max: "
+              << spyRange.first().at(1).toDouble() << std::endl;
+    return EXIT_FAILURE; 
+    }
+
+  // while it might not change the underline (int) slider, we still need to fire
+  // an event
+  slider.setMaximum(499.00);
+  
+  if (spyRange.count() != 1 ||
+      spyRange.first().at(0).toDouble() != 200. ||
+      spyRange.first().at(1).toDouble() != 499.00)
+    {
+    std::cerr << "ctkDoubleRangeSlider::setMaximum() failed: "
+              << spyRange.count() << " min: "
+              << (spyRange.count() ? spyRange.first().at(0).toDouble() : -1.)
+              << " max: "
+              << (spyRange.count() ? spyRange.first().at(1).toDouble() : -1.)
+              << std::endl;
+    return EXIT_FAILURE; 
+    }
+  
+  slider.show();
+  
+  if (argc < 2 || QString(argv[1]) != "-I" )
+    {
+    QTimer::singleShot(200, &app, SLOT(quit()));
+    }
+
+  return app.exec();
+}

+ 16 - 3
Libs/Widgets/ctkDoubleRangeSlider.cpp

@@ -183,6 +183,7 @@ ctkDoubleRangeSlider::~ctkDoubleRangeSlider()
 void ctkDoubleRangeSlider::setMinimum(double min)
 {
   Q_D(ctkDoubleRangeSlider);
+  double oldMin = d->Minimum;
   d->Minimum = min;
   if (d->Minimum >= d->MinValue)
     {// TBD: use same offset
@@ -195,7 +196,10 @@ void ctkDoubleRangeSlider::setMinimum(double min)
   d->SettingRange = true;
   d->Slider->setMinimum(d->toInt(min));
   d->SettingRange = false;
-  emit this->rangeChanged(d->Minimum, d->Maximum);
+  if (d->Minimum != oldMin)
+    {
+    emit this->rangeChanged(d->Minimum, d->Maximum);
+    }
 }
 
 // --------------------------------------------------------------------------
@@ -209,6 +213,7 @@ double ctkDoubleRangeSlider::minimum()const
 void ctkDoubleRangeSlider::setMaximum(double max)
 {
   Q_D(ctkDoubleRangeSlider);
+  double oldMax = d->Maximum;
   d->Maximum = max;
   if (d->Maximum <= d->MinValue)
     {// TBD: use same offset
@@ -221,7 +226,10 @@ void ctkDoubleRangeSlider::setMaximum(double max)
   d->SettingRange = true;
   d->Slider->setMaximum(d->toInt(max));
   d->SettingRange = false;
-  emit this->rangeChanged(d->Minimum, d->Maximum);
+  if (d->Maximum != oldMax)
+    {
+    emit this->rangeChanged(d->Minimum, d->Maximum);
+    }
 }
 
 // --------------------------------------------------------------------------
@@ -235,6 +243,8 @@ double ctkDoubleRangeSlider::maximum()const
 void ctkDoubleRangeSlider::setRange(double min, double max)
 {
   Q_D(ctkDoubleRangeSlider);
+  double oldMin = d->Minimum;
+  double oldMax = d->Maximum;
   d->Minimum = min;
   d->Maximum = max;
   if (d->Minimum >= d->MinValue)
@@ -256,7 +266,10 @@ void ctkDoubleRangeSlider::setRange(double min, double max)
   d->SettingRange = true;
   d->Slider->setRange(d->toInt(min), d->toInt(max));
   d->SettingRange = false;
-  emit this->rangeChanged(d->Minimum, d->Maximum);
+  if (d->Minimum != oldMin || d->Maximum != oldMax)
+    {
+    emit this->rangeChanged(d->Minimum, d->Maximum);
+    }
 }
 
 // --------------------------------------------------------------------------