From: ana Date: Thu, 3 Dec 2009 09:58:51 +0000 (+0000) Subject: Implement tree persistence for "light" SALOME modules X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=refs%2Fheads%2FLIGHT_PERSIST;p=modules%2Fgui.git Implement tree persistence for "light" SALOME modules --- diff --git a/src/LightApp/LightApp_Application.cxx b/src/LightApp/LightApp_Application.cxx index 2b524699c..a2961c22c 100644 --- a/src/LightApp/LightApp_Application.cxx +++ b/src/LightApp/LightApp_Application.cxx @@ -1411,6 +1411,8 @@ void LightApp_Application::onStudyOpened( SUIT_Study* theStudy ) if ( objectBrowser() ) objectBrowser()->openLevels(); + updateObjectBrowser( true ); + emit studyOpened(); } diff --git a/src/LightApp/LightApp_Driver.cxx b/src/LightApp/LightApp_Driver.cxx index e85660778..ee62188e6 100644 --- a/src/LightApp/LightApp_Driver.cxx +++ b/src/LightApp/LightApp_Driver.cxx @@ -31,6 +31,7 @@ #include #include +#include #ifdef WIN32 #include @@ -56,12 +57,14 @@ bool LightApp_Driver::SaveDatasInFile( const char* theFileName, bool isMultiFile { int aNbModules = 0; std::map::const_iterator it; + std::map::const_iterator it1; for (it = myMap.begin(); it != myMap.end(); ++it) aNbModules++; unsigned char** aBuffer = new unsigned char*[aNbModules]; long* aBufferSize = new long[aNbModules]; char** aModuleName = new char*[aNbModules]; + char** aTree = new char*[aNbModules]; if(aBuffer == NULL || aBufferSize == NULL || aModuleName == NULL) return false; @@ -79,6 +82,14 @@ bool LightApp_Driver::SaveDatasInFile( const char* theFileName, bool isMultiFile i++; } int n = i; + + i = 0; + for ( it1 = myMapTree.begin(); it1 != myMapTree.end(); ++it1 ) { + aFileBufferSize += 4; //Add 4 bytes: a length of the object tree + aTree[i] = const_cast( it1->second.c_str() ); + aFileBufferSize += strlen( aTree[i] ) + 1; + i++; + } unsigned char* aFileBuffer = new unsigned char[aFileBufferSize]; if(aFileBuffer == NULL) @@ -95,6 +106,7 @@ bool LightApp_Driver::SaveDatasInFile( const char* theFileName, bool isMultiFile aCurrentPos += 4; int aBufferNameSize = 0; + int aTreeSize = 0; for (i = 0; i < n; i++) { aBufferNameSize = strlen(aModuleName[i])+1; //Initialize 4 bytes of the buffer by 0 @@ -114,6 +126,16 @@ bool LightApp_Driver::SaveDatasInFile( const char* theFileName, bool isMultiFile //Copy the module buffer to the buffer memcpy((aFileBuffer + aCurrentPos), aBuffer[i], aBufferSize[i]); aCurrentPos += aBufferSize[i]; + + //Initialize 4 bytes of the buffer by 0 + memset((aFileBuffer + aCurrentPos), 0, 4); + aTreeSize = strlen(aTree[i]) + 1; + //Copy the length of the object tree + memcpy((aFileBuffer + aCurrentPos), &aTreeSize, ((sizeof(int) > 4) ? : sizeof(int))); + aCurrentPos += 4; + //Copy the object tree to the buffer + memcpy((aFileBuffer + aCurrentPos), aTree[i], aTreeSize); + aCurrentPos += aTreeSize; } #ifdef WIN32 @@ -128,6 +150,7 @@ bool LightApp_Driver::SaveDatasInFile( const char* theFileName, bool isMultiFile delete[] aBufferSize; delete[] aModuleName; delete[] aFileBuffer; + delete[] aTree; return true; } @@ -173,17 +196,32 @@ bool LightApp_Driver::ReadDatasFromFile( const char* theFileName, bool isMultiFi memcpy(&aBufferSize, (aFileBuffer + aCurrentPos), ((sizeof(long) > 8) ? 8 : sizeof(long))); aCurrentPos += 8; unsigned char *aBuffer = new unsigned char[aBufferSize]; - + //Put a buffer for current module to aBuffer - memcpy(aBuffer, (aFileBuffer + aCurrentPos), aBufferSize); + memcpy(aBuffer, (aFileBuffer + aCurrentPos), aBufferSize); aCurrentPos += aBufferSize; + //Put a length of the object tree buffer to aTreeSize + int aTreeSize; + memcpy(&aTreeSize, (aFileBuffer + aCurrentPos), ((sizeof(int) > 4) ? 4 : sizeof(int))); + aCurrentPos += 4; + char *aTree = new char[aTreeSize]; + + //Put an object tree for current module to aTree + memcpy(aTree, (aFileBuffer + aCurrentPos), aTreeSize); + aCurrentPos += aTreeSize; + // Put buffer to aListOfFiles and set to myMap ListOfFiles aListOfFiles = PutStreamToFiles(aBuffer, aBufferSize, isMultiFile); SetListOfFiles(aModuleName, aListOfFiles); + // Set the object tree to myMapTree + std::string aTmpTree(aTree); + SetTree(aModuleName, aTmpTree); + delete[] aModuleName; delete[] aBuffer; + delete[] aTree; } delete[] aFileBuffer; @@ -223,6 +261,27 @@ void LightApp_Driver::SetListOfFiles( const char* theModuleName, const ListOfFil { std::string aName (theModuleName); myMap[aName] = theListOfFiles; + +} + +/*! + Sets the object tree for module with name 'theModuleName' +*/ +void LightApp_Driver::SetTree( const std::string& theModuleName, const std::string& theTree ) +{ + myMapTree[theModuleName] = theTree; +} + +/*! + Gets the object tree for module with name 'theModuleName' +*/ +QString LightApp_Driver::GetTree( const std::string& theModuleName ) +{ + if ( myMapTree.count( theModuleName ) ) { + QString aTree( myMapTree[theModuleName].c_str() ); + return aTree; + } + return QString(); } /*! @@ -552,4 +611,3 @@ std::string LightApp_Driver::GetDirFromPath( const std::string& thePath ) { aDirString.ChangeAll('|','/'); return aDirString.ToCString(); } - diff --git a/src/LightApp/LightApp_Driver.h b/src/LightApp/LightApp_Driver.h index 5148426c5..69b710844 100644 --- a/src/LightApp/LightApp_Driver.h +++ b/src/LightApp/LightApp_Driver.h @@ -28,6 +28,8 @@ #include "vector" #include "map" +#include + #ifdef WIN32 #pragma warning( disable:4251 ) #endif @@ -54,6 +56,9 @@ public: virtual void ClearDriverContents(); + QString GetTree (const std::string& theModuleName); + void SetTree (const std::string& theModuleName, const std::string& theTree); + protected: void PutFilesToStream(const std::string& theModuleName, unsigned char*& theBuffer, long& theBufferSize, bool theNamesOnly = false); @@ -69,6 +74,8 @@ protected: protected: typedef std::map MapOfListOfFiles; MapOfListOfFiles myMap; + typedef std::map MapTrees; + MapTrees myMapTree; std::string myTmpDir; private: diff --git a/src/LightApp/LightApp_HDFDriver.cxx b/src/LightApp/LightApp_HDFDriver.cxx index 168f18013..5768ac741 100644 --- a/src/LightApp/LightApp_HDFDriver.cxx +++ b/src/LightApp/LightApp_HDFDriver.cxx @@ -94,6 +94,17 @@ bool LightApp_HDFDriver::SaveDatasInFile( const char* theFileName, bool isMultiF hdf_dataset->WriteOnDisk(aBuffer); //Save the stream in the HDF file hdf_dataset->CloseOnDisk(); hdf_dataset = 0; //will be deleted by hdf_sco_group destructor + + if ( myMapTree.count( aName ) ) { + char* aTree = const_cast( myMapTree[ aName ].c_str() ); + aHDFSize[0] = strlen(aTree)+1; + + hdf_dataset = new HDFdataset ("TREE_STREAM", hdf_sco_group, HDF_STRING, aHDFSize, 1); + hdf_dataset->CreateOnDisk(); + hdf_dataset->WriteOnDisk(aTree); //Save the stream of tree in the HDF file + hdf_dataset->CloseOnDisk(); + hdf_dataset = 0; //will be deleted by hdf_sco_group destructor + } // store multifile state aHDFSize[0] = 2; @@ -291,6 +302,25 @@ bool LightApp_HDFDriver::ReadDatasFromFile( const char* theFileName, bool isMult hdf_dataset = 0; } + // Read the Tree + char* aStreamTree = NULL; + int aTreeStreamSize = 0; + + if (hdf_sco_group->ExistInternalObject("TREE_STREAM")) { + HDFdataset *hdf_dataset = new HDFdataset("TREE_STREAM", hdf_sco_group); + hdf_dataset->OpenOnDisk(); + aTreeStreamSize = hdf_dataset->GetSize(); + aStreamTree = new char[aTreeStreamSize]; + if (aStreamTree == NULL) { + isError = true; + } else { + hdf_dataset->ReadFromDisk(aStreamTree); + } + + hdf_dataset->CloseOnDisk(); + hdf_dataset = 0; + } + HDFdataset *multifile_hdf_dataset = new HDFdataset("MULTIFILE_STATE", hdf_sco_group); multifile_hdf_dataset->OpenOnDisk(); multifile_hdf_dataset->ReadFromDisk(aMultifileState); @@ -314,6 +344,14 @@ bool LightApp_HDFDriver::ReadDatasFromFile( const char* theFileName, bool isMult delete [] aStreamFile; } + if (aStreamTree != NULL) { + // Set buffer to to myMapTree + std::string aTree(aStreamTree); + SetTree(mapEntryName[name], aTree); + + delete [] aStreamTree; + } + hdf_sco_group->CloseOnDisk(); } } diff --git a/src/LightApp/LightApp_IDataObject.cxx b/src/LightApp/LightApp_IDataObject.cxx new file mode 100644 index 000000000..52b3f6512 --- /dev/null +++ b/src/LightApp/LightApp_IDataObject.cxx @@ -0,0 +1,150 @@ +// Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE +// +// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +// +// 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. +// +// 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 for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +#include "LightApp_IDataObject.h" + +#define ENTRY "entry" +#define NAME "name" +#define OBJECT_NODE "objectNode" +#define ROOT_NODE "rootNode" + +/*! + \class LightApp_IDataObject + \brief Base data object class to build the object tree in Object Browser. +*/ + +/*! + \brief Constructor. + \param parent parent data object +*/ +LightApp_IDataObject::LightApp_IDataObject( SUIT_DataObject* parent ) +: CAM_DataObject( parent ), + LightApp_DataObject( parent ), + myName( "" ), + myEntry( "" ) +{ +} + +/*! + \brief Destructor. +*/ +LightApp_IDataObject::~LightApp_IDataObject() +{ +} + +/*! + \brief Set the data object name. +*/ +void LightApp_IDataObject::setName( const QString& theName ) +{ + myName = theName; +} + +/*! + \brief Set the data object ID. +*/ +void LightApp_IDataObject::setEntry( const QString& theEntry ) +{ + myEntry = theEntry; +} + +/*! + \brief Return the data object name. +*/ +QString LightApp_IDataObject::name() const +{ + return myName; +} + +/*! + \brief Return the data object name. +*/ +QString LightApp_IDataObject::entry() const +{ + return myEntry; +} + +QString LightApp_IDataObject::GetTreeDoc( CAM_DataObject* theRoot ) +{ + + QDomDocument aDoc; + QDomElement treeRoot = aDoc.createElement( ROOT_NODE ); + if ( dumpObject( theRoot, treeRoot ) ) + { + aDoc.appendChild( treeRoot ); + dumpTree( theRoot, treeRoot ); + } + return aDoc.toString(); +} + +bool LightApp_IDataObject::dumpObject( SUIT_DataObject* objNode, QDomElement& treeNode ) +{ + if( objNode != 0 ) { + LightApp_DataObject* obj = dynamic_cast( objNode ); + treeNode.setAttribute( ENTRY, obj->entry() ); + treeNode.setAttribute( NAME, obj->name() ); + return true; + } + return false; +} + +void LightApp_IDataObject::dumpTree( SUIT_DataObject* currentNode, QDomElement& currentRoot ) +{ + for( int i=0; i < currentNode->childCount(); i++ ) { + QDomElement treeChild = currentRoot.ownerDocument().createElement( OBJECT_NODE ); + SUIT_DataObject* obj = currentNode->childObject( i ); + dumpObject( obj, treeChild ); + currentRoot.appendChild( treeChild ); + dumpTree( obj, treeChild ); + } +} + +void LightApp_IDataObject::BuildITree( SUIT_DataObject* theRoot, const QString& theTree ) +{ + QDomDocument aDoc; + if( aDoc.setContent( theTree ) ) { + QDomElement aRoot = aDoc.documentElement(); + LightApp_IDataObject* obj = new LightApp_IDataObject(); + BuildNode( aRoot, *obj ); + theRoot->appendChild( obj ); + BuildIModuleTree( aRoot, *obj ); + } +} + +void LightApp_IDataObject::BuildNode( const QDomElement& treeNode, LightApp_IDataObject& objNode ) +{ + QString aName = treeNode.attribute( NAME ); + objNode.setName( aName ); + QString aEntry = treeNode.attribute( ENTRY ); + objNode.setEntry( aEntry ); +} + +void LightApp_IDataObject::BuildIModuleTree( const QDomElement& currentRoot, LightApp_IDataObject& currentNode ) +{ + for( QDomElement treeChild = currentRoot.firstChildElement(); !treeChild.isNull(); + treeChild = treeChild.nextSiblingElement() ) { + LightApp_IDataObject* obj = new LightApp_IDataObject(); + BuildNode( treeChild, *obj ); + currentNode.appendChild( obj ); + BuildIModuleTree( treeChild, *obj ); + } +} diff --git a/src/LightApp/LightApp_IDataObject.h b/src/LightApp/LightApp_IDataObject.h new file mode 100644 index 000000000..38aab09e4 --- /dev/null +++ b/src/LightApp/LightApp_IDataObject.h @@ -0,0 +1,58 @@ +// Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE +// +// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +// +// 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. +// +// 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 for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +#ifndef LightApp_IDATAOBJECT_H +#define LightApp_IDATAOBJECT_H + +#include +#include +#include +#include +#include + +class LIGHTAPP_EXPORT LightApp_IDataObject : public LightApp_DataObject +{ + +public: + LightApp_IDataObject( SUIT_DataObject* = 0 ); + virtual ~LightApp_IDataObject(); + + virtual QString name() const; + virtual QString entry() const; + + void setName( const QString& ); + void setEntry( const QString& ); + static QString GetTreeDoc( CAM_DataObject* theRoot ); + static void BuildITree( SUIT_DataObject* theRoot, const QString& theTree = 0 ); + +protected: + static bool dumpObject( SUIT_DataObject* objNode, QDomElement& treeNode ); + static void dumpTree( SUIT_DataObject* currentNode, QDomElement& currentRoot ); + static void BuildNode( const QDomElement& treeNode, LightApp_IDataObject& objNode ); + static void BuildIModuleTree( const QDomElement& currentRoot, LightApp_IDataObject& currentNode ); + + +private: + QString myName; + QString myEntry; +}; + +#endif // LightApp_IDATAOBJECT_H diff --git a/src/LightApp/LightApp_Study.cxx b/src/LightApp/LightApp_Study.cxx index e0356bcf8..08c127ad4 100644 --- a/src/LightApp/LightApp_Study.cxx +++ b/src/LightApp/LightApp_Study.cxx @@ -26,6 +26,7 @@ #include "LightApp_Application.h" #include "LightApp_DataModel.h" #include "LightApp_DataObject.h" +#include "LightApp_IDataObject.h" #include "LightApp_HDFDriver.h" #include "SUIT_ResourceMgr.h" @@ -80,7 +81,15 @@ bool LightApp_Study::openDocument( const QString& theFileName ) return false; setRoot( new LightApp_RootObject( this ) ); // create myRoot - + + //create object tree for all modules + CAM_Application* app = dynamic_cast( application() ); + QStringList listOfModules; + app->modules( listOfModules, false ); + + for ( QStringList::Iterator it = listOfModules.begin(); it != listOfModules.end(); ++it ) + LightApp_IDataObject::BuildITree( root(), myDriver->GetTree( ( app->moduleName( (*it) ) ).toStdString() ) ); + // update loaded data models: call open() and update() on them. ModelList dm_s; dataModels( dm_s ); @@ -155,7 +164,7 @@ bool LightApp_Study::saveDocumentAs( const QString& theFileName ) aModel->saveAs( theFileName, this, listOfFiles ); if ( !listOfFiles.isEmpty() ) saveModuleData(aModel->module()->name(), listOfFiles); - + myDriver->SetTree(aModel->module()->name().toStdString(),LightApp_IDataObject::GetTreeDoc(aModel->root()).toStdString()); // Remove files if necessary. File is removed if it was in the list of files before // saving and it is not contained in the list after saving. This provides correct // removing previous temporary files. These files are not removed before saving @@ -204,6 +213,7 @@ bool LightApp_Study::saveDocument() myDriver->ClearDriverContents(); QStringList listOfFiles; QListIterator itList( list ); + while ( itList.hasNext() ) { LightApp_DataModel* aModel = (LightApp_DataModel*)itList.next(); if ( !aModel ) continue; @@ -211,6 +221,7 @@ bool LightApp_Study::saveDocument() listOfFiles.clear(); aModel->save( listOfFiles ); saveModuleData(aModel->module()->name(), listOfFiles); + myDriver->SetTree(aModel->module()->name().toStdString(),LightApp_IDataObject::GetTreeDoc(aModel->root()).toStdString()); } bool res = saveStudyData(studyName()); diff --git a/src/LightApp/Makefile.am b/src/LightApp/Makefile.am index 66d8cc884..4a720c955 100755 --- a/src/LightApp/Makefile.am +++ b/src/LightApp/Makefile.am @@ -41,6 +41,7 @@ salomeinclude_HEADERS = \ LightApp_Driver.h \ LightApp_EventFilter.h \ LightApp_HDFDriver.h \ + LightApp_IDataObject.h \ LightApp_Module.h \ LightApp_ModuleAction.h \ LightApp_ModuleDlg.h \ @@ -89,6 +90,7 @@ dist_libLightApp_la_SOURCES = \ LightApp_Driver.cxx \ LightApp_EventFilter.cxx \ LightApp_HDFDriver.cxx \ + LightApp_IDataObject.cxx \ LightApp_Module.cxx \ LightApp_ModuleAction.cxx \ LightApp_ModuleDlg.cxx \