Simplify PdmDocument class and move fileName to private

Remove resolveReferencesRecursively() and initAfterReadRecursively() from PdmDocument::readFile(). These functions will be called in RiaApplication::loadProject after the file paths modifications are done. This will ensure that file paths can be used in initAfterRead() functions.
This commit is contained in:
Magne Sjaastad 2024-06-05 11:00:55 +02:00
parent beccd2454e
commit 41d5e498d7
9 changed files with 66 additions and 48 deletions

View File

@ -461,8 +461,9 @@ bool RiaApplication::loadProject( const QString& projectFileName, ProjectLoadAct
return false; return false;
} }
m_project->fileName = fullPathProjectFileName; m_project->setFileName( fullPathProjectFileName );
m_project->readFile(); m_project->readFile();
m_project->updatesAfterProjectFileIsRead();
// Apply any modifications to the loaded project before we go ahead and load actual data // Apply any modifications to the loaded project before we go ahead and load actual data
if ( projectModifier ) if ( projectModifier )
@ -493,6 +494,11 @@ bool RiaApplication::loadProject( const QString& projectFileName, ProjectLoadAct
return true; return true;
} }
// At this point, all the file paths variables are replaced and all file paths updated to the new location. This will enable use of file
// paths in initAfterRead().
m_project->resolveReferencesRecursively();
m_project->initAfterReadRecursively();
// Migrate all RimGridCases to RimFileSummaryCase // Migrate all RimGridCases to RimFileSummaryCase
RimGridSummaryCase_obsolete::convertGridCasesToSummaryFileCases( m_project.get() ); RimGridSummaryCase_obsolete::convertGridCasesToSummaryFileCases( m_project.get() );
@ -802,7 +808,7 @@ bool RiaApplication::saveProjectAs( const QString& fileName, gsl::not_null<QStri
{ {
CAF_ASSERT( m_project ); CAF_ASSERT( m_project );
// Make sure we always store path with forward slash to avoid issues when opening the project file on Linux // Make sure we always store path with forward slash to avoid issues when opening the project file on Linux
m_project->fileName = RiaFilePathTools::toInternalSeparator( fileName ); m_project->setFileName( RiaFilePathTools::toInternalSeparator( fileName ) );
onProjectBeingSaved(); onProjectBeingSaved();

View File

@ -113,7 +113,7 @@ void RicExportSelectedWellPathsFeature::writeWellPathGeometryToStream( QTextStre
if ( writeProjectInfo ) if ( writeProjectInfo )
{ {
formatter.comment( "Project: " + RimProject::current()->fileName ); formatter.comment( "Project: " + RimProject::current()->fileName() );
stream << "\n"; stream << "\n";
} }

View File

@ -283,7 +283,7 @@ void RimProject::close()
delete viewLinkerCollection->viewLinker(); delete viewLinkerCollection->viewLinker();
viewLinkerCollection->viewLinker = nullptr; viewLinkerCollection->viewLinker = nullptr;
fileName = ""; setFileName( "" );
m_globalPathList = ""; m_globalPathList = "";
@ -303,7 +303,7 @@ void RimProject::close()
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
/// ///
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
void RimProject::beforeInitAfterRead() void RimProject::updatesAfterProjectFileIsRead()
{ {
distributePathsFromGlobalPathList(); distributePathsFromGlobalPathList();
@ -408,7 +408,7 @@ bool RimProject::writeProjectFile()
{ {
transferPathsToGlobalPathList(); transferPathsToGlobalPathList();
QFile xmlFile( fileName ); QFile xmlFile( fileName() );
if ( !xmlFile.open( QIODevice::WriteOnly | QIODevice::Text ) ) return false; if ( !xmlFile.open( QIODevice::WriteOnly | QIODevice::Text ) ) return false;
QString content = documentAsString(); QString content = documentAsString();
@ -416,7 +416,7 @@ bool RimProject::writeProjectFile()
if ( RiaPreferences::current()->storeBackupOfProjectFiles() ) if ( RiaPreferences::current()->storeBackupOfProjectFiles() )
{ {
QString backupFilename = fileName + "db"; QString backupFilename = fileName() + "db";
const int maximumRecordCount = 50; const int maximumRecordCount = 50;
RiaProjectBackupTools::appendTextToDatabase( backupFilename, maximumRecordCount, content ); RiaProjectBackupTools::appendTextToDatabase( backupFilename, maximumRecordCount, content );
} }
@ -488,9 +488,10 @@ bool RimProject::isProjectFileVersionEqualOrOlderThan( const QString& otherProje
void RimProject::setProjectFileNameAndUpdateDependencies( const QString& projectFileName ) void RimProject::setProjectFileNameAndUpdateDependencies( const QString& projectFileName )
{ {
// Extract the filename of the project file when it was saved // Extract the filename of the project file when it was saved
QString oldProjectFileName = fileName; QString oldProjectFileName = fileName();
// Replace with the new actual filename // Replace with the new actual filename
fileName = projectFileName; setFileName( projectFileName );
QFileInfo fileInfo( projectFileName ); QFileInfo fileInfo( projectFileName );
QString newProjectPath = fileInfo.path(); QString newProjectPath = fileInfo.path();

View File

@ -195,8 +195,9 @@ public:
std::vector<caf::FilePath*> allFilePaths() const; std::vector<caf::FilePath*> allFilePaths() const;
void updatesAfterProjectFileIsRead();
protected: protected:
void beforeInitAfterRead() override;
void initAfterRead() override; void initAfterRead() override;
void setupBeforeSave() override; void setupBeforeSave() override;

View File

@ -59,7 +59,7 @@ TEST( RimRelocatePathTest, findPathsInProjectFile )
RimProject project; RimProject project;
project.fileName = fileName; project.setFileName( fileName );
project.readFile(); project.readFile();
std::vector<caf::FilePath*> filePaths; std::vector<caf::FilePath*> filePaths;

View File

@ -48,7 +48,23 @@ CAF_PDM_SOURCE_INIT( PdmDocument, "PdmDocument" );
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
PdmDocument::PdmDocument() PdmDocument::PdmDocument()
{ {
CAF_PDM_InitFieldNoDefault( &fileName, "DocumentFileName", "File Name" ); CAF_PDM_InitFieldNoDefault( &m_fileName, "DocumentFileName", "File Name" );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString PdmDocument::fileName() const
{
return m_fileName;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmDocument::setFileName( const QString& fileName )
{
m_fileName = fileName;
} }
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
@ -56,7 +72,7 @@ PdmDocument::PdmDocument()
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
void PdmDocument::readFile() void PdmDocument::readFile()
{ {
QFile xmlFile( fileName ); QFile xmlFile( m_fileName );
if ( !xmlFile.open( QIODevice::ReadOnly | QIODevice::Text ) ) return; if ( !xmlFile.open( QIODevice::ReadOnly | QIODevice::Text ) ) return;
readFile( &xmlFile ); readFile( &xmlFile );
@ -76,19 +92,11 @@ void PdmDocument::readFile( QIODevice* xmlFile )
{ {
if ( !matchesClassKeyword( xmlStream.name().toString() ) ) if ( !matchesClassKeyword( xmlStream.name().toString() ) )
{ {
// Error: This is not a Ceetron Pdm based xml document
return; return;
} }
readFields( xmlStream, PdmDefaultObjectFactory::instance(), false ); readFields( xmlStream, PdmDefaultObjectFactory::instance(), false );
} }
} }
// Ask all objects to initialize and set up internal data structures and pointers
// after everything is read from file
resolveReferencesRecursively();
beforeInitAfterRead();
initAfterReadRecursively();
} }
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
@ -96,7 +104,7 @@ void PdmDocument::readFile( QIODevice* xmlFile )
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
bool PdmDocument::writeFile() bool PdmDocument::writeFile()
{ {
QFile xmlFile( fileName ); QFile xmlFile( m_fileName );
if ( !xmlFile.open( QIODevice::WriteOnly | QIODevice::Text ) ) return false; if ( !xmlFile.open( QIODevice::WriteOnly | QIODevice::Text ) ) return false;
writeFile( &xmlFile ); writeFile( &xmlFile );
@ -161,13 +169,6 @@ void PdmDocument::updateUiIconStateRecursively( PdmObjectHandle* object )
} }
} }
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmDocument::beforeInitAfterRead()
{
}
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
/// ///
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------

View File

@ -54,23 +54,24 @@ class PdmDocument : public PdmObject
public: public:
PdmDocument(); PdmDocument();
PdmField<QString> fileName; QString fileName() const;
void setFileName( const QString& fileName );
void readFile(); void readFile();
bool writeFile(); bool writeFile();
QString documentAsString();
void readFile( QIODevice* device );
void writeFile( QIODevice* device );
static void updateUiIconStateRecursively( PdmObjectHandle* root ); static void updateUiIconStateRecursively( PdmObjectHandle* root );
protected: protected:
virtual void beforeInitAfterRead(); QString documentAsString();
private: private:
void writeDocumentToXmlStream( QXmlStreamWriter& xmlStream ); void writeDocumentToXmlStream( QXmlStreamWriter& xmlStream );
void readFile( QIODevice* device );
void writeFile( QIODevice* device );
private:
PdmField<QString> m_fileName;
}; };
} // End of namespace caf } // End of namespace caf

View File

@ -476,6 +476,9 @@ TEST( BaseTest, ReadWrite )
{ {
QString xmlDocumentContentWithErrors; QString xmlDocumentContentWithErrors;
QString fileName = "PdmTestFil.xml";
QString fileNameCopy = "PdmTestFil_copy.xml";
{ {
MyPdmDocument xmlDoc; MyPdmDocument xmlDoc;
@ -524,7 +527,7 @@ TEST( BaseTest, ReadWrite )
xmlDoc.objects.push_back( id2 ); xmlDoc.objects.push_back( id2 );
// Write file // Write file
xmlDoc.fileName = "PdmTestFil.xml"; xmlDoc.setFileName( fileName );
xmlDoc.writeFile(); xmlDoc.writeFile();
caf::PdmObjectGroup pog; caf::PdmObjectGroup pog;
@ -557,9 +560,14 @@ TEST( BaseTest, ReadWrite )
MyPdmDocument xmlDoc; MyPdmDocument xmlDoc;
// Read file // Read file
xmlDoc.fileName = "PdmTestFil.xml"; xmlDoc.setFileName( fileName );
xmlDoc.readFile(); xmlDoc.readFile();
QFile f( fileNameCopy );
f.remove();
std::rename( fileName.toStdString().data(), fileNameCopy.toStdString().data() );
caf::PdmObjectGroup pog; caf::PdmObjectGroup pog;
for ( size_t i = 0; i < xmlDoc.objects.size(); i++ ) for ( size_t i = 0; i < xmlDoc.objects.size(); i++ )
{ {
@ -578,16 +586,14 @@ TEST( BaseTest, ReadWrite )
EXPECT_EQ( QString( "ÆØÅ Test text end" ), ihDObjs[0]->m_textField() ); EXPECT_EQ( QString( "ÆØÅ Test text end" ), ihDObjs[0]->m_textField() );
// Write file // Write file
QFile xmlFile( "PdmTestFil2.xml" ); xmlDoc.setFileName( fileName );
xmlFile.open( QIODevice::WriteOnly | QIODevice::Text ); xmlDoc.writeFile();
xmlDoc.writeFile( &xmlFile );
xmlFile.close();
} }
// Check that the files are identical // Check that the files are identical
{ {
QFile f1( "PdmTestFil.xml" ); QFile f1( fileName );
QFile f2( "PdmTestFil2.xml" ); QFile f2( fileNameCopy );
f1.open( QIODevice::ReadOnly | QIODevice::Text ); f1.open( QIODevice::ReadOnly | QIODevice::Text );
f2.open( QIODevice::ReadOnly | QIODevice::Text ); f2.open( QIODevice::ReadOnly | QIODevice::Text );
QByteArray ba1 = f1.readAll(); QByteArray ba1 = f1.readAll();
@ -677,7 +683,7 @@ TEST( BaseTest, ReadWrite )
// Read the document containing errors // Read the document containing errors
MyPdmDocument xmlErrorDoc; MyPdmDocument xmlErrorDoc;
xmlErrorDoc.fileName = "PdmTestFilWithError.xml"; xmlErrorDoc.setFileName( "PdmTestFilWithError.xml" );
xmlErrorDoc.readFile(); xmlErrorDoc.readFile();
caf::PdmObjectGroup pog; caf::PdmObjectGroup pog;

View File

@ -1612,9 +1612,11 @@ void MainWindow::slotLoadProject()
setPdmRoot( nullptr ); setPdmRoot( nullptr );
releaseTestData(); releaseTestData();
m_testRoot = new DemoPdmObjectGroup; m_testRoot = new DemoPdmObjectGroup;
m_testRoot->fileName = fileName; m_testRoot->setFileName( fileName );
m_testRoot->readFile(); m_testRoot->readFile();
m_testRoot->resolveReferencesRecursively();
m_testRoot->initAfterReadRecursively();
setPdmRoot( m_testRoot ); setPdmRoot( m_testRoot );
} }
@ -1631,7 +1633,7 @@ void MainWindow::slotSaveProject()
"Project Files (*.proj);;All files(*.*)" ); "Project Files (*.proj);;All files(*.*)" );
if ( !fileName.isEmpty() ) if ( !fileName.isEmpty() )
{ {
m_testRoot->fileName = fileName; m_testRoot->setFileName( fileName );
m_testRoot->writeFile(); m_testRoot->writeFile();
} }
} }