Files
ResInsight/ApplicationLibCode/ProjectDataModel/Formations/RimFormationNamesCollection.cpp
T
Kristian Bendiksen d87a0d3048 Refactor RifFormationNamesReader to return std::expected
Replace the QString* errorMessage out-parameter with std::expected<RigFormationNames, QString> in RifFormationNamesReader, and propagate the pattern to RimFormationNames::readFormationNamesFile() which now returns std::expected<void, QString>.

Fatal errors (file cannot be opened, FMU line-parse failure) return std::unexpected. Malformed .lyr lines are still skipped so the rest of the file loads, with warnings logged via RiaLogging instead of collected in the out-parameter. RimFormationNamesCollection::readAllFormationNames() now logs errors it previously discarded.
2026-08-05 16:03:42 +02:00

122 lines
4.3 KiB
C++

/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 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 "RimFormationNamesCollection.h"
#include "RiaLogging.h"
#include "RimCase.h"
#include "RimColorLegendCollection.h"
#include "RimFormationNames.h"
#include "RimProject.h"
#include "RiuMessageDialog.h"
CAF_PDM_SOURCE_INIT( RimFormationNamesCollection, "FormationNamesCollectionObject" );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimFormationNamesCollection::RimFormationNamesCollection()
{
CAF_PDM_InitObject( "Formations", ":/FormationCollection16x16.png" );
CAF_PDM_InitFieldNoDefault( &m_formationNamesList, "FormationNamesList", "Formations" );
setDeletable( true );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimFormationNamesCollection::readAllFormationNames()
{
for ( RimFormationNames* fmNames : m_formationNamesList )
{
if ( auto result = fmNames->readFormationNamesFile(); !result )
{
RiaLogging::error( result.error().toStdString() );
}
RimProject::current()->colorLegendCollection->createColorLegendFromFormationNames( fmNames );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<RimFormationNames*> RimFormationNamesCollection::importFiles( const QStringList& fileNames )
{
QStringList newFileNames;
std::vector<RimFormationNames*> formNamesObjsToReload;
for ( const QString& newFileName : fileNames )
{
bool isFound = false;
for ( RimFormationNames* fmNames : m_formationNamesList )
{
if ( fmNames->fileName() == newFileName )
{
formNamesObjsToReload.push_back( fmNames );
isFound = true;
break;
}
}
if ( !isFound )
{
newFileNames.push_back( newFileName );
}
}
for ( const QString& newFileName : newFileNames )
{
RimFormationNames* newFNs = new RimFormationNames;
newFNs->setFileName( newFileName );
m_formationNamesList.push_back( newFNs );
formNamesObjsToReload.push_back( newFNs );
}
QString totalErrorMessage;
for ( RimFormationNames* fmNames : formNamesObjsToReload )
{
if ( auto result = fmNames->readFormationNamesFile(); !result )
{
totalErrorMessage += "\nError in: " + fmNames->fileName() + "\n\t" + result.error();
}
}
if ( !totalErrorMessage.isEmpty() )
{
RiuMessageDialog::showError( nullptr, "Import Formation Names", totalErrorMessage );
}
return m_formationNamesList.childrenByType();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimFormationNamesCollection::onChildDeleted( caf::PdmChildArrayFieldHandle* childArray,
std::vector<caf::PdmObjectHandle*>& referringObjects )
{
for ( caf::PdmObjectHandle* reffingObj : referringObjects )
{
RimCase* aCase = dynamic_cast<RimCase*>( reffingObj );
if ( aCase ) aCase->updateFormationNamesData();
}
}