caf: Finally removed the enum-index-based communication used in the optionValueLists for AppEnums. Now custom option menues based on AppEnums can use the enum values naturally instead of the enum index.

This commit is contained in:
Jacob Støren 2016-10-13 12:11:32 +02:00
parent 89cb050223
commit eb066a71b0
2 changed files with 62 additions and 78 deletions

View File

@ -140,15 +140,8 @@ public:
//================================================================================================== //==================================================================================================
/// Partial specialization for PdmField< caf::AppEnum<T> > /// Partial specialization for PdmField< caf::AppEnum<T> >
///
/// Note : Makes the setUiValue() and uiValue() interface index based, and NOT based on real enum values.
/// The valueOptions() interface is thus also index based (the value in the PdmOptionItemInfo is index NOT enum)
/// This is probably going to change, ans it is strange.
/// This conversion should really be done in the editors we think (now)
//================================================================================================== //==================================================================================================
#define PDMFIELDAPPENUM_USE_INDEX_BASED_INTERFACE 1
template <typename T> template <typename T>
class PdmUiFieldSpecialization < caf::AppEnum<T> > class PdmUiFieldSpecialization < caf::AppEnum<T> >
{ {
@ -156,22 +149,14 @@ public:
/// Convert the field value into a QVariant /// Convert the field value into a QVariant
static QVariant convert(const caf::AppEnum<T>& value) static QVariant convert(const caf::AppEnum<T>& value)
{ {
#if PDMFIELDAPPENUM_USE_INDEX_BASED_INTERFACE int enumIntVal = value;
return QVariant(static_cast<unsigned int>(caf::AppEnum<T>::index(value))); return QVariant(enumIntVal);
#else
unsigned int enumVal = value;
return QVariant(enumVal);
#endif
} }
/// Set the field value from a QVariant /// Set the field value from a QVariant
static void setFromVariant(const QVariant& variantValue, caf::AppEnum<T>& value) static void setFromVariant(const QVariant& variantValue, caf::AppEnum<T>& value)
{ {
#if PDMFIELDAPPENUM_USE_INDEX_BASED_INTERFACE
value.setFromIndex(variantValue.toInt());
#else
value = static_cast<T> (variantValue.toInt()); value = static_cast<T> (variantValue.toInt());
#endif
} }
static bool isDataElementEqual(const QVariant& variantValue, const QVariant& variantValue2) static bool isDataElementEqual(const QVariant& variantValue, const QVariant& variantValue2)
@ -184,16 +169,12 @@ public:
{ {
if (useOptionsOnly) *useOptionsOnly = true; if (useOptionsOnly) *useOptionsOnly = true;
QStringList optionTexts = caf::AppEnum<T>::uiTexts();
QList<PdmOptionItemInfo> optionList; QList<PdmOptionItemInfo> optionList;
int i;
for (i = 0; i < optionTexts.size(); ++i) for (size_t i = 0; i < caf::AppEnum<T>::size(); ++i)
{ {
#if PDMFIELDAPPENUM_USE_INDEX_BASED_INTERFACE int enumIntVal = caf::AppEnum<T>::fromIndex(i);
optionList.push_back(PdmOptionItemInfo(optionTexts[i], static_cast<unsigned int>(i))); optionList.push_back(PdmOptionItemInfo(caf::AppEnum<T>::uiTextFromIndex(i), enumIntVal));
#else
optionList.push_back(PdmOptionItemInfo(optionTexts[i], caf::AppEnum<T>::fromIndex(i)));
#endif
} }
return optionList; return optionList;

View File

@ -10,7 +10,7 @@ namespace caf
/// This method triggers PdmObject::fieldChangedByUi() and PdmObject::updateConnectedEditors(), an thus /// This method triggers PdmObject::fieldChangedByUi() and PdmObject::updateConnectedEditors(), an thus
/// makes the application and the UI aware of the change. /// makes the application and the UI aware of the change.
/// ///
/// Note : If the field has optionValues the interface is _index-based_. The QVariant must contain /// Note : If the field has m_optionEntryCache the interface is _index-based_. The QVariant must contain
/// an UInt representing the index to the option selected by the user interface. /// an UInt representing the index to the option selected by the user interface.
/// ///
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
@ -26,10 +26,14 @@ void caf::PdmFieldUiCap<FieldType>::setValueFromUi(const QVariant& uiValue)
// This has an option based GUI, the uiValue is only indexes into the m_optionEntryCache // This has an option based GUI, the uiValue is only indexes into the m_optionEntryCache
if (uiValue.type() == QVariant::UInt) if (uiValue.type() == QVariant::UInt)
{ {
assert(uiValue.toUInt() < static_cast<unsigned int>(m_optionEntryCache.size())); uint optionIndex = uiValue.toUInt();
typename FieldType::FieldDataType value; assert(optionIndex < static_cast<unsigned int>(m_optionEntryCache.size()));
PdmUiFieldSpecialization<typename FieldType::FieldDataType>::setFromVariant(m_optionEntryCache[uiValue.toUInt()].value, value);
m_field->setValue(value); QVariant optionVariantValue = m_optionEntryCache[optionIndex].value;
typename FieldType::FieldDataType fieldValue;
PdmUiFieldSpecialization<typename FieldType::FieldDataType>::setFromVariant(optionVariantValue, fieldValue);
m_field->setValue(fieldValue);
} }
else if (uiValue.type() == QVariant::List) else if (uiValue.type() == QVariant::List)
{ {
@ -98,7 +102,7 @@ void caf::PdmFieldUiCap<FieldType>::setValueFromUi(const QVariant& uiValue)
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
/// Extracts a QVariant representation of the data in the field to be used in the UI. /// Extracts a QVariant representation of the data in the field to be used in the UI.
/// ///
/// Note : For fields with a none-empty valueOptions list, the returned QVariant contains the /// Note : For fields with a none-empty m_optionEntryCache list, the returned QVariant contains the
/// _indexes_ to the selected options rather than the actual values, if they can be found. /// _indexes_ to the selected options rather than the actual values, if they can be found.
/// ///
/// If this is a multivalue field, and we cant find all of the field values among the options, /// If this is a multivalue field, and we cant find all of the field values among the options,
@ -158,12 +162,22 @@ QVariant caf::PdmFieldUiCap<FieldType>::uiValue() const
template<typename FieldType > template<typename FieldType >
QList<PdmOptionItemInfo> caf::PdmFieldUiCap<FieldType>::valueOptions(bool* useOptionsOnly) QList<PdmOptionItemInfo> caf::PdmFieldUiCap<FieldType>::valueOptions(bool* useOptionsOnly)
{ {
// First check if the owner PdmObject has a value options specification. m_optionEntryCache.clear();
// if it has, use it.
// First check if the owner PdmObject has a value options specification. If it has, we use it.
if (m_field->ownerObject()) if (m_field->ownerObject())
{ {
m_optionEntryCache = uiObj(m_field->ownerObject())->calculateValueOptions(this->m_field, useOptionsOnly); m_optionEntryCache = uiObj(m_field->ownerObject())->calculateValueOptions(this->m_field, useOptionsOnly);
if (m_optionEntryCache.size()) }
// If we got no options, use the options defined by the type. Normally only caf::AppEnum type
if(!m_optionEntryCache.size())
{
m_optionEntryCache = PdmUiFieldSpecialization<typename FieldType::FieldDataType>::valueOptions(useOptionsOnly, m_field->value());
}
if(m_optionEntryCache.size() && isAutoAddingOptionFromValue())
{ {
// Make sure the options contain the field values, event though they not necessarily // Make sure the options contain the field values, event though they not necessarily
// is supplied as possible options by the application. This is a convenience making sure // is supplied as possible options by the application. This is a convenience making sure
@ -180,7 +194,7 @@ QList<PdmOptionItemInfo> caf::PdmFieldUiCap<FieldType>::valueOptions(bool* useOp
// If not all are found, we have to add the missing to the list, to be able to show it // If not all are found, we have to add the missing to the list, to be able to show it
// This will only work if the field data type (or elemnt type for containers) is supported by QVariant.toString(). Custom classes don't // This will only work if the field data type (or elemnt type for containers) is supported by QVariant.toString(). Custom classes don't
if (isAutoAddingOptionFromValue() && !foundAllFieldValues) if( !foundAllFieldValues)
{ {
if(uiBasedQVariant.type() != QVariant::List) // Single value field if(uiBasedQVariant.type() != QVariant::List) // Single value field
{ {
@ -210,20 +224,9 @@ QList<PdmOptionItemInfo> caf::PdmFieldUiCap<FieldType>::valueOptions(bool* useOp
} }
} }
} }
return m_optionEntryCache;
}
} }
// If we have no options, use the options defined by the type. Normally only caf::AppEnum type
#if 0
m_optionEntryCache = PdmFieldTypeSpecialization<typename FieldType::FieldDataType>::valueOptions(useOptionsOnly, m_fieldValue);
return m_optionEntryCache; return m_optionEntryCache;
#else
return PdmUiFieldSpecialization<typename FieldType::FieldDataType>::valueOptions(useOptionsOnly, m_field->value());
#endif
} }
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------