Skip to content

Commit

Permalink
ENH: Add method to check if double-spinbox value is being edited
Browse files Browse the repository at this point in the history
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 Slicer/Slicer#7574
  • Loading branch information
lassoan committed Sep 18, 2024
1 parent 627842c commit b9e08ae
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Libs/Widgets/ctkDoubleSpinBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
#include <QStyleOptionSpinBox>
#include <QVariant>

//------------------------------------------------------------------------------
CTK_GET_CPP(ctkDoubleSpinBox, bool, isSettingValue, IsSettingValue)

//-----------------------------------------------------------------------------
// ctkQDoubleSpinBox
//----------------------------------------------------------------------------
Expand Down Expand Up @@ -187,6 +190,7 @@ ctkDoubleSpinBoxPrivate::ctkDoubleSpinBoxPrivate(ctkDoubleSpinBox& object)
this->InputRange[0] = 0.;
this->InputRange[1] = 99.99;
this->ForceInputValueUpdate = false;
this->IsSettingValue = false;
}

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -316,6 +322,7 @@ void ctkDoubleSpinBoxPrivate::setValue(double value, int dec)
this->CachedMinimumSizeHint = QSize();
q->updateGeometry();
}
this->IsSettingValue = wasSettingValue;
}

//-----------------------------------------------------------------------------
Expand Down
11 changes: 11 additions & 0 deletions Libs/Widgets/ctkDoubleSpinBox.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions Libs/Widgets/ctkDoubleSpinBox_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ class ctkDoubleSpinBoxPrivate: public QObject
mutable QSize CachedMinimumSizeHint;
bool ForceInputValueUpdate;

bool IsSettingValue;

QPointer<ctkValueProxy> Proxy;

void init();
Expand Down
7 changes: 7 additions & 0 deletions Libs/Widgets/ctkSliderWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
3 changes: 3 additions & 0 deletions Libs/Widgets/ctkSliderWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b9e08ae

Please sign in to comment.