mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
Compiling and working test application
This commit is contained in:
parent
2f86743b44
commit
26227dbcfc
@ -36,8 +36,10 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "cafAppEnumMapper.h"
|
||||||
#include "cafAssert.h"
|
#include "cafAssert.h"
|
||||||
|
|
||||||
|
#include "cafTypeNameHelper.h"
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QStringList>
|
#include <QStringList>
|
||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
@ -102,7 +104,11 @@ template <class T>
|
|||||||
class AppEnum
|
class AppEnum
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
AppEnum() { m_value = EnumMapper::instance()->defaultValue(); }
|
AppEnum()
|
||||||
|
{
|
||||||
|
auto enumInteger = EnumMapper::instance()->defaultEnumValue( caf::cafTypeName<T>() );
|
||||||
|
m_value = caf::convertToEnum<T>( enumInteger );
|
||||||
|
}
|
||||||
AppEnum( T value )
|
AppEnum( T value )
|
||||||
: m_value( value )
|
: m_value( value )
|
||||||
{
|
{
|
||||||
@ -110,42 +116,66 @@ public:
|
|||||||
|
|
||||||
operator T() const { return m_value; }
|
operator T() const { return m_value; }
|
||||||
|
|
||||||
T value() const { return m_value; }
|
T value() const { return m_value; }
|
||||||
size_t index() const { return EnumMapper::instance()->index( m_value ); }
|
size_t index() const
|
||||||
QString text() const { return EnumMapper::instance()->text( m_value ); }
|
{
|
||||||
QString uiText() const { return EnumMapper::instance()->uiText( m_value ); }
|
return EnumMapper::instance()->index( caf::cafTypeName<T>(), caf::convertToInteger( m_value ) );
|
||||||
|
}
|
||||||
|
QString text() const
|
||||||
|
{
|
||||||
|
return EnumMapper::instance()->text( caf::cafTypeName<T>(), caf::convertToInteger( m_value ) );
|
||||||
|
}
|
||||||
|
QString uiText() const
|
||||||
|
{
|
||||||
|
return EnumMapper::instance()->uiText( caf::cafTypeName<T>(), caf::convertToInteger( m_value ) );
|
||||||
|
}
|
||||||
|
|
||||||
AppEnum& operator=( T value )
|
AppEnum& operator=( T value )
|
||||||
{
|
{
|
||||||
m_value = value;
|
m_value = value;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
bool setFromText( const QString& text ) { return EnumMapper::instance()->enumVal( m_value, text ); }
|
bool setFromText( const QString& text )
|
||||||
bool setFromIndex( size_t index ) { return EnumMapper::instance()->enumVal( m_value, index ); }
|
{
|
||||||
|
m_value = fromText( text );
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool setFromIndex( size_t index )
|
||||||
|
{
|
||||||
|
m_value = fromIndex( index );
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
// Static interface to access the properties of the enum definition
|
// Static interface to access the properties of the enum definition
|
||||||
|
|
||||||
static bool isValid( const QString& text ) { return EnumMapper::instance()->isValid( text ); }
|
static bool isValid( const QString& text ) { return EnumMapper::instance()->isValid( text ); }
|
||||||
static bool isValid( size_t index ) { return index < EnumMapper::instance()->size(); }
|
static bool isValid( size_t index ) { return index < EnumMapper::instance()->size(); }
|
||||||
static size_t size() { return EnumMapper::instance()->size(); }
|
static size_t size() { return EnumMapper::instance()->size( caf::cafTypeName<T>() ); }
|
||||||
|
|
||||||
static QStringList uiTexts() { return EnumMapper::instance()->uiTexts(); }
|
static QStringList uiTexts() { return EnumMapper::instance()->uiTexts(); }
|
||||||
static T fromIndex( size_t idx )
|
static T fromIndex( size_t idx )
|
||||||
{
|
{
|
||||||
T val;
|
auto enumInteger = EnumMapper::instance()->fromIndex( caf::cafTypeName<T>(), idx );
|
||||||
EnumMapper::instance()->enumVal( val, idx );
|
return caf::convertToEnum<T>( enumInteger );
|
||||||
return val;
|
|
||||||
}
|
}
|
||||||
static T fromText( const QString& text )
|
static T fromText( const QString& text )
|
||||||
{
|
{
|
||||||
T val;
|
auto enumInteger = EnumMapper::instance()->fromText( caf::cafTypeName<T>(), text );
|
||||||
EnumMapper::instance()->enumVal( val, text );
|
return caf::convertToEnum<T>( enumInteger );
|
||||||
return val;
|
}
|
||||||
|
static size_t index( T enumValue )
|
||||||
|
{
|
||||||
|
return EnumMapper::instance()->index( caf::cafTypeName<T>(), caf::convertToInteger( enumValue ) );
|
||||||
|
}
|
||||||
|
static QString text( T enumValue )
|
||||||
|
{
|
||||||
|
return EnumMapper::instance()->text( caf::cafTypeName<T>(), caf::convertToInteger( enumValue ) );
|
||||||
}
|
}
|
||||||
static size_t index( T enumValue ) { return EnumMapper::instance()->index( enumValue ); }
|
|
||||||
static QString text( T enumValue ) { return EnumMapper::instance()->text( enumValue ); }
|
|
||||||
static QString textFromIndex( size_t idx ) { return text( fromIndex( idx ) ); }
|
static QString textFromIndex( size_t idx ) { return text( fromIndex( idx ) ); }
|
||||||
static QString uiText( T enumValue ) { return EnumMapper::instance()->uiText( enumValue ); }
|
static QString uiText( T enumValue )
|
||||||
|
{
|
||||||
|
return EnumMapper::instance()->uiText( caf::cafTypeName<T>(), caf::convertToInteger( enumValue ) );
|
||||||
|
}
|
||||||
static QString uiTextFromIndex( size_t idx ) { return uiText( fromIndex( idx ) ); }
|
static QString uiTextFromIndex( size_t idx ) { return uiText( fromIndex( idx ) ); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -157,10 +187,15 @@ private:
|
|||||||
static void setUp();
|
static void setUp();
|
||||||
static void addItem( T enumVal, const QString& text, const QString& uiText, const QStringList& aliases = {} )
|
static void addItem( T enumVal, const QString& text, const QString& uiText, const QStringList& aliases = {} )
|
||||||
{
|
{
|
||||||
EnumMapper::instance()->addItem( enumVal, text, uiText, aliases );
|
auto key = caf::cafTypeName<T>();
|
||||||
|
auto enumInteger = caf::convertToInteger<T>( enumVal );
|
||||||
|
EnumMapper::instance()->addItem( key, enumInteger, text, uiText, aliases );
|
||||||
}
|
}
|
||||||
|
|
||||||
static void setDefault( T defaultEnumValue ) { EnumMapper::instance()->setDefault( defaultEnumValue ); }
|
static void setDefault( T defaultEnumValue )
|
||||||
|
{
|
||||||
|
EnumMapper::instance()->setDefault( caf::cafTypeName<T>(), caf::convertToInteger<T>( defaultEnumValue ) );
|
||||||
|
}
|
||||||
|
|
||||||
T m_value;
|
T m_value;
|
||||||
|
|
||||||
@ -173,172 +208,198 @@ private:
|
|||||||
|
|
||||||
class EnumMapper
|
class EnumMapper
|
||||||
{
|
{
|
||||||
private:
|
/*
|
||||||
class EnumData
|
private:
|
||||||
{
|
class EnumData
|
||||||
public:
|
{
|
||||||
EnumData( T enumVal, const QString& text, const QString& uiText, const QStringList& aliases )
|
public:
|
||||||
: m_enumVal( enumVal )
|
EnumData( T enumVal, const QString& text, const QString& uiText, const QStringList& aliases )
|
||||||
, m_text( text )
|
: m_enumVal( enumVal )
|
||||||
, m_uiText( uiText )
|
, m_text( text )
|
||||||
, m_aliases( aliases )
|
, m_uiText( uiText )
|
||||||
{
|
, m_aliases( aliases )
|
||||||
}
|
{
|
||||||
|
}
|
||||||
|
|
||||||
bool isMatching( const QString& text ) const { return ( text == m_text || m_aliases.contains( text ) ); }
|
bool isMatching( const QString& text ) const { return ( text == m_text || m_aliases.contains( text )
|
||||||
|
); }
|
||||||
|
|
||||||
T m_enumVal;
|
T m_enumVal;
|
||||||
QString m_text;
|
QString m_text;
|
||||||
QString m_uiText;
|
QString m_uiText;
|
||||||
QStringList m_aliases;
|
QStringList m_aliases;
|
||||||
};
|
};
|
||||||
|
*/
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void addItem( T enumVal, const QString& text, QString uiText, const QStringList& aliases )
|
void addItem( T enumVal, const QString& text, QString uiText, const QStringList& aliases )
|
||||||
{
|
{
|
||||||
// Make sure the alias text is unique for enum
|
caf::AppEnumMapper::instance()->addItem( caf::cafTypeName<T>(),
|
||||||
for ( const auto& alias : aliases )
|
caf::convertToInteger<T>( enumVal ),
|
||||||
{
|
text,
|
||||||
for ( const auto& enumData : instance()->m_mapping )
|
uiText,
|
||||||
{
|
aliases );
|
||||||
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
|
// Make sure the alias text is unique for enum
|
||||||
instance()->m_mapping.push_back( EnumData( enumVal, text.trimmed(), uiText, aliases ) );
|
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* instance()
|
||||||
|
{
|
||||||
|
static EnumMapper storedInstance;
|
||||||
|
static bool isInitialized = false;
|
||||||
|
if ( !isInitialized )
|
||||||
|
{
|
||||||
|
isInitialized = true;
|
||||||
|
AppEnum<T>::setUp();
|
||||||
|
}
|
||||||
|
return &storedInstance;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
static caf::AppEnumMapper* instance()
|
||||||
{
|
{
|
||||||
static EnumMapper storedInstance;
|
static bool isInitialized = false;
|
||||||
static bool isInitialized = false;
|
|
||||||
if ( !isInitialized )
|
if ( !isInitialized )
|
||||||
{
|
{
|
||||||
isInitialized = true;
|
isInitialized = true;
|
||||||
AppEnum<T>::setUp();
|
AppEnum<T>::setUp();
|
||||||
}
|
}
|
||||||
return &storedInstance;
|
return caf::AppEnumMapper::instance();
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
|
||||||
void setDefault( T defaultEnumValue )
|
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;
|
m_defaultValue = defaultEnumValue;
|
||||||
return true;
|
m_defaultValueIsSet = true;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool enumVal( T& value, size_t index ) const
|
T defaultValue() const
|
||||||
{
|
{
|
||||||
value = defaultValue();
|
if ( m_defaultValueIsSet )
|
||||||
if ( index < m_mapping.size() )
|
{
|
||||||
{
|
return m_defaultValue;
|
||||||
value = m_mapping[index].m_enumVal;
|
}
|
||||||
return true;
|
else
|
||||||
}
|
{
|
||||||
else
|
// CAF_ASSERT(m_mapping.size());
|
||||||
return false;
|
return m_mapping[0].m_enumVal;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
size_t index( T enumValue ) const
|
bool isValid( const QString& text ) const
|
||||||
{
|
{
|
||||||
size_t idx;
|
size_t idx;
|
||||||
for ( idx = 0; idx < m_mapping.size(); ++idx )
|
for ( idx = 0; idx < m_mapping.size(); ++idx )
|
||||||
{
|
{
|
||||||
if ( enumValue == m_mapping[idx].m_enumVal ) return idx;
|
if ( text == m_mapping[idx].m_text ) return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return idx;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString uiText( T value ) const
|
size_t size() const { return m_mapping.size(); }
|
||||||
{
|
|
||||||
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
|
bool enumVal( T& value, const QString& text ) const
|
||||||
{
|
{
|
||||||
QStringList uiTextList;
|
value = defaultValue();
|
||||||
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
|
QString trimmedText = text.trimmed();
|
||||||
{
|
|
||||||
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:
|
for ( size_t idx = 0; idx < m_mapping.size(); ++idx )
|
||||||
EnumMapper()
|
{
|
||||||
: m_defaultValue( T() )
|
// Make sure the text parsed from a text stream is trimmed
|
||||||
, m_defaultValueIsSet( false )
|
// https://github.com/OPM/ResInsight/issues/7829
|
||||||
{
|
if ( m_mapping[idx].isMatching( trimmedText ) )
|
||||||
}
|
{
|
||||||
|
value = m_mapping[idx].m_enumVal;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
friend class AppEnum<T>;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<EnumData> m_mapping;
|
size_t index( T enumValue ) const
|
||||||
T m_defaultValue;
|
{
|
||||||
bool m_defaultValueIsSet;
|
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<T>;
|
||||||
|
|
||||||
|
std::vector<EnumData> m_mapping;
|
||||||
|
T m_defaultValue;
|
||||||
|
bool m_defaultValueIsSet;
|
||||||
|
*/
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -28,18 +28,23 @@ void AppEnumMapper::addItem( const std::string& enumKey,
|
|||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
///
|
///
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
void AppEnumMapper::addDefaultItem( const std::string& enumKey,
|
void AppEnumMapper::setDefault( const std::string& enumKey, int enumValue )
|
||||||
int enumValue,
|
|
||||||
const QString& text,
|
|
||||||
const QString& uiText,
|
|
||||||
const QStringList& aliases /*= {} */ )
|
|
||||||
{
|
{
|
||||||
// Make sure the text is trimmed, as this text is streamed to XML and will be trimmed when read back
|
auto it = m_enumMap.find( enumKey );
|
||||||
// from XML text https://github.com/OPM/ResInsight/issues/7829
|
if ( it != m_enumMap.end() )
|
||||||
auto enumData = AppEnumMapper::EnumData( enumValue, text.trimmed(), uiText, aliases );
|
{
|
||||||
enumData.m_isDefault = true;
|
for ( auto& enumData : it->second )
|
||||||
|
{
|
||||||
m_enumMap[enumKey].emplace_back( enumData );
|
if ( enumData.m_enumVal == enumValue )
|
||||||
|
{
|
||||||
|
enumData.m_isDefault = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
enumData.m_isDefault = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
@ -70,8 +75,13 @@ int AppEnumMapper::defaultEnumValue( const std::string& enumKey ) const
|
|||||||
return enumData.m_enumVal;
|
return enumData.m_enumVal;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( !it->second.empty() )
|
||||||
|
{
|
||||||
|
return it->second.front().m_enumVal;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
@ -134,7 +144,7 @@ QString AppEnumMapper::uiText( const std::string& enumKey, int enumValue ) const
|
|||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
///
|
///
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
int AppEnumMapper::enumValue( const std::string& enumKey, const QString& text ) const
|
int AppEnumMapper::fromText( const std::string& enumKey, const QString& text ) const
|
||||||
{
|
{
|
||||||
auto it = m_enumMap.find( enumKey );
|
auto it = m_enumMap.find( enumKey );
|
||||||
if ( it != m_enumMap.end() )
|
if ( it != m_enumMap.end() )
|
||||||
@ -147,13 +157,13 @@ int AppEnumMapper::enumValue( const std::string& enumKey, const QString& text )
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
return defaultEnumValue( enumKey );
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
///
|
///
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
int AppEnumMapper::enumValue( const std::string& enumKey, int enumIndex ) const
|
int AppEnumMapper::fromIndex( const std::string& enumKey, size_t enumIndex ) const
|
||||||
{
|
{
|
||||||
auto it = m_enumMap.find( enumKey );
|
auto it = m_enumMap.find( enumKey );
|
||||||
if ( it != m_enumMap.end() )
|
if ( it != m_enumMap.end() )
|
||||||
@ -163,7 +173,7 @@ int AppEnumMapper::enumValue( const std::string& enumKey, int enumIndex ) const
|
|||||||
return it->second[enumIndex].m_enumVal;
|
return it->second[enumIndex].m_enumVal;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
return defaultEnumValue( enumKey );
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
@ -39,11 +39,7 @@ public:
|
|||||||
const QString& uiText,
|
const QString& uiText,
|
||||||
const QStringList& aliases = {} );
|
const QStringList& aliases = {} );
|
||||||
|
|
||||||
void addDefaultItem( const std::string& enumKey,
|
void setDefault( const std::string& enumKey, int enumValue );
|
||||||
int enumValue,
|
|
||||||
const QString& text,
|
|
||||||
const QString& uiText,
|
|
||||||
const QStringList& aliases = {} );
|
|
||||||
|
|
||||||
size_t size( const std::string& enumKey ) const;
|
size_t size( const std::string& enumKey ) const;
|
||||||
size_t index( const std::string& enumKey, int enumValue ) const;
|
size_t index( const std::string& enumKey, int enumValue ) const;
|
||||||
@ -51,8 +47,8 @@ public:
|
|||||||
QString uiText( const std::string& enumKey, int enumValue ) const;
|
QString uiText( const std::string& enumKey, int enumValue ) const;
|
||||||
|
|
||||||
int defaultEnumValue( const std::string& enumKey ) const;
|
int defaultEnumValue( const std::string& enumKey ) const;
|
||||||
int enumValue( const std::string& enumKey, const QString& text ) const;
|
int fromText( const std::string& enumKey, const QString& text ) const;
|
||||||
int enumValue( const std::string& enumKey, int enumIndex ) const;
|
int fromIndex( const std::string& enumKey, size_t enumIndex ) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::map<std::string, std::vector<EnumData>> m_enumMap;
|
std::map<std::string, std::vector<EnumData>> m_enumMap;
|
||||||
|
@ -20,6 +20,8 @@ enum class TestEnum2
|
|||||||
ValueC
|
ValueC
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//#define addItem( enumValue, text, uiText ) addItem( enumValue, #enumValue, text, uiText )
|
||||||
|
|
||||||
namespace caf
|
namespace caf
|
||||||
{
|
{
|
||||||
template <>
|
template <>
|
||||||
@ -30,6 +32,17 @@ void caf::AppEnum<TestEnum>::setUp()
|
|||||||
addItem( TestEnum::Value3, "VALUE_3", "Val 3" );
|
addItem( TestEnum::Value3, "VALUE_3", "Val 3" );
|
||||||
addItem( TestEnum::Value4, "VALUE_4", "Val 4" );
|
addItem( TestEnum::Value4, "VALUE_4", "Val 4" );
|
||||||
setDefault( TestEnum::Value2 );
|
setDefault( TestEnum::Value2 );
|
||||||
|
|
||||||
|
auto instance = caf::AppEnumMapper::instance();
|
||||||
|
instance->addItem( caf::cafTypeName<TestEnum>(), caf::convertToInteger<TestEnum>( TestEnum::Value1 ), "VALUE_1", "Val 1" );
|
||||||
|
instance->addItem( caf::cafTypeName<TestEnum>(), caf::convertToInteger<TestEnum>( TestEnum::Value2 ), "VALUE_2", "Val 2" );
|
||||||
|
instance->addItem( caf::cafTypeName<TestEnum>(), caf::convertToInteger<TestEnum>( TestEnum::Value3 ), "VALUE_3", "Val 3" );
|
||||||
|
instance->addItem( caf::cafTypeName<TestEnum>(), caf::convertToInteger<TestEnum>( TestEnum::Value4 ), "VALUE_4", "Val 4" );
|
||||||
|
instance->setDefault( caf::cafTypeName<TestEnum>(), caf::convertToInteger<TestEnum>( TestEnum::Value2 ) );
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
@ -92,3 +105,18 @@ TEST( PdmEnumMapperTest, ConverEnumToInteger )
|
|||||||
EXPECT_EQ( enumValue, sourceEnumValue );
|
EXPECT_EQ( enumValue, sourceEnumValue );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
///
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
TEST( PdmEnumMapperTest, GetUiText )
|
||||||
|
{
|
||||||
|
caf::AppEnumMapper* instance = caf::AppEnumMapper::instance();
|
||||||
|
|
||||||
|
{
|
||||||
|
auto TestEnum_key = caf::cafTypeName<TestEnum>();
|
||||||
|
auto intValue = caf::convertToInteger( TestEnum::Value1 );
|
||||||
|
|
||||||
|
instance->uiText( TestEnum_key, intValue );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user