mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
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:
@@ -48,7 +48,23 @@ CAF_PDM_SOURCE_INIT( 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()
|
||||
{
|
||||
QFile xmlFile( fileName );
|
||||
QFile xmlFile( m_fileName );
|
||||
if ( !xmlFile.open( QIODevice::ReadOnly | QIODevice::Text ) ) return;
|
||||
|
||||
readFile( &xmlFile );
|
||||
@@ -76,19 +92,11 @@ void PdmDocument::readFile( QIODevice* xmlFile )
|
||||
{
|
||||
if ( !matchesClassKeyword( xmlStream.name().toString() ) )
|
||||
{
|
||||
// Error: This is not a Ceetron Pdm based xml document
|
||||
return;
|
||||
}
|
||||
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()
|
||||
{
|
||||
QFile xmlFile( fileName );
|
||||
QFile xmlFile( m_fileName );
|
||||
if ( !xmlFile.open( QIODevice::WriteOnly | QIODevice::Text ) ) return false;
|
||||
|
||||
writeFile( &xmlFile );
|
||||
@@ -161,13 +169,6 @@ void PdmDocument::updateUiIconStateRecursively( PdmObjectHandle* object )
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void PdmDocument::beforeInitAfterRead()
|
||||
{
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
|
||||
@@ -54,23 +54,24 @@ class PdmDocument : public PdmObject
|
||||
public:
|
||||
PdmDocument();
|
||||
|
||||
PdmField<QString> fileName;
|
||||
QString fileName() const;
|
||||
void setFileName( const QString& fileName );
|
||||
|
||||
void readFile();
|
||||
bool writeFile();
|
||||
|
||||
QString documentAsString();
|
||||
|
||||
void readFile( QIODevice* device );
|
||||
void writeFile( QIODevice* device );
|
||||
|
||||
static void updateUiIconStateRecursively( PdmObjectHandle* root );
|
||||
|
||||
protected:
|
||||
virtual void beforeInitAfterRead();
|
||||
QString documentAsString();
|
||||
|
||||
private:
|
||||
void writeDocumentToXmlStream( QXmlStreamWriter& xmlStream );
|
||||
void readFile( QIODevice* device );
|
||||
void writeFile( QIODevice* device );
|
||||
|
||||
private:
|
||||
PdmField<QString> m_fileName;
|
||||
};
|
||||
|
||||
} // End of namespace caf
|
||||
|
||||
@@ -476,6 +476,9 @@ TEST( BaseTest, ReadWrite )
|
||||
{
|
||||
QString xmlDocumentContentWithErrors;
|
||||
|
||||
QString fileName = "PdmTestFil.xml";
|
||||
QString fileNameCopy = "PdmTestFil_copy.xml";
|
||||
|
||||
{
|
||||
MyPdmDocument xmlDoc;
|
||||
|
||||
@@ -524,7 +527,7 @@ TEST( BaseTest, ReadWrite )
|
||||
xmlDoc.objects.push_back( id2 );
|
||||
|
||||
// Write file
|
||||
xmlDoc.fileName = "PdmTestFil.xml";
|
||||
xmlDoc.setFileName( fileName );
|
||||
xmlDoc.writeFile();
|
||||
|
||||
caf::PdmObjectGroup pog;
|
||||
@@ -557,9 +560,14 @@ TEST( BaseTest, ReadWrite )
|
||||
MyPdmDocument xmlDoc;
|
||||
|
||||
// Read file
|
||||
xmlDoc.fileName = "PdmTestFil.xml";
|
||||
xmlDoc.setFileName( fileName );
|
||||
xmlDoc.readFile();
|
||||
|
||||
QFile f( fileNameCopy );
|
||||
f.remove();
|
||||
|
||||
std::rename( fileName.toStdString().data(), fileNameCopy.toStdString().data() );
|
||||
|
||||
caf::PdmObjectGroup pog;
|
||||
for ( size_t i = 0; i < xmlDoc.objects.size(); i++ )
|
||||
{
|
||||
@@ -578,16 +586,14 @@ TEST( BaseTest, ReadWrite )
|
||||
EXPECT_EQ( QString( "<EFBFBD><EFBFBD><EFBFBD> Test text end" ), ihDObjs[0]->m_textField() );
|
||||
|
||||
// Write file
|
||||
QFile xmlFile( "PdmTestFil2.xml" );
|
||||
xmlFile.open( QIODevice::WriteOnly | QIODevice::Text );
|
||||
xmlDoc.writeFile( &xmlFile );
|
||||
xmlFile.close();
|
||||
xmlDoc.setFileName( fileName );
|
||||
xmlDoc.writeFile();
|
||||
}
|
||||
|
||||
// Check that the files are identical
|
||||
{
|
||||
QFile f1( "PdmTestFil.xml" );
|
||||
QFile f2( "PdmTestFil2.xml" );
|
||||
QFile f1( fileName );
|
||||
QFile f2( fileNameCopy );
|
||||
f1.open( QIODevice::ReadOnly | QIODevice::Text );
|
||||
f2.open( QIODevice::ReadOnly | QIODevice::Text );
|
||||
QByteArray ba1 = f1.readAll();
|
||||
@@ -677,7 +683,7 @@ TEST( BaseTest, ReadWrite )
|
||||
// Read the document containing errors
|
||||
|
||||
MyPdmDocument xmlErrorDoc;
|
||||
xmlErrorDoc.fileName = "PdmTestFilWithError.xml";
|
||||
xmlErrorDoc.setFileName( "PdmTestFilWithError.xml" );
|
||||
xmlErrorDoc.readFile();
|
||||
|
||||
caf::PdmObjectGroup pog;
|
||||
|
||||
Reference in New Issue
Block a user