1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #include "LightApp_Study.h"
24 #include "CAM_DataModel.h"
25 #include "CAM_Module.h"
26 #include "LightApp_Application.h"
27 #include "LightApp_DataModel.h"
28 #include "LightApp_DataObject.h"
29 #include "LightApp_HDFDriver.h"
31 #include "SUIT_ResourceMgr.h"
32 #include "SUIT_DataObjectIterator.h"
40 LightApp_Study::LightApp_Study( SUIT_Application* app )
44 myDriver = new LightApp_HDFDriver();
45 //myDriver = new LightApp_Driver();
51 LightApp_Study::~LightApp_Study()
58 bool LightApp_Study::createDocument( const QString& theStr )
60 setStudyName( QString( "Study%1" ).arg( LightApp_Application::studyId() ) );
63 setRoot( new LightApp_RootObject( this ) );
65 bool aRet = CAM_Study::createDocument( theStr );
75 bool LightApp_Study::openDocument( const QString& theFileName )
77 myDriver->ClearDriverContents();
78 // create files for models from theFileName
79 if( !openStudyData(theFileName))
82 setRoot( new LightApp_RootObject( this ) ); // create myRoot
84 // update loaded data models: call open() and update() on them.
87 QListIterator<CAM_DataModel*> it( dm_s );
88 while ( it.hasNext() )
89 openDataModel( studyName(), it.next() );
90 // this will build a SUIT_DataObject-s tree under myRoot member field
91 // passing "false" in order NOT to rebuild existing data models' trees - it was done in previous step
92 // but tree that corresponds to not-loaded data models will be updated any way.
93 ((LightApp_Application*)application())->updateObjectBrowser( false );
95 bool res = CAM_Study::openDocument( theFileName );
104 bool LightApp_Study::loadDocument( const QString& theStudyName )
106 myDriver->ClearDriverContents();
107 if( !openStudyData(theStudyName))
110 setRoot( new LightApp_RootObject( this ) ); // create myRoot
112 //SRN: BugID IPAL9021, put there the same code as in a method openDocument
114 // update loaded data models: call open() and update() on them.
118 QListIterator<CAM_DataModel*> it( dm_s );
119 while ( it.hasNext() )
120 openDataModel( studyName(), it.next() );
122 // this will build a SUIT_DataObject-s tree under myRoot member field
123 // passing "false" in order NOT to rebuild existing data models' trees - it was done in previous step
124 // but tree that corresponds to not-loaded data models will be updated any way.
125 ((LightApp_Application*)application())->updateObjectBrowser( false );
127 bool res = CAM_Study::openDocument( theStudyName );
129 //SRN: BugID IPAL9021: End
136 bool LightApp_Study::saveDocumentAs( const QString& theFileName )
138 SUIT_ResourceMgr* resMgr = application()->resourceMgr();
145 QStringList listOfFiles;
146 bool isMultiFile = resMgr->booleanValue( "Study", "multi_file", false );
147 QListIterator<CAM_DataModel*> itList( list );
148 while ( itList.hasNext() )
150 LightApp_DataModel* aModel = (LightApp_DataModel*)itList.next();
151 if ( !aModel ) continue;
153 std::vector<std::string> anOldList = myDriver->GetListOfFiles( aModel->module()->name().toLatin1().constData() );
155 aModel->saveAs( theFileName, this, listOfFiles );
156 if ( !listOfFiles.isEmpty() )
157 saveModuleData(aModel->module()->name(), listOfFiles);
159 // Remove files if necessary. File is removed if it was in the list of files before
160 // saving and it is not contained in the list after saving. This provides correct
161 // removing previous temporary files. These files are not removed before saving
162 // because they may be required for it.
164 std::vector<std::string> aNewList = myDriver->GetListOfFiles( aModel->module()->name().toLatin1().constData() );
166 std::set<std::string> aNewNames;
167 std::set<std::string> toRemove;
169 for( i = 0, n = aNewList.size(); i < n; i++ )
170 aNewNames.insert( aNewList[ i ] );
171 for( i = 0, n = anOldList.size(); i < n; i++ )
173 if ( i == 0 ) // directory is always inserted in list
174 toRemove.insert( anOldList[ i ] );
175 else if ( aNewNames.find( anOldList[ i ] ) == aNewNames.end() )
176 toRemove.insert( anOldList[ i ] );
179 std::vector<std::string> toRemoveList( toRemove.size() );
180 std::set<std::string>::iterator anIter;
181 for( anIter = toRemove.begin(), i = 0; anIter != toRemove.end(); ++anIter, ++i )
182 toRemoveList[ i ] = *anIter;
185 myDriver->RemoveFiles( toRemoveList, isMultiFile );
188 bool res = saveStudyData(theFileName);
189 res = res && CAM_Study::saveDocumentAs( theFileName );
190 //SRN: BugID IPAL9377, removed usage of uninitialized variable <res>
200 bool LightApp_Study::saveDocument()
202 ModelList list; dataModels( list );
204 myDriver->ClearDriverContents();
205 QStringList listOfFiles;
206 QListIterator<CAM_DataModel*> itList( list );
207 while ( itList.hasNext() ) {
208 LightApp_DataModel* aModel = (LightApp_DataModel*)itList.next();
209 if ( !aModel ) continue;
212 aModel->save( listOfFiles );
213 saveModuleData(aModel->module()->name(), listOfFiles);
216 bool res = saveStudyData(studyName());
217 res = res && CAM_Study::saveDocument();
227 void LightApp_Study::closeDocument(bool permanently)
229 // Inform everybody that this study is going to close when it's most safe to,
230 // i.e. in the very beginning
233 CAM_Study::closeDocument(permanently);
235 // Remove temporary files
236 myDriver->ClearDriverContents();
240 \return real entry by entry of reference
241 \param entry - entry of reference object
243 QString LightApp_Study::referencedToEntry( const QString& entry ) const
249 \return entries of object children
251 void LightApp_Study::children( const QString&, QStringList& ) const
256 \return true if entry corresponds to component
258 bool LightApp_Study::isComponent( const QString& entry ) const
264 root()->children( ch );
265 DataObjectList::const_iterator anIt = ch.begin(), aLast = ch.end();
266 for( ; anIt!=aLast; anIt++ )
268 LightApp_DataObject* obj = dynamic_cast<LightApp_DataObject*>( *anIt );
269 if( obj && obj->entry()==entry )
276 \return component data type for entry
278 QString LightApp_Study::componentDataType( const QString& entry ) const
280 LightApp_DataObject* aCurObj;
281 for ( SUIT_DataObjectIterator it( root(), SUIT_DataObjectIterator::DepthLeft ); it.current(); ++it ) {
282 aCurObj = dynamic_cast<LightApp_DataObject*>( it.current() );
283 if ( aCurObj && aCurObj->entry() == entry ) {
284 return aCurObj->componentDataType();
291 \return true if study is modified
293 bool LightApp_Study::isModified() const
295 bool isAnyChanged = CAM_Study::isModified();
296 ModelList list; dataModels( list );
298 LightApp_DataModel* aModel = 0;
299 QListIterator<CAM_DataModel*> it( list );
300 while ( it.hasNext() && !isAnyChanged ) {
301 aModel = dynamic_cast<LightApp_DataModel*>( it.next() );
303 isAnyChanged = aModel->isModified();
309 \return true if data model is saved
311 bool LightApp_Study::isSaved() const
313 return CAM_Study::isSaved();
317 Creates SComponent for module, necessary for SalomeApp study
319 void LightApp_Study::addComponent(const CAM_DataModel* dm)
324 Saves list file for module 'theModuleName'
326 void LightApp_Study::saveModuleData(QString theModuleName, QStringList theListOfFiles)
328 int aNb = theListOfFiles.count();
332 std::vector<std::string> aListOfFiles ( aNb );
334 for ( QStringList::Iterator it = theListOfFiles.begin(); it != theListOfFiles.end(); ++it ) {
335 if ( (*it).isEmpty() )
337 aListOfFiles[anIndex] = (*it).toLatin1().constData();
340 myDriver->SetListOfFiles(theModuleName.toLatin1().constData(), aListOfFiles);
344 Gets list of file for module 'theModuleNam'
346 void LightApp_Study::openModuleData(QString theModuleName, QStringList& theListOfFiles)
348 std::vector<std::string> aListOfFiles = myDriver->GetListOfFiles(theModuleName.toLatin1().constData());
349 int i, aLength = aListOfFiles.size() - 1;
353 //Get a temporary directory for saved a file
354 theListOfFiles.append(aListOfFiles[0].c_str());
355 for(i = 0; i < aLength; i++)
356 theListOfFiles.append(aListOfFiles[i+1].c_str());
360 Saves data from study
362 bool LightApp_Study::saveStudyData( const QString& theFileName )
364 ModelList list; dataModels( list );
365 SUIT_ResourceMgr* resMgr = application()->resourceMgr();
368 bool isMultiFile = resMgr->booleanValue( "Study", "multi_file", false );
370 bool aRes = myDriver->SaveDatasInFile(theFileName.toLatin1(), isMultiFile);
377 bool LightApp_Study::openStudyData( const QString& theFileName )
379 SUIT_ResourceMgr* resMgr = application()->resourceMgr();
382 bool isMultiFile = resMgr->booleanValue( "Study", "multi_file", false );
384 bool aRes = myDriver->ReadDatasFromFile(theFileName.toLatin1(), isMultiFile);
391 bool LightApp_Study::openDataModel( const QString& studyName, CAM_DataModel* dm )
396 QStringList listOfFiles;
397 openModuleData(dm->module()->name(), listOfFiles);
398 if (dm && dm->open(studyName, this, listOfFiles)) {
399 // Something has been read -> create data model tree
400 LightApp_DataModel* aDM = dynamic_cast<LightApp_DataModel*>( dm );
402 aDM->update(NULL, this);
409 \return temporary directory for saving files of modules
411 std::string LightApp_Study::GetTmpDir (const char* theURL,
412 const bool isMultiFile)
414 return myDriver->GetTmpDir(theURL, isMultiFile);
418 \return list of files necessary for module
419 \param theModuleName - name of module
421 std::vector<std::string> LightApp_Study::GetListOfFiles(const char* theModuleName) const
423 std::vector<std::string> aListOfFiles;
424 aListOfFiles = myDriver->GetListOfFiles(theModuleName);
429 Sets list of files necessary for module
430 \param theModuleName - name of module
431 \param theListOfFiles - list of files
433 void LightApp_Study::SetListOfFiles (const char* theModuleName, const std::vector<std::string> theListOfFiles)
435 myDriver->SetListOfFiles(theModuleName, theListOfFiles);
439 Removes temporary files
441 void LightApp_Study::RemoveTemporaryFiles (const char* theModuleName, const bool isMultiFile) const
445 bool isDirDeleted = true;
446 myDriver->RemoveTemporaryFiles(theModuleName, isDirDeleted);
450 Fills list with components names
451 \param comp - list to be filled
453 void LightApp_Study::components( QStringList& comp ) const
455 DataObjectList children = root()->children();
456 DataObjectList::const_iterator anIt = children.begin(), aLast = children.end();
457 for( ; anIt!=aLast; anIt++ )
459 LightApp_DataObject* obj = dynamic_cast<LightApp_DataObject*>( *anIt );
460 if( obj && obj->entry()!="Interface Applicative" )
461 comp.append( obj->entry() );