diff --git a/ApplicationCode/ProjectDataModel/RimRegularLegendConfig.cpp b/ApplicationCode/ProjectDataModel/RimRegularLegendConfig.cpp index 4c044d8126..d40ec3e124 100644 --- a/ApplicationCode/ProjectDataModel/RimRegularLegendConfig.cpp +++ b/ApplicationCode/ProjectDataModel/RimRegularLegendConfig.cpp @@ -57,6 +57,7 @@ #include "cafPdmUiComboBoxEditor.h" #include "cafPdmUiLineEditor.h" +#include "cvfMath.h" #include "cvfScalarMapperContinuousLinear.h" #include "cvfScalarMapperContinuousLog.h" #include "cvfScalarMapperDiscreteLinear.h" @@ -99,10 +100,7 @@ void RimRegularLegendConfig::ColorRangeEnum::setUp() addItem( RimRegularLegendConfig::ColorRangesType::UNDEFINED, "UNDEFINED", "Undefined" ); setDefault( RimRegularLegendConfig::ColorRangesType::UNDEFINED ); } -} // namespace caf -namespace caf -{ template <> void RimRegularLegendConfig::MappingEnum::setUp() { @@ -113,10 +111,7 @@ void RimRegularLegendConfig::MappingEnum::setUp() addItem( RimRegularLegendConfig::MappingType::CATEGORY_INTEGER, "Category", "Category" ); setDefault( RimRegularLegendConfig::MappingType::LINEAR_CONTINUOUS ); } -} // namespace caf -namespace caf -{ template <> void AppEnum::setUp() { @@ -125,6 +120,17 @@ void AppEnum::setUp() addItem( RimRegularLegendConfig::NumberFormatType::SCIENTIFIC, "SCIENTIFIC", "Scientific notation" ); setDefault( RimRegularLegendConfig::NumberFormatType::FIXED ); } + +template <> +void AppEnum::setUp() +{ + addItem( RimRegularLegendConfig::CategoryColorModeType::INTERPOLATE, "INTERPOLATE", "Interpolate" ); + addItem( RimRegularLegendConfig::CategoryColorModeType::COLOR_LEGEND_VALUES, + "COLOR_LEGEND_VALUES", + "Color Legend Values" ); + setDefault( RimRegularLegendConfig::CategoryColorModeType::INTERPOLATE ); +} + } // namespace caf //-------------------------------------------------------------------------------------------------- @@ -190,6 +196,9 @@ RimRegularLegendConfig::RimRegularLegendConfig() "", "Min value of the legend (if mapping is logarithmic only positive values are valid)", "" ); + + CAF_PDM_InitFieldNoDefault( &m_categoryColorMode, "CategoryColorMode", "Category Mode", "", "", "" ); + CAF_PDM_InitField( &resultVariableName, "ResultVariableUsage", QString( "" ), "", "", "", "" ); resultVariableName.uiCapability()->setUiHidden( true ); @@ -436,16 +445,7 @@ void RimRegularLegendConfig::updateLegend() m_currentScalarMapper = m_logDiscreteScalarMapper.p(); break; case MappingType::CATEGORY_INTEGER: - m_categoryMapper->setCategoriesWithNames( m_categories, m_categoryNames ); - - if ( m_categoryColors.size() > 0 ) - { - m_categoryMapper->setCycleColors( m_categoryColors ); - } - else - { - m_categoryMapper->setInterpolateColors( legendColors ); - } + configureCategoryMapper(); m_currentScalarMapper = m_categoryMapper.p(); break; default: @@ -617,6 +617,9 @@ void RimRegularLegendConfig::updateFieldVisibility() m_userDefinedMaxValue.uiCapability()->setUiHidden( true ); m_userDefinedMinValue.uiCapability()->setUiHidden( true ); } + + bool isCategoryMappingMode = ( m_mappingMode == MappingType::CATEGORY_INTEGER ); + m_categoryColorMode.uiCapability()->setUiHidden( !isCategoryMappingMode ); } //-------------------------------------------------------------------------------------------------- @@ -709,6 +712,51 @@ void RimRegularLegendConfig::updateCategoryItems() } } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RimRegularLegendConfig::configureCategoryMapper() +{ + if ( m_categoryColorMode() == CategoryColorModeType::COLOR_LEGEND_VALUES ) + { + std::vector legendItems = m_colorLegend()->colorLegendItems(); + cvf::Color3ubArray colorArray; + + if ( !m_categories.empty() ) colorArray.resize( m_categories.size() ); + + colorArray.setAll( cvf::Color3ub( RiaColorTables::undefinedCellColor() ) ); + + for ( auto value : m_categories ) + { + for ( auto legendItem : legendItems ) + { + if ( legendItem->categoryValue() == value ) + { + int zeroBasedIndex = cvf::Math::clamp( value - 1, 0, int( colorArray.size() - 1 ) ); + colorArray.set( zeroBasedIndex, cvf::Color3ub( legendItem->color() ) ); + } + } + } + + m_categoryMapper->setCategoriesValueNameColor( m_categories, m_categoryNames, colorArray ); + } + else if ( m_categoryColorMode() == CategoryColorModeType::INTERPOLATE ) + { + m_categoryMapper->setCategoriesWithNames( m_categories, m_categoryNames ); + + if ( m_categoryColors.size() > 0 ) + { + m_categoryMapper->setCycleColors( m_categoryColors ); + } + else + { + cvf::Color3ubArray legendColors = m_colorLegend()->colorArray(); + + m_categoryMapper->setInterpolateColors( legendColors ); + } + } +} + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- @@ -1018,6 +1066,7 @@ void RimRegularLegendConfig::defineUiOrdering( QString uiConfigName, caf::PdmUiO mappingGr->add( &m_rangeMode ); mappingGr->add( &m_userDefinedMaxValue ); mappingGr->add( &m_userDefinedMinValue ); + mappingGr->add( &m_categoryColorMode ); } updateFieldVisibility(); } diff --git a/ApplicationCode/ProjectDataModel/RimRegularLegendConfig.h b/ApplicationCode/ProjectDataModel/RimRegularLegendConfig.h index 7d7a28f4ec..6858367273 100644 --- a/ApplicationCode/ProjectDataModel/RimRegularLegendConfig.h +++ b/ApplicationCode/ProjectDataModel/RimRegularLegendConfig.h @@ -109,9 +109,16 @@ public: SCIENTIFIC, FIXED }; - typedef caf::AppEnum MappingEnum; - void onRecreateLegend(); + + enum class CategoryColorModeType + { + INTERPOLATE, + COLOR_LEGEND_VALUES + }; + typedef caf::AppEnum CategoryColorModeEnum; + + void onRecreateLegend(); void setColorLegend( RimColorLegend* colorLegend ); RimColorLegend* colorLegend() const; @@ -166,6 +173,7 @@ private: double roundToNumSignificantDigits( double value, double precision ); void updateCategoryItems(); + void configureCategoryMapper(); friend class RimViewLinker; @@ -198,16 +206,18 @@ private: cvf::Color3ubArray m_categoryColors; // Fields - caf::PdmField m_showLegend; - caf::PdmField m_numLevels; - caf::PdmField m_precision; - caf::PdmField> m_tickNumberFormat; - caf::PdmField m_rangeMode; - caf::PdmField m_userDefinedMaxValue; - caf::PdmField m_userDefinedMinValue; - caf::PdmField> m_colorRangeMode_OBSOLETE; - caf::PdmField> m_mappingMode; - caf::PdmPtrField m_colorLegend; + caf::PdmField m_showLegend; + caf::PdmField m_numLevels; + caf::PdmField m_precision; + caf::PdmField> m_tickNumberFormat; + caf::PdmField m_rangeMode; + caf::PdmField m_userDefinedMaxValue; + caf::PdmField m_userDefinedMinValue; + caf::PdmField> m_colorRangeMode_OBSOLETE; + caf::PdmField> m_mappingMode; + caf::PdmField> m_categoryColorMode; + + caf::PdmPtrField m_colorLegend; QString m_title; int m_significantDigitsInData; diff --git a/Fwk/AppFwk/cafVizExtensions/cafCategoryMapper.cpp b/Fwk/AppFwk/cafVizExtensions/cafCategoryMapper.cpp index 4985a65462..1bde21435a 100644 --- a/Fwk/AppFwk/cafVizExtensions/cafCategoryMapper.cpp +++ b/Fwk/AppFwk/cafVizExtensions/cafCategoryMapper.cpp @@ -46,6 +46,20 @@ void CategoryMapper::setCategoriesWithNames( const std::vector& cat setInterpolateColors( *( colorArr.p() ) ); } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void CategoryMapper::setCategoriesValueNameColor( const std::vector& categoryValues, + const std::vector& categoryNames, + const cvf::Color3ubArray& colorArray ) +{ + m_categoryValues = categoryValues; + m_categoryNames = categoryNames; + m_colors = colorArray; + + recomputeMaxTexCoord(); +} + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- diff --git a/Fwk/AppFwk/cafVizExtensions/cafCategoryMapper.h b/Fwk/AppFwk/cafVizExtensions/cafCategoryMapper.h index 0050a86c11..fe9850ce87 100644 --- a/Fwk/AppFwk/cafVizExtensions/cafCategoryMapper.h +++ b/Fwk/AppFwk/cafVizExtensions/cafCategoryMapper.h @@ -20,6 +20,10 @@ public: void setCategories( const std::vector& categoryValues ); void setCategoriesWithNames( const std::vector& categoryValues, const std::vector& categoryNames ); + void setCategoriesValueNameColor( const std::vector& categoryValues, + const std::vector& categoryNames, + const cvf::Color3ubArray& colorArray ); + // Colors in color array are cycled, if category count is larger than color count, colors are reused void setCycleColors( const cvf::Color3ubArray& colorArray );