Skip to content

Commit

Permalink
Cleanup use of slider tools
Browse files Browse the repository at this point in the history
  • Loading branch information
magnesj committed Sep 21, 2023
1 parent 5b94ce8 commit 90d62fd
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 77 deletions.
74 changes: 2 additions & 72 deletions Fwk/AppFwk/cafUserInterface/cafPdmUiDoubleSliderEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,36 +44,6 @@
#include <QDoubleValidator>
#include <QHBoxLayout>

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
class PdmDoubleValidator : public QDoubleValidator
{
public:
explicit PdmDoubleValidator( QObject* parent = nullptr )
: QDoubleValidator( parent )
{
}

PdmDoubleValidator( double bottom, double top, int decimals, QObject* parent )
: QDoubleValidator( bottom, top, decimals, parent )
{
}

~PdmDoubleValidator() override {}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void fixup( QString& stringValue ) const override
{
double doubleValue = stringValue.toDouble();
doubleValue = qBound( bottom(), doubleValue, top() );

stringValue = QString::number( doubleValue, 'g', decimals() );
}
};

namespace caf
{
CAF_PDM_UI_FIELD_EDITOR_SOURCE_INIT( PdmUiDoubleSliderEditor );
Expand Down Expand Up @@ -126,7 +96,7 @@ void PdmUiDoubleSliderEditor::configureAndUpdateUi( const QString& uiConfigName
m_lineEdit->setText( textValue );

m_sliderValue = doubleValue;
updateSliderPosition( doubleValue );
PdmUiSliderTools::updateSliderPosition( m_slider, doubleValue, m_attributes );
}

//--------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -182,7 +152,7 @@ void PdmUiDoubleSliderEditor::slotEditingFinished()
//--------------------------------------------------------------------------------------------------
void PdmUiDoubleSliderEditor::slotSliderValueChanged( int value )
{
double newDoubleValue = convertFromSliderValue( value );
double newDoubleValue = PdmUiSliderTools::convertFromSliderValue( m_slider, value, m_attributes );
m_sliderValue = newDoubleValue;

if ( m_attributes.m_delaySliderUpdateUntilRelease )
Expand All @@ -203,20 +173,6 @@ void PdmUiDoubleSliderEditor::slotSliderReleased()
writeValueToField( m_sliderValue );
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiDoubleSliderEditor::updateSliderPosition( double value )
{
int newSliderPosition = convertToSliderValue( value );
if ( m_slider->value() != newSliderPosition )
{
m_slider->blockSignals( true );
m_slider->setValue( newSliderPosition );
m_slider->blockSignals( false );
}
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
Expand All @@ -226,30 +182,4 @@ void PdmUiDoubleSliderEditor::writeValueToField( double value )
this->setValueToField( v );
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int PdmUiDoubleSliderEditor::convertToSliderValue( double value )
{
double exactSliderValue =
m_slider->maximum() * ( value - m_attributes.m_minimum ) / ( m_attributes.m_maximum - m_attributes.m_minimum );

int sliderValue = static_cast<int>( exactSliderValue + 0.5 );
sliderValue = qBound( m_slider->minimum(), sliderValue, m_slider->maximum() );

return sliderValue;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
double PdmUiDoubleSliderEditor::convertFromSliderValue( int sliderValue )
{
double newDoubleValue = m_attributes.m_minimum +
sliderValue * ( m_attributes.m_maximum - m_attributes.m_minimum ) / m_slider->maximum();
newDoubleValue = qBound( m_attributes.m_minimum, newDoubleValue, m_attributes.m_maximum );

return newDoubleValue;
}

} // end namespace caf
7 changes: 2 additions & 5 deletions Fwk/AppFwk/cafUserInterface/cafPdmUiDoubleSliderEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
#include <QPointer>
#include <QSlider>
#include <QString>
#include <QWidget>

class QWidget;

namespace caf
{
Expand Down Expand Up @@ -72,12 +73,8 @@ protected slots:
void slotSliderReleased();

private:
void updateSliderPosition( double value );
void writeValueToField( double value );

int convertToSliderValue( double value );
double convertFromSliderValue( int sliderValue );

private:
QPointer<QLineEdit> m_lineEdit;
QPointer<QSlider> m_slider;
Expand Down

0 comments on commit 90d62fd

Please sign in to comment.