Encapsulate AppEnum to improve readability

This commit is contained in:
Magne Sjaastad 2024-03-10 09:36:30 +01:00
parent 2386ad8985
commit 4a3f799568
4 changed files with 55 additions and 5 deletions

View File

@ -68,6 +68,7 @@ set(PROJECT_FILES
cafAsyncObjectDeleter.inl
cafAsyncWorkerManager.h
cafAsyncWorkerManager.cpp
cafPdmEnumField.h
)
add_library(${PROJECT_NAME} ${PROJECT_FILES})

View File

@ -0,0 +1,44 @@
#pragma once
#include "cafAppEnum.h"
#include "cafPdmValueField.h"
namespace caf
{
template <typename T>
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<T>;
PdmEnumField() { static_assert( std::is_enum_v<T> == true ); };
QVariant toQVariant() const override
{
auto enumValue = static_cast<std::underlying_type_t<T>>( m_fieldValue.value() );
return enumValue;
}
void setFromQVariant( const QVariant& variant ) override { m_fieldValue = static_cast<T>( 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<T> m_fieldValue;
};
} // namespace caf

View File

@ -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>( 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<double> m_doubleField;
caf::PdmField<int> m_intField;
caf::PdmField<QString> m_textField;
caf::PdmField<caf::AppEnum<TestEnumType>> m_testEnumField;
caf::PdmField<caf::AppEnum<TestEnumType>> m_testAppEnumField;
caf::PdmEnumField<TestEnumType> m_testEnumField;
caf::PdmPtrField<SmallDemoPdmObjectA*> m_ptrField;
caf::PdmProxyValueField<caf::AppEnum<TestEnumType>> m_proxyEnumField;
@ -817,7 +823,7 @@ public:
// See PdmUiFieldSpecialization<caf::AppEnum<T>>
auto enumValue = static_cast<std::underlying_type_t<TestEnumType>>( 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<caf::AppEnum<T>>
auto enumValue = static_cast<std::underlying_type_t<TestEnumType>>( value );
m_testEnumField.uiCapability()->setAutoValue( enumValue );
m_testAppEnumField.uiCapability()->setAutoValue( enumValue );
}
void setAutoValueForDouble( double value )

View File

@ -1,7 +1,6 @@
#pragma once
#include "cafAppEnum.h"
#include "cafPdmField.h"
#include "cafPdmObject.h"