diff --git a/ApplicationLibCode/Commands/ColorLegendCommands/RicImportColorCategoriesFeature.cpp b/ApplicationLibCode/Commands/ColorLegendCommands/RicImportColorCategoriesFeature.cpp index 4efd1aabe5..75c622e0df 100644 --- a/ApplicationLibCode/Commands/ColorLegendCommands/RicImportColorCategoriesFeature.cpp +++ b/ApplicationLibCode/Commands/ColorLegendCommands/RicImportColorCategoriesFeature.cpp @@ -67,13 +67,10 @@ void RicImportColorCategoriesFeature::onActionTriggered( bool isChecked ) // Remember the path to next time app->setLastUsedDialogDirectory( "BINARY_GRID", QFileInfo( fileName ).absolutePath() ); - QString errormessage; - auto formations = RifFormationNamesReader::readFormationNamesFile( fileName, &errormessage ); - if ( !formations || !errormessage.isEmpty() ) + auto formations = RifFormationNamesReader::readFormationNamesFile( fileName ); + if ( !formations ) { - QMessageBox::warning( Riu3DMainWindowTools::mainWindowWidget(), - "Import Formation File Failed", - errormessage.isEmpty() ? "Unknown error reading formation file." : errormessage ); + QMessageBox::warning( Riu3DMainWindowTools::mainWindowWidget(), "Import Formation File Failed", formations.error() ); return; } diff --git a/ApplicationLibCode/Commands/RicReloadFormationNamesFeature.cpp b/ApplicationLibCode/Commands/RicReloadFormationNamesFeature.cpp index 2f04863e3b..65765b24d3 100644 --- a/ApplicationLibCode/Commands/RicReloadFormationNamesFeature.cpp +++ b/ApplicationLibCode/Commands/RicReloadFormationNamesFeature.cpp @@ -61,11 +61,9 @@ void RicReloadFormationNamesFeature::onActionTriggered( bool isChecked ) const auto selectedFormationNamesObjs = caf::SelectionManager::instance()->objectsByType(); for ( RimFormationNames* fnames : selectedFormationNamesObjs ) { - QString errorMessage; - fnames->readFormationNamesFile( &errorMessage ); - if ( !errorMessage.isEmpty() ) + if ( auto result = fnames->readFormationNamesFile(); !result ) { - RiuMessageDialog::showError( nullptr, "Reload Formation Names", errorMessage ); + RiuMessageDialog::showError( nullptr, "Reload Formation Names", result.error() ); } fnames->updateConnectedViews(); diff --git a/ApplicationLibCode/FileInterface/RifFormationNamesReader.cpp b/ApplicationLibCode/FileInterface/RifFormationNamesReader.cpp index e9f0fcd93a..b7e0ac9815 100644 --- a/ApplicationLibCode/FileInterface/RifFormationNamesReader.cpp +++ b/ApplicationLibCode/FileInterface/RifFormationNamesReader.cpp @@ -18,11 +18,10 @@ #include "RifFormationNamesReader.h" +#include "RiaLogging.h" #include "RiaTextStringTools.h" #include "RigFormationNames.h" -#include - #include "cafAssert.h" #include "cafPdmUiFilePathEditor.h" @@ -34,35 +33,34 @@ //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -std::unique_ptr RifFormationNamesReader::readFormationNamesFile( const QString& fileName, QString* errorMessage ) +std::expected RifFormationNamesReader::readFormationNamesFile( const QString& fileName ) { QFileInfo fileInfo( fileName ); if ( fileInfo.fileName() == "layer_zone_table.txt" ) { - return RifFormationNamesReader::readFmuFormationNameFile( fileName, errorMessage ); + return RifFormationNamesReader::readFmuFormationNameFile( fileName ); } else { - return RifFormationNamesReader::readLyrFormationNameFile( fileName, errorMessage ); + return RifFormationNamesReader::readLyrFormationNameFile( fileName ); } } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -std::unique_ptr RifFormationNamesReader::readLyrFormationNameFile( const QString& fileName, QString* errorMessage ) +std::expected RifFormationNamesReader::readLyrFormationNameFile( const QString& fileName ) { QFile dataFile( fileName ); if ( !dataFile.open( QFile::ReadOnly ) ) { - if ( errorMessage ) ( *errorMessage ) += "Could not open file: " + fileName + "\n"; - return nullptr; + return std::unexpected( "Could not open file: " + fileName ); } - auto formationNames = std::make_unique(); - QTextStream stream( &dataFile ); + RigFormationNames formationNames; + QTextStream stream( &dataFile ); int lineNumber = 1; while ( !stream.atEnd() ) @@ -74,7 +72,7 @@ std::unique_ptr RifFormationNamesReader::readLyrFormationName if ( lineSegs.size() == 1 ) continue; // No name present. Comment line ? if ( lineSegs.size() == 2 ) { - if ( errorMessage ) ( *errorMessage ) += "Missing quote on line : " + QString::number( lineNumber ) + "\n"; + RiaLogging::warning( ( fileName + ": missing quote on line " + QString::number( lineNumber ) ).toStdString() ); continue; // One quote present } @@ -103,7 +101,7 @@ std::unique_ptr RifFormationNamesReader::readLyrFormationName if ( !( isNumber2 && isNumber1 ) ) { - if ( errorMessage ) ( *errorMessage ) += "Format error on line: " + QString::number( lineNumber ) + "\n"; + RiaLogging::warning( ( fileName + ": format error on line " + QString::number( lineNumber ) ).toStdString() ); continue; } @@ -116,11 +114,11 @@ std::unique_ptr RifFormationNamesReader::readLyrFormationName cvf::Color3f formationColor; convertStringToColor( colorWord, &formationColor ); - formationNames->appendFormationRange( formationName, formationColor, startK - 1, endK - 1 ); + formationNames.appendFormationRange( formationName, formationColor, startK - 1, endK - 1 ); } else // no color present { - formationNames->appendFormationRange( formationName, startK - 1, endK - 1 ); + formationNames.appendFormationRange( formationName, startK - 1, endK - 1 ); } } else if ( numberWords.size() == 1 ) @@ -130,7 +128,7 @@ std::unique_ptr RifFormationNamesReader::readLyrFormationName if ( !isNumber1 ) { - if ( errorMessage ) ( *errorMessage ) += "Format error on line: " + QString::number( lineNumber ) + "\n"; + RiaLogging::warning( ( fileName + ": format error on line " + QString::number( lineNumber ) ).toStdString() ); continue; } @@ -139,16 +137,16 @@ std::unique_ptr RifFormationNamesReader::readLyrFormationName cvf::Color3f formationColor; convertStringToColor( colorWord, &formationColor ); - formationNames->appendFormationRangeHeight( formationName, formationColor, kLayerCount ); + formationNames.appendFormationRangeHeight( formationName, formationColor, kLayerCount ); } else // no color present { - formationNames->appendFormationRangeHeight( formationName, kLayerCount ); + formationNames.appendFormationRangeHeight( formationName, kLayerCount ); } } else { - if ( errorMessage ) ( *errorMessage ) += "Format error on line: " + QString::number( lineNumber ) + "\n"; + RiaLogging::warning( ( fileName + ": format error on line " + QString::number( lineNumber ) ).toStdString() ); } } @@ -161,18 +159,17 @@ std::unique_ptr RifFormationNamesReader::readLyrFormationName //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -std::unique_ptr RifFormationNamesReader::readFmuFormationNameFile( const QString& fileName, QString* errorMessage ) +std::expected RifFormationNamesReader::readFmuFormationNameFile( const QString& fileName ) { QFile dataFile( fileName ); if ( !dataFile.open( QFile::ReadOnly ) ) { - if ( errorMessage ) ( *errorMessage ) += "Could not open file: " + fileName + "\n"; - return nullptr; + return std::unexpected( "Could not open file: " + fileName ); } - auto formationNames = std::make_unique(); - QTextStream stream( &dataFile ); + RigFormationNames formationNames; + QTextStream stream( &dataFile ); int lineNumber = 1; @@ -188,7 +185,7 @@ std::unique_ptr RifFormationNamesReader::readFmuFormationName // Make sure we append the last formation if ( !currentFormationName.isEmpty() ) { - formationNames->appendFormationRange( currentFormationName, startK - 1, endK - 1 ); + formationNames.appendFormationRange( currentFormationName, startK - 1, endK - 1 ); } break; } @@ -203,8 +200,7 @@ std::unique_ptr RifFormationNamesReader::readFmuFormationName if ( lineStream.status() != QTextStream::Ok ) { - *errorMessage = QString( "Failed to parse line %1 of '%2'" ).arg( lineNumber ).arg( fileName ); - return formationNames; + return std::unexpected( QString( "Failed to parse line %1 of '%2'" ).arg( lineNumber ).arg( fileName ) ); } if ( formationName != currentFormationName ) @@ -212,7 +208,7 @@ std::unique_ptr RifFormationNamesReader::readFmuFormationName // Append previous formation if ( !currentFormationName.isEmpty() ) { - formationNames->appendFormationRange( currentFormationName, startK - 1, endK - 1 ); + formationNames.appendFormationRange( currentFormationName, startK - 1, endK - 1 ); } // Start new formation @@ -230,7 +226,7 @@ std::unique_ptr RifFormationNamesReader::readFmuFormationName // Append previous formation at the end of the stream if ( !currentFormationName.isEmpty() ) { - formationNames->appendFormationRange( currentFormationName, startK - 1, endK - 1 ); + formationNames.appendFormationRange( currentFormationName, startK - 1, endK - 1 ); } return formationNames; diff --git a/ApplicationLibCode/FileInterface/RifFormationNamesReader.h b/ApplicationLibCode/FileInterface/RifFormationNamesReader.h index e1bd562672..3ab4f01cdf 100644 --- a/ApplicationLibCode/FileInterface/RifFormationNamesReader.h +++ b/ApplicationLibCode/FileInterface/RifFormationNamesReader.h @@ -20,10 +20,11 @@ #include "cafPdmField.h" #include "cafPdmObject.h" -#include +#include "RigFormationNames.h" -class RigFormationNames; -class QString; +#include + +#include namespace cvf { @@ -36,11 +37,11 @@ class Color3f; class RifFormationNamesReader { public: - [[nodiscard]] static std::unique_ptr readFormationNamesFile( const QString& fileName, QString* errorMessage ); + [[nodiscard]] static std::expected readFormationNamesFile( const QString& fileName ); private: - static std::unique_ptr readLyrFormationNameFile( const QString& fileName, QString* errorMessage ); - static std::unique_ptr readFmuFormationNameFile( const QString& fileName, QString* errorMessage ); + static std::expected readLyrFormationNameFile( const QString& fileName ); + static std::expected readFmuFormationNameFile( const QString& fileName ); static bool convertStringToColor( const QString& word, cvf::Color3f* color ); }; diff --git a/ApplicationLibCode/ProjectDataModel/Formations/RimFormationNames.cpp b/ApplicationLibCode/ProjectDataModel/Formations/RimFormationNames.cpp index 27bcb25bd9..d6ac3e1477 100644 --- a/ApplicationLibCode/ProjectDataModel/Formations/RimFormationNames.cpp +++ b/ApplicationLibCode/ProjectDataModel/Formations/RimFormationNames.cpp @@ -67,11 +67,9 @@ void RimFormationNames::fieldChangedByUi( const caf::PdmFieldHandle* changedFiel if ( &m_formationNamesFileName == changedField ) { updateUiTreeName(); - QString errorMessage; - readFormationNamesFile( &errorMessage ); - if ( !errorMessage.isEmpty() ) + if ( auto result = readFormationNamesFile(); !result ) { - RiuMessageDialog::showError( nullptr, "Formation Names", errorMessage ); + RiuMessageDialog::showError( nullptr, "Formation Names", result.error() ); } updateConnectedViews(); } @@ -150,9 +148,17 @@ void RimFormationNames::updateConnectedViews() //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -void RimFormationNames::readFormationNamesFile( QString* errorMessage ) +std::expected RimFormationNames::readFormationNamesFile() { - m_formationNamesData = RifFormationNamesReader::readFormationNamesFile( m_formationNamesFileName().path(), errorMessage ); + auto result = RifFormationNamesReader::readFormationNamesFile( m_formationNamesFileName().path() ); + if ( !result ) + { + m_formationNamesData.reset(); + return std::unexpected( result.error() ); + } + + m_formationNamesData = std::make_unique( std::move( *result ) ); + return {}; } //-------------------------------------------------------------------------------------------------- diff --git a/ApplicationLibCode/ProjectDataModel/Formations/RimFormationNames.h b/ApplicationLibCode/ProjectDataModel/Formations/RimFormationNames.h index 70d8dc4958..74c93a473b 100644 --- a/ApplicationLibCode/ProjectDataModel/Formations/RimFormationNames.h +++ b/ApplicationLibCode/ProjectDataModel/Formations/RimFormationNames.h @@ -20,6 +20,7 @@ #include "cafPdmField.h" #include "cafPdmObject.h" +#include #include class RigFormationNames; @@ -45,7 +46,7 @@ public: void updateConnectedViews(); - void readFormationNamesFile( QString* errorMessage ); + std::expected readFormationNamesFile(); static QString layerZoneTableFileName(); diff --git a/ApplicationLibCode/ProjectDataModel/Formations/RimFormationNamesCollection.cpp b/ApplicationLibCode/ProjectDataModel/Formations/RimFormationNamesCollection.cpp index 2bfad9cd2f..7ffbf5ab2c 100644 --- a/ApplicationLibCode/ProjectDataModel/Formations/RimFormationNamesCollection.cpp +++ b/ApplicationLibCode/ProjectDataModel/Formations/RimFormationNamesCollection.cpp @@ -46,7 +46,10 @@ void RimFormationNamesCollection::readAllFormationNames() { for ( RimFormationNames* fmNames : m_formationNamesList ) { - fmNames->readFormationNamesFile( nullptr ); + if ( auto result = fmNames->readFormationNamesFile(); !result ) + { + RiaLogging::error( result.error().toStdString() ); + } RimProject::current()->colorLegendCollection->createColorLegendFromFormationNames( fmNames ); } } @@ -90,12 +93,9 @@ std::vector RimFormationNamesCollection::importFiles( const for ( RimFormationNames* fmNames : formNamesObjsToReload ) { - QString errormessage; - - fmNames->readFormationNamesFile( &errormessage ); - if ( !errormessage.isEmpty() ) + if ( auto result = fmNames->readFormationNamesFile(); !result ) { - totalErrorMessage += "\nError in: " + fmNames->fileName() + "\n\t" + errormessage; + totalErrorMessage += "\nError in: " + fmNames->fileName() + "\n\t" + result.error(); } } diff --git a/ApplicationLibCode/UnitTests/RifFormationNamesReader-Test.cpp b/ApplicationLibCode/UnitTests/RifFormationNamesReader-Test.cpp index a48502f068..7647a2df16 100644 --- a/ApplicationLibCode/UnitTests/RifFormationNamesReader-Test.cpp +++ b/ApplicationLibCode/UnitTests/RifFormationNamesReader-Test.cpp @@ -18,10 +18,8 @@ TEST( RifFormationNamesReader, ReadLYRFileWithoutColor ) const QString filePath = baseFolder.absoluteFilePath( filename ); EXPECT_TRUE( QFile::exists( filePath ) ); - QString errormessage; - - auto fm = RifFormationNamesReader::readFormationNamesFile( filePath, &errormessage ); - EXPECT_TRUE( errormessage.isEmpty() ); + auto fm = RifFormationNamesReader::readFormationNamesFile( filePath ); + ASSERT_TRUE( fm.has_value() ); QString formationName_K1 = fm->formationNameFromKLayerIdx( 0 ); int formationIndex = fm->formationIndexFromKLayerIdx( 1 ); @@ -38,10 +36,8 @@ TEST( RifFormationNamesReader, ReadLYRFileWithColorName ) const QString filePath = baseFolder.absoluteFilePath( filename ); EXPECT_TRUE( QFile::exists( filePath ) ); - QString errormessage; - - auto fm = RifFormationNamesReader::readFormationNamesFile( filePath, &errormessage ); - EXPECT_TRUE( errormessage.isEmpty() ); + auto fm = RifFormationNamesReader::readFormationNamesFile( filePath ); + ASSERT_TRUE( fm.has_value() ); QString formationName_K1 = fm->formationNameFromKLayerIdx( 1 ); int formationIndex = fm->formationIndexFromKLayerIdx( 1 ); @@ -65,10 +61,8 @@ TEST( RifFormationNamesReader, ReadLYRFileWithColorHTML ) const QString filePath = baseFolder.absoluteFilePath( filename ); EXPECT_TRUE( QFile::exists( filePath ) ); - QString errormessage; - - auto fm = RifFormationNamesReader::readFormationNamesFile( filePath, &errormessage ); - EXPECT_TRUE( errormessage.isEmpty() ); + auto fm = RifFormationNamesReader::readFormationNamesFile( filePath ); + ASSERT_TRUE( fm.has_value() ); QString formationName_K1 = fm->formationNameFromKLayerIdx( 1 ); int formationIndex = fm->formationIndexFromKLayerIdx( 1 ); diff --git a/ApplicationLibCode/UnitTests/RimFormationNames-Test.cpp b/ApplicationLibCode/UnitTests/RimFormationNames-Test.cpp index 0faa3e8f02..18e44709f3 100644 --- a/ApplicationLibCode/UnitTests/RimFormationNames-Test.cpp +++ b/ApplicationLibCode/UnitTests/RimFormationNames-Test.cpp @@ -72,9 +72,8 @@ std::unique_ptr readNorneFormationNames() auto formationNames = std::make_unique(); formationNames->setFileName( filePath ); - QString errorMessage; - formationNames->readFormationNamesFile( &errorMessage ); - EXPECT_TRUE( errorMessage.isEmpty() ); + auto result = formationNames->readFormationNamesFile(); + EXPECT_TRUE( result.has_value() ); return formationNames; } @@ -106,9 +105,8 @@ TEST( RimFormationNamesTest, CaseDataKeepsFormationNamesAliveAfterReloadAndDelet EXPECT_FALSE( namesBeforeReload.empty() ); // Reload replaces the data owned by RimFormationNames - QString errorMessage; - formationNames->readFormationNamesFile( &errorMessage ); - EXPECT_TRUE( errorMessage.isEmpty() ); + auto result = formationNames->readFormationNamesFile(); + EXPECT_TRUE( result.has_value() ); EXPECT_EQ( namesBeforeReload, joinedFormationNames( mockCase.eclipseCase.p() ) );