From b9e08aeb3a5d8f69cc62c2e137da5f278c898e4e Mon Sep 17 00:00:00 2001 From: Andras Lasso Date: Wed, 18 Sep 2024 00:17:52 -0400 Subject: [PATCH] ENH: Add method to check if double-spinbox value is being edited New method is added to ctkDoubleSpinBox and ctkSliderWidget to check if a value is currently being set using the spinbox. This is useful because when there are many sibling widgets then many signals may be emitted and it may be difficult to determine which widget was the source of all changes. This feature was needed for fixing https://github.com/Slicer/Slicer/issues/7574 --- Libs/Widgets/ctkDoubleSpinBox.cpp | 7 +++++++ Libs/Widgets/ctkDoubleSpinBox.h | 11 +++++++++++ Libs/Widgets/ctkDoubleSpinBox_p.h | 2 ++ Libs/Widgets/ctkSliderWidget.cpp | 7 +++++++ Libs/Widgets/ctkSliderWidget.h | 3 +++ 5 files changed, 30 insertions(+) diff --git a/Libs/Widgets/ctkDoubleSpinBox.cpp b/Libs/Widgets/ctkDoubleSpinBox.cpp index 1283b7bcd5..5e0cd3d175 100644 --- a/Libs/Widgets/ctkDoubleSpinBox.cpp +++ b/Libs/Widgets/ctkDoubleSpinBox.cpp @@ -37,6 +37,9 @@ #include #include +//------------------------------------------------------------------------------ +CTK_GET_CPP(ctkDoubleSpinBox, bool, isSettingValue, IsSettingValue) + //----------------------------------------------------------------------------- // ctkQDoubleSpinBox //---------------------------------------------------------------------------- @@ -187,6 +190,7 @@ ctkDoubleSpinBoxPrivate::ctkDoubleSpinBoxPrivate(ctkDoubleSpinBox& object) this->InputRange[0] = 0.; this->InputRange[1] = 99.99; this->ForceInputValueUpdate = false; + this->IsSettingValue = false; } //----------------------------------------------------------------------------- @@ -285,6 +289,8 @@ int ctkDoubleSpinBoxPrivate::decimalsForValue(double value) const void ctkDoubleSpinBoxPrivate::setValue(double value, int dec) { Q_Q(ctkDoubleSpinBox); + bool wasSettingValue = this->IsSettingValue; + this->IsSettingValue = true; dec = this->boundDecimals(dec); const bool changeDecimals = dec != q->decimals(); if (changeDecimals) @@ -316,6 +322,7 @@ void ctkDoubleSpinBoxPrivate::setValue(double value, int dec) this->CachedMinimumSizeHint = QSize(); q->updateGeometry(); } + this->IsSettingValue = wasSettingValue; } //----------------------------------------------------------------------------- diff --git a/Libs/Widgets/ctkDoubleSpinBox.h b/Libs/Widgets/ctkDoubleSpinBox.h index 4cbcdcb510..e130339642 100644 --- a/Libs/Widgets/ctkDoubleSpinBox.h +++ b/Libs/Widgets/ctkDoubleSpinBox.h @@ -89,6 +89,9 @@ class CTK_WIDGETS_EXPORT ctkDoubleSpinBox : public QWidget /// SizeHintByMinMax by default /// SizeHintPolicy, sizeHintPolicy(), setSizeHintPolicy() Q_PROPERTY(SizeHintPolicy sizeHintPolicy READ sizeHintPolicy WRITE setSizeHintPolicy) + /// This property is true while the spinbox is setting a value. + /// \sa isSettingValue() + Q_PROPERTY(bool isSettingValue READ isSettingValue) public: @@ -319,6 +322,14 @@ public Q_SLOTS: /// \sa isReadOnly void setReadOnly(bool readOnly); + /// Return true if the spinbox is in the progress of setting a value. + /// + /// Setting of value is performed in two steps: first the value is set in the spinbox + /// and then the valueChanged and decimalsChanged signals are emitted. + /// During this entire time, isSettingValue() returns true, because in some cases + /// it is important to know which of the sibling widgets initiated an update. + bool isSettingValue()const; + Q_SIGNALS: /// Emitted every time the spinbox value is modified /// \sa QDoubleSpinBox::valueChanged() diff --git a/Libs/Widgets/ctkDoubleSpinBox_p.h b/Libs/Widgets/ctkDoubleSpinBox_p.h index a67c4d49d9..0109419a73 100644 --- a/Libs/Widgets/ctkDoubleSpinBox_p.h +++ b/Libs/Widgets/ctkDoubleSpinBox_p.h @@ -101,6 +101,8 @@ class ctkDoubleSpinBoxPrivate: public QObject mutable QSize CachedMinimumSizeHint; bool ForceInputValueUpdate; + bool IsSettingValue; + QPointer Proxy; void init(); diff --git a/Libs/Widgets/ctkSliderWidget.cpp b/Libs/Widgets/ctkSliderWidget.cpp index 4f848d5214..5bf4a8f761 100644 --- a/Libs/Widgets/ctkSliderWidget.cpp +++ b/Libs/Widgets/ctkSliderWidget.cpp @@ -738,3 +738,10 @@ void ctkSliderWidget::onValueProxyModified() d->SpinBox->setValue(d->Slider->value()); Q_ASSERT(d->equal(d->SpinBox->value(),d->Slider->value())); } + +// -------------------------------------------------------------------------- +bool ctkSliderWidget::isSettingValueFromSpinBox()const +{ + Q_D(const ctkSliderWidget); + return d->SpinBox->isSettingValue(); +} diff --git a/Libs/Widgets/ctkSliderWidget.h b/Libs/Widgets/ctkSliderWidget.h index b6944eda85..fa1e9d3c11 100644 --- a/Libs/Widgets/ctkSliderWidget.h +++ b/Libs/Widgets/ctkSliderWidget.h @@ -265,6 +265,9 @@ class CTK_WIDGETS_EXPORT ctkSliderWidget : public QWidget virtual void setValueProxy(ctkValueProxy* proxy); virtual ctkValueProxy* valueProxy() const; + /// Return true if a value is currently being set in the spinbox. + bool isSettingValueFromSpinBox()const; + public Q_SLOTS: /// /// Reset the slider and spinbox to zero (value and position)