Skip to content

Commit f74e9b8

Browse files
committed
#14192 Python: Fix duplicate discrete property legend on repeated category-name calls
RimColorLegendCollection::deleteColorLegend erased the default-legend map entry before looking the legend up, so findDefaultLegendForResult always returned null and the old legend was never removed from m_customColorLegends. Each set_discrete_property_category_names call for the same property left an orphaned legend behind and appended a new one. Look up the legend before erasing the map key, and guard against deleting a standard legend that may have been registered as a default.
1 parent 093ee4f commit f74e9b8

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

ApplicationLibCode/ProjectDataModel/RimColorLegendCollection.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,11 @@ RimColorLegend* RimColorLegendCollection::createColorLegend( const QString& colo
119119
//--------------------------------------------------------------------------------------------------
120120
void RimColorLegendCollection::deleteColorLegend( const RimCase* rimCase, const QString& resultName )
121121
{
122+
auto legend = findDefaultLegendForResult( rimCase, resultName );
123+
122124
m_defaultColorLegendNameForResult.erase( createLookupKey( rimCase, resultName ) );
123125

124-
auto legend = findDefaultLegendForResult( rimCase, resultName );
125-
if ( !legend ) return;
126+
if ( !legend || isStandardColorLegend( legend ) ) return;
126127

127128
m_customColorLegends.removeChild( legend );
128129
delete legend;

GrpcInterface/Python/rips/tests/test_color_legend.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,32 @@ def test_discrete_property_category_round_trip(rips_instance, initialize_test):
101101
case.set_discrete_property_category_names(property_name="FACIES", value_names={})
102102
assert case.discrete_property_category_names("FACIES") == {}
103103
assert case.discrete_property_category_colors("FACIES") == {}
104+
105+
106+
def test_discrete_property_category_no_duplicate_legend(rips_instance, initialize_test):
107+
case_path = dataroot.PATH + "/TEST10K_FLT_LGR_NNC/TEST10K_FLT_LGR_NNC.EGRID"
108+
case = rips_instance.project.load_case(path=case_path)
109+
assert case is not None
110+
111+
collection = rips_instance.project.color_legend_collection()
112+
113+
def legend_count():
114+
return len(collection.descendants(rips.ColorLegend))
115+
116+
case.set_discrete_property_category_names(
117+
property_name="FACIES", value_names={0: "Sand", 1: "Shale"}
118+
)
119+
after_first = legend_count()
120+
121+
# Calling again for the same property must replace, not accumulate.
122+
case.set_discrete_property_category_names(
123+
property_name="FACIES", value_names={0: "Sand", 1: "Shale", 2: "Coal"}
124+
)
125+
assert legend_count() == after_first
126+
127+
# The mapping still reflects the latest call.
128+
assert case.discrete_property_category_names("FACIES") == {
129+
0: "Sand",
130+
1: "Shale",
131+
2: "Coal",
132+
}

0 commit comments

Comments
 (0)