diff --git a/Fwk/AppFwk/cafTests/cafTestApplication/MainWindow.cpp b/Fwk/AppFwk/cafTests/cafTestApplication/MainWindow.cpp index c7a7979810..741d220360 100644 --- a/Fwk/AppFwk/cafTests/cafTestApplication/MainWindow.cpp +++ b/Fwk/AppFwk/cafTests/cafTestApplication/MainWindow.cpp @@ -41,6 +41,7 @@ #include "cafPdmUiTextEditor.h" #include "cafPdmUiTreeSelectionEditor.h" #include "cafPdmUiTreeView.h" +#include "cafPdmUiValueRangeEditor.h" #include "cafSelectionManager.h" #include @@ -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, @@ -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 ); @@ -1041,6 +1047,8 @@ class DemoPdmObject : public caf::PdmObject caf::PdmField m_filePath; + caf::PdmField> m_minMaxSlider; + caf::PdmField m_longText; caf::PdmField> m_multiSelectList; diff --git a/Fwk/AppFwk/cafUserInterface/CMakeLists.txt b/Fwk/AppFwk/cafUserInterface/CMakeLists.txt index 8581ee3334..81bc726a87 100644 --- a/Fwk/AppFwk/cafUserInterface/CMakeLists.txt +++ b/Fwk/AppFwk/cafUserInterface/CMakeLists.txt @@ -52,6 +52,7 @@ set(MOC_HEADER_FILES cafPdmUiPickableLineEditor.h cafPdmUiLabelEditor.h cafPdmUiCheckBoxAndTextEditor.h + cafPdmUiValueRangeEditor.h ) find_package( @@ -108,6 +109,8 @@ set(PROJECT_FILES cafPdmUiLabelEditor.cpp cafPdmUiCheckBoxAndTextEditor.h cafPdmUiCheckBoxAndTextEditor.cpp + cafPdmUiValueRangeEditor.h + cafPdmUiValueRangeEditor.cpp # object editors cafPdmUiDefaultObjectEditor.cpp cafPdmUiDefaultObjectEditor.h @@ -171,6 +174,8 @@ set(PROJECT_FILES cafPdmUiTreeAttributes.h cafUiAppearanceSettings.cpp cafUiIconFactory.cpp + cafPdmUiSliderTools.h + cafPdmUiSliderTools.cpp ) add_library( diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUiDefaultObjectEditor.cpp b/Fwk/AppFwk/cafUserInterface/cafPdmUiDefaultObjectEditor.cpp index b6a4d4d182..50392c715d 100644 --- a/Fwk/AppFwk/cafUserInterface/cafPdmUiDefaultObjectEditor.cpp +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUiDefaultObjectEditor.cpp @@ -48,6 +48,7 @@ #include "cafPdmUiListEditor.h" #include "cafPdmUiTimeEditor.h" #include "cafPdmUiTreeSelectionEditor.h" +#include "cafPdmUiValueRangeEditor.h" #include @@ -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 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::instance()->registerCreator( QString( typeid( caf::PdmField> ).name() ) ); -static bool my2PdmUiCheckBoxAndTextEditor73 = +static bool myPdmUiCheckBoxAndTextEditor_proxy = caf::Factory::instance()->registerCreator( QString( typeid( caf::PdmProxyValueField> ).name() ) ); +static bool myPdmUiValueRangeEditor = + caf::Factory::instance()->registerCreator( + QString( typeid( caf::PdmField> ).name() ) ); +static bool myPdmUiValueRangeEditor_proxy = + caf::Factory::instance()->registerCreator( + QString( typeid( caf::PdmProxyValueField> ).name() ) ); + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUiDoubleSliderEditor.cpp b/Fwk/AppFwk/cafUserInterface/cafPdmUiDoubleSliderEditor.cpp index 63df36dbfd..890e8a7005 100644 --- a/Fwk/AppFwk/cafUserInterface/cafPdmUiDoubleSliderEditor.cpp +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUiDoubleSliderEditor.cpp @@ -44,39 +44,24 @@ #include #include +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 ); +} //-------------------------------------------------------------------------------------------------- /// @@ -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 ); } //-------------------------------------------------------------------------------------------------- @@ -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 ) @@ -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 ); - } -} - //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- @@ -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( 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 diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUiDoubleSliderEditor.h b/Fwk/AppFwk/cafUserInterface/cafPdmUiDoubleSliderEditor.h index bbfc903a4d..34c73c5b6b 100644 --- a/Fwk/AppFwk/cafUserInterface/cafPdmUiDoubleSliderEditor.h +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUiDoubleSliderEditor.h @@ -35,39 +35,20 @@ //################################################################################################## #pragma once + #include "cafPdmUiFieldEditorHandle.h" +#include "cafPdmUiSliderTools.h" #include #include #include #include #include -#include + +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; -}; //================================================================================================== /// @@ -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; @@ -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 m_lineEdit; QPointer m_slider; diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUiSliderTools.cpp b/Fwk/AppFwk/cafUserInterface/cafPdmUiSliderTools.cpp new file mode 100644 index 0000000000..6ba2b5d2ba --- /dev/null +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUiSliderTools.cpp @@ -0,0 +1,115 @@ +//################################################################################################## +// +// Custom Visualization Core library +// Copyright (C) 2011-2013 Ceetron AS +// +// This library may be used under the terms of either the GNU General Public License or +// the GNU Lesser General Public License as follows: +// +// GNU General Public License Usage +// This library is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. +// +// See the GNU General Public License at <> +// for more details. +// +// GNU Lesser General Public License Usage +// This library is free software; you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. +// +// See the GNU Lesser General Public License at <> +// for more details. +// +//################################################################################################## + +#include "cafPdmUiSliderTools.h" + +#include + +namespace caf +{ + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +PdmDoubleValidator::PdmDoubleValidator( QObject* parent /*= nullptr */ ) + : QDoubleValidator( parent ) +{ +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +PdmDoubleValidator::PdmDoubleValidator( double bottom, double top, int decimals, QObject* parent ) + : QDoubleValidator( bottom, top, decimals, parent ) +{ +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void PdmDoubleValidator::fixup( QString& stringValue ) const +{ + double doubleValue = stringValue.toDouble(); + doubleValue = qBound( bottom(), doubleValue, top() ); + + 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( 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 diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUiSliderTools.h b/Fwk/AppFwk/cafUserInterface/cafPdmUiSliderTools.h new file mode 100644 index 0000000000..f5df6ee5a4 --- /dev/null +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUiSliderTools.h @@ -0,0 +1,91 @@ +//################################################################################################## +// +// Custom Visualization Core library +// Copyright (C) 2011-2013 Ceetron AS +// +// This library may be used under the terms of either the GNU General Public License or +// the GNU Lesser General Public License as follows: +// +// GNU General Public License Usage +// This library is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. +// +// See the GNU General Public License at <> +// for more details. +// +// GNU Lesser General Public License Usage +// This library is free software; you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. +// +// See the GNU Lesser General Public License at <> +// for more details. +// +//################################################################################################## + +#pragma once + +#include "cafPdmUiFieldEditorHandle.h" + +#include + +class QSlider; + +namespace caf +{ +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +class PdmDoubleValidator : public QDoubleValidator +{ +public: + explicit PdmDoubleValidator( QObject* parent = nullptr ); + explicit PdmDoubleValidator( double bottom, double top, int decimals, QObject* parent ); + + void fixup( QString& stringValue ) const override; +}; + +//================================================================================================== +/// +//================================================================================================== +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; +}; + +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 diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUiValueRangeEditor.cpp b/Fwk/AppFwk/cafUserInterface/cafPdmUiValueRangeEditor.cpp new file mode 100644 index 0000000000..bf3743ff6b --- /dev/null +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUiValueRangeEditor.cpp @@ -0,0 +1,301 @@ +//################################################################################################## +// +// Custom Visualization Core library +// Copyright (C) 2023 Ceetron Solutions AS +// +// This library may be used under the terms of either the GNU General Public License or +// the GNU Lesser General Public License as follows: +// +// GNU General Public License Usage +// This library is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. +// +// See the GNU General Public License at <> +// for more details. +// +// GNU Lesser General Public License Usage +// This library is free software; you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. +// +// See the GNU Lesser General Public License at <> +// for more details. +// +//################################################################################################## + +#include "cafPdmUiValueRangeEditor.h" + +#include "cafPdmField.h" +#include "cafPdmObject.h" +#include "cafPdmUiDefaultObjectEditor.h" +#include "cafPdmUiFieldEditorHandle.h" +#include "cafPdmUiLineEditor.h" +#include "cafPdmUiOrdering.h" +#include "cafQShortenedLabel.h" + +#include +#include + +namespace caf +{ +CAF_PDM_UI_FIELD_EDITOR_SOURCE_INIT( PdmUiValueRangeEditor ); + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void PdmUiValueRangeEditor::configureAndUpdateUi( const QString& uiConfigName ) +{ + PdmUiFieldEditorHandle::updateLabelFromField( m_label, uiConfigName ); + + m_lineEditMin->setEnabled( !uiField()->isUiReadOnly( uiConfigName ) ); + m_sliderMin->setEnabled( !uiField()->isUiReadOnly( uiConfigName ) ); + m_lineEditMax->setEnabled( !uiField()->isUiReadOnly( uiConfigName ) ); + m_sliderMax->setEnabled( !uiField()->isUiReadOnly( uiConfigName ) ); + + caf::PdmUiObjectHandle* uiObject = uiObj( uiField()->fieldHandle()->ownerObject() ); + if ( uiObject ) + { + uiObject->editorAttribute( uiField()->fieldHandle(), uiConfigName, &m_attributes ); + } + + // A pair is represented as a list of QVariant in PdmValueFieldSpecialization> + auto getTwoDoublesFromVariant = [this]() -> std::pair + { + double firstValue = 0.0; + double secondValue = 0.0; + + auto variantValue = uiField()->uiValue(); + if ( variantValue.canConvert>() ) + { + QList lst = variantValue.toList(); + if ( lst.size() == 2 ) + { + firstValue = lst[0].toDouble(); + secondValue = lst[1].toDouble(); + } + } + + return std::make_pair( firstValue, secondValue ); + }; + + auto [minimum, maximum] = getTwoDoublesFromVariant(); + + m_sliderMin->blockSignals( true ); + m_sliderMin->setMaximum( m_attributes.m_sliderTickCount ); + m_sliderMin->blockSignals( false ); + + QString textValueMin = QString( "%1" ).arg( minimum ); + + PdmDoubleValidator* pdmValidator = + new PdmDoubleValidator( m_attributes.m_minimum, m_attributes.m_maximum, m_attributes.m_decimals, this ); + pdmValidator->fixup( textValueMin ); + + m_lineEditMin->setValidator( pdmValidator ); + m_lineEditMin->setText( textValueMin ); + + m_sliderValueMin = minimum; + PdmUiSliderTools::updateSliderPosition( m_sliderMin, minimum, m_attributes ); + + m_sliderMax->blockSignals( true ); + m_sliderMax->setMaximum( m_attributes.m_sliderTickCount ); + m_sliderMax->blockSignals( false ); + + QString textValueMax = QString( "%1" ).arg( maximum ); + + pdmValidator->fixup( textValueMax ); + + m_lineEditMax->setValidator( pdmValidator ); + m_lineEditMax->setText( textValueMax ); + + m_sliderValueMax = maximum; + PdmUiSliderTools::updateSliderPosition( m_sliderMax, maximum, m_attributes ); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void PdmUiValueRangeEditor::slotMinEditingFinished() +{ + QString minText = m_lineEditMin->text(); + QString maxText = m_lineEditMax->text(); + + double minValue = minText.toDouble(); + double maxValue = maxText.toDouble(); + + clampAndWriteValues( minValue, maxValue, true ); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void PdmUiValueRangeEditor::slotMaxEditingFinished() +{ + QString minText = m_lineEditMin->text(); + QString maxText = m_lineEditMax->text(); + + double minValue = minText.toDouble(); + double maxValue = maxText.toDouble(); + + clampAndWriteValues( minValue, maxValue, false ); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void PdmUiValueRangeEditor::slotMinSliderValueChanged( int value ) +{ + double newDoubleValue = PdmUiSliderTools::convertFromSliderValue( m_sliderMin, value, m_attributes ); + m_sliderValueMin = newDoubleValue; + + if ( m_attributes.m_delaySliderUpdateUntilRelease ) + { + m_lineEditMin->setText( QString( "%1" ).arg( m_sliderValueMin ) ); + } + else + { + clampAndWriteValues( m_sliderValueMin, m_sliderValueMax, true ); + } +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void PdmUiValueRangeEditor::slotMaxSliderValueChanged( int value ) +{ + double newDoubleValue = PdmUiSliderTools::convertFromSliderValue( m_sliderMax, value, m_attributes ); + m_sliderValueMax = newDoubleValue; + + if ( m_attributes.m_delaySliderUpdateUntilRelease ) + { + m_lineEditMax->setText( QString( "%1" ).arg( m_sliderValueMax ) ); + } + else + { + clampAndWriteValues( m_sliderValueMin, m_sliderValueMax, false ); + } +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void PdmUiValueRangeEditor::slotMinSliderReleased() +{ + clampAndWriteValues( m_sliderValueMin, m_sliderValueMax, true ); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void PdmUiValueRangeEditor::slotMaxSliderReleased() +{ + clampAndWriteValues( m_sliderValueMin, m_sliderValueMax, false ); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void PdmUiValueRangeEditor::clampAndWriteValues( double valueMin, double valueMax, bool isMinChanged ) +{ + if ( isMinChanged && valueMin > valueMax ) + { + valueMax = valueMin; + } + else if ( valueMax < valueMin ) + { + valueMin = valueMax; + } + + valueMin = qBound( m_attributes.m_minimum, valueMin, m_attributes.m_maximum ); + valueMax = qBound( m_attributes.m_minimum, valueMax, m_attributes.m_maximum ); + m_sliderValueMin = valueMin; + m_sliderValueMax = valueMax; + + writeValues( valueMin, valueMax ); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void PdmUiValueRangeEditor::writeValues( double valueMin, double valueMax ) +{ + auto pairValue = std::make_pair( valueMin, valueMax ); + + QVariant v = caf::PdmValueFieldSpecialization>::convert( pairValue ); + setValueToField( v ); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +PdmUiValueRangeEditor::PdmUiValueRangeEditor() + : m_sliderValueMin( 0.0 ) + , m_sliderValueMax( 0.0 ) +{ +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +PdmUiValueRangeEditor::~PdmUiValueRangeEditor() +{ +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +QWidget* PdmUiValueRangeEditor::createEditorWidget( QWidget* parent ) +{ + auto containerWidget = new QWidget( parent ); + + auto layout = new QGridLayout(); + layout->setMargin( 0 ); + containerWidget->setLayout( layout ); + + m_lineEditMin = new QLineEdit( containerWidget ); + m_lineEditMin->setMaximumWidth( 100 ); + connect( m_lineEditMin, SIGNAL( editingFinished() ), this, SLOT( slotMinEditingFinished() ) ); + + m_sliderMin = new QSlider( Qt::Horizontal, containerWidget ); + + layout->addWidget( m_lineEditMin, 0, 0 ); + layout->addWidget( m_sliderMin, 0, 1 ); + + connect( m_sliderMin, SIGNAL( valueChanged( int ) ), this, SLOT( slotMinSliderValueChanged( int ) ) ); + connect( m_sliderMin, SIGNAL( sliderReleased() ), this, SLOT( slotMinSliderReleased() ) ); + + m_lineEditMax = new QLineEdit( containerWidget ); + m_lineEditMax->setMaximumWidth( 100 ); + connect( m_lineEditMax, SIGNAL( editingFinished() ), this, SLOT( slotMaxEditingFinished() ) ); + + m_sliderMax = new QSlider( Qt::Horizontal, containerWidget ); + + layout->addWidget( m_lineEditMax, 1, 0 ); + layout->addWidget( m_sliderMax, 1, 1 ); + + connect( m_sliderMax, SIGNAL( valueChanged( int ) ), this, SLOT( slotMaxSliderValueChanged( int ) ) ); + connect( m_sliderMax, SIGNAL( sliderReleased() ), this, SLOT( slotMaxSliderReleased() ) ); + + return containerWidget; +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +QWidget* PdmUiValueRangeEditor::createLabelWidget( QWidget* parent ) +{ + m_label = new QShortenedLabel( parent ); + return m_label; +} + +} // end namespace caf diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUiValueRangeEditor.h b/Fwk/AppFwk/cafUserInterface/cafPdmUiValueRangeEditor.h new file mode 100644 index 0000000000..20cabef17d --- /dev/null +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUiValueRangeEditor.h @@ -0,0 +1,88 @@ +//################################################################################################## +// +// Custom Visualization Core library +// Copyright (C) 2023 Ceetron Solutions AS +// +// This library may be used under the terms of either the GNU General Public License or +// the GNU Lesser General Public License as follows: +// +// GNU General Public License Usage +// This library is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. +// +// See the GNU General Public License at <> +// for more details. +// +// GNU Lesser General Public License Usage +// This library is free software; you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. +// +// See the GNU Lesser General Public License at <> +// for more details. +// +//################################################################################################## + +#pragma once + +#include "cafPdmUiFieldEditorHandle.h" +#include "cafPdmUiSliderTools.h" + +class QLineEdit; + +namespace caf +{ +//================================================================================================== +/// +//================================================================================================== +class PdmUiValueRangeEditor : public PdmUiFieldEditorHandle +{ + Q_OBJECT + CAF_PDM_UI_FIELD_EDITOR_HEADER_INIT; + +public: + PdmUiValueRangeEditor(); + ~PdmUiValueRangeEditor() override; + +protected: + QWidget* createEditorWidget( QWidget* parent ) override; + QWidget* createLabelWidget( QWidget* parent ) override; + void configureAndUpdateUi( const QString& uiConfigName ) override; + +private slots: + void slotMinEditingFinished(); + void slotMaxEditingFinished(); + void slotMinSliderValueChanged( int value ); + void slotMaxSliderValueChanged( int value ); + void slotMinSliderReleased(); + void slotMaxSliderReleased(); + +private: + void clampAndWriteValues( double valueMin, double valueMax, bool isMinChanged ); + void writeValues( double valueMin, double valueMax ); + +private: + QPointer m_lineEditMin; + QPointer m_sliderMin; + QPointer m_lineEditMax; + QPointer m_sliderMax; + + QPointer m_label; + double m_sliderValueMin; + double m_sliderValueMax; + + PdmUiDoubleSliderEditorAttribute m_attributes; +}; + +} // end namespace caf