Add Value Range editor backed by std::pair<double, double>

This commit is contained in:
Magne Sjaastad 2023-09-21 15:03:42 +02:00
parent 6f845f0d42
commit a0d696e596
9 changed files with 642 additions and 104 deletions

View File

@ -41,6 +41,7 @@
#include "cafPdmUiTextEditor.h"
#include "cafPdmUiTreeSelectionEditor.h"
#include "cafPdmUiTreeView.h"
#include "cafPdmUiValueRangeEditor.h"
#include "cafSelectionManager.h"
#include <QAction>
@ -956,6 +957,9 @@ public:
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 @@ public:
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 @@ public:
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;

View File

@ -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(

View File

@ -48,6 +48,7 @@
#include "cafPdmUiListEditor.h"
#include "cafPdmUiTimeEditor.h"
#include "cafPdmUiTreeSelectionEditor.h"
#include "cafPdmUiValueRangeEditor.h"
#include <QGridLayout>
@ -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() ) );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------

View File

@ -44,40 +44,25 @@
#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 );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
PdmUiDoubleSliderEditor::PdmUiDoubleSliderEditor()
: m_sliderValue( 0.0 )
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
PdmUiDoubleSliderEditor::~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<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

View File

@ -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;
};
//==================================================================================================
///
@ -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<QLineEdit> m_lineEdit;
QPointer<QSlider> m_slider;

View File

@ -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 <<http://www.gnu.org/licenses/gpl.html>>
// 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 <<http://www.gnu.org/licenses/lgpl-2.1.html>>
// for more details.
//
//##################################################################################################
#include "cafPdmUiSliderTools.h"
#include <QSlider>
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<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

View File

@ -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 <<http://www.gnu.org/licenses/gpl.html>>
// 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 <<http://www.gnu.org/licenses/lgpl-2.1.html>>
// for more details.
//
//##################################################################################################
#pragma once
#include "cafPdmUiFieldEditorHandle.h"
#include <QValidator>
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

View File

@ -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 <<http://www.gnu.org/licenses/gpl.html>>
// 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 <<http://www.gnu.org/licenses/lgpl-2.1.html>>
// 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 <QLabel>
#include <QSlider>
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<std::pair<T, U>>
auto getTwoDoublesFromVariant = [this]() -> std::pair<double, double>
{
double firstValue = 0.0;
double secondValue = 0.0;
auto variantValue = uiField()->uiValue();
if ( variantValue.canConvert<QList<QVariant>>() )
{
QList<QVariant> 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<std::pair<double, double>>::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

View File

@ -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 <<http://www.gnu.org/licenses/gpl.html>>
// 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 <<http://www.gnu.org/licenses/lgpl-2.1.html>>
// 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<QLineEdit> m_lineEditMin;
QPointer<QSlider> m_sliderMin;
QPointer<QLineEdit> m_lineEditMax;
QPointer<QSlider> m_sliderMax;
QPointer<QShortenedLabel> m_label;
double m_sliderValueMin;
double m_sliderValueMax;
PdmUiDoubleSliderEditorAttribute m_attributes;
};
} // end namespace caf