Skip to content

Commit

Permalink
Use static methods
Browse files Browse the repository at this point in the history
  • Loading branch information
magnesj committed Sep 21, 2023
1 parent 6d0c540 commit c0fc26f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 57 deletions.
46 changes: 46 additions & 0 deletions Fwk/AppFwk/cafUserInterface/cafPdmUiSliderTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

#include "cafPdmUiSliderTools.h"

#include <QSlider>

namespace caf
{

Expand Down Expand Up @@ -66,4 +68,48 @@ void PdmDoubleValidator::fixup( QString& stringValue ) const
stringValue = QString::number( doubleValue, 'g', decimals() );
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiSliderTools::updateSliderPosition( QSlider* slider, double value, const PdmUiDoubleSliderEditorAttribute& attributes )
{
if ( !slider ) return;

int newSliderPosition = convertToSliderValue( slider, value, attributes );
if ( slider->value() != newSliderPosition )
{
slider->blockSignals( true );
slider->setValue( newSliderPosition );
slider->blockSignals( false );
}
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int PdmUiSliderTools::convertToSliderValue( QSlider* slider, double value, const PdmUiDoubleSliderEditorAttribute& attributes )
{
double exactSliderValue =
slider->maximum() * ( value - attributes.m_minimum ) / ( attributes.m_maximum - attributes.m_minimum );

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

return sliderValue;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
double PdmUiSliderTools::convertFromSliderValue( QSlider* slider,
int sliderValue,
const PdmUiDoubleSliderEditorAttribute& attributes )
{
double clampedValue = attributes.m_minimum +
sliderValue * ( attributes.m_maximum - attributes.m_minimum ) / slider->maximum();
clampedValue = qBound( attributes.m_minimum, clampedValue, attributes.m_maximum );

return clampedValue;
}

} //namespace caf
11 changes: 11 additions & 0 deletions Fwk/AppFwk/cafUserInterface/cafPdmUiSliderTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@

#include <QValidator>

class QSlider;

namespace caf
{
//--------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -77,4 +79,13 @@ class PdmUiDoubleSliderEditorAttribute : public PdmUiEditorAttribute
bool m_delaySliderUpdateUntilRelease;
};

class PdmUiSliderTools
{
public:
static void updateSliderPosition( QSlider* slider, double value, const PdmUiDoubleSliderEditorAttribute& attributes );
static int convertToSliderValue( QSlider* slider, double value, const PdmUiDoubleSliderEditorAttribute& attributes );
static double
convertFromSliderValue( QSlider* slider, int sliderValue, const PdmUiDoubleSliderEditorAttribute& attributes );
};

} //namespace caf
56 changes: 4 additions & 52 deletions Fwk/AppFwk/cafUserInterface/cafPdmUiValueRangeEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ void PdmUiValueRangeEditor::configureAndUpdateUi( const QString& uiConfigName )
m_lineEditMin->setText( textValueMin );

m_sliderValueMin = firstValue;
updateSliderPosition( m_sliderMin, firstValue, m_attributes );
PdmUiSliderTools::updateSliderPosition( m_sliderMin, firstValue, m_attributes );
}

{
Expand All @@ -149,7 +149,7 @@ void PdmUiValueRangeEditor::configureAndUpdateUi( const QString& uiConfigName )
m_lineEditMax->setText( textValueMax );

m_sliderValueMax = secondValue;
updateSliderPosition( m_sliderMax, secondValue, m_attributes );
PdmUiSliderTools::updateSliderPosition( m_sliderMax, secondValue, m_attributes );
}
}

Expand Down Expand Up @@ -186,7 +186,7 @@ void PdmUiValueRangeEditor::slotMaxEditingFinished()
//--------------------------------------------------------------------------------------------------
void PdmUiValueRangeEditor::slotMinSliderValueChanged( int value )
{
double newDoubleValue = convertFromSliderValue( m_sliderMin, value, m_attributes );
double newDoubleValue = PdmUiSliderTools::convertFromSliderValue( m_sliderMin, value, m_attributes );
m_sliderValueMin = newDoubleValue;

if ( m_attributes.m_delaySliderUpdateUntilRelease )
Expand All @@ -204,7 +204,7 @@ void PdmUiValueRangeEditor::slotMinSliderValueChanged( int value )
//--------------------------------------------------------------------------------------------------
void PdmUiValueRangeEditor::slotMaxSliderValueChanged( int value )
{
double newDoubleValue = convertFromSliderValue( m_sliderMax, value, m_attributes );
double newDoubleValue = PdmUiSliderTools::convertFromSliderValue( m_sliderMax, value, m_attributes );
m_sliderValueMax = newDoubleValue;

if ( m_attributes.m_delaySliderUpdateUntilRelease )
Expand Down Expand Up @@ -233,24 +233,6 @@ void PdmUiValueRangeEditor::slotSliderReleasedMax()
clampAndWriteValues( m_sliderValueMin, m_sliderValueMax, false );
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiValueRangeEditor::updateSliderPosition( QSlider* slider,
double value,
const PdmUiDoubleSliderEditorAttribute& attributes )
{
if ( !slider ) return;

int newSliderPosition = convertToSliderValue( slider, value, attributes );
if ( slider->value() != newSliderPosition )
{
slider->blockSignals( true );
slider->setValue( newSliderPosition );
slider->blockSignals( false );
}
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -284,36 +266,6 @@ void PdmUiValueRangeEditor::clampAndWriteValues( double valueMin, double valueMa
setValueToField( v );
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int PdmUiValueRangeEditor::convertToSliderValue( QSlider* slider,
double value,
const PdmUiDoubleSliderEditorAttribute& attributes )
{
double exactSliderValue =
slider->maximum() * ( value - attributes.m_minimum ) / ( attributes.m_maximum - attributes.m_minimum );

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

return sliderValue;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
double PdmUiValueRangeEditor::convertFromSliderValue( QSlider* slider,
int sliderValue,
const PdmUiDoubleSliderEditorAttribute& attributes )
{
double clampedValue = attributes.m_minimum +
sliderValue * ( attributes.m_maximum - attributes.m_minimum ) / slider->maximum();
clampedValue = qBound( attributes.m_minimum, clampedValue, attributes.m_maximum );

return clampedValue;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
Expand Down
5 changes: 0 additions & 5 deletions Fwk/AppFwk/cafUserInterface/cafPdmUiValueRangeEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,6 @@ protected slots:
void clampAndWriteValues( double valueMin, double valueMax, bool isMinChanged );
void clampAndWriteValues( double valueMin, double valueMax );

static void updateSliderPosition( QSlider* slider, double value, const PdmUiDoubleSliderEditorAttribute& attributes );
static int convertToSliderValue( QSlider* slider, double value, const PdmUiDoubleSliderEditorAttribute& attributes );
static double
convertFromSliderValue( QSlider* slider, int sliderValue, const PdmUiDoubleSliderEditorAttribute& attributes );

private:
QPointer<QLineEdit> m_lineEditMin;
QPointer<QSlider> m_sliderMin;
Expand Down

0 comments on commit c0fc26f

Please sign in to comment.