#14192 Update color legend in place on roff property re-import

Reuse RimColorLegendCollection::updateColorLegend in the two roff import
sites that previously deleted and recreated the legend bound to a result,
so views referencing the legend keep their binding on re-import.

Extract the rock color matching from createColorLegendMatchDefaultRockColors
into matchDefaultRockColors so the facies branch can pass matched colors to
updateColorLegend. Remove the now-unused
RimColorLegendCollection::createColorLegend.
This commit is contained in:
Kristian Bendiksen
2026-06-10 18:02:07 +02:00
committed by Magne Sjaastad
parent a4fe22508a
commit d78d7cf430
5 changed files with 61 additions and 45 deletions
@@ -87,17 +87,40 @@ void RicFaciesPropertiesImportTools::importFaciesPropertiesFromFile( const QStri
//--------------------------------------------------------------------------------------------------
RimColorLegend* RicFaciesPropertiesImportTools::createColorLegendMatchDefaultRockColors( const std::map<int, QString>& codeNames )
{
const caf::ColorTable& colorTable = RiaColorTables::contrastCategoryPaletteColors();
RimColorLegendCollection* colorLegendCollection = RimProject::current()->colorLegendCollection;
RimColorLegend* rockTypeColorLegend = colorLegendCollection->findByName( RiaDefines::rockTypeColorLegendName() );
const auto colors = matchDefaultRockColors( codeNames );
auto colorLegend = new RimColorLegend;
colorLegend->setColorLegendName( RiaDefines::faciesColorLegendName() );
size_t colorIndex = 0;
for ( const auto& it : codeNames )
{
auto colorLegendItem = new RimColorLegendItem;
colorLegendItem->setValues( it.second, it.first, colors[colorIndex++] );
colorLegend->appendColorLegendItem( colorLegendItem );
}
colorLegendCollection->appendCustomColorLegend( colorLegend );
colorLegendCollection->updateConnectedEditors();
return colorLegend;
}
//--------------------------------------------------------------------------------------------------
/// Find a color for each category name by fuzzy matching against the rock type color legend.
/// Returns one color per entry in codeNames, ordered by ascending value.
//--------------------------------------------------------------------------------------------------
std::vector<cvf::Color3f> RicFaciesPropertiesImportTools::matchDefaultRockColors( const std::map<int, QString>& codeNames )
{
const caf::ColorTable& colorTable = RiaColorTables::contrastCategoryPaletteColors();
RimColorLegendCollection* colorLegendCollection = RimProject::current()->colorLegendCollection;
RimColorLegend* rockTypeColorLegend = colorLegendCollection->findByName( RiaDefines::rockTypeColorLegendName() );
std::vector<cvf::Color3f> colors;
for ( const auto& it : codeNames )
{
// Try to find a color from the rock type color legend by fuzzy matching names
cvf::Color3f color;
if ( rockTypeColorLegend && !predefinedColorMatch( it.second, rockTypeColorLegend, color ) &&
@@ -107,14 +130,10 @@ RimColorLegend* RicFaciesPropertiesImportTools::createColorLegendMatchDefaultRoc
color = colorTable.cycledColor3f( it.first );
}
colorLegendItem->setValues( it.second, it.first, color );
colorLegend->appendColorLegendItem( colorLegendItem );
colors.push_back( color );
}
colorLegendCollection->appendCustomColorLegend( colorLegend );
colorLegendCollection->updateConnectedEditors();
return colorLegend;
return colors;
}
//--------------------------------------------------------------------------------------------------
@@ -19,6 +19,7 @@
#pragma once
#include <map>
#include <vector>
class RimColorLegend;
class RimStimPlanModelTemplate;
@@ -42,6 +43,8 @@ public:
static RimColorLegend* createColorLegendMatchDefaultRockColors( const std::map<int, QString>& codeNames );
static std::vector<cvf::Color3f> matchDefaultRockColors( const std::map<int, QString>& codeNames );
private:
static int computeEditDistance( const QString& a, const QString& b );
static bool matchByName( const QString& name, RimColorLegend* colorLegend, cvf::Color3f& color );
@@ -19,6 +19,7 @@
#include "RifRoffFileTools.h"
#include "RiaApplication.h"
#include "RiaFractureDefines.h"
#include "RiaLogging.h"
#include "RiaQStringFormatter.h"
#include "RiaResultNames.h"
@@ -579,20 +580,17 @@ std::pair<bool, std::map<QString, QString>> RifRoffFileTools::createInputPropert
auto rimCase = eclipseCaseData->ownerCase();
// Delete existing color legend, as new legend will be populated by values from file
colorLegendCollection->deleteColorLegend( rimCase, newResultName );
RimColorLegend* colorLegend = nullptr;
// Update existing color legend in place, or create one, with values from file
if ( keywordUpperCase == RiaResultNames::facies() )
{
colorLegend = RicFaciesPropertiesImportTools::createColorLegendMatchDefaultRockColors( codeNames );
const auto colors = RicFaciesPropertiesImportTools::matchDefaultRockColors( codeNames );
colorLegendCollection->updateColorLegend( rimCase, newResultName, RiaDefines::faciesColorLegendName(), codeNames, colors );
}
else
{
colorLegend = colorLegendCollection->createColorLegend( newResultName, codeNames );
colorLegendCollection->updateColorLegend( rimCase, newResultName, newResultName, codeNames );
}
colorLegendCollection->setDefaultColorLegendForResult( rimCase, newResultName, colorLegend );
colorLegendCollection->updateAllRequiredEditors();
}
@@ -831,9 +829,7 @@ bool RifRoffFileTools::appendZoneIndexPropertyFromSubgrids( RigEclipseCaseData*
auto rimCase = caseData->ownerCase();
colorLegendCollection->deleteColorLegend( rimCase, resultName );
RimColorLegend* colorLegend = colorLegendCollection->createColorLegend( resultName, codeNames );
colorLegendCollection->setDefaultColorLegendForResult( rimCase, resultName, colorLegend );
colorLegendCollection->updateColorLegend( rimCase, resultName, resultName, codeNames );
colorLegendCollection->updateAllRequiredEditors();
}
}
@@ -88,32 +88,6 @@ void RimColorLegendCollection::deleteCustomColorLegends()
m_customColorLegends.deleteChildren();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimColorLegend* RimColorLegendCollection::createColorLegend( const QString& colorLegendName, const std::map<int, QString>& valuesAndNames )
{
auto colors = RiaColorTables::categoryPaletteColors().color3ubArray();
auto colorLegend = new RimColorLegend();
colorLegend->setColorLegendName( colorLegendName );
int colorIndex = 0;
for ( const auto& [value, name] : valuesAndNames )
{
auto item = new RimColorLegendItem();
auto color = colors[colorIndex++ % colors.size()];
cvf::Color3f color3f( color );
item->setValues( name, value, color3f );
colorLegend->appendColorLegendItem( item );
}
appendCustomColorLegend( colorLegend );
return colorLegend;
}
//--------------------------------------------------------------------------------------------------
/// Update the custom color legend registered as default for the given result in place, so that
/// objects referring to the legend keep their binding. Creates and registers a new custom legend
@@ -158,6 +132,26 @@ RimColorLegend* RimColorLegendCollection::updateColorLegend( const RimCase*
return legend;
}
//--------------------------------------------------------------------------------------------------
/// Overload taking values and names as a map, with optional colors ordered by ascending value.
//--------------------------------------------------------------------------------------------------
RimColorLegend* RimColorLegendCollection::updateColorLegend( const RimCase* rimCase,
const QString& resultName,
const QString& colorLegendName,
const std::map<int, QString>& valuesAndNames,
const std::vector<cvf::Color3f>& colors )
{
std::vector<int> categoryValues;
std::vector<QString> categoryNames;
for ( const auto& [value, name] : valuesAndNames )
{
categoryValues.push_back( value );
categoryNames.push_back( name );
}
return updateColorLegend( rimCase, resultName, colorLegendName, categoryValues, categoryNames, colors );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -51,13 +51,17 @@ public:
bool isStandardColorLegend( RimColorLegend* colorLegend );
void deleteCustomColorLegends();
RimColorLegend* createColorLegend( const QString& colorLegendName, const std::map<int, QString>& valuesAndNames );
RimColorLegend* updateColorLegend( const RimCase* rimCase,
const QString& resultName,
const QString& colorLegendName,
const std::vector<int>& categoryValues,
const std::vector<QString>& categoryNames,
const std::vector<cvf::Color3f>& colors );
RimColorLegend* updateColorLegend( const RimCase* rimCase,
const QString& resultName,
const QString& colorLegendName,
const std::map<int, QString>& valuesAndNames,
const std::vector<cvf::Color3f>& colors = {} );
void createColorLegendFromFormationNames( RimFormationNames* rimFormationNames );
void deleteColorLegend( const RimCase* rimCase, const QString& resultName );
void setDefaultColorLegendForResult( const RimCase* rimCase, const QString& resultName, RimColorLegend* colorLegend );