mirror of
https://github.com/OPM/ResInsight.git
synced 2024-12-28 01:41:42 -06:00
Encapsulate AppEnum to improve readability
This commit is contained in:
parent
2386ad8985
commit
4a3f799568
@ -68,6 +68,7 @@ set(PROJECT_FILES
|
||||
cafAsyncObjectDeleter.inl
|
||||
cafAsyncWorkerManager.h
|
||||
cafAsyncWorkerManager.cpp
|
||||
cafPdmEnumField.h
|
||||
)
|
||||
|
||||
add_library(${PROJECT_NAME} ${PROJECT_FILES})
|
||||
|
44
Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafPdmEnumField.h
Normal file
44
Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafPdmEnumField.h
Normal 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
|
@ -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 )
|
||||
|
@ -1,7 +1,6 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cafAppEnum.h"
|
||||
#include "cafPdmField.h"
|
||||
#include "cafPdmObject.h"
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user