123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658 |
- /*=========================================================================
- 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 <QHBoxLayout>
- // CTK includes
- #include "ctkRangeSlider.h"
- #include "ctkDoubleRangeSlider.h"
- // STD includes
- #include <limits>
- //-----------------------------------------------------------------------------
- class ctkDoubleRangeSliderPrivate
- {
- Q_DECLARE_PUBLIC(ctkDoubleRangeSlider);
- protected:
- ctkDoubleRangeSlider* const q_ptr;
- public:
- ctkDoubleRangeSliderPrivate(ctkDoubleRangeSlider& object);
-
- int toInt(double _value)const;
- double minFromInt(int _value)const;
- double maxFromInt(int _value)const;
- double safeMinFromInt(int _value)const;
- double safeMaxFromInt(int _value)const;
-
- void init();
- void connectSlider();
- void updateMinOffset(double value);
- void updateMaxOffset(double value);
- ctkRangeSlider* Slider;
- double Minimum;
- double Maximum;
- bool SettingRange;
- // we should have a MinValueOffset and MinPositionOffset (and MinimumOffset?)
- double MinOffset;
- // we should have a MaxValueOffset and MaxPositionOffset (and MaximumOffset?)
- double MaxOffset;
- double SingleStep;
- double MinValue;
- double MaxValue;
- };
- // --------------------------------------------------------------------------
- ctkDoubleRangeSliderPrivate::ctkDoubleRangeSliderPrivate(ctkDoubleRangeSlider& object)
- :q_ptr(&object)
- {
- // the initial values will be overwritten in
- // ctkDoubleRangeSliderPrivate::init()
- this->Slider = 0;
- this->Minimum = 0.;
- this->Maximum = 99.;
- this->SettingRange = false;
- this->MinOffset = 0.;
- this->MaxOffset = 0.;
- this->SingleStep = 1.;
- this->MinValue = 0.;
- this->MaxValue = 99.;
- }
-
- // --------------------------------------------------------------------------
- void ctkDoubleRangeSliderPrivate::init()
- {
- Q_Q(ctkDoubleRangeSlider);
- this->Slider = new ctkRangeSlider(q);
- QHBoxLayout* l = new QHBoxLayout(q);
- l->addWidget(this->Slider);
- l->setContentsMargins(0,0,0,0);
-
- this->Minimum = this->Slider->minimum();
- this->Maximum = this->Slider->maximum();
- this->MinValue = this->Slider->minimumValue();
- this->MaxValue = this->Slider->maximumValue();
- this->SingleStep = this->Slider->singleStep();
- q->setSizePolicy(this->Slider->sizePolicy());
- q->setAttribute(Qt::WA_WState_OwnSizePolicy, false);
- this->connectSlider();
- }
- // --------------------------------------------------------------------------
- void ctkDoubleRangeSliderPrivate::connectSlider()
- {
- Q_Q(ctkDoubleRangeSlider);
- q->connect(this->Slider, SIGNAL(valuesChanged(int,int)),
- q, SLOT(onValuesChanged(int,int)));
- q->connect(this->Slider, SIGNAL(minimumPositionChanged(int)),
- q, SLOT(onMinPosChanged(int)));
- q->connect(this->Slider, SIGNAL(maximumPositionChanged(int)),
- q, SLOT(onMaxPosChanged(int)));
- q->connect(this->Slider, SIGNAL(positionsChanged(int,int)),
- q, SLOT(onPositionsChanged(int,int)));
- q->connect(this->Slider, SIGNAL(sliderPressed()),
- q, SIGNAL(sliderPressed()));
- q->connect(this->Slider, SIGNAL(sliderReleased()),
- q, SIGNAL(sliderReleased()));
- q->connect(this->Slider, SIGNAL(rangeChanged(int,int)),
- q, SLOT(onRangeChanged(int,int)));
- }
- // --------------------------------------------------------------------------
- int ctkDoubleRangeSliderPrivate::toInt(double doubleValue)const
- {
- double tmp = doubleValue / this->SingleStep;
- static const double minInt = std::numeric_limits<int>::min();
- static const double maxInt = std::numeric_limits<int>::max();
- #ifndef QT_NO_DEBUG
- if (tmp < minInt || tmp > maxInt)
- {
- qWarning() << __FUNCTION__ << ": value " << doubleValue
- << " for singleStep " << this->SingleStep
- << " is out of integer bounds !";
- }
- #endif
- tmp = qBound(minInt, tmp, maxInt);
- int intValue = qRound(tmp);
- //qDebug() << __FUNCTION__ << doubleValue << tmp << intValue;
- return intValue;
- }
- // --------------------------------------------------------------------------
- double ctkDoubleRangeSliderPrivate::minFromInt(int intValue)const
- {
- double doubleValue = this->SingleStep * (this->MinOffset + intValue) ;
- return doubleValue;
- }
- // --------------------------------------------------------------------------
- double ctkDoubleRangeSliderPrivate::maxFromInt(int intValue)const
- {
- double doubleValue = this->SingleStep * (this->MaxOffset + intValue) ;
- return doubleValue;
- }
- // --------------------------------------------------------------------------
- double ctkDoubleRangeSliderPrivate::safeMinFromInt(int intValue)const
- {
- return qBound(this->Minimum, this->minFromInt(intValue), this->Maximum);
- }
- // --------------------------------------------------------------------------
- double ctkDoubleRangeSliderPrivate::safeMaxFromInt(int intValue)const
- {
- return qBound(this->Minimum, this->maxFromInt(intValue), this->Maximum);
- }
- // --------------------------------------------------------------------------
- void ctkDoubleRangeSliderPrivate::updateMinOffset(double value)
- {
- this->MinOffset = (value / this->SingleStep) - this->toInt(value);
- }
- // --------------------------------------------------------------------------
- void ctkDoubleRangeSliderPrivate::updateMaxOffset(double value)
- {
- this->MaxOffset = (value / this->SingleStep) - this->toInt(value);
- }
- // --------------------------------------------------------------------------
- ctkDoubleRangeSlider::ctkDoubleRangeSlider(QWidget* _parent) : Superclass(_parent)
- , d_ptr(new ctkDoubleRangeSliderPrivate(*this))
- {
- Q_D(ctkDoubleRangeSlider);
- d->init();
- }
- // --------------------------------------------------------------------------
- ctkDoubleRangeSlider::ctkDoubleRangeSlider(Qt::Orientation _orientation, QWidget* _parent)
- : Superclass(_parent)
- , d_ptr(new ctkDoubleRangeSliderPrivate(*this))
- {
- Q_D(ctkDoubleRangeSlider);
- d->init();
- this->setOrientation(_orientation);
- }
- // --------------------------------------------------------------------------
- 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
- d->updateMinOffset(d->Minimum);
- }
- if (d->Minimum >= d->MaxValue)
- {// TBD: use same offset
- d->updateMaxOffset(d->Minimum);
- }
- d->SettingRange = true;
- d->Slider->setMinimum(d->toInt(min));
- d->SettingRange = false;
- if (d->Minimum != oldMin)
- {
- emit this->rangeChanged(d->Minimum, d->Maximum);
- }
- }
- // --------------------------------------------------------------------------
- double ctkDoubleRangeSlider::minimum()const
- {
- Q_D(const ctkDoubleRangeSlider);
- return d->Minimum;
- }
- // --------------------------------------------------------------------------
- void ctkDoubleRangeSlider::setMaximum(double max)
- {
- Q_D(ctkDoubleRangeSlider);
- double oldMax = d->Maximum;
- d->Maximum = max;
- if (d->Maximum <= d->MinValue)
- {// TBD: use same offset
- d->updateMinOffset(d->Maximum);
- }
- if (d->Maximum <= d->MaxValue)
- {// TBD: use same offset ?
- d->updateMaxOffset(d->Maximum);
- }
- d->SettingRange = true;
- d->Slider->setMaximum(d->toInt(max));
- d->SettingRange = false;
- if (d->Maximum != oldMax)
- {
- emit this->rangeChanged(d->Minimum, d->Maximum);
- }
- }
- // --------------------------------------------------------------------------
- double ctkDoubleRangeSlider::maximum()const
- {
- Q_D(const ctkDoubleRangeSlider);
- return d->Maximum;
- }
- // --------------------------------------------------------------------------
- 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)
- {// TBD: use same offset
- d->updateMinOffset(d->Minimum);
- }
- if (d->Minimum >= d->MaxValue)
- {// TBD: use same offset
- d->updateMaxOffset(d->Minimum);
- }
- if (d->Maximum <= d->MinValue)
- {// TBD: use same offset
- d->updateMinOffset(d->Maximum);
- }
- if (d->Maximum <= d->MaxValue)
- {// TBD: use same offset ?
- d->updateMaxOffset(d->Maximum);
- }
- d->SettingRange = true;
- d->Slider->setRange(d->toInt(min), d->toInt(max));
- d->SettingRange = false;
- if (d->Minimum != oldMin || d->Maximum != oldMax)
- {
- emit this->rangeChanged(d->Minimum, d->Maximum);
- }
- }
- // --------------------------------------------------------------------------
- double ctkDoubleRangeSlider::minimumPosition()const
- {
- Q_D(const ctkDoubleRangeSlider);
- return d->safeMinFromInt(d->Slider->minimumPosition());
- }
- // --------------------------------------------------------------------------
- void ctkDoubleRangeSlider::setMinimumPosition(double minPos)
- {
- Q_D(ctkDoubleRangeSlider);
- d->Slider->setMinimumPosition(d->toInt(minPos));
- }
- // --------------------------------------------------------------------------
- double ctkDoubleRangeSlider::maximumPosition()const
- {
- Q_D(const ctkDoubleRangeSlider);
- return d->safeMaxFromInt(d->Slider->maximumPosition());
- }
- // --------------------------------------------------------------------------
- void ctkDoubleRangeSlider::setMaximumPosition(double maxPos)
- {
- Q_D(ctkDoubleRangeSlider);
- d->Slider->setMaximumPosition(d->toInt(maxPos));
- }
- // --------------------------------------------------------------------------
- void ctkDoubleRangeSlider::setPositions(double minPos, double maxPos)
- {
- Q_D(ctkDoubleRangeSlider);
- d->Slider->setPositions(d->toInt(minPos), d->toInt(maxPos));
- }
- // --------------------------------------------------------------------------
- double ctkDoubleRangeSlider::minimumValue()const
- {
- Q_D(const ctkDoubleRangeSlider);
- return d->MinValue;
- }
- // --------------------------------------------------------------------------
- void ctkDoubleRangeSlider::setMinimumValue(double newMinValue)
- {
- Q_D(ctkDoubleRangeSlider);
- newMinValue = qBound(d->Minimum, newMinValue, d->Maximum);
- d->updateMinOffset(newMinValue);
- if (newMinValue >= d->MaxValue)
- {
- d->updateMaxOffset(newMinValue);
- }
- int newIntValue = d->toInt(newMinValue);
- if (newIntValue != d->Slider->minimumValue())
- {
- // d->Slider will emit a minimumValueChanged signal that is connected to
- // ctkDoubleSlider::onValueChanged
- d->Slider->setMinimumValue(newIntValue);
- }
- else
- {
- double oldValue = d->MinValue;
- d->MinValue = newMinValue;
- // don't emit a valuechanged signal if the new value is quite
- // similar to the old value.
- if (qAbs(newMinValue - oldValue) > (d->SingleStep * 0.000000001))
- {
- emit this->valuesChanged(newMinValue, this->maximumValue());
- emit this->minimumValueChanged(newMinValue);
- }
- }
- }
- // --------------------------------------------------------------------------
- double ctkDoubleRangeSlider::maximumValue()const
- {
- Q_D(const ctkDoubleRangeSlider);
- return d->MaxValue;
- }
- // --------------------------------------------------------------------------
- void ctkDoubleRangeSlider::setMaximumValue(double newMaxValue)
- {
- Q_D(ctkDoubleRangeSlider);
- newMaxValue = qBound(d->Minimum, newMaxValue, d->Maximum);
- d->updateMaxOffset(newMaxValue);
- if (newMaxValue <= d->MinValue)
- {
- d->updateMinOffset(newMaxValue);
- }
- int newIntValue = d->toInt(newMaxValue);
- if (newIntValue != d->Slider->maximumValue())
- {
- // d->Slider will emit a maximumValueChanged signal that is connected to
- // ctkDoubleSlider::onValueChanged
- d->Slider->setMaximumValue(newIntValue);
- }
- else
- {
- double oldValue = d->MaxValue;
- d->MaxValue = newMaxValue;
- // don't emit a valuechanged signal if the new value is quite
- // 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 newMinVal, double newMaxVal)
- {
- Q_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 = qBound(d->Minimum, qMin(newMinVal, newMaxVal), d->Maximum);
- double newMaxValue = qBound(d->Minimum, qMax(newMinVal, newMaxVal), d->Maximum);
- 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);
- }
- }
- }
- }
- // --------------------------------------------------------------------------
- double ctkDoubleRangeSlider::singleStep()const
- {
- Q_D(const ctkDoubleRangeSlider);
- return d->SingleStep;
- }
- // --------------------------------------------------------------------------
- void ctkDoubleRangeSlider::setSingleStep(double newStep)
- {
- Q_D(ctkDoubleRangeSlider);
- d->SingleStep = newStep;
- // The following can fire A LOT of signals that shouldn't be
- // fired.
- bool oldBlockSignals = this->blockSignals(true);
- d->updateMinOffset(d->MinValue);
- d->updateMaxOffset(d->MaxValue);
- // update the new values of the ctkRangeSlider
- double _minvalue = d->MinValue;
- double _maxvalue = d->MaxValue;
- // calling setMinimum or setMaximum can change the values MinimumValue
- // and MaximumValue, this is why we re-set them later.
- this->setMinimum(d->Minimum);
- this->setMaximum(d->Maximum);
- this->setMinimumValue(_minvalue);
- this->setMinimumPosition(_minvalue);
- this->setMaximumValue(_maxvalue);
- this->setMaximumPosition(_maxvalue);
- this->blockSignals(oldBlockSignals);
- }
- // --------------------------------------------------------------------------
- double ctkDoubleRangeSlider::tickInterval()const
- {
- Q_D(const ctkDoubleRangeSlider);
- return d->SingleStep * d->Slider->tickInterval();
- }
- // --------------------------------------------------------------------------
- void ctkDoubleRangeSlider::setTickInterval(double newTickInterval)
- {
- Q_D(ctkDoubleRangeSlider);
- d->Slider->setTickInterval(d->toInt(newTickInterval));
- }
- // --------------------------------------------------------------------------
- QSlider::TickPosition ctkDoubleRangeSlider::tickPosition()const
- {
- Q_D(const ctkDoubleRangeSlider);
- return d->Slider->tickPosition();
- }
- // --------------------------------------------------------------------------
- void ctkDoubleRangeSlider::setTickPosition(QSlider::TickPosition newTickPosition)
- {
- Q_D(ctkDoubleRangeSlider);
- d->Slider->setTickPosition(newTickPosition);
- }
- // --------------------------------------------------------------------------
- bool ctkDoubleRangeSlider::hasTracking()const
- {
- Q_D(const ctkDoubleRangeSlider);
- return d->Slider->hasTracking();
- }
- // --------------------------------------------------------------------------
- void ctkDoubleRangeSlider::setTracking(bool enable)
- {
- Q_D(ctkDoubleRangeSlider);
- d->Slider->setTracking(enable);
- }
- // --------------------------------------------------------------------------
- void ctkDoubleRangeSlider::triggerAction( QAbstractSlider::SliderAction action)
- {
- Q_D(ctkDoubleRangeSlider);
- d->Slider->triggerAction(action);
- }
- // --------------------------------------------------------------------------
- void ctkDoubleRangeSlider::setOrientation(Qt::Orientation newOrientation)
- {
- Q_D(ctkDoubleRangeSlider);
- if (this->orientation() == newOrientation)
- {
- return;
- }
- if (!testAttribute(Qt::WA_WState_OwnSizePolicy))
- {
- QSizePolicy sp = this->sizePolicy();
- sp.transpose();
- this->setSizePolicy(sp);
- this->setAttribute(Qt::WA_WState_OwnSizePolicy, false);
- }
- // d->Slider will take care of calling updateGeometry
- d->Slider->setOrientation(newOrientation);
- }
- // --------------------------------------------------------------------------
- Qt::Orientation ctkDoubleRangeSlider::orientation()const
- {
- Q_D(const ctkDoubleRangeSlider);
- return d->Slider->orientation();
- }
- // --------------------------------------------------------------------------
- bool ctkDoubleRangeSlider::symmetricMoves()const
- {
- Q_D(const ctkDoubleRangeSlider);
- return d->Slider->symmetricMoves();
- }
- // --------------------------------------------------------------------------
- void ctkDoubleRangeSlider::setSymmetricMoves(bool symmetry)
- {
- Q_D(ctkDoubleRangeSlider);
- d->Slider->setSymmetricMoves(symmetry);
- }
- // --------------------------------------------------------------------------
- void ctkDoubleRangeSlider::onValuesChanged(int newMinValue, int newMaxValue)
- {
- Q_D(ctkDoubleRangeSlider);
- double doubleNewMinValue = d->safeMinFromInt(newMinValue);
- double doubleNewMaxValue = d->safeMaxFromInt(newMaxValue);
- bool emitMinValueChanged = (d->MinValue != doubleNewMinValue);
- bool emitMaxValueChanged = (d->MaxValue != doubleNewMaxValue);
- if (!emitMinValueChanged && !emitMaxValueChanged)
- {
- return;
- }
- d->MinValue = doubleNewMinValue;
- d->MaxValue = doubleNewMaxValue;
- emit this->valuesChanged(d->MinValue, d->MaxValue);
- if (emitMinValueChanged)
- {
- emit this->minimumValueChanged(d->MinValue);
- }
- if (emitMaxValueChanged)
- {
- emit this->maximumValueChanged(d->MaxValue);
- }
- }
- // --------------------------------------------------------------------------
- void ctkDoubleRangeSlider::onMinPosChanged(int newPosition)
- {
- Q_D(const ctkDoubleRangeSlider);
- emit this->minimumPositionChanged(d->safeMinFromInt(newPosition));
- }
- // --------------------------------------------------------------------------
- void ctkDoubleRangeSlider::onMaxPosChanged(int newPosition)
- {
- Q_D(const ctkDoubleRangeSlider);
- emit this->maximumPositionChanged(d->safeMaxFromInt(newPosition));
- }
- // --------------------------------------------------------------------------
- void ctkDoubleRangeSlider::onPositionsChanged(int min, int max)
- {
- Q_D(const ctkDoubleRangeSlider);
- emit this->positionsChanged(d->safeMinFromInt(min), d->safeMaxFromInt(max));
- }
- // --------------------------------------------------------------------------
- void ctkDoubleRangeSlider::onRangeChanged(int min, int max)
- {
- Q_D(const ctkDoubleRangeSlider);
- if (!d->SettingRange)
- {
- this->setRange(d->minFromInt(min), d->maxFromInt(max));
- }
- }
- // --------------------------------------------------------------------------
- ctkRangeSlider* ctkDoubleRangeSlider::slider()const
- {
- Q_D(const ctkDoubleRangeSlider);
- return d->Slider;
- }
- // --------------------------------------------------------------------------
- void ctkDoubleRangeSlider::setSlider(ctkRangeSlider* slider)
- {
- Q_D(ctkDoubleRangeSlider);
- slider->setOrientation(d->Slider->orientation());
- slider->setMinimum(d->Slider->minimum());
- slider->setMaximum(d->Slider->maximum());
- slider->setValues(d->Slider->minimumValue(), d->Slider->maximumValue());
- slider->setSingleStep(d->Slider->singleStep());
- slider->setTracking(d->Slider->hasTracking());
- slider->setTickInterval(d->Slider->tickInterval());
- slider->setTickPosition(d->Slider->tickPosition());
- delete d->Slider;
- qobject_cast<QHBoxLayout*>(this->layout())->addWidget(slider);
- d->Slider = slider;
- d->connectSlider();
- }
|