Files
ResInsight/ApplicationLibCode/Commands/RicImportFormationNamesFeature.cpp
T
Magne Sjaastad 1cd3cf3b67 Fix zone colors to honor LYR-file specification (#13658)
* #12974 Fix zone colors to honor LYR-file specification

- Map formation colors by name instead of palette index in updateFormationNamesOnPlot()
- Map formation colors by categoryValue lookup in updateCellResultLegend(), fixing
  incorrect colors when showOnlyVisibleCategoriesInLegend filters the active set
- Add RimColorLegendCollection::createColorLegendFromFormationNames() and call it
  from readAllFormationNames() so LYR colors are available after project load
- Auto-assign formation color legend when a formation case is set on a well log track
- Rename fileNameWoPath() to shortName() and switch to baseName() (no extension)
- Remove addCustomColorLegend() from RicImportFormationNamesFeature; logic now lives
  in RimColorLegendCollection
- Set formation legend when creating new Well Log Extraction Curve track
2026-02-25 18:16:03 +01:00

197 lines
7.6 KiB
C++

/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2016- Statoil ASA
//
// 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 "RicImportFormationNamesFeature.h"
#include "RiaApplication.h"
#include "RiaResultNames.h"
#include "RigEclipseCaseData.h"
#include "RigFemPartResultsCollection.h"
#include "RigFormationNames.h"
#include "RigGeoMechCaseData.h"
#include "ContourMap/RimEclipseContourMapView.h"
#include "Formations/RimFormationNames.h"
#include "Formations/RimFormationNamesCollection.h"
#include "Rim3dView.h"
#include "RimCase.h"
#include "RimColorLegend.h"
#include "RimColorLegendCollection.h"
#include "RimColorLegendItem.h"
#include "RimEclipseCase.h"
#include "RimEclipseCellColors.h"
#include "RimEclipseView.h"
#include "RimGeoMechCase.h"
#include "RimGeoMechCellColors.h"
#include "RimGeoMechContourMapView.h"
#include "RimGeoMechView.h"
#include "RimOilField.h"
#include "RimProject.h"
#include "RimRegularLegendConfig.h"
#include "Riu3DMainWindowTools.h"
#include "RiuFileDialogTools.h"
#include <QAction>
#include <QFileInfo>
CAF_CMD_SOURCE_INIT( RicImportFormationNamesFeature, "RicImportFormationNamesFeature" );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimFormationNames* RicImportFormationNamesFeature::importFormationFiles( const QStringList& fileNames )
{
RimProject* proj = RimProject::current();
RimFormationNamesCollection* fomNameColl = proj->activeOilField()->formationNamesCollection();
if ( !fomNameColl )
{
fomNameColl = new RimFormationNamesCollection;
proj->activeOilField()->formationNamesCollection = fomNameColl;
}
// For each file, find existing Formation names item, or create new
std::vector<RimFormationNames*> formationNames = fomNameColl->importFiles( fileNames );
fomNameColl->updateConnectedEditors();
RimColorLegendCollection* colorLegendCollection = proj->colorLegendCollection;
for ( auto* fmNames : formationNames )
{
colorLegendCollection->createColorLegendFromFormationNames( fmNames );
}
colorLegendCollection->updateConnectedEditors();
if ( !formationNames.empty() ) return formationNames.back();
return nullptr;
}
//--------------------------------------------------------------------------------------------------
/// If only one formation file is imported, the formation will automatically be set in the active
/// views case. Import of LYR files with colors create custom color legends according to color
/// definition on each file. However, color legend must be set by the user.
//--------------------------------------------------------------------------------------------------
void RicImportFormationNamesFeature::onActionTriggered( bool isChecked )
{
RiaApplication* app = RiaApplication::instance();
QString defaultDir = app->lastUsedDialogDirectory( "BINARY_GRID" );
QString filterText = QString( "Formation Names description File (*.lyr);;FMU Layer Zone Table(%1);;All Files (*.*)" )
.arg( RimFormationNames::layerZoneTableFileName() );
QStringList fileNames =
RiuFileDialogTools::getOpenFileNames( Riu3DMainWindowTools::mainWindowWidget(), "Import Formation Names", defaultDir, filterText );
if ( fileNames.isEmpty() ) return;
// Remember the path to next time
app->setLastUsedDialogDirectory( "BINARY_GRID", QFileInfo( fileNames.last() ).absolutePath() );
// Find or create the FormationNamesCollection
RimFormationNames* formationNames = importFormationFiles( fileNames );
// If we have more than 1 formation file, do not modify selected formation for the case in active view.
if ( fileNames.size() > 1 ) return;
if ( formationNames )
{
RimProject* proj = RimProject::current();
std::vector<RimCase*> cases = proj->allGridCases();
// Legend name is base name of the one formation file, c.f. RicImportFormationNamesFeature::importFormationFiles()
QString legendName = QFileInfo( fileNames.last() ).baseName();
if ( !cases.empty() )
{
Rim3dView* activeView = RiaApplication::instance()->activeReservoirView();
if ( activeView )
{
RimCase* ownerCase = activeView->ownerCase();
if ( ownerCase )
{
ownerCase->setFormationNames( formationNames );
ownerCase->updateFormationNamesData();
setFormationCellResultAndLegend( activeView, legendName );
ownerCase->updateConnectedEditors();
}
}
}
if ( formationNames )
{
Riu3DMainWindowTools::selectAsCurrentItem( formationNames );
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicImportFormationNamesFeature::setupActionLook( QAction* actionToSetup )
{
actionToSetup->setIcon( QIcon( ":/FormationCollection16x16.png" ) );
actionToSetup->setText( "Import Formations" );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicImportFormationNamesFeature::setFormationCellResultAndLegend( Rim3dView* activeView, QString& legendName )
{
auto eclipseContourMapView = dynamic_cast<RimEclipseContourMapView*>( activeView );
if ( eclipseContourMapView ) return;
auto geoMechContourMapView = dynamic_cast<RimGeoMechContourMapView*>( activeView );
if ( geoMechContourMapView ) return;
RimRegularLegendConfig* legendConfig = nullptr;
auto* eclView = dynamic_cast<RimEclipseView*>( activeView );
if ( eclView )
{
eclView->cellResult()->setResultType( RiaDefines::ResultCatType::FORMATION_NAMES );
eclView->cellResult()->setResultVariable( RiaResultNames::activeFormationNamesResultName() );
legendConfig = eclView->cellResult()->legendConfig();
eclView->cellResult()->updateUiFieldsFromActiveResult();
eclView->cellResult()->loadDataAndUpdate();
eclView->updateAllRequiredEditors();
}
auto* geoMechView = dynamic_cast<RimGeoMechView*>( activeView );
if ( geoMechView )
{
legendConfig = geoMechView->cellResult()->legendConfig();
}
if ( legendConfig )
{
RimColorLegendCollection* colorLegendCollection = RimProject::current()->colorLegendCollection;
RimColorLegend* legend = colorLegendCollection->findByName( legendName );
if ( legend )
{
legendConfig->setColorLegend( legend );
}
}
if ( eclView ) eclView->updateDisplayModelForCurrentTimeStepAndRedraw();
}