2012-05-18 02:45:23 -05:00
/////////////////////////////////////////////////////////////////////////////////
//
2014-09-23 08:04:57 -05:00
// Copyright (C) 2011- Statoil ASA
// Copyright (C) 2013- Ceetron Solutions AS
// Copyright (C) 2011-2012 Ceetron AS
2012-05-18 02:45:23 -05:00
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
# include "RimLegendConfig.h"
2013-05-06 05:02:32 -05:00
2016-08-01 15:27:03 -05:00
# include "RiaApplication.h"
2016-08-02 03:25:55 -05:00
# include "RimEclipseCellColors.h"
2016-08-01 15:27:03 -05:00
# include "RimEclipseView.h"
2016-08-02 03:25:55 -05:00
# include "cafCategoryLegend.h"
# include "cafCategoryMapper.h"
2012-06-26 09:10:41 -05:00
# include "cafFactory.h"
2014-05-06 15:40:25 -05:00
# include "cafPdmFieldCvfColor.h"
# include "cafPdmFieldCvfMat4d.h"
2012-06-26 09:10:41 -05:00
# include "cafPdmUiComboBoxEditor.h"
2014-05-06 15:40:25 -05:00
# include "cafPdmUiLineEditor.h"
2013-05-06 05:02:32 -05:00
2013-05-06 03:55:00 -05:00
# include "cvfOverlayScalarMapperLegend.h"
2014-05-06 15:40:25 -05:00
# include "cvfScalarMapperContinuousLinear.h"
# include "cvfScalarMapperContinuousLog.h"
2013-05-06 03:55:00 -05:00
# include "cvfScalarMapperDiscreteLinear.h"
2014-05-06 15:40:25 -05:00
# include "cvfScalarMapperDiscreteLog.h"
2013-05-06 05:02:32 -05:00
# include <cmath>
2016-09-06 03:27:30 -05:00
# include "RimGeoMechCellColors.h"
2016-09-06 05:17:02 -05:00
# include "RimCellEdgeColors.h"
2013-05-06 05:02:32 -05:00
2012-05-18 02:45:23 -05:00
CAF_PDM_SOURCE_INIT ( RimLegendConfig , " Legend " ) ;
namespace caf {
template < >
void AppEnum < RimLegendConfig : : RangeModeType > : : setUp ( )
{
2015-06-29 03:42:56 -05:00
addItem ( RimLegendConfig : : AUTOMATIC_ALLTIMESTEPS , " AUTOMATIC_ALLTIMESTEPS " , " All Timesteps " ) ;
addItem ( RimLegendConfig : : AUTOMATIC_CURRENT_TIMESTEP , " AUTOMATIC_CURRENT_TIMESTEP " , " Current Timestep " ) ;
addItem ( RimLegendConfig : : USER_DEFINED , " USER_DEFINED_MAX_MIN " , " User Defined Range " ) ;
2012-05-18 02:45:23 -05:00
setDefault ( RimLegendConfig : : AUTOMATIC_ALLTIMESTEPS ) ;
}
}
namespace caf {
template < >
void RimLegendConfig : : ColorRangeEnum : : setUp ( )
{
addItem ( RimLegendConfig : : NORMAL , " NORMAL " , " Full color, Red on top " ) ;
addItem ( RimLegendConfig : : OPPOSITE_NORMAL , " OPPOSITE_NORMAL " , " Full color, Blue on top " ) ;
addItem ( RimLegendConfig : : WHITE_PINK , " WHITE_PIMK " , " White to pink " ) ;
addItem ( RimLegendConfig : : PINK_WHITE , " PINK_WHITE " , " Pink to white " ) ;
2016-08-02 03:25:55 -05:00
addItem ( RimLegendConfig : : BLUE_WHITE_RED , " BLUE_WHITE_RED " , " Blue, white, red " ) ;
addItem ( RimLegendConfig : : RED_WHITE_BLUE , " RED_WHITE_BLUE " , " Red, white, blue " ) ;
2012-05-18 02:45:23 -05:00
addItem ( RimLegendConfig : : WHITE_BLACK , " WHITE_BLACK " , " White to black " ) ;
addItem ( RimLegendConfig : : BLACK_WHITE , " BLACK_WHITE " , " Black to white " ) ;
2016-08-02 03:25:55 -05:00
addItem ( RimLegendConfig : : CATEGORY , " CATEGORY " , " Category colors " ) ;
2012-05-18 02:45:23 -05:00
setDefault ( RimLegendConfig : : NORMAL ) ;
}
}
namespace caf {
template < >
void RimLegendConfig : : MappingEnum : : setUp ( )
{
addItem ( RimLegendConfig : : LINEAR_DISCRETE , " LinearDiscrete " , " Discrete Linear " ) ;
addItem ( RimLegendConfig : : LINEAR_CONTINUOUS , " LinearContinuous " , " Continuous Linear " ) ;
addItem ( RimLegendConfig : : LOG10_CONTINUOUS , " Log10Continuous " , " Continuous Logarithmic " ) ;
2012-10-02 03:17:52 -05:00
addItem ( RimLegendConfig : : LOG10_DISCRETE , " Log10Discrete " , " Discrete Logarithmic " ) ;
2016-08-02 03:25:55 -05:00
addItem ( RimLegendConfig : : CATEGORY_INTEGER , " Category " , " Category " ) ;
2012-05-18 02:45:23 -05:00
setDefault ( RimLegendConfig : : LINEAR_CONTINUOUS ) ;
}
}
2013-04-30 07:48:53 -05:00
namespace caf {
template < >
void AppEnum < RimLegendConfig : : NumberFormatType > : : setUp ( )
{
addItem ( RimLegendConfig : : AUTO , " AUTO " , " Automatic " ) ;
addItem ( RimLegendConfig : : FIXED , " FIXED " , " Fixed, decimal " ) ;
addItem ( RimLegendConfig : : SCIENTIFIC , " SCIENTIFIC " , " Scientific notation " ) ;
setDefault ( RimLegendConfig : : FIXED ) ;
}
}
2012-05-18 02:45:23 -05:00
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimLegendConfig : : RimLegendConfig ( )
: m_globalAutoMax ( cvf : : UNDEFINED_DOUBLE ) ,
m_globalAutoMin ( cvf : : UNDEFINED_DOUBLE ) ,
m_localAutoMax ( cvf : : UNDEFINED_DOUBLE ) ,
2013-06-03 06:08:11 -05:00
m_localAutoMin ( cvf : : UNDEFINED_DOUBLE ) ,
m_globalAutoPosClosestToZero ( 0 ) ,
m_globalAutoNegClosestToZero ( 0 ) ,
m_localAutoPosClosestToZero ( 0 ) ,
m_localAutoNegClosestToZero ( 0 )
2012-05-18 02:45:23 -05:00
{
CAF_PDM_InitObject ( " Legend Definition " , " :/Legend.png " , " " , " " ) ;
2013-09-26 03:44:20 -05:00
CAF_PDM_InitField ( & m_numLevels , " NumberOfLevels " , 8 , " Number of levels " , " " , " A hint on how many tick marks you whish. " , " " ) ;
2013-10-03 04:06:18 -05:00
CAF_PDM_InitField ( & m_precision , " Precision " , 4 , " Significant digits " , " " , " The number of significant digits displayed in the legend numbers " , " " ) ;
2013-09-26 03:44:20 -05:00
CAF_PDM_InitField ( & m_tickNumberFormat , " TickNumberFormat " , caf : : AppEnum < RimLegendConfig : : NumberFormatType > ( FIXED ) , " Number format " , " " , " " , " " ) ;
2013-04-30 07:48:53 -05:00
2013-09-26 03:44:20 -05:00
CAF_PDM_InitField ( & m_colorRangeMode , " ColorRangeMode " , ColorRangeEnum ( NORMAL ) , " Colors " , " " , " " , " " ) ;
2012-05-18 02:45:23 -05:00
CAF_PDM_InitField ( & m_mappingMode , " MappingMode " , MappingEnum ( LINEAR_CONTINUOUS ) , " Mapping " , " " , " " , " " ) ;
2014-04-25 09:32:06 -05:00
CAF_PDM_InitField ( & m_rangeMode , " RangeType " , RangeModeEnum ( AUTOMATIC_ALLTIMESTEPS ) , " Range type " , " " , " Switches between automatic and user defined range on the legend " , " " ) ;
2012-05-18 02:45:23 -05:00
CAF_PDM_InitField ( & m_userDefinedMaxValue , " UserDefinedMax " , 1.0 , " Max " , " " , " Min value of the legend " , " " ) ;
CAF_PDM_InitField ( & m_userDefinedMinValue , " UserDefinedMin " , 0.0 , " Min " , " " , " Max value of the legend " , " " ) ;
CAF_PDM_InitField ( & resultVariableName , " ResultVariableUsage " , QString ( " " ) , " " , " " , " " , " " ) ;
2015-08-05 06:27:36 -05:00
resultVariableName . uiCapability ( ) - > setUiHidden ( true ) ;
2012-05-18 02:45:23 -05:00
2012-10-02 03:17:52 -05:00
m_linDiscreteScalarMapper = new cvf : : ScalarMapperDiscreteLinear ;
m_logDiscreteScalarMapper = new cvf : : ScalarMapperDiscreteLog ;
2012-05-18 02:45:23 -05:00
m_linSmoothScalarMapper = new cvf : : ScalarMapperContinuousLinear ;
2012-10-02 03:17:52 -05:00
m_logSmoothScalarMapper = new cvf : : ScalarMapperContinuousLog ;
2012-05-18 02:45:23 -05:00
m_currentScalarMapper = m_linDiscreteScalarMapper ;
2016-08-02 03:25:55 -05:00
m_categoryMapper = new caf : : CategoryMapper ;
2013-04-19 01:02:36 -05:00
cvf : : Font * standardFont = RiaApplication : : instance ( ) - > standardFont ( ) ;
2016-08-01 15:27:03 -05:00
m_scalarMapperLegend = new cvf : : OverlayScalarMapperLegend ( standardFont ) ;
2016-08-02 03:25:55 -05:00
m_categoryLegend = new caf : : CategoryLegend ( standardFont , m_categoryMapper . p ( ) ) ;
2012-05-18 02:45:23 -05:00
updateFieldVisibility ( ) ;
updateLegend ( ) ;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimLegendConfig : : ~ RimLegendConfig ( )
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimLegendConfig : : fieldChangedByUi ( const caf : : PdmFieldHandle * changedField , const QVariant & oldValue , const QVariant & newValue )
{
if ( changedField = = & m_numLevels )
{
int upperLimit = std : : numeric_limits < int > : : max ( ) ;
m_numLevels = cvf : : Math : : clamp ( m_numLevels . v ( ) , 1 , upperLimit ) ;
}
2016-08-02 03:25:55 -05:00
else if ( changedField = = & m_rangeMode | |
changedField = = & m_mappingMode )
2012-05-18 02:45:23 -05:00
{
if ( m_rangeMode = = USER_DEFINED )
{
if ( m_userDefinedMaxValue = = m_userDefinedMaxValue . defaultValue ( ) & & m_globalAutoMax ! = cvf : : UNDEFINED_DOUBLE )
{
2013-09-25 18:05:09 -05:00
m_userDefinedMaxValue = roundToNumSignificantDigits ( m_globalAutoMax , m_precision ) ;
2012-05-18 02:45:23 -05:00
}
if ( m_userDefinedMinValue = = m_userDefinedMinValue . defaultValue ( ) & & m_globalAutoMin ! = cvf : : UNDEFINED_DOUBLE )
{
2013-09-25 18:05:09 -05:00
m_userDefinedMinValue = roundToNumSignificantDigits ( m_globalAutoMin , m_precision ) ;
2012-05-18 02:45:23 -05:00
}
}
updateFieldVisibility ( ) ;
}
updateLegend ( ) ;
if ( m_reservoirView ) m_reservoirView - > updateCurrentTimeStepAndRedraw ( ) ;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimLegendConfig : : updateLegend ( )
{
double adjustedMin = cvf : : UNDEFINED_DOUBLE ;
double adjustedMax = cvf : : UNDEFINED_DOUBLE ;
2013-06-03 06:08:11 -05:00
double posClosestToZero = cvf : : UNDEFINED_DOUBLE ;
double negClosestToZero = cvf : : UNDEFINED_DOUBLE ;
2012-05-18 02:45:23 -05:00
if ( m_rangeMode = = AUTOMATIC_ALLTIMESTEPS )
{
2013-09-25 18:05:09 -05:00
adjustedMin = roundToNumSignificantDigits ( m_globalAutoMin , m_precision ) ;
adjustedMax = roundToNumSignificantDigits ( m_globalAutoMax , m_precision ) ;
2013-06-03 06:08:11 -05:00
posClosestToZero = m_globalAutoPosClosestToZero ;
negClosestToZero = m_globalAutoNegClosestToZero ;
2016-08-02 03:25:55 -05:00
m_categoryMapper - > setCategories ( m_globalCategories ) ;
2012-05-18 02:45:23 -05:00
}
else if ( m_rangeMode = = AUTOMATIC_CURRENT_TIMESTEP )
{
2013-09-25 18:05:09 -05:00
adjustedMin = roundToNumSignificantDigits ( m_localAutoMin , m_precision ) ;
adjustedMax = roundToNumSignificantDigits ( m_localAutoMax , m_precision ) ;
2013-06-03 06:08:11 -05:00
posClosestToZero = m_localAutoPosClosestToZero ;
negClosestToZero = m_localAutoNegClosestToZero ;
2016-08-02 03:25:55 -05:00
m_categoryMapper - > setCategories ( m_localCategories ) ;
2012-05-18 02:45:23 -05:00
}
else
{
2013-09-25 18:05:09 -05:00
adjustedMin = roundToNumSignificantDigits ( m_userDefinedMinValue , m_precision ) ;
adjustedMax = roundToNumSignificantDigits ( m_userDefinedMaxValue , m_precision ) ;
2013-06-03 06:08:11 -05:00
posClosestToZero = m_globalAutoPosClosestToZero ;
negClosestToZero = m_globalAutoNegClosestToZero ;
2012-05-18 02:45:23 -05:00
}
m_linDiscreteScalarMapper - > setRange ( adjustedMin , adjustedMax ) ;
2013-06-03 06:08:11 -05:00
m_linSmoothScalarMapper - > setRange ( adjustedMin , adjustedMax ) ;
if ( m_mappingMode = = LOG10_CONTINUOUS | | m_mappingMode = = LOG10_DISCRETE )
{
if ( adjustedMin ! = adjustedMax )
{
if ( adjustedMin = = 0 )
{
if ( adjustedMax > adjustedMin )
{
adjustedMin = posClosestToZero ;
}
else
{
adjustedMin = negClosestToZero ;
}
}
else if ( adjustedMax = = 0 )
{
if ( adjustedMin > adjustedMax )
{
adjustedMax = posClosestToZero ;
}
else
{
adjustedMax = negClosestToZero ;
}
}
else if ( adjustedMin < 0 & & adjustedMax > 0 )
{
adjustedMin = posClosestToZero ;
}
else if ( adjustedMax < 0 & & adjustedMin > 0 )
{
adjustedMin = negClosestToZero ;
}
}
}
2012-10-02 03:17:52 -05:00
m_logDiscreteScalarMapper - > setRange ( adjustedMin , adjustedMax ) ;
2012-05-18 02:45:23 -05:00
m_logSmoothScalarMapper - > setRange ( adjustedMin , adjustedMax ) ;
cvf : : Color3ubArray legendColors ;
switch ( m_colorRangeMode ( ) )
{
case NORMAL :
{
2012-10-02 03:17:52 -05:00
legendColors . reserve ( 7 ) ;
2012-05-18 02:45:23 -05:00
legendColors . add ( cvf : : Color3ub ( 0 , 0 , 255 ) ) ;
2012-10-02 03:17:52 -05:00
legendColors . add ( cvf : : Color3ub ( 0 , 127 , 255 ) ) ;
2012-05-18 02:45:23 -05:00
legendColors . add ( cvf : : Color3ub ( 0 , 255 , 255 ) ) ;
legendColors . add ( cvf : : Color3ub ( 0 , 255 , 0 ) ) ;
legendColors . add ( cvf : : Color3ub ( 255 , 255 , 0 ) ) ;
2012-10-02 03:17:52 -05:00
legendColors . add ( cvf : : Color3ub ( 255 , 127 , 0 ) ) ;
2012-05-18 02:45:23 -05:00
legendColors . add ( cvf : : Color3ub ( 255 , 0 , 0 ) ) ;
}
break ;
case OPPOSITE_NORMAL :
{
2012-10-02 03:17:52 -05:00
legendColors . reserve ( 7 ) ;
2012-05-18 02:45:23 -05:00
legendColors . add ( cvf : : Color3ub ( 255 , 0 , 0 ) ) ;
2012-10-02 03:17:52 -05:00
legendColors . add ( cvf : : Color3ub ( 255 , 127 , 0 ) ) ;
2012-05-18 02:45:23 -05:00
legendColors . add ( cvf : : Color3ub ( 255 , 255 , 0 ) ) ;
legendColors . add ( cvf : : Color3ub ( 0 , 255 , 0 ) ) ;
legendColors . add ( cvf : : Color3ub ( 0 , 255 , 255 ) ) ;
2012-10-02 03:17:52 -05:00
legendColors . add ( cvf : : Color3ub ( 0 , 127 , 255 ) ) ;
2012-05-18 02:45:23 -05:00
legendColors . add ( cvf : : Color3ub ( 0 , 0 , 255 ) ) ;
}
2013-09-25 08:43:06 -05:00
break ;
case BLACK_WHITE :
2012-05-18 02:45:23 -05:00
case WHITE_BLACK :
{
legendColors . reserve ( 2 ) ;
if ( m_colorRangeMode ( ) = = BLACK_WHITE )
{
legendColors . add ( cvf : : Color3ub : : BLACK ) ;
legendColors . add ( cvf : : Color3ub : : WHITE ) ;
}
else
{
legendColors . add ( cvf : : Color3ub : : WHITE ) ;
legendColors . add ( cvf : : Color3ub : : BLACK ) ;
}
}
break ;
case PINK_WHITE :
case WHITE_PINK :
{
legendColors . reserve ( 2 ) ;
if ( m_colorRangeMode ( ) = = PINK_WHITE )
{
legendColors . add ( cvf : : Color3ub : : DEEP_PINK ) ;
legendColors . add ( cvf : : Color3ub : : WHITE ) ;
}
else
{
legendColors . add ( cvf : : Color3ub : : WHITE ) ;
legendColors . add ( cvf : : Color3ub : : DEEP_PINK ) ;
}
}
break ;
2013-09-25 08:43:06 -05:00
case BLUE_WHITE_RED :
case RED_WHITE_BLUE :
{
legendColors . reserve ( 3 ) ;
if ( m_colorRangeMode ( ) = = BLUE_WHITE_RED )
{
legendColors . add ( cvf : : Color3ub : : BLUE ) ;
legendColors . add ( cvf : : Color3ub : : WHITE ) ;
legendColors . add ( cvf : : Color3ub : : RED ) ;
}
else
{
legendColors . add ( cvf : : Color3ub : : RED ) ;
legendColors . add ( cvf : : Color3ub : : WHITE ) ;
legendColors . add ( cvf : : Color3ub : : BLUE ) ;
}
}
break ;
2016-08-02 03:25:55 -05:00
case CATEGORY :
{
// Based on http://stackoverflow.com/questions/470690/how-to-automatically-generate-n-distinct-colors
// and Kelly Colors
legendColors . reserve ( 20 ) ;
legendColors . add ( cvf : : Color3ub ( 255 , 179 , 0 ) ) ; // vivid_yellow
legendColors . add ( cvf : : Color3ub ( 128 , 62 , 117 ) ) ; // strong_purple
legendColors . add ( cvf : : Color3ub ( 255 , 104 , 0 ) ) ; // vivid_orange
legendColors . add ( cvf : : Color3ub ( 166 , 189 , 215 ) ) ; // very_light_blue
legendColors . add ( cvf : : Color3ub ( 193 , 0 , 32 ) ) ; // vivid_red
legendColors . add ( cvf : : Color3ub ( 206 , 162 , 98 ) ) ; // grayish_yellow
legendColors . add ( cvf : : Color3ub ( 129 , 112 , 102 ) ) ; // medium_gray
// these aren't good for people with defective color vision
legendColors . add ( cvf : : Color3ub ( 0 , 125 , 52 ) ) ; // vivid_green
legendColors . add ( cvf : : Color3ub ( 246 , 118 , 142 ) ) ; //strong_purplish_pink
legendColors . add ( cvf : : Color3ub ( 0 , 83 , 138 ) ) ; //strong_blue
legendColors . add ( cvf : : Color3ub ( 255 , 122 , 92 ) ) ; //strong_yellowish_pink
legendColors . add ( cvf : : Color3ub ( 83 , 55 , 122 ) ) ; //strong_violet
legendColors . add ( cvf : : Color3ub ( 255 , 142 , 0 ) ) ; //vivid_orange_yellow
legendColors . add ( cvf : : Color3ub ( 179 , 40 , 81 ) ) ; //strong_purplish_red
legendColors . add ( cvf : : Color3ub ( 244 , 200 , 0 ) ) ; //vivid_greenish_yellow
legendColors . add ( cvf : : Color3ub ( 127 , 24 , 13 ) ) ; //strong_reddish_brown
legendColors . add ( cvf : : Color3ub ( 147 , 170 , 0 ) ) ; //vivid_yellowish_green
legendColors . add ( cvf : : Color3ub ( 89 , 51 , 21 ) ) ; //deep_yellowish_brown
legendColors . add ( cvf : : Color3ub ( 241 , 58 , 19 ) ) ; //vivid_reddish_orange
legendColors . add ( cvf : : Color3ub ( 35 , 44 , 22 ) ) ; //dark_olive_green
}
break ;
2012-05-18 02:45:23 -05:00
}
2012-10-02 03:17:52 -05:00
m_linDiscreteScalarMapper - > setColors ( legendColors ) ;
m_logDiscreteScalarMapper - > setColors ( legendColors ) ;
2012-05-18 02:45:23 -05:00
m_logSmoothScalarMapper - > setColors ( legendColors ) ;
m_linSmoothScalarMapper - > setColors ( legendColors ) ;
2016-08-02 03:25:55 -05:00
m_categoryMapper - > setColors ( legendColors ) ;
2012-10-02 03:17:52 -05:00
m_linDiscreteScalarMapper - > setLevelCount ( m_numLevels , true ) ;
m_logDiscreteScalarMapper - > setLevelCount ( m_numLevels , true ) ;
m_logSmoothScalarMapper - > setLevelCount ( m_numLevels , true ) ;
m_linSmoothScalarMapper - > setLevelCount ( m_numLevels , true ) ;
2012-05-18 02:45:23 -05:00
switch ( m_mappingMode ( ) )
{
case LINEAR_DISCRETE :
m_currentScalarMapper = m_linDiscreteScalarMapper . p ( ) ;
break ;
case LINEAR_CONTINUOUS :
m_currentScalarMapper = m_linSmoothScalarMapper . p ( ) ;
break ;
case LOG10_CONTINUOUS :
m_currentScalarMapper = m_logSmoothScalarMapper . p ( ) ;
break ;
2012-10-02 03:17:52 -05:00
case LOG10_DISCRETE :
m_currentScalarMapper = m_logDiscreteScalarMapper . p ( ) ;
break ;
2016-08-02 03:25:55 -05:00
case CATEGORY_INTEGER :
m_currentScalarMapper = m_categoryMapper . p ( ) ;
break ;
2012-05-18 02:45:23 -05:00
default :
break ;
}
2012-10-02 03:17:52 -05:00
2016-08-02 03:25:55 -05:00
if ( m_currentScalarMapper ! = m_categoryMapper . p ( ) )
{
m_scalarMapperLegend - > setScalarMapper ( m_currentScalarMapper . p ( ) ) ;
}
2013-09-25 18:05:09 -05:00
double decadesInRange = 0 ;
2013-04-30 07:48:53 -05:00
2013-09-25 18:05:09 -05:00
if ( m_mappingMode = = LOG10_CONTINUOUS | | m_mappingMode = = LOG10_DISCRETE )
{
2013-09-26 03:44:20 -05:00
// For log mapping, use the min value as reference for num valid digits
2013-09-27 07:43:11 -05:00
decadesInRange = cvf : : Math : : abs ( adjustedMin ) < cvf : : Math : : abs ( adjustedMax ) ? cvf : : Math : : abs ( adjustedMin ) : cvf : : Math : : abs ( adjustedMax ) ;
2013-09-25 18:05:09 -05:00
decadesInRange = log10 ( decadesInRange ) ;
}
else
{
2013-09-26 03:44:20 -05:00
// For linear mapping, use the max value as reference for num valid digits
2013-09-27 07:43:11 -05:00
double absRange = CVF_MAX ( cvf : : Math : : abs ( adjustedMax ) , cvf : : Math : : abs ( adjustedMin ) ) ;
2013-09-25 18:05:09 -05:00
decadesInRange = log10 ( absRange ) ;
}
decadesInRange = cvf : : Math : : ceil ( decadesInRange ) ;
// Using Fixed format
2013-04-30 07:48:53 -05:00
NumberFormatType nft = m_tickNumberFormat ( ) ;
2016-08-01 15:27:03 -05:00
m_scalarMapperLegend - > setTickFormat ( ( cvf : : OverlayScalarMapperLegend : : NumberFormat ) nft ) ;
2012-05-18 02:45:23 -05:00
2013-09-25 18:05:09 -05:00
// Set the fixed number of digits after the decimal point to the number needed to show all the significant digits.
2013-09-26 03:44:20 -05:00
int numDecimalDigits = m_precision ( ) ;
if ( nft ! = SCIENTIFIC )
{
2013-09-27 07:43:11 -05:00
numDecimalDigits - = static_cast < int > ( decadesInRange ) ;
2013-09-26 03:44:20 -05:00
}
2016-08-01 15:27:03 -05:00
m_scalarMapperLegend - > setTickPrecision ( cvf : : Math : : clamp ( numDecimalDigits , 0 , 20 ) ) ;
2013-09-25 18:05:09 -05:00
2012-05-18 02:45:23 -05:00
if ( m_globalAutoMax ! = cvf : : UNDEFINED_DOUBLE )
{
2015-08-05 06:27:36 -05:00
m_userDefinedMaxValue . uiCapability ( ) - > setUiName ( QString ( " Max " ) + " ( " + QString : : number ( m_globalAutoMax , ' g ' , m_precision ) + " ) " ) ;
2012-05-18 02:45:23 -05:00
}
else
{
2015-08-05 06:27:36 -05:00
m_userDefinedMaxValue . uiCapability ( ) - > setUiName ( QString ( ) ) ;
2012-05-18 02:45:23 -05:00
}
if ( m_globalAutoMin ! = cvf : : UNDEFINED_DOUBLE )
{
2015-08-05 06:27:36 -05:00
m_userDefinedMinValue . uiCapability ( ) - > setUiName ( QString ( " Min " ) + " ( " + QString : : number ( m_globalAutoMin , ' g ' , m_precision ) + " ) " ) ;
2012-05-18 02:45:23 -05:00
}
else
{
2015-08-05 06:27:36 -05:00
m_userDefinedMinValue . uiCapability ( ) - > setUiName ( QString ( ) ) ;
2012-05-18 02:45:23 -05:00
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimLegendConfig : : setAutomaticRanges ( double globalMin , double globalMax , double localMin , double localMax )
{
2013-09-30 06:29:29 -05:00
double candidateGlobalAutoMin = roundToNumSignificantDigits ( globalMin , m_precision ) ;
double candidateGlobalAutoMax = roundToNumSignificantDigits ( globalMax , m_precision ) ;
2012-05-18 02:45:23 -05:00
2013-09-30 06:29:29 -05:00
double candidateLocalAutoMin = roundToNumSignificantDigits ( localMin , m_precision ) ;
double candidateLocalAutoMax = roundToNumSignificantDigits ( localMax , m_precision ) ;
2012-05-18 02:45:23 -05:00
2013-09-30 06:29:29 -05:00
bool needsUpdate = false ;
const double epsilon = std : : numeric_limits < double > : : epsilon ( ) ;
if ( cvf : : Math : : abs ( candidateGlobalAutoMax - m_globalAutoMax ) > epsilon )
{
needsUpdate = true ;
}
if ( cvf : : Math : : abs ( candidateGlobalAutoMin - m_globalAutoMin ) > epsilon )
{
needsUpdate = true ;
}
if ( cvf : : Math : : abs ( candidateLocalAutoMax - m_localAutoMax ) > epsilon )
{
needsUpdate = true ;
}
if ( cvf : : Math : : abs ( candidateLocalAutoMin - m_localAutoMin ) > epsilon )
{
needsUpdate = true ;
}
if ( needsUpdate )
{
m_globalAutoMin = candidateGlobalAutoMin ;
m_globalAutoMax = candidateGlobalAutoMax ;
m_localAutoMin = candidateLocalAutoMin ;
m_localAutoMax = candidateLocalAutoMax ;
updateLegend ( ) ;
}
2012-05-18 02:45:23 -05:00
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimLegendConfig : : initAfterRead ( )
{
updateFieldVisibility ( ) ;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimLegendConfig : : updateFieldVisibility ( )
{
2016-08-02 03:25:55 -05:00
bool showRangeItems = m_mappingMode = = CATEGORY_INTEGER ? false : true ;
m_numLevels . uiCapability ( ) - > setUiHidden ( ! showRangeItems ) ;
m_precision . uiCapability ( ) - > setUiHidden ( ! showRangeItems ) ;
m_tickNumberFormat . uiCapability ( ) - > setUiHidden ( ! showRangeItems ) ;
m_rangeMode . uiCapability ( ) - > setUiHidden ( ! showRangeItems ) ;
if ( showRangeItems & & m_rangeMode = = USER_DEFINED )
2012-05-18 02:45:23 -05:00
{
2015-08-05 06:27:36 -05:00
m_userDefinedMaxValue . uiCapability ( ) - > setUiHidden ( false ) ;
m_userDefinedMinValue . uiCapability ( ) - > setUiHidden ( false ) ;
2012-05-18 02:45:23 -05:00
}
else
{
2015-08-05 06:27:36 -05:00
m_userDefinedMaxValue . uiCapability ( ) - > setUiHidden ( true ) ;
m_userDefinedMinValue . uiCapability ( ) - > setUiHidden ( true ) ;
2012-05-18 02:45:23 -05:00
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimLegendConfig : : setColorRangeMode ( ColorRangesType colorMode )
{
m_colorRangeMode = colorMode ;
updateLegend ( ) ;
}
2016-09-06 07:03:02 -05:00
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimLegendConfig : : setMappingMode ( MappingType mappingType )
{
m_mappingMode = mappingType ;
updateLegend ( ) ;
}
2012-10-02 03:17:52 -05:00
/*
2012-05-18 02:45:23 -05:00
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
cvf : : ref < cvf : : Color3ubArray > RimLegendConfig : : interpolateColorArray ( const cvf : : Color3ubArray & colorArray , cvf : : uint targetColorCount )
{
uint inputColorCount = static_cast < uint > ( colorArray . size ( ) ) ;
CVF_ASSERT ( inputColorCount > 1 ) ;
CVF_ASSERT ( targetColorCount > 1 ) ;
cvf : : ref < cvf : : Color3ubArray > colors = new cvf : : Color3ubArray ;
colors - > reserve ( targetColorCount ) ;
const uint inputLevelCount = inputColorCount - 1 ;
const uint outputLevelCount = targetColorCount - 1 ;
uint outputLevelIdx ;
for ( outputLevelIdx = 0 ; outputLevelIdx < outputLevelCount ; outputLevelIdx + + )
{
double dblInputLevelIndex = inputLevelCount * ( outputLevelIdx / static_cast < double > ( outputLevelCount ) ) ;
const uint inputLevelIndex = static_cast < uint > ( dblInputLevelIndex ) ;
CVF_ASSERT ( inputLevelIndex < inputLevelCount ) ;
double t = dblInputLevelIndex - inputLevelIndex ;
CVF_ASSERT ( t > = 0 & & t < = 1.0 ) ;
cvf : : Color3ub c1 = colorArray [ inputLevelIndex ] ;
cvf : : Color3ub c2 = colorArray [ inputLevelIndex + 1 ] ;
int r = static_cast < int > ( c1 . r ( ) + t * ( c2 . r ( ) - c1 . r ( ) ) + 0.5 ) ;
int g = static_cast < int > ( c1 . g ( ) + t * ( c2 . g ( ) - c1 . g ( ) ) + 0.5 ) ;
int b = static_cast < int > ( c1 . b ( ) + t * ( c2 . b ( ) - c1 . b ( ) ) + 0.5 ) ;
r = cvf : : Math : : clamp ( r , 0 , 255 ) ;
g = cvf : : Math : : clamp ( g , 0 , 255 ) ;
b = cvf : : Math : : clamp ( b , 0 , 255 ) ;
cvf : : Color3ub col ( ( cvf : : ubyte ) r , ( cvf : : ubyte ) g , ( cvf : : ubyte ) b ) ;
colors - > add ( col ) ;
}
colors - > add ( colorArray [ colorArray . size ( ) - 1 ] ) ;
return colors ;
}
2012-10-02 03:17:52 -05:00
*/
2012-05-18 02:45:23 -05:00
2016-08-02 03:25:55 -05:00
2012-05-18 02:45:23 -05:00
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimLegendConfig : : recreateLegend ( )
{
// Due to possible visualization bug, we need to recreate the legend if the last viewer
// has been removed, (and thus the opengl resources has been deleted) The text in
// the legend disappeared because of this, so workaround: recreate the legend when needed:
2013-04-19 01:02:36 -05:00
cvf : : Font * standardFont = RiaApplication : : instance ( ) - > standardFont ( ) ;
2016-08-01 15:27:03 -05:00
m_scalarMapperLegend = new cvf : : OverlayScalarMapperLegend ( standardFont ) ;
2016-08-02 03:25:55 -05:00
m_categoryLegend = new caf : : CategoryLegend ( standardFont , m_categoryMapper . p ( ) ) ;
2012-05-18 02:45:23 -05:00
updateLegend ( ) ;
}
//--------------------------------------------------------------------------------------------------
2013-09-25 18:05:09 -05:00
/// Rounding the double value to given number of significant digits
2012-05-18 02:45:23 -05:00
//--------------------------------------------------------------------------------------------------
2013-09-25 18:05:09 -05:00
double RimLegendConfig : : roundToNumSignificantDigits ( double domainValue , double numSignificantDigits )
2012-05-18 02:45:23 -05:00
{
2013-02-26 05:45:41 -06:00
double absDomainValue = cvf : : Math : : abs ( domainValue ) ;
if ( absDomainValue = = 0.0 )
2012-05-18 02:45:23 -05:00
{
return 0.0 ;
}
2013-02-26 05:45:41 -06:00
double logDecValue = log10 ( absDomainValue ) ;
2012-05-18 02:45:23 -05:00
logDecValue = cvf : : Math : : ceil ( logDecValue ) ;
2013-09-25 18:05:09 -05:00
double factor = pow ( 10.0 , numSignificantDigits - logDecValue ) ;
2012-05-18 02:45:23 -05:00
double tmp = domainValue * factor ;
double integerPart ;
2013-09-25 18:05:09 -05:00
double fraction = modf ( tmp , & integerPart ) ;
2013-09-27 07:43:11 -05:00
if ( cvf : : Math : : abs ( fraction ) > = 0.5 ) ( integerPart > = 0 ) ? integerPart + + : integerPart - - ;
2012-05-18 02:45:23 -05:00
double newDomainValue = integerPart / factor ;
return newDomainValue ;
}
2013-06-03 06:08:11 -05:00
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimLegendConfig : : setClosestToZeroValues ( double globalPosClosestToZero , double globalNegClosestToZero , double localPosClosestToZero , double localNegClosestToZero )
{
2013-09-30 06:29:29 -05:00
bool needsUpdate = false ;
const double epsilon = std : : numeric_limits < double > : : epsilon ( ) ;
2013-06-03 06:08:11 -05:00
2013-09-30 06:29:29 -05:00
if ( cvf : : Math : : abs ( globalPosClosestToZero - m_globalAutoPosClosestToZero ) > epsilon )
{
needsUpdate = true ;
}
if ( cvf : : Math : : abs ( globalNegClosestToZero - m_globalAutoNegClosestToZero ) > epsilon )
{
needsUpdate = true ;
}
if ( cvf : : Math : : abs ( localPosClosestToZero - m_localAutoPosClosestToZero ) > epsilon )
{
needsUpdate = true ;
}
if ( cvf : : Math : : abs ( localNegClosestToZero - m_localAutoNegClosestToZero ) > epsilon )
{
needsUpdate = true ;
}
2013-06-03 06:08:11 -05:00
2013-09-30 06:29:29 -05:00
if ( needsUpdate )
{
m_globalAutoPosClosestToZero = globalPosClosestToZero ;
m_globalAutoNegClosestToZero = globalNegClosestToZero ;
m_localAutoPosClosestToZero = localPosClosestToZero ;
m_localAutoNegClosestToZero = localNegClosestToZero ;
if ( m_globalAutoPosClosestToZero = = HUGE_VAL ) m_globalAutoPosClosestToZero = 0 ;
if ( m_globalAutoNegClosestToZero = = - HUGE_VAL ) m_globalAutoNegClosestToZero = 0 ;
if ( m_localAutoPosClosestToZero = = HUGE_VAL ) m_localAutoPosClosestToZero = 0 ;
if ( m_localAutoNegClosestToZero = = - HUGE_VAL ) m_localAutoNegClosestToZero = 0 ;
updateLegend ( ) ;
}
2013-06-03 06:08:11 -05:00
}
2016-08-02 03:25:55 -05:00
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimLegendConfig : : setCategories ( const std : : set < int > & globalCategories , const std : : set < int > & localCategories )
{
m_globalCategories . resize ( globalCategories . size ( ) ) ;
m_localCategories . resize ( localCategories . size ( ) ) ;
{
size_t i = 0 ;
for ( auto val : globalCategories )
{
m_globalCategories . set ( i + + , val ) ;
}
}
{
size_t i = 0 ;
for ( auto val : localCategories )
{
m_localCategories . set ( i + + , val ) ;
}
}
2016-09-06 07:03:02 -05:00
updateLegend ( ) ;
2016-08-02 03:25:55 -05:00
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimLegendConfig : : setTitle ( const cvf : : String & title )
{
m_scalarMapperLegend - > setTitle ( title ) ;
m_categoryLegend - > setTitle ( title ) ;
}
2016-08-01 15:27:03 -05:00
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
cvf : : OverlayItem * RimLegendConfig : : legend ( )
{
2016-08-02 03:25:55 -05:00
if ( m_currentScalarMapper = = m_categoryMapper )
{
return m_categoryLegend . p ( ) ;
}
else
{
return m_scalarMapperLegend . p ( ) ;
}
2016-08-01 15:27:03 -05:00
}
2013-09-26 03:44:20 -05:00
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimLegendConfig : : defineUiOrdering ( QString uiConfigName , caf : : PdmUiOrdering & uiOrdering )
{
2014-04-14 01:04:09 -05:00
{
caf : : PdmUiOrdering * formatGr = uiOrdering . addNewGroup ( " Format " ) ;
formatGr - > add ( & m_numLevels ) ;
formatGr - > add ( & m_precision ) ;
formatGr - > add ( & m_tickNumberFormat ) ;
formatGr - > add ( & m_colorRangeMode ) ;
caf : : PdmUiOrdering * mappingGr = uiOrdering . addNewGroup ( " Mapping " ) ;
mappingGr - > add ( & m_mappingMode ) ;
mappingGr - > add ( & m_rangeMode ) ;
mappingGr - > add ( & m_userDefinedMaxValue ) ;
mappingGr - > add ( & m_userDefinedMinValue ) ;
}
2016-09-06 07:03:02 -05:00
updateFieldVisibility ( ) ;
2014-04-14 01:04:09 -05:00
}
2016-08-02 03:25:55 -05:00
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QList < caf : : PdmOptionItemInfo > RimLegendConfig : : calculateValueOptions ( const caf : : PdmFieldHandle * fieldNeedingOptions , bool * useOptionsOnly )
{
QStringList optionTexts ;
bool isCategoryResult = false ;
{
2016-09-06 03:27:30 -05:00
RimEclipseCellColors * eclCellColors = nullptr ;
this - > firstAnchestorOrThisOfType ( eclCellColors ) ;
RimGeoMechCellColors * gmCellColors = nullptr ;
this - > firstAnchestorOrThisOfType ( gmCellColors ) ;
2016-09-06 05:17:02 -05:00
RimCellEdgeColors * eclCellEdgColors = nullptr ;
this - > firstAnchestorOrThisOfType ( eclCellEdgColors ) ;
2016-09-06 03:27:30 -05:00
2016-09-06 05:17:02 -05:00
if ( ( eclCellColors & & eclCellColors - > hasCategoryResult ( ) )
| | ( gmCellColors & & gmCellColors - > hasCategoryResult ( ) )
| | ( eclCellEdgColors & & eclCellEdgColors - > hasCategoryResult ( ) ) )
2016-09-06 03:27:30 -05:00
{
isCategoryResult = true ;
}
2016-08-02 03:25:55 -05:00
}
if ( fieldNeedingOptions = = & m_mappingMode )
{
// This is an app enum field, see cafInternalPdmFieldTypeSpecializations.h for the default specialization of this type
optionTexts < < m_mappingMode . v ( ) . uiText ( LINEAR_DISCRETE ) ;
optionTexts < < m_mappingMode . v ( ) . uiText ( LINEAR_CONTINUOUS ) ;
optionTexts < < m_mappingMode . v ( ) . uiText ( LOG10_CONTINUOUS ) ;
optionTexts < < m_mappingMode . v ( ) . uiText ( LOG10_DISCRETE ) ;
if ( isCategoryResult )
{
optionTexts < < m_mappingMode . v ( ) . uiText ( CATEGORY_INTEGER ) ;
}
}
else if ( fieldNeedingOptions = = & m_colorRangeMode )
{
// This is an app enum field, see cafInternalPdmFieldTypeSpecializations.h for the default specialization of this type
optionTexts < < m_colorRangeMode . v ( ) . uiText ( NORMAL ) ;
optionTexts < < m_colorRangeMode . v ( ) . uiText ( OPPOSITE_NORMAL ) ;
optionTexts < < m_colorRangeMode . v ( ) . uiText ( WHITE_PINK ) ;
optionTexts < < m_colorRangeMode . v ( ) . uiText ( PINK_WHITE ) ;
optionTexts < < m_colorRangeMode . v ( ) . uiText ( BLUE_WHITE_RED ) ;
optionTexts < < m_colorRangeMode . v ( ) . uiText ( RED_WHITE_BLUE ) ;
optionTexts < < m_colorRangeMode . v ( ) . uiText ( WHITE_BLACK ) ;
optionTexts < < m_colorRangeMode . v ( ) . uiText ( BLACK_WHITE ) ;
if ( isCategoryResult )
{
optionTexts < < m_colorRangeMode . v ( ) . uiText ( CATEGORY ) ;
}
}
QList < caf : : PdmOptionItemInfo > optionList ;
for ( int i = 0 ; i < optionTexts . size ( ) ; + + i )
{
optionList . push_back ( caf : : PdmOptionItemInfo ( optionTexts [ i ] , static_cast < unsigned int > ( i ) ) ) ;
}
return optionList ;
}