From b10ff4607ca612751559f34b2de8525e6a85fab2 Mon Sep 17 00:00:00 2001 From: Magne Sjaastad Date: Fri, 8 Mar 2024 23:08:29 +0100 Subject: [PATCH] Cleanup --- .../cafPdmCore/cafAppEnum.h | 214 ++---------------- .../cafPdmCore/cafAppEnumMapper.cpp | 18 ++ .../cafPdmCore/cafAppEnumMapper.h | 10 +- .../cafInternalPdmStreamOperators.cpp | 20 ++ .../cafPdmXml/cafInternalPdmStreamOperators.h | 9 + 5 files changed, 65 insertions(+), 206 deletions(-) diff --git a/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafAppEnum.h b/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafAppEnum.h index 9b9361c0fe..8c240aa775 100644 --- a/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafAppEnum.h +++ b/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafAppEnum.h @@ -42,12 +42,18 @@ #include "cafTypeNameHelper.h" #include #include -#include #include namespace caf { +class AppEnumInterface +{ +public: + virtual QString textForSerialization() const = 0; + virtual void setTextForSerialization( const QString& text ) = 0; +}; + //================================================================================================== /// An enum class to make it easier to handle IO and UI based on the enum. /// Usage: @@ -101,7 +107,7 @@ namespace caf //================================================================================================== template -class AppEnum +class AppEnum : public AppEnumInterface { public: AppEnum() @@ -146,13 +152,17 @@ public: return true; } + QString textForSerialization() const override { return text(); } + + void setTextForSerialization( const QString& text ) override { setFromText( text ); } + // Static interface to access the properties of the enum definition static bool isValid( const QString& text ) { return EnumMapper::instance()->isValid( text ); } static bool isValid( size_t index ) { return index < EnumMapper::instance()->size(); } static size_t size() { return EnumMapper::instance()->size( caf::cafTypeName() ); } - static QStringList uiTexts() { return EnumMapper::instance()->uiTexts(); } + static QStringList uiTexts() { return EnumMapper::instance()->uiTexts( caf::cafTypeName() ); } static T fromIndex( size_t idx ) { auto enumInteger = EnumMapper::instance()->fromIndex( caf::cafTypeName(), idx ); @@ -208,29 +218,6 @@ private: class EnumMapper { - /* - private: - class EnumData - { - public: - EnumData( T enumVal, const QString& text, const QString& uiText, const QStringList& aliases ) - : m_enumVal( enumVal ) - , m_text( text ) - , m_uiText( uiText ) - , m_aliases( aliases ) - { - } - - bool isMatching( const QString& text ) const { return ( text == m_text || m_aliases.contains( text ) - ); } - - T m_enumVal; - QString m_text; - QString m_uiText; - QStringList m_aliases; - }; - */ - public: void addItem( T enumVal, const QString& text, QString uiText, const QStringList& aliases ) { @@ -239,37 +226,8 @@ private: text, uiText, aliases ); - - /* - // Make sure the alias text is unique for enum - for ( const auto& alias : aliases ) - { - for ( const auto& enumData : instance()->m_mapping ) - { - CAF_ASSERT( !enumData.isMatching( alias ) ); - } - } - - // Make sure the text is trimmed, as this text is streamed to XML and will be trimmed when read - back - // from XML text https://github.com/OPM/ResInsight/issues/7829 - instance()->m_mapping.push_back( EnumData( enumVal, text.trimmed(), uiText, aliases ) ); - */ } - /* - static EnumMapper* instance() - { - static EnumMapper storedInstance; - static bool isInitialized = false; - if ( !isInitialized ) - { - isInitialized = true; - AppEnum::setUp(); - } - return &storedInstance; - } - */ static caf::AppEnumMapper* instance() { static bool isInitialized = false; @@ -280,153 +238,7 @@ private: } return caf::AppEnumMapper::instance(); } - /* - - void setDefault( T defaultEnumValue ) - { - m_defaultValue = defaultEnumValue; - m_defaultValueIsSet = true; - } - - T defaultValue() const - { - if ( m_defaultValueIsSet ) - { - return m_defaultValue; - } - else - { - // CAF_ASSERT(m_mapping.size()); - return m_mapping[0].m_enumVal; - } - } - - bool isValid( const QString& text ) const - { - size_t idx; - for ( idx = 0; idx < m_mapping.size(); ++idx ) - { - if ( text == m_mapping[idx].m_text ) return true; - } - - return false; - } - - size_t size() const { return m_mapping.size(); } - - bool enumVal( T& value, const QString& text ) const - { - value = defaultValue(); - - QString trimmedText = text.trimmed(); - - for ( size_t idx = 0; idx < m_mapping.size(); ++idx ) - { - // Make sure the text parsed from a text stream is trimmed - // https://github.com/OPM/ResInsight/issues/7829 - if ( m_mapping[idx].isMatching( trimmedText ) ) - { - value = m_mapping[idx].m_enumVal; - return true; - } - } - return false; - } - - bool enumVal( T& value, size_t index ) const - { - value = defaultValue(); - if ( index < m_mapping.size() ) - { - value = m_mapping[index].m_enumVal; - return true; - } - else - return false; - } - - size_t index( T enumValue ) const - { - size_t idx; - for ( idx = 0; idx < m_mapping.size(); ++idx ) - { - if ( enumValue == m_mapping[idx].m_enumVal ) return idx; - } - - return idx; - } - - QString uiText( T value ) const - { - size_t idx; - for ( idx = 0; idx < m_mapping.size(); ++idx ) - { - if ( value == m_mapping[idx].m_enumVal ) return m_mapping[idx].m_uiText; - } - return ""; - } - - QStringList uiTexts() const - { - QStringList uiTextList; - size_t idx; - for ( idx = 0; idx < m_mapping.size(); ++idx ) - { - uiTextList.append( m_mapping[idx].m_uiText ); - } - return uiTextList; - } - - QString text( T value ) const - { - size_t idx; - for ( idx = 0; idx < m_mapping.size(); ++idx ) - { - if ( value == m_mapping[idx].m_enumVal ) return m_mapping[idx].m_text; - } - return ""; - } - - private: - EnumMapper() - : m_defaultValue( T() ) - , m_defaultValueIsSet( false ) - { - } - - friend class AppEnum; - - std::vector m_mapping; - T m_defaultValue; - bool m_defaultValueIsSet; - */ }; }; } // namespace caf - -//================================================================================================== -/// Implementation of stream operators to make PdmField > work smoothly -/// Assumes that the stream ends at the end of the enum text -//================================================================================================== - -template -QTextStream& operator>>( QTextStream& str, caf::AppEnum& appEnum ) -{ - QString text; - str >> text; - - // Make sure the text parsed from a text stream is trimmed - // https://github.com/OPM/ResInsight/issues/7829 - appEnum.setFromText( text.trimmed() ); - - return str; -} - -template -QTextStream& operator<<( QTextStream& str, const caf::AppEnum& appEnum ) -{ - str << appEnum.text(); - - return str; -} diff --git a/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafAppEnumMapper.cpp b/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafAppEnumMapper.cpp index 74427b7523..2385826da3 100644 --- a/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafAppEnumMapper.cpp +++ b/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafAppEnumMapper.cpp @@ -141,6 +141,24 @@ QString AppEnumMapper::uiText( const std::string& enumKey, int enumValue ) const return {}; } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +QStringList AppEnumMapper::uiTexts( const std::string& enumKey ) const +{ + QStringList uiTextList; + + auto it = m_enumMap.find( enumKey ); + if ( it != m_enumMap.end() ) + { + for ( const auto& enumData : it->second ) + { + uiTextList.append( enumData.m_uiText ); + } + } + return uiTextList; +} + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- diff --git a/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafAppEnumMapper.h b/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafAppEnumMapper.h index 34259fd97b..8eaf46bc61 100644 --- a/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafAppEnumMapper.h +++ b/Fwk/AppFwk/cafProjectDataModel/cafPdmCore/cafAppEnumMapper.h @@ -4,7 +4,6 @@ #include #include -#include #include namespace caf @@ -41,10 +40,11 @@ public: void setDefault( const std::string& enumKey, int enumValue ); - size_t size( const std::string& enumKey ) const; - size_t index( const std::string& enumKey, int enumValue ) const; - QString text( const std::string& enumKey, int enumValue ) const; - QString uiText( const std::string& enumKey, int enumValue ) const; + size_t size( const std::string& enumKey ) const; + size_t index( const std::string& enumKey, int enumValue ) const; + QString text( const std::string& enumKey, int enumValue ) const; + QString uiText( const std::string& enumKey, int enumValue ) const; + QStringList uiTexts( const std::string& enumKey ) const; int defaultEnumValue( const std::string& enumKey ) const; int fromText( const std::string& enumKey, const QString& text ) const; diff --git a/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafInternalPdmStreamOperators.cpp b/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafInternalPdmStreamOperators.cpp index d326cefcb8..964bacbc26 100644 --- a/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafInternalPdmStreamOperators.cpp +++ b/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafInternalPdmStreamOperators.cpp @@ -1,6 +1,8 @@ #include #include +#include "cafAppEnum.h" + //-------------------------------------------------------------------------------------------------- /// Specialized read operation for Bool`s //-------------------------------------------------------------------------------------------------- @@ -83,3 +85,21 @@ QTextStream& operator<<( QTextStream& str, const QTime& value ) str << text; return str; } + +//-------------------------------------------------------------------------------------------------- +/// Specialized read operation for AppEnum +//-------------------------------------------------------------------------------------------------- +QTextStream& operator>>( QTextStream& str, caf::AppEnumInterface& value ) +{ + QString text; + str >> text; + value.setTextForSerialization( text ); + return str; +} + +QTextStream& operator<<( QTextStream& str, const caf::AppEnumInterface& value ) +{ + QString text = value.textForSerialization(); + str << text; + return str; +} diff --git a/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafInternalPdmStreamOperators.h b/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafInternalPdmStreamOperators.h index 8983658a4b..085c03d512 100644 --- a/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafInternalPdmStreamOperators.h +++ b/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafInternalPdmStreamOperators.h @@ -34,6 +34,15 @@ QTextStream& operator<<( QTextStream& str, const QDate& value ); QTextStream& operator>>( QTextStream& str, QTime& value ); QTextStream& operator<<( QTextStream& str, const QTime& value ); +// AppEnum +namespace caf +{ +class AppEnumInterface; +} + +QTextStream& operator>>( QTextStream& str, caf::AppEnumInterface& value ); +QTextStream& operator<<( QTextStream& str, const caf::AppEnumInterface& value ); + //================================================================================================== /// QTextStream Stream operator overloading for std::vector of things. /// Makes automated IO of PdmField< std::vector< Whatever > possible as long as