From 4a3f799568866e6cff65a7d28bb1c9d51f257164 Mon Sep 17 00:00:00 2001 From: Magne Sjaastad Date: Sun, 10 Mar 2024 09:36:30 +0100 Subject: [PATCH] Encapsulate AppEnum to improve readability --- .../cafPdmCore/CMakeLists.txt | 1 + .../cafPdmCore/cafPdmEnumField.h | 44 +++++++++++++++++++ .../cafTestApplication/MainWindow.cpp | 14 ++++-- .../cafTests/cafTestApplication/TamComboBox.h | 1 - 4 files changed, 55 insertions(+), 5 deletions(-) create mode 100644 Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafPdmEnumField.h diff --git a/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/CMakeLists.txt b/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/CMakeLists.txt index b33de76ebe..f4191b8d3b 100644 --- a/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/CMakeLists.txt +++ b/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/CMakeLists.txt @@ -68,6 +68,7 @@ set(PROJECT_FILES cafAsyncObjectDeleter.inl cafAsyncWorkerManager.h cafAsyncWorkerManager.cpp + cafPdmEnumField.h ) add_library(${PROJECT_NAME} ${PROJECT_FILES}) diff --git a/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafPdmEnumField.h b/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafPdmEnumField.h new file mode 100644 index 0000000000..1f24aa6390 --- /dev/null +++ b/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafPdmEnumField.h @@ -0,0 +1,44 @@ + +#pragma once + +#include "cafAppEnum.h" +#include "cafPdmValueField.h" + +namespace caf +{ +template +class PdmEnumField : public PdmValueField +{ +public: + // FieldDataType is differently than in PdmDataValueField, where the FieldDataType is defined as T. + // Similar concept is use in PdmPtrField and others. + using FieldDataType = caf::AppEnum; + + PdmEnumField() { static_assert( std::is_enum_v == true ); }; + + QVariant toQVariant() const override + { + auto enumValue = static_cast>( m_fieldValue.value() ); + return enumValue; + } + + void setFromQVariant( const QVariant& variant ) override { m_fieldValue = static_cast( variant.toInt() ); } + + bool isReadOnly() const override { return false; } + + void setValue( const T& fieldValue ) { m_fieldValue = fieldValue; } + + T value() const { return m_fieldValue; } + + // Required to be able to assign a enum value to a PdmEnumField + PdmEnumField& operator=( T value ) + { + m_fieldValue = value; + return *this; + } + +private: + caf::AppEnum m_fieldValue; +}; + +} // namespace caf diff --git a/Fwk/AppFwk/cafTests/cafTestApplication/MainWindow.cpp b/Fwk/AppFwk/cafTests/cafTestApplication/MainWindow.cpp index 3e0bdee720..030f5882fa 100644 --- a/Fwk/AppFwk/cafTests/cafTestApplication/MainWindow.cpp +++ b/Fwk/AppFwk/cafTests/cafTestApplication/MainWindow.cpp @@ -21,6 +21,7 @@ #include "cafCmdFeatureMenuBuilder.h" #include "cafFilePath.h" #include "cafPdmDocument.h" +#include "cafPdmEnumField.h" #include "cafPdmObject.h" #include "cafPdmObjectGroup.h" #include "cafPdmProxyValueField.h" @@ -707,13 +708,17 @@ public: "Enter some small number here", "This is a place you can enter a small integer value if you want" ); CAF_PDM_InitField( &m_textField, "TextField", QString( "Small Demo Object A" ), "Name Text Field", "", "", "" ); - CAF_PDM_InitField( &m_testEnumField, + CAF_PDM_InitField( &m_testAppEnumField, "TestEnumValue", caf::AppEnum( TestEnumType::T1 ), "EnumField", "", "", "" ); + + CAF_PDM_InitFieldNoDefault( &m_testEnumField, "TestAppEnumValue", "AppEnum Field" ); + m_testEnumField = TestEnumType::T2; + CAF_PDM_InitFieldNoDefault( &m_ptrField, "m_ptrField", "PtrField", "", "", "" ); CAF_PDM_InitFieldNoDefault( &m_proxyEnumField, "ProxyEnumValue", "ProxyEnum", "", "", "" ); @@ -731,7 +736,8 @@ public: caf::PdmField m_doubleField; caf::PdmField m_intField; caf::PdmField m_textField; - caf::PdmField> m_testEnumField; + caf::PdmField> m_testAppEnumField; + caf::PdmEnumField m_testEnumField; caf::PdmPtrField m_ptrField; caf::PdmProxyValueField> m_proxyEnumField; @@ -817,7 +823,7 @@ public: // See PdmUiFieldSpecialization> auto enumValue = static_cast>( value ); - m_testEnumField.uiCapability()->enableAndSetAutoValue( enumValue ); + m_testAppEnumField.uiCapability()->enableAndSetAutoValue( enumValue ); } void enableAutoValueForDouble( double value ) { m_doubleField.uiCapability()->enableAndSetAutoValue( value ); } @@ -830,7 +836,7 @@ public: // See PdmUiFieldSpecialization> auto enumValue = static_cast>( value ); - m_testEnumField.uiCapability()->setAutoValue( enumValue ); + m_testAppEnumField.uiCapability()->setAutoValue( enumValue ); } void setAutoValueForDouble( double value ) diff --git a/Fwk/AppFwk/cafTests/cafTestApplication/TamComboBox.h b/Fwk/AppFwk/cafTests/cafTestApplication/TamComboBox.h index f5b8fceb6b..4594f8bc28 100644 --- a/Fwk/AppFwk/cafTests/cafTestApplication/TamComboBox.h +++ b/Fwk/AppFwk/cafTests/cafTestApplication/TamComboBox.h @@ -1,7 +1,6 @@ #pragma once -#include "cafAppEnum.h" #include "cafPdmField.h" #include "cafPdmObject.h"