Skip to content

Commit

Permalink
Add Value Range editor backed by std::pair<double, double>
Browse files Browse the repository at this point in the history
  • Loading branch information
magnesj committed Sep 21, 2023
1 parent 9c25bd7 commit 2fc2f63
Show file tree
Hide file tree
Showing 9 changed files with 639 additions and 101 deletions.
8 changes: 8 additions & 0 deletions Fwk/AppFwk/cafTests/cafTestApplication/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "cafPdmUiTextEditor.h"
#include "cafPdmUiTreeSelectionEditor.h"
#include "cafPdmUiTreeView.h"
#include "cafPdmUiValueRangeEditor.h"
#include "cafSelectionManager.h"

#include <QAction>
Expand Down Expand Up @@ -956,6 +957,9 @@ class DemoPdmObject : public caf::PdmObject
CAF_PDM_InitField( &m_filePath, "FilePath", QString( "" ), "Filename", "", "", "" );
CAF_PDM_InitField( &m_longText, "LongText", QString( "Test text" ), "Long Text", "", "", "" );

CAF_PDM_InitField( &m_minMaxSlider, "MinMaxSlider", std::make_pair( 2.5, 10.1 ), "Min max slider", "", "", "" );
m_minMaxSlider.uiCapability()->setUiEditorTypeName( caf::PdmUiValueRangeEditor::uiEditorTypeName() );

CAF_PDM_InitFieldNoDefault( &m_multiSelectList, "MultiSelect", "Selection List", "", "List", "This is a multi selection list" );
CAF_PDM_InitFieldNoDefault( &m_objectList, "ObjectList", "Objects list Field", "", "List", "This is a list of PdmObjects" );
CAF_PDM_InitFieldNoDefault( &m_objectListOfSameType,
Expand Down Expand Up @@ -984,6 +988,8 @@ class DemoPdmObject : public caf::PdmObject
uiOrdering.add( &m_applyAutoOnChildObjectFields );
uiOrdering.add( &m_updateAutoValues );

uiOrdering.add( &m_minMaxSlider );

uiOrdering.add( &m_objectListOfSameType );
uiOrdering.add( &m_ptrField );
uiOrdering.add( &m_boolField );
Expand Down Expand Up @@ -1041,6 +1047,8 @@ class DemoPdmObject : public caf::PdmObject

caf::PdmField<QString> m_filePath;

caf::PdmField<std::pair<double, double>> m_minMaxSlider;

caf::PdmField<QString> m_longText;
caf::PdmField<std::vector<QString>> m_multiSelectList;

Expand Down
5 changes: 5 additions & 0 deletions Fwk/AppFwk/cafUserInterface/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ set(MOC_HEADER_FILES
cafPdmUiPickableLineEditor.h
cafPdmUiLabelEditor.h
cafPdmUiCheckBoxAndTextEditor.h
cafPdmUiValueRangeEditor.h
)

find_package(
Expand Down Expand Up @@ -108,6 +109,8 @@ set(PROJECT_FILES
cafPdmUiLabelEditor.cpp
cafPdmUiCheckBoxAndTextEditor.h
cafPdmUiCheckBoxAndTextEditor.cpp
cafPdmUiValueRangeEditor.h
cafPdmUiValueRangeEditor.cpp
# object editors
cafPdmUiDefaultObjectEditor.cpp
cafPdmUiDefaultObjectEditor.h
Expand Down Expand Up @@ -171,6 +174,8 @@ set(PROJECT_FILES
cafPdmUiTreeAttributes.h
cafUiAppearanceSettings.cpp
cafUiIconFactory.cpp
cafPdmUiSliderTools.h
cafPdmUiSliderTools.cpp
)

add_library(
Expand Down
14 changes: 11 additions & 3 deletions Fwk/AppFwk/cafUserInterface/cafPdmUiDefaultObjectEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "cafPdmUiListEditor.h"
#include "cafPdmUiTimeEditor.h"
#include "cafPdmUiTreeSelectionEditor.h"
#include "cafPdmUiValueRangeEditor.h"

#include <QGridLayout>

Expand Down Expand Up @@ -75,15 +76,22 @@ CAF_PDM_UI_REGISTER_DEFAULT_FIELD_EDITOR( PdmUiTreeSelectionEditor, std::vector<

CAF_PDM_UI_REGISTER_DEFAULT_FIELD_EDITOR( PdmUiFilePathEditor, FilePath );

// As the macro CAF_PDM_UI_REGISTER_DEFAULT_FIELD_EDITOR() is not working correctly for std::pair<bool, double> the
// As the macro CAF_PDM_UI_REGISTER_DEFAULT_FIELD_EDITOR() is not working correctly for std::pair<> the
// registration of this type in the factory has to be written directly
static bool myPdmUiCheckBoxAndTextEditor73 =
static bool myPdmUiCheckBoxAndTextEditor =
caf::Factory<caf::PdmUiFieldEditorHandle, QString>::instance()->registerCreator<PdmUiCheckBoxAndTextEditor>(
QString( typeid( caf::PdmField<std::pair<bool, double>> ).name() ) );
static bool my2PdmUiCheckBoxAndTextEditor73 =
static bool myPdmUiCheckBoxAndTextEditor_proxy =
caf::Factory<caf::PdmUiFieldEditorHandle, QString>::instance()->registerCreator<PdmUiCheckBoxAndTextEditor>(
QString( typeid( caf::PdmProxyValueField<std::pair<bool, double>> ).name() ) );

static bool myPdmUiValueRangeEditor =
caf::Factory<caf::PdmUiFieldEditorHandle, QString>::instance()->registerCreator<PdmUiValueRangeEditor>(
QString( typeid( caf::PdmField<std::pair<double, double>> ).name() ) );
static bool myPdmUiValueRangeEditor_proxy =
caf::Factory<caf::PdmUiFieldEditorHandle, QString>::instance()->registerCreator<PdmUiValueRangeEditor>(
QString( typeid( caf::PdmProxyValueField<std::pair<double, double>> ).name() ) );

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
Expand Down
83 changes: 14 additions & 69 deletions Fwk/AppFwk/cafUserInterface/cafPdmUiDoubleSliderEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,39 +44,24 @@
#include <QDoubleValidator>
#include <QHBoxLayout>

namespace caf
{
CAF_PDM_UI_FIELD_EDITOR_SOURCE_INIT( PdmUiDoubleSliderEditor );

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
class PdmDoubleValidator : public QDoubleValidator
PdmUiDoubleSliderEditor::PdmUiDoubleSliderEditor()
: m_sliderValue( 0.0 )
{
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
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
PdmUiDoubleSliderEditor::~PdmUiDoubleSliderEditor()
{
CAF_PDM_UI_FIELD_EDITOR_SOURCE_INIT( PdmUiDoubleSliderEditor );
}

//--------------------------------------------------------------------------------------------------
///
Expand Down Expand Up @@ -111,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 @@ -167,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 @@ -188,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 @@ -211,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
35 changes: 6 additions & 29 deletions Fwk/AppFwk/cafUserInterface/cafPdmUiDoubleSliderEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,39 +35,20 @@
//##################################################################################################

#pragma once

#include "cafPdmUiFieldEditorHandle.h"
#include "cafPdmUiSliderTools.h"

#include <QLabel>
#include <QLineEdit>
#include <QPointer>
#include <QSlider>
#include <QString>
#include <QWidget>

class QWidget;

namespace caf
{
//==================================================================================================
///
//==================================================================================================
class PdmUiDoubleSliderEditorAttribute : public PdmUiEditorAttribute
{
public:
PdmUiDoubleSliderEditorAttribute()
{
m_minimum = 0;
m_maximum = 10;
m_decimals = 6;
m_sliderTickCount = 2000;
m_delaySliderUpdateUntilRelease = false;
}

public:
double m_minimum;
double m_maximum;
int m_decimals;
int m_sliderTickCount;
bool m_delaySliderUpdateUntilRelease;
};

//==================================================================================================
///
Expand All @@ -78,8 +59,8 @@ class PdmUiDoubleSliderEditor : public PdmUiFieldEditorHandle
CAF_PDM_UI_FIELD_EDITOR_HEADER_INIT;

public:
PdmUiDoubleSliderEditor() {}
~PdmUiDoubleSliderEditor() override {}
PdmUiDoubleSliderEditor();
~PdmUiDoubleSliderEditor() override;

protected:
void configureAndUpdateUi( const QString& uiConfigName ) override;
Expand All @@ -92,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
Loading

0 comments on commit 2fc2f63

Please sign in to comment.