Files
ResInsight/Fwk/AppFwk/cafProjectDataModel/cafPdmDocument.cpp

166 lines
5.7 KiB
C++
Raw Normal View History

//##################################################################################################
//
// Custom Visualization Core library
// Copyright (C) 2011-2013 Ceetron AS
//
// This library may be used under the terms of either the GNU General Public License or
// the GNU Lesser General Public License as follows:
//
// GNU General Public License Usage
// This library 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.
//
// This library 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.
//
// GNU Lesser General Public License Usage
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library 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 Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
// for more details.
//
//##################################################################################################
#include "cafPdmDocument.h"
#include <QFile>
#include <QXmlStreamReader>
namespace caf
{
2020-06-19 07:53:59 +02:00
CAF_PDM_SOURCE_INIT( PdmDocument, "PdmDocument" );
//--------------------------------------------------------------------------------------------------
2020-06-19 07:53:59 +02:00
///
//--------------------------------------------------------------------------------------------------
2020-06-19 07:53:59 +02:00
PdmDocument::PdmDocument()
{
CAF_PDM_InitFieldNoDefault( &fileName, "DocumentFileName", "File Name" );
}
//--------------------------------------------------------------------------------------------------
2020-06-19 07:53:59 +02:00
///
//--------------------------------------------------------------------------------------------------
void PdmDocument::readFile()
{
2020-06-19 07:53:59 +02:00
QFile xmlFile( fileName );
if ( !xmlFile.open( QIODevice::ReadOnly | QIODevice::Text ) ) return;
2020-06-19 07:53:59 +02:00
readFile( &xmlFile );
}
//--------------------------------------------------------------------------------------------------
2020-06-19 07:53:59 +02:00
///
//--------------------------------------------------------------------------------------------------
2020-06-19 07:53:59 +02:00
void PdmDocument::readFile( QIODevice* xmlFile )
{
2020-06-19 07:53:59 +02:00
QXmlStreamReader xmlStream( xmlFile );
2020-06-19 07:53:59 +02:00
while ( !xmlStream.atEnd() )
{
xmlStream.readNext();
2020-06-19 07:53:59 +02:00
if ( xmlStream.isStartElement() )
{
2020-06-19 07:53:59 +02:00
if ( !matchesClassKeyword( xmlStream.name().toString() ) )
{
// Error: This is not a Ceetron Pdm based xml document
return;
}
2020-06-19 07:53:59 +02:00
readFields( xmlStream, PdmDefaultObjectFactory::instance(), false );
}
}
2020-06-19 07:53:59 +02:00
// Ask all objects to initialize and set up internal datastructure and pointers
// after everything is read from file
resolveReferencesRecursively();
beforeInitAfterRead();
initAfterReadRecursively();
}
//--------------------------------------------------------------------------------------------------
2020-06-19 07:53:59 +02:00
///
//--------------------------------------------------------------------------------------------------
bool PdmDocument::writeFile()
{
2020-06-19 07:53:59 +02:00
QFile xmlFile( fileName );
if ( !xmlFile.open( QIODevice::WriteOnly | QIODevice::Text ) ) return false;
2020-06-19 07:53:59 +02:00
writeFile( &xmlFile );
return true;
}
//--------------------------------------------------------------------------------------------------
2020-06-19 07:53:59 +02:00
///
//--------------------------------------------------------------------------------------------------
2020-06-19 07:53:59 +02:00
void PdmDocument::writeFile( QIODevice* xmlFile )
{
// Ask all objects to make them ready to write themselves to file
setupBeforeSaveRecursively();
2020-06-19 07:53:59 +02:00
QXmlStreamWriter xmlStream( xmlFile );
xmlStream.setAutoFormatting( true );
xmlStream.writeStartDocument();
2020-06-19 07:53:59 +02:00
QString className = classKeyword();
2020-06-19 07:53:59 +02:00
xmlStream.writeStartElement( "", className );
writeFields( xmlStream );
xmlStream.writeEndElement();
xmlStream.writeEndDocument();
}
//--------------------------------------------------------------------------------------------------
2020-06-19 07:53:59 +02:00
///
//--------------------------------------------------------------------------------------------------
2020-06-19 07:53:59 +02:00
void PdmDocument::updateUiIconStateRecursively( PdmObjectHandle* object )
{
2020-06-19 07:53:59 +02:00
if ( object == nullptr ) return;
std::vector<PdmFieldHandle*> fields = object->fields();
2020-06-19 07:53:59 +02:00
std::vector<PdmObjectHandle*> children;
for ( size_t fIdx = 0; fIdx < fields.size(); ++fIdx )
{
if ( fields[fIdx] )
{
auto fieldChildren = fields[fIdx]->children();
children.insert( children.end(), fieldChildren.begin(), fieldChildren.end() );
}
}
for ( size_t cIdx = 0; cIdx < children.size(); ++cIdx )
{
2020-06-19 07:53:59 +02:00
PdmDocument::updateUiIconStateRecursively( children[cIdx] );
}
2020-06-19 07:53:59 +02:00
PdmUiObjectHandle* uiObjectHandle = uiObj( object );
if ( uiObjectHandle )
{
uiObjectHandle->updateUiIconFromToggleField();
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmDocument::beforeInitAfterRead()
{
}
2020-06-19 07:53:59 +02:00
} // End of namespace caf