<commit_before>/*=========================================================================

  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.apache.org/licenses/LICENSE-2.0.txt

  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 <QDebug>
#include <QMouseEvent>
#include <QPointer>

// CTK includes
#include "ctkRangeWidget.h"
#include "ctkValueProxy.h"
#include "ui_ctkRangeWidget.h"

// STD includes
#include <cmath>
#include <limits>

//-----------------------------------------------------------------------------
class ctkRangeWidgetPrivate: public Ui_ctkRangeWidget
{
  Q_DECLARE_PUBLIC(ctkRangeWidget);
protected:
  ctkRangeWidget* const q_ptr;
public:
  ctkRangeWidgetPrivate(ctkRangeWidget& object);
  void connectSlider();

  void updateSpinBoxWidth();
  int synchronizedSpinBoxWidth()const;
  void synchronizeSiblingSpinBox(int newWidth);
  bool equal(double v1, double v2)const;
  void relayout();
  bool useCustomSpinBoxesLimits()const;

  bool          Tracking;
  bool          Changing;
  bool          SettingSliderRange;
  bool          BlockSliderUpdate;
  double        MinimumValueBeforeChange;
  double        MaximumValueBeforeChange;
  bool          AutoSpinBoxWidth;
  double        CustomSpinBoxesLimitsMin;
  double        CustomSpinBoxesLimitsMax;
  Qt::Alignment SpinBoxAlignment;
  QPointer<ctkValueProxy> Proxy;
};

// --------------------------------------------------------------------------
bool ctkRangeWidgetPrivate::equal(double v1, double v2)const
{
  if (v1 == v2)
    {// don't bother computing difference as it could fail for infinity numbers
    return true;
    }
  if (v1 != v1 && v2 != v2)
    {// NaN check
    return true;
    }
  return qAbs(v1 - v2) < pow(10., -this->MinimumSpinBox->decimals());
}

// --------------------------------------------------------------------------
ctkRangeWidgetPrivate::ctkRangeWidgetPrivate(ctkRangeWidget& object)
  :q_ptr(&object)
{
  this->Tracking = true;
  this->Changing = false;
  this->SettingSliderRange = false;
  this->BlockSliderUpdate = false;
  this->MinimumValueBeforeChange = 0.;
  this->MaximumValueBeforeChange = 0.;
  this->AutoSpinBoxWidth = true;
  this->CustomSpinBoxesLimitsMin = std::numeric_limits<double>::max();
  this->CustomSpinBoxesLimitsMax = std::numeric_limits<double>::min();
  this->SpinBoxAlignment = Qt::AlignVCenter;
}

// --------------------------------------------------------------------------
void ctkRangeWidgetPrivate::connectSlider()
{
  Q_Q(ctkRangeWidget);
  QObject::connect(this->Slider, SIGNAL(valuesChanged(double,double)),
                   q, SLOT(changeValues(double,double)));
  QObject::connect(this->Slider, SIGNAL(minimumValueChanged(double)),
                   q, SLOT(changeMinimumValue(double)));
  QObject::connect(this->Slider, SIGNAL(maximumValueChanged(double)),
                   q, SLOT(changeMaximumValue(double)));

  QObject::connect(this->MinimumSpinBox, SIGNAL(valueChanged(double)),
                   q, SLOT(setSliderValues()));
  QObject::connect(this->MaximumSpinBox, SIGNAL(valueChanged(double)),
                   q, SLOT(setSliderValues()));
  QObject::connect(this->MinimumSpinBox, SIGNAL(valueChanged(double)),
                   q, SLOT(setMinimumToMaximumSpinBox(double)));
  QObject::connect(this->MaximumSpinBox, SIGNAL(valueChanged(double)),
                   q, SLOT(setMaximumToMinimumSpinBox(double)));
  QObject::connect(this->MinimumSpinBox, SIGNAL(decimalsChanged(int)),
                   q, SLOT(setDecimals(int)));
  QObject::connect(this->MaximumSpinBox, SIGNAL(decimalsChanged(int)),
                   q, SLOT(setDecimals(int)));

  QObject::connect(this->Slider, SIGNAL(sliderPressed()),
                   q, SLOT(startChanging()));
  QObject::connect(this->Slider, SIGNAL(sliderReleased()),
                   q, SLOT(stopChanging()));
  QObject::connect(this->Slider, SIGNAL(rangeChanged(double,double)),
                   q, SLOT(onSliderRangeChanged(double,double)));
}

// --------------------------------------------------------------------------
void ctkRangeWidgetPrivate::updateSpinBoxWidth()
{
  int spinBoxWidth = this->synchronizedSpinBoxWidth();
  if (this->AutoSpinBoxWidth)
    {
    this->MinimumSpinBox->setMinimumWidth(spinBoxWidth);
    this->MaximumSpinBox->setMinimumWidth(spinBoxWidth);
    }
  else
    {
    this->MinimumSpinBox->setMinimumWidth(0);
    this->MaximumSpinBox->setMinimumWidth(0);
    }
  this->synchronizeSiblingSpinBox(spinBoxWidth);
}

// --------------------------------------------------------------------------
int ctkRangeWidgetPrivate::synchronizedSpinBoxWidth()const
{
  Q_Q(const ctkRangeWidget);
  //Q_ASSERT(this->MinimumSpinBox->sizeHint() == this->MaximumSpinBox->sizeHint());
  int maxWidth = qMax(this->MinimumSpinBox->sizeHint().width(),
                      this->MaximumSpinBox->sizeHint().width());
  if (!q->parent())
    {
    return maxWidth;
    }
  QList<ctkRangeWidget*> siblings =
    q->parent()->findChildren<ctkRangeWidget*>();
  foreach(ctkRangeWidget* sibling, siblings)
    {
    maxWidth = qMax(maxWidth, qMax(sibling->d_func()->MaximumSpinBox->sizeHint().width(),
                                   sibling->d_func()->MaximumSpinBox->sizeHint().width()));
    }
  return maxWidth;
}

// --------------------------------------------------------------------------
void ctkRangeWidgetPrivate::synchronizeSiblingSpinBox(int width)
{
  Q_Q(const ctkRangeWidget);
  QList<ctkRangeWidget*> siblings =
    q->parent()->findChildren<ctkRangeWidget*>();
  foreach(ctkRangeWidget* sibling, siblings)
    {
    if (sibling != q && sibling->isAutoSpinBoxWidth())
      {
      sibling->d_func()->MinimumSpinBox->setMinimumWidth(width);
      sibling->d_func()->MaximumSpinBox->setMinimumWidth(width);
      }
    }
}

// --------------------------------------------------------------------------
void ctkRangeWidgetPrivate::relayout()
{
  this->GridLayout->removeWidget(this->MinimumSpinBox);
  this->GridLayout->removeWidget(this->MaximumSpinBox);
  this->GridLayout->removeWidget(this->Slider);
  if (this->SpinBoxAlignment & Qt::AlignTop)
    {
    this->GridLayout->addWidget(this->MinimumSpinBox,0,0);
    this->GridLayout->addWidget(this->MaximumSpinBox,0,2);
    this->GridLayout->addWidget(this->Slider,1,0,1,3);
    }
  else if (this->SpinBoxAlignment & Qt::AlignBottom)
    {
    this->GridLayout->addWidget(this->MinimumSpinBox,1,0);
    this->GridLayout->addWidget(this->MaximumSpinBox,1,2);
    this->GridLayout->addWidget(this->Slider,0, 0, 1, 3);
    }
  else if (this->SpinBoxAlignment & Qt::AlignRight)
    {
    this->GridLayout->addWidget(this->Slider, 0, 0);
    this->GridLayout->addWidget(this->MinimumSpinBox,0,1);
    this->GridLayout->addWidget(this->MaximumSpinBox,0,2);
    }
  else if (this->SpinBoxAlignment & Qt::AlignLeft)
    {
    this->GridLayout->addWidget(this->MinimumSpinBox,0,0);
    this->GridLayout->addWidget(this->MaximumSpinBox,0,1);
    this->GridLayout->addWidget(this->Slider, 0, 2);
    }
  else // Qt::AlignVCenter (or any other bad alignment)
    {
    this->GridLayout->addWidget(this->MinimumSpinBox,0,0);
    this->GridLayout->addWidget(this->Slider,0,1);
    this->GridLayout->addWidget(this->MaximumSpinBox,0,2);
    }
}

// --------------------------------------------------------------------------
bool ctkRangeWidgetPrivate::useCustomSpinBoxesLimits()const
{
  return this->CustomSpinBoxesLimitsMin <= this->CustomSpinBoxesLimitsMax;
}

// --------------------------------------------------------------------------
ctkRangeWidget::ctkRangeWidget(QWidget* _parent) : Superclass(_parent)
  , d_ptr(new ctkRangeWidgetPrivate(*this))
{
  Q_D(ctkRangeWidget);
  
  d->setupUi(this);

  if (d->useCustomSpinBoxesLimits())
    {
    d->MinimumSpinBox->setRange(d->CustomSpinBoxesLimitsMin, d->Slider->maximum());
    d->MaximumSpinBox->setRange(d->Slider->minimum(), d->CustomSpinBoxesLimitsMax);
    }
  else
    {
    d->MinimumSpinBox->setRange(d->Slider->minimum(), d->Slider->maximum());
    d->MaximumSpinBox->setRange(d->Slider->minimum(), d->Slider->maximum());
    }
  d->MinimumSpinBox->setValue(d->Slider->minimumValue());
  d->MaximumSpinBox->setValue(d->Slider->maximumValue());
  
  d->connectSlider();

  d->MinimumSpinBox->installEventFilter(this);
  d->MaximumSpinBox->installEventFilter(this);
}

// --------------------------------------------------------------------------
ctkRangeWidget::~ctkRangeWidget()
{
}

// --------------------------------------------------------------------------
double ctkRangeWidget::minimum()const
{
  Q_D(const ctkRangeWidget);
  if (!d->useCustomSpinBoxesLimits())
    {
    Q_ASSERT(d->equal(d->MinimumSpinBox->minimum(),d->Slider->minimum()));
    }
  return d->Slider->minimum();
}

// --------------------------------------------------------------------------
double ctkRangeWidget::maximum()const
{
  Q_D(const ctkRangeWidget);
  if (!d->useCustomSpinBoxesLimits())
    {
    Q_ASSERT(d->equal(d->MaximumSpinBox->maximum(), d->Slider->maximum()));
    }
  return d->Slider->maximum();
}

// --------------------------------------------------------------------------
void ctkRangeWidget::setMinimum(double min)
{
  Q_D(ctkRangeWidget);
  bool blocked = d->MinimumSpinBox->blockSignals(true);
  blocked = d->MaximumSpinBox->blockSignals(true);
  if (d->useCustomSpinBoxesLimits())
    {
    d->MinimumSpinBox->setMinimum(d->CustomSpinBoxesLimitsMin);
    }
  else
    {
    d->MinimumSpinBox->setMinimum(min);
    }
  d->MaximumSpinBox->setMinimum(min);
  d->MinimumSpinBox->blockSignals(blocked);
  d->MaximumSpinBox->blockSignals(blocked);
  // SpinBox can truncate min (depending on decimals).
  // use Spinbox's min to set Slider's min
  d->SettingSliderRange = true;
  d->Slider->setMinimum(d->MaximumSpinBox->minimum());
  d->SettingSliderRange = false;
  if (!d->useCustomSpinBoxesLimits())
    {
    Q_ASSERT(d->equal(d->MinimumSpinBox->minimum(),d->Slider->minimum()));
    }
  d->updateSpinBoxWidth();
}

// --------------------------------------------------------------------------
void ctkRangeWidget::setMaximum(double max)
{
  Q_D(ctkRangeWidget);
  bool blocked = d->MinimumSpinBox->blockSignals(true);
  blocked = d->MaximumSpinBox->blockSignals(true);
  if (d->useCustomSpinBoxesLimits())
    {
    d->MaximumSpinBox->setMaximum(d->CustomSpinBoxesLimitsMax);
    }
  else
    {
    d->MaximumSpinBox->setMaximum(max);
    }
  d->MinimumSpinBox->setMaximum(max);
  d->MinimumSpinBox->blockSignals(blocked);
  d->MaximumSpinBox->blockSignals(blocked);
  // SpinBox can truncate max (depending on decimals).
  // use Spinbox's max to set Slider's max
  d->SettingSliderRange = true;
  d->Slider->setMaximum(d->MinimumSpinBox->maximum());
  d->SettingSliderRange = false;
  if (!d->useCustomSpinBoxesLimits())
    {
    Q_ASSERT(d->equal(d->MaximumSpinBox->maximum(), d->Slider->maximum()));
    }
  d->updateSpinBoxWidth();
}

// --------------------------------------------------------------------------
void ctkRangeWidget::setRange(double min, double max)
{
  Q_D(ctkRangeWidget);

  if (d->useCustomSpinBoxesLimits())
    {
    min = qBound(d->CustomSpinBoxesLimitsMin, min, d->CustomSpinBoxesLimitsMax);
    max = qBound(d->CustomSpinBoxesLimitsMin, max, d->CustomSpinBoxesLimitsMax);
    }

  double oldMin = d->MinimumSpinBox->minimum();
  double oldMax = d->MaximumSpinBox->maximum();
  bool blocked = d->MinimumSpinBox->blockSignals(true);
  if (d->useCustomSpinBoxesLimits())
    {
    d->MinimumSpinBox->setRange(d->CustomSpinBoxesLimitsMin, qMax(min,max));
    }
  else
    {
    d->MinimumSpinBox->setRange(qMin(min,max), qMax(min,max));
    }
  d->MinimumSpinBox->blockSignals(blocked);
  blocked = d->MaximumSpinBox->blockSignals(true);
  if (d->useCustomSpinBoxesLimits())
    {
    d->MaximumSpinBox->setRange(qMin(min,max), d->CustomSpinBoxesLimitsMax);
    }
  else
    {
    d->MaximumSpinBox->setRange(qMin(min,max), qMax(min,max));
    }
  d->MaximumSpinBox->blockSignals(blocked);
  // SpinBox can truncate the range (depending on decimals).
  // use Spinbox's range to set Slider's range
  d->SettingSliderRange = true;
  d->Slider->setRange(d->MaximumSpinBox->minimum(), d->MinimumSpinBox->maximum());
  d->SettingSliderRange = false;
  if (!d->useCustomSpinBoxesLimits())
    {
    Q_ASSERT(d->equal(d->MinimumSpinBox->minimum(), d->Slider->minimum()));
    Q_ASSERT(d->equal(d->MaximumSpinBox->maximum(), d->Slider->maximum()));
    Q_ASSERT(d->equal(d->Slider->minimumValue(), d->MinimumSpinBox->value()));
    Q_ASSERT(d->equal(d->Slider->maximumValue(), d->MaximumSpinBox->value()));
    }
  d->updateSpinBoxWidth();
  if (!d->useCustomSpinBoxesLimits())<commit_msg>BUG: Fix crash in Debug replacing Q_ASSERT by qWarning in ctkRangeWidget::setRange (#948)<commit_after>/*=========================================================================

  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.apache.org/licenses/LICENSE-2.0.txt

  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 <QDebug>
#include <QMouseEvent>
#include <QPointer>

// CTK includes
#include "ctkRangeWidget.h"
#include "ctkValueProxy.h"
#include "ui_ctkRangeWidget.h"

// STD includes
#include <cmath>
#include <limits>

//-----------------------------------------------------------------------------
class ctkRangeWidgetPrivate: public Ui_ctkRangeWidget
{
  Q_DECLARE_PUBLIC(ctkRangeWidget);
protected:
  ctkRangeWidget* const q_ptr;
public:
  ctkRangeWidgetPrivate(ctkRangeWidget& object);
  void connectSlider();

  void updateSpinBoxWidth();
  int synchronizedSpinBoxWidth()const;
  void synchronizeSiblingSpinBox(int newWidth);
  bool equal(double v1, double v2)const;
  void relayout();
  bool useCustomSpinBoxesLimits()const;

  bool          Tracking;
  bool          Changing;
  bool          SettingSliderRange;
  bool          BlockSliderUpdate;
  double        MinimumValueBeforeChange;
  double        MaximumValueBeforeChange;
  bool          AutoSpinBoxWidth;
  double        CustomSpinBoxesLimitsMin;
  double        CustomSpinBoxesLimitsMax;
  Qt::Alignment SpinBoxAlignment;
  QPointer<ctkValueProxy> Proxy;
};

// --------------------------------------------------------------------------
bool ctkRangeWidgetPrivate::equal(double v1, double v2)const
{
  if (v1 == v2)
    {// don't bother computing difference as it could fail for infinity numbers
    return true;
    }
  if (v1 != v1 && v2 != v2)
    {// NaN check
    return true;
    }
  return qAbs(v1 - v2) < pow(10., -this->MinimumSpinBox->decimals());
}

// --------------------------------------------------------------------------
ctkRangeWidgetPrivate::ctkRangeWidgetPrivate(ctkRangeWidget& object)
  :q_ptr(&object)
{
  this->Tracking = true;
  this->Changing = false;
  this->SettingSliderRange = false;
  this->BlockSliderUpdate = false;
  this->MinimumValueBeforeChange = 0.;
  this->MaximumValueBeforeChange = 0.;
  this->AutoSpinBoxWidth = true;
  this->CustomSpinBoxesLimitsMin = std::numeric_limits<double>::max();
  this->CustomSpinBoxesLimitsMax = std::numeric_limits<double>::min();
  this->SpinBoxAlignment = Qt::AlignVCenter;
}

// --------------------------------------------------------------------------
void ctkRangeWidgetPrivate::connectSlider()
{
  Q_Q(ctkRangeWidget);
  QObject::connect(this->Slider, SIGNAL(valuesChanged(double,double)),
                   q, SLOT(changeValues(double,double)));
  QObject::connect(this->Slider, SIGNAL(minimumValueChanged(double)),
                   q, SLOT(changeMinimumValue(double)));
  QObject::connect(this->Slider, SIGNAL(maximumValueChanged(double)),
                   q, SLOT(changeMaximumValue(double)));

  QObject::connect(this->MinimumSpinBox, SIGNAL(valueChanged(double)),
                   q, SLOT(setSliderValues()));
  QObject::connect(this->MaximumSpinBox, SIGNAL(valueChanged(double)),
                   q, SLOT(setSliderValues()));
  QObject::connect(this->MinimumSpinBox, SIGNAL(valueChanged(double)),
                   q, SLOT(setMinimumToMaximumSpinBox(double)));
  QObject::connect(this->MaximumSpinBox, SIGNAL(valueChanged(double)),
                   q, SLOT(setMaximumToMinimumSpinBox(double)));
  QObject::connect(this->MinimumSpinBox, SIGNAL(decimalsChanged(int)),
                   q, SLOT(setDecimals(int)));
  QObject::connect(this->MaximumSpinBox, SIGNAL(decimalsChanged(int)),
                   q, SLOT(setDecimals(int)));

  QObject::connect(this->Slider, SIGNAL(sliderPressed()),
                   q, SLOT(startChanging()));
  QObject::connect(this->Slider, SIGNAL(sliderReleased()),
                   q, SLOT(stopChanging()));
  QObject::connect(this->Slider, SIGNAL(rangeChanged(double,double)),
                   q, SLOT(onSliderRangeChanged(double,double)));
}

// --------------------------------------------------------------------------
void ctkRangeWidgetPrivate::updateSpinBoxWidth()
{
  int spinBoxWidth = this->synchronizedSpinBoxWidth();
  if (this->AutoSpinBoxWidth)
    {
    this->MinimumSpinBox->setMinimumWidth(spinBoxWidth);
    this->MaximumSpinBox->setMinimumWidth(spinBoxWidth);
    }
  else
    {
    this->MinimumSpinBox->setMinimumWidth(0);
    this->MaximumSpinBox->setMinimumWidth(0);
    }
  this->synchronizeSiblingSpinBox(spinBoxWidth);
}

// --------------------------------------------------------------------------
int ctkRangeWidgetPrivate::synchronizedSpinBoxWidth()const
{
  Q_Q(const ctkRangeWidget);
  //Q_ASSERT(this->MinimumSpinBox->sizeHint() == this->MaximumSpinBox->sizeHint());
  int maxWidth = qMax(this->MinimumSpinBox->sizeHint().width(),
                      this->MaximumSpinBox->sizeHint().width());
  if (!q->parent())
    {
    return maxWidth;
    }
  QList<ctkRangeWidget*> siblings =
    q->parent()->findChildren<ctkRangeWidget*>();
  foreach(ctkRangeWidget* sibling, siblings)
    {
    maxWidth = qMax(maxWidth, qMax(sibling->d_func()->MaximumSpinBox->sizeHint().width(),
                                   sibling->d_func()->MaximumSpinBox->sizeHint().width()));
    }
  return maxWidth;
}

// --------------------------------------------------------------------------
void ctkRangeWidgetPrivate::synchronizeSiblingSpinBox(int width)
{
  Q_Q(const ctkRangeWidget);
  QList<ctkRangeWidget*> siblings =
    q->parent()->findChildren<ctkRangeWidget*>();
  foreach(ctkRangeWidget* sibling, siblings)
    {
    if (sibling != q && sibling->isAutoSpinBoxWidth())
      {
      sibling->d_func()->MinimumSpinBox->setMinimumWidth(width);
      sibling->d_func()->MaximumSpinBox->setMinimumWidth(width);
      }
    }
}

// --------------------------------------------------------------------------
void ctkRangeWidgetPrivate::relayout()
{
  this->GridLayout->removeWidget(this->MinimumSpinBox);
  this->GridLayout->removeWidget(this->MaximumSpinBox);
  this->GridLayout->removeWidget(this->Slider);
  if (this->SpinBoxAlignment & Qt::AlignTop)
    {
    this->GridLayout->addWidget(this->MinimumSpinBox,0,0);
    this->GridLayout->addWidget(this->MaximumSpinBox,0,2);
    this->GridLayout->addWidget(this->Slider,1,0,1,3);
    }
  else if (this->SpinBoxAlignment & Qt::AlignBottom)
    {
    this->GridLayout->addWidget(this->MinimumSpinBox,1,0);
    this->GridLayout->addWidget(this->MaximumSpinBox,1,2);
    this->GridLayout->addWidget(this->Slider,0, 0, 1, 3);
    }
  else if (this->SpinBoxAlignment & Qt::AlignRight)
    {
    this->GridLayout->addWidget(this->Slider, 0, 0);
    this->GridLayout->addWidget(this->MinimumSpinBox,0,1);
    this->GridLayout->addWidget(this->MaximumSpinBox,0,2);
    }
  else if (this->SpinBoxAlignment & Qt::AlignLeft)
    {
    this->GridLayout->addWidget(this->MinimumSpinBox,0,0);
    this->GridLayout->addWidget(this->MaximumSpinBox,0,1);
    this->GridLayout->addWidget(this->Slider, 0, 2);
    }
  else // Qt::AlignVCenter (or any other bad alignment)
    {
    this->GridLayout->addWidget(this->MinimumSpinBox,0,0);
    this->GridLayout->addWidget(this->Slider,0,1);
    this->GridLayout->addWidget(this->MaximumSpinBox,0,2);
    }
}

// --------------------------------------------------------------------------
bool ctkRangeWidgetPrivate::useCustomSpinBoxesLimits()const
{
  return this->CustomSpinBoxesLimitsMin <= this->CustomSpinBoxesLimitsMax;
}

// --------------------------------------------------------------------------
ctkRangeWidget::ctkRangeWidget(QWidget* _parent) : Superclass(_parent)
  , d_ptr(new ctkRangeWidgetPrivate(*this))
{
  Q_D(ctkRangeWidget);
  
  d->setupUi(this);

  if (d->useCustomSpinBoxesLimits())
    {
    d->MinimumSpinBox->setRange(d->CustomSpinBoxesLimitsMin, d->Slider->maximum());
    d->MaximumSpinBox->setRange(d->Slider->minimum(), d->CustomSpinBoxesLimitsMax);
    }
  else
    {
    d->MinimumSpinBox->setRange(d->Slider->minimum(), d->Slider->maximum());
    d->MaximumSpinBox->setRange(d->Slider->minimum(), d->Slider->maximum());
    }
  d->MinimumSpinBox->setValue(d->Slider->minimumValue());
  d->MaximumSpinBox->setValue(d->Slider->maximumValue());
  
  d->connectSlider();

  d->MinimumSpinBox->installEventFilter(this);
  d->MaximumSpinBox->installEventFilter(this);
}

// --------------------------------------------------------------------------
ctkRangeWidget::~ctkRangeWidget()
{
}

// --------------------------------------------------------------------------
double ctkRangeWidget::minimum()const
{
  Q_D(const ctkRangeWidget);
  if (!d->useCustomSpinBoxesLimits())
    {
    Q_ASSERT(d->equal(d->MinimumSpinBox->minimum(),d->Slider->minimum()));
    }
  return d->Slider->minimum();
}

// --------------------------------------------------------------------------
double ctkRangeWidget::maximum()const
{
  Q_D(const ctkRangeWidget);
  if (!d->useCustomSpinBoxesLimits())
    {
    Q_ASSERT(d->equal(d->MaximumSpinBox->maximum(), d->Slider->maximum()));
    }
  return d->Slider->maximum();
}

// --------------------------------------------------------------------------
void ctkRangeWidget::setMinimum(double min)
{
  Q_D(ctkRangeWidget);
  bool blocked = d->MinimumSpinBox->blockSignals(true);
  blocked = d->MaximumSpinBox->blockSignals(true);
  if (d->useCustomSpinBoxesLimits())
    {
    d->MinimumSpinBox->setMinimum(d->CustomSpinBoxesLimitsMin);
    }
  else
    {
    d->MinimumSpinBox->setMinimum(min);
    }
  d->MaximumSpinBox->setMinimum(min);
  d->MinimumSpinBox->blockSignals(blocked);
  d->MaximumSpinBox->blockSignals(blocked);
  // SpinBox can truncate min (depending on decimals).
  // use Spinbox's min to set Slider's min
  d->SettingSliderRange = true;
  d->Slider->setMinimum(d->MaximumSpinBox->minimum());
  d->SettingSliderRange = false;
  if (!d->useCustomSpinBoxesLimits())
    {
    Q_ASSERT(d->equal(d->MinimumSpinBox->minimum(),d->Slider->minimum()));
    }
  d->updateSpinBoxWidth();
}

// --------------------------------------------------------------------------
void ctkRangeWidget::setMaximum(double max)
{
  Q_D(ctkRangeWidget);
  bool blocked = d->MinimumSpinBox->blockSignals(true);
  blocked = d->MaximumSpinBox->blockSignals(true);
  if (d->useCustomSpinBoxesLimits())
    {
    d->MaximumSpinBox->setMaximum(d->CustomSpinBoxesLimitsMax);
    }
  else
    {
    d->MaximumSpinBox->setMaximum(max);
    }
  d->MinimumSpinBox->setMaximum(max);
  d->MinimumSpinBox->blockSignals(blocked);
  d->MaximumSpinBox->blockSignals(blocked);
  // SpinBox can truncate max (depending on decimals).
  // use Spinbox's max to set Slider's max
  d->SettingSliderRange = true;
  d->Slider->setMaximum(d->MinimumSpinBox->maximum());
  d->SettingSliderRange = false;
  if (!d->useCustomSpinBoxesLimits())
    {
    Q_ASSERT(d->equal(d->MaximumSpinBox->maximum(), d->Slider->maximum()));
    }
  d->updateSpinBoxWidth();
}

// --------------------------------------------------------------------------
void ctkRangeWidget::setRange(double min, double max)
{
  Q_D(ctkRangeWidget);

  if (d->useCustomSpinBoxesLimits())
    {
    min = qBound(d->CustomSpinBoxesLimitsMin, min, d->CustomSpinBoxesLimitsMax);
    max = qBound(d->CustomSpinBoxesLimitsMin, max, d->CustomSpinBoxesLimitsMax);
    }

  double oldMin = d->MinimumSpinBox->minimum();
  double oldMax = d->MaximumSpinBox->maximum();
  bool blocked = d->MinimumSpinBox->blockSignals(true);
  if (d->useCustomSpinBoxesLimits())
    {
    d->MinimumSpinBox->setRange(d->CustomSpinBoxesLimitsMin, qMax(min,max));
    }
  else
    {
    d->MinimumSpinBox->setRange(qMin(min,max), qMax(min,max));
    }
  d->MinimumSpinBox->blockSignals(blocked);
  blocked = d->MaximumSpinBox->blockSignals(true);
  if (d->useCustomSpinBoxesLimits())
    {
    d->MaximumSpinBox->setRange(qMin(min,max), d->CustomSpinBoxesLimitsMax);
    }
  else
    {
    d->MaximumSpinBox->setRange(qMin(min,max), qMax(min,max));
    }
  d->MaximumSpinBox->blockSignals(blocked);
  // SpinBox can truncate the range (depending on decimals).
  // use Spinbox's range to set Slider's range
  d->SettingSliderRange = true;
  d->Slider->setRange(d->MaximumSpinBox->minimum(), d->MinimumSpinBox->maximum());
  d->SettingSliderRange = false;
  if (!d->useCustomSpinBoxesLimits())
    {
    if (!(d->equal(d->MinimumSpinBox->minimum(), d->Slider->minimum())) ||
        !(d->equal(d->MaximumSpinBox->maximum(), d->Slider->maximum())) ||
        !(d->equal(d->Slider->minimumValue(), d->MinimumSpinBox->value())) ||
        !(d->equal(d->Slider->maximumValue(), d->MaximumSpinBox->value())))
      {
      qWarning("ctkRangeWidget::setRange : slider and spinbox are not synchronized");
      }
    }
  d->updateSpinBoxWidth();
  if (!d->useCustomSpinBoxesLimits())
{'fraction_non_alphanumeric': 0.23694688603410713, 'fraction_numerical': 0.004319735557058072, 'mean_word_length': 2.813090804926955, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 611, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7125865', 'n_tokens_mistral': 8444, 'n_tokens_neox': 7875, 'n_words': 1176}