1 // Copyright (C) 2007-2014 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, or (at your option) any later version.
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
23 #include "LightApp_Study.h"
25 #include "CAM_DataModel.h"
26 #include "CAM_Module.h"
27 #include "LightApp_Application.h"
28 #include "LightApp_DataModel.h"
29 #include "LightApp_DataObject.h"
30 #include "LightApp_HDFDriver.h"
32 #include "SUIT_ResourceMgr.h"
33 #include "SUIT_DataObjectIterator.h"
34 #include "SUIT_DataBrowser.h"
35 #include "SUIT_TreeModel.h"
42 LightApp_Study::LightApp_Study( SUIT_Application* app )
46 myDriver = new LightApp_HDFDriver();
47 //myDriver = new LightApp_Driver();
53 LightApp_Study::~LightApp_Study()
55 delete myDriver; myDriver = 0;
61 bool LightApp_Study::createDocument( const QString& theStr )
63 setStudyName( QString( "Study%1" ).arg( LightApp_Application::studyId() ) );
66 setRoot( new LightApp_RootObject( this ) );
68 bool aRet = CAM_Study::createDocument( theStr );
78 bool LightApp_Study::openDocument( const QString& theFileName )
80 myDriver->ClearDriverContents();
81 // create files for models from theFileName
82 if( !openStudyData(theFileName))
85 setRoot( new LightApp_RootObject( this ) ); // create myRoot
87 // update loaded data models: call open() and update() on them.
90 QListIterator<CAM_DataModel*> it( dm_s );
91 while ( it.hasNext() )
92 openDataModel( studyName(), it.next() );
93 // this will build a SUIT_DataObject-s tree under myRoot member field
94 // passing "false" in order NOT to rebuild existing data models' trees - it was done in previous step
95 // but tree that corresponds to not-loaded data models will be updated any way.
96 ((LightApp_Application*)application())->updateObjectBrowser( false );
98 bool res = CAM_Study::openDocument( theFileName );
107 bool LightApp_Study::loadDocument( const QString& theStudyName )
109 myDriver->ClearDriverContents();
110 if( !openStudyData(theStudyName))
113 setRoot( new LightApp_RootObject( this ) ); // create myRoot
115 //SRN: BugID IPAL9021, put there the same code as in a method openDocument
117 // update loaded data models: call open() and update() on them.
121 QListIterator<CAM_DataModel*> it( dm_s );
122 while ( it.hasNext() )
123 openDataModel( studyName(), it.next() );
125 // this will build a SUIT_DataObject-s tree under myRoot member field
126 // passing "false" in order NOT to rebuild existing data models' trees - it was done in previous step
127 // but tree that corresponds to not-loaded data models will be updated any way.
128 ((LightApp_Application*)application())->updateObjectBrowser( false );
130 bool res = CAM_Study::openDocument( theStudyName );
132 //SRN: BugID IPAL9021: End
139 bool LightApp_Study::saveDocumentAs( const QString& theFileName )
141 SUIT_ResourceMgr* resMgr = application()->resourceMgr();
148 QStringList listOfFiles;
149 bool isMultiFile = resMgr->booleanValue( "Study", "multi_file", false );
150 QListIterator<CAM_DataModel*> itList( list );
151 while ( itList.hasNext() )
153 LightApp_DataModel* aModel = (LightApp_DataModel*)itList.next();
154 if ( !aModel ) continue;
156 std::vector<std::string> anOldList = myDriver->GetListOfFiles( aModel->module()->name().toLatin1().constData() );
158 aModel->saveAs( theFileName, this, listOfFiles );
159 if ( !listOfFiles.isEmpty() )
160 saveModuleData(aModel->module()->name(), listOfFiles);
162 // Remove files if necessary. File is removed if it was in the list of files before
163 // saving and it is not contained in the list after saving. This provides correct
164 // removing previous temporary files. These files are not removed before saving
165 // because they may be required for it.
167 std::vector<std::string> aNewList = myDriver->GetListOfFiles( aModel->module()->name().toLatin1().constData() );
169 std::set<std::string> aNewNames;
170 std::set<std::string> toRemove;
172 for( i = 0, n = aNewList.size(); i < n; i++ )
173 aNewNames.insert( aNewList[ i ] );
174 for( i = 0, n = anOldList.size(); i < n; i++ )
176 if ( i == 0 ) // directory is always inserted in list
177 toRemove.insert( anOldList[ i ] );
178 else if ( aNewNames.find( anOldList[ i ] ) == aNewNames.end() )
179 toRemove.insert( anOldList[ i ] );
182 std::vector<std::string> toRemoveList( toRemove.size() );
183 std::set<std::string>::iterator anIter;
184 for( anIter = toRemove.begin(), i = 0; anIter != toRemove.end(); ++anIter, ++i )
185 toRemoveList[ i ] = *anIter;
188 myDriver->RemoveFiles( toRemoveList, isMultiFile );
191 bool res = saveStudyData(theFileName);
192 res = res && CAM_Study::saveDocumentAs( theFileName );
193 //SRN: BugID IPAL9377, removed usage of uninitialized variable <res>
203 bool LightApp_Study::saveDocument()
205 ModelList list; dataModels( list );
207 myDriver->ClearDriverContents();
208 QStringList listOfFiles;
209 QListIterator<CAM_DataModel*> itList( list );
210 while ( itList.hasNext() ) {
211 LightApp_DataModel* aModel = (LightApp_DataModel*)itList.next();
212 if ( !aModel ) continue;
215 aModel->save( listOfFiles );
216 saveModuleData(aModel->module()->name(), listOfFiles);
219 bool res = saveStudyData(studyName());
220 res = res && CAM_Study::saveDocument();
230 void LightApp_Study::closeDocument(bool permanently)
232 // Inform everybody that this study is going to close when it's most safe to,
233 // i.e. in the very beginning
236 CAM_Study::closeDocument(permanently);
238 // Remove temporary files
239 myDriver->ClearDriverContents();
243 \return real entry by entry of reference
244 \param entry - entry of reference object
246 QString LightApp_Study::referencedToEntry( const QString& entry ) const
252 \return entries of object children
254 void LightApp_Study::children( const QString&, QStringList& ) const
259 \return true if entry corresponds to component
261 bool LightApp_Study::isComponent( const QString& entry ) const
267 root()->children( ch );
268 DataObjectList::const_iterator anIt = ch.begin(), aLast = ch.end();
269 for( ; anIt!=aLast; anIt++ )
271 LightApp_DataObject* obj = dynamic_cast<LightApp_DataObject*>( *anIt );
272 if( obj && obj->entry()==entry )
279 \return component data type for entry
281 QString LightApp_Study::componentDataType( const QString& entry ) const
283 LightApp_DataObject* aCurObj;
284 for ( SUIT_DataObjectIterator it( root(), SUIT_DataObjectIterator::DepthLeft ); it.current(); ++it ) {
285 aCurObj = dynamic_cast<LightApp_DataObject*>( it.current() );
286 if ( aCurObj && aCurObj->entry() == entry ) {
287 return aCurObj->componentDataType();
294 \return true if study is modified
296 bool LightApp_Study::isModified() const
298 bool isAnyChanged = CAM_Study::isModified();
299 ModelList list; dataModels( list );
301 LightApp_DataModel* aModel = 0;
302 QListIterator<CAM_DataModel*> it( list );
303 while ( it.hasNext() && !isAnyChanged ) {
304 aModel = dynamic_cast<LightApp_DataModel*>( it.next() );
306 isAnyChanged = aModel->isModified();
312 \return true if data model is saved
314 bool LightApp_Study::isSaved() const
316 return CAM_Study::isSaved();
320 Creates SComponent for module, necessary for SalomeApp study
322 void LightApp_Study::addComponent(const CAM_DataModel* dm)
327 Saves list file for module 'theModuleName'
329 void LightApp_Study::saveModuleData(QString theModuleName, QStringList theListOfFiles)
331 int aNb = theListOfFiles.count();
335 std::vector<std::string> aListOfFiles ( aNb );
337 for ( QStringList::Iterator it = theListOfFiles.begin(); it != theListOfFiles.end(); ++it ) {
338 if ( (*it).isEmpty() )
340 aListOfFiles[anIndex] = (*it).toLatin1().constData();
343 myDriver->SetListOfFiles(theModuleName.toLatin1().constData(), aListOfFiles);
347 Gets list of file for module 'theModuleNam'
349 void LightApp_Study::openModuleData(QString theModuleName, QStringList& theListOfFiles)
351 std::vector<std::string> aListOfFiles = myDriver->GetListOfFiles(theModuleName.toLatin1().constData());
352 int i, aLength = aListOfFiles.size() - 1;
356 //Get a temporary directory for saved a file
357 theListOfFiles.append(aListOfFiles[0].c_str());
358 for(i = 0; i < aLength; i++)
359 theListOfFiles.append(aListOfFiles[i+1].c_str());
363 Saves data from study
365 bool LightApp_Study::saveStudyData( const QString& theFileName )
367 ModelList list; dataModels( list );
368 SUIT_ResourceMgr* resMgr = application()->resourceMgr();
371 bool isMultiFile = resMgr->booleanValue( "Study", "multi_file", false );
373 bool aRes = myDriver->SaveDatasInFile(theFileName.toLatin1(), isMultiFile);
380 bool LightApp_Study::openStudyData( const QString& theFileName )
382 SUIT_ResourceMgr* resMgr = application()->resourceMgr();
385 bool isMultiFile = resMgr->booleanValue( "Study", "multi_file", false );
387 bool aRes = myDriver->ReadDatasFromFile(theFileName.toLatin1(), isMultiFile);
394 bool LightApp_Study::openDataModel( const QString& studyName, CAM_DataModel* dm )
399 QStringList listOfFiles;
400 openModuleData(dm->module()->name(), listOfFiles);
401 if (dm && dm->open(studyName, this, listOfFiles)) {
402 // Something has been read -> create data model tree
403 LightApp_DataModel* aDM = dynamic_cast<LightApp_DataModel*>( dm );
405 aDM->update(NULL, this);
412 \return temporary directory for saving files of modules
414 std::string LightApp_Study::GetTmpDir (const char* theURL,
415 const bool isMultiFile)
417 return myDriver->GetTmpDir(theURL, isMultiFile);
421 \return list of files necessary for module
422 \param theModuleName - name of module
424 std::vector<std::string> LightApp_Study::GetListOfFiles(const char* theModuleName) const
426 std::vector<std::string> aListOfFiles;
427 aListOfFiles = myDriver->GetListOfFiles(theModuleName);
432 Sets list of files necessary for module
433 \param theModuleName - name of module
434 \param theListOfFiles - list of files
436 void LightApp_Study::SetListOfFiles (const char* theModuleName, const std::vector<std::string> theListOfFiles)
438 myDriver->SetListOfFiles(theModuleName, theListOfFiles);
442 Removes temporary files
444 void LightApp_Study::RemoveTemporaryFiles (const char* theModuleName, const bool isMultiFile) const
448 bool isDirDeleted = true;
449 myDriver->RemoveTemporaryFiles(theModuleName, isDirDeleted);
453 Virtual method that creates the root object (module object) for the given data model.
454 The type of the created object depends on the study class, therefore data model classes
455 should not create their module objects directly and should instead use
456 LightApp_DataModel::createModuleObject() that in its turn relies on this method.
458 \param theDataModel - data model instance to create a module object for
459 \param theParent - the module object's parent (normally it's the study root)
460 \return the module object instance
461 \sa LightApp_DataModel class, SalomeApp_Study class, LightApp_ModuleObject class
463 CAM_ModuleObject* LightApp_Study::createModuleObject( LightApp_DataModel* theDataModel,
464 SUIT_DataObject* theParent ) const
466 // Calling addComponent() for symmetry with SalomeApp_Study
467 // Currently it has empty implementation, but maybe in the future things will change...
468 LightApp_Study* that = const_cast<LightApp_Study*>( this );
469 that->addComponent( theDataModel );
471 // Avoid creating multiple module objects for the same module
472 CAM_ModuleObject* res = 0;
474 DataObjectList children = root()->children();
475 DataObjectList::const_iterator anIt = children.begin(), aLast = children.end();
476 for( ; !res && anIt!=aLast; anIt++ )
478 LightApp_ModuleObject* obj = dynamic_cast<LightApp_ModuleObject*>( *anIt );
479 if ( obj && obj->name() == theDataModel->module()->moduleName() )
484 res = new LightApp_ModuleObject( theDataModel, theParent );
491 Fills list with components names
492 \param comp - list to be filled
494 void LightApp_Study::components( QStringList& comp ) const
496 DataObjectList children = root()->children();
497 DataObjectList::const_iterator anIt = children.begin(), aLast = children.end();
498 for( ; anIt!=aLast; anIt++ )
500 LightApp_DataObject* obj = dynamic_cast<LightApp_DataObject*>( *anIt );
501 if ( obj && obj->entry() != getVisualComponentName() )
502 comp.append( obj->entry() );
507 Get the entry for the given module
508 \param comp - list to be filled
509 \return module root's entry
511 QString LightApp_Study::centry( const QString& comp ) const
515 dataModels( dmlist );
516 QListIterator<CAM_DataModel*> it( dmlist );
517 while ( it.hasNext() && e.isEmpty() ) {
518 CAM_DataModel* dm = it.next();
519 if ( dm->module() && dm->module()->name() == comp ) {
520 LightApp_DataObject* r = dynamic_cast<LightApp_DataObject*>( dm->root() );
521 if ( r ) e = r->entry();
528 \return a name of the component where visual parameters are stored
530 QString LightApp_Study::getVisualComponentName() const
532 return "Interface Applicative";
540 Set a visual property of the object
541 \param theViewId - Id of the viewer namager
542 \param theEntry - Entry of the object
543 \param thePropName - the name of the visual property
544 \param theValue - the value of the visual property
546 void LightApp_Study::setObjectProperty(int theViewId, QString theEntry, QString thePropName, QVariant theValue) {
548 //Try to find viewer manager in the map
549 ViewMgrMap::Iterator v_it = myViewMgrMap.find(theViewId);
550 if(v_it == myViewMgrMap.end()) {
552 //1) Create property map
554 aPropMap.insert(thePropName, theValue);
556 //2) Create object map
558 anObjMap.insert(theEntry,aPropMap);
560 //3) Insert in the view manager map
561 myViewMgrMap.insert(theViewId, anObjMap);
564 ObjMap& anObjMap = v_it.value();
565 ObjMap::Iterator o_it = anObjMap.find(theEntry);
566 if(o_it == anObjMap.end()) {
567 //1) Create property map
569 aPropMap.insert(thePropName, theValue);
571 //2) Insert in the object map
572 anObjMap.insert(theEntry, aPropMap);
574 PropMap& aPropMap = o_it.value();
575 aPropMap.insert(thePropName, theValue);
581 Get a visual property of the object identified by theViewMgrId, theEntry and thePropName.
582 \param theViewMgrId - Id of the viewer manager.
583 \param theEntry - Entry of the object.
584 \param thePropName - the name of the visual property.
585 \param theDefValue - the default value of the visual property.
586 \return value of the visual propetry. If value is't found then return theDefValue.
588 QVariant LightApp_Study::getObjectProperty(int theViewMgrId, QString theEntry, QString thePropName, QVariant theDefValue) const {
589 QVariant& aResult = theDefValue;
590 ViewMgrMap::ConstIterator v_it = myViewMgrMap.find(theViewMgrId);
591 if(v_it != myViewMgrMap.end()){
592 const ObjMap& anObjectMap = v_it.value();
593 ObjMap::ConstIterator o_it = anObjectMap.find(theEntry);
594 if(o_it != anObjectMap.end()) {
595 const PropMap& aPropMap = o_it.value();
596 PropMap::ConstIterator p_it = aPropMap.find(thePropName);
597 if(p_it != aPropMap.end()) {
598 aResult = p_it.value();
606 Remove view manager with all objects.
607 \param theViewMgrId - Id of the viewer manager.
609 void LightApp_Study::removeViewMgr( int theViewMgrId ) {
610 myViewMgrMap.remove(theViewMgrId);
615 Get a map of the properties of the object identified by theViewMgrId and theEntry.
616 \param theViewMgrId - Id of the viewer manager.
617 \param theEntry - Entry of the object.
618 \return a map of the properties of the object.
620 const PropMap& LightApp_Study::getObjectPropMap(int theViewMgrId, QString theEntry) {
621 ViewMgrMap::Iterator v_it = myViewMgrMap.find(theViewMgrId);
622 if (v_it != myViewMgrMap.end()) {
623 ObjMap& anObjectMap = v_it.value();
624 ObjMap::Iterator o_it = anObjectMap.find(theEntry);
625 if(o_it != anObjectMap.end()) {
629 anObjectMap.insert(theEntry, aPropMap);
630 return anObjectMap.find(theEntry).value();
635 anObjMap.insert(theEntry,aPropMap);
636 myViewMgrMap.insert(theViewMgrId, anObjMap);
638 ObjMap& anObjectMap = myViewMgrMap.find(theViewMgrId).value();
639 return anObjectMap.find(theEntry).value();
644 Set a map of the properties of the object identified by theViewMgrId and theEntry.
645 \param theViewMgrId - Id of the viewer manager.
646 \param theEntry - Entry of the object.
648 void LightApp_Study::setObjectPropMap(int theViewMgrId, QString theEntry, PropMap thePropMap) {
649 //Try to find viewer manager in the map
650 ViewMgrMap::Iterator v_it = myViewMgrMap.find(theViewMgrId);
651 if(v_it == myViewMgrMap.end()) {
653 //1) Create object map
655 anObjMap.insert(theEntry,thePropMap);
657 //3) Insert in the view manager map
658 myViewMgrMap.insert(theViewMgrId, anObjMap);
660 ObjMap& anObjMap = v_it.value();
661 anObjMap.insert(theEntry,thePropMap);
666 Remove object's properties from all view managers.
667 \param theEntry - Entry of the object.
669 void LightApp_Study::removeObjectFromAll( QString theEntry ) {
670 ViewMgrMap::Iterator v_it = myViewMgrMap.begin();
671 for( ;v_it != myViewMgrMap.end(); v_it++ ) {
672 v_it.value().remove(theEntry);
677 Get all objects and it's properties from view manager identified by theViewMgrId.
678 \param theEntry - Entry of the object.
680 const ObjMap& LightApp_Study::getObjectMap ( int theViewMgrId ) {
681 ViewMgrMap::Iterator v_it = myViewMgrMap.find(theViewMgrId);
682 if( v_it == myViewMgrMap.end() ) {
684 myViewMgrMap.insert(theViewMgrId , anObjMap);
685 return myViewMgrMap.find(theViewMgrId).value();
691 Set 'visibility state' property of the object.
692 \param theEntry - Entry of the object.
693 \param theState - visibility status
695 void LightApp_Study::setVisibilityState(const QString& theEntry, Qtx::VisibilityState theState) {
696 LightApp_Application* app = (LightApp_Application*)application();
699 SUIT_DataBrowser* db = app->objectBrowser();
703 SUIT_AbstractModel* treeModel = dynamic_cast<SUIT_AbstractModel*>(db->model());
706 treeModel->setVisibilityState(theEntry,theState);
710 Set 'visibility state' property for all object.
711 \param theEntry - Entry of the object.
713 void LightApp_Study::setVisibilityStateForAll(Qtx::VisibilityState theState) {
715 LightApp_Application* app = (LightApp_Application*)application();
718 SUIT_DataBrowser* db = app->objectBrowser();
722 SUIT_AbstractModel* treeModel = dynamic_cast<SUIT_AbstractModel*>(db->model());
725 treeModel->setVisibilityStateForAll(theState);
729 Get 'visibility state' property of the object.
730 \param theEntry - Entry of the object.
731 \return 'visibility state' property of the object.
733 Qtx::VisibilityState LightApp_Study::visibilityState(const QString& theEntry) const {
734 LightApp_Application* app = (LightApp_Application*)application();
737 SUIT_DataBrowser* db = app->objectBrowser();
739 SUIT_AbstractModel* treeModel = dynamic_cast<SUIT_AbstractModel*>(db->model());
741 return treeModel->visibilityState(theEntry);
744 return Qtx::UnpresentableState;
748 Find a data object by the specified entry.
749 \param theEntry - Entry of the object.
752 LightApp_DataObject* LightApp_Study::findObjectByEntry( const QString& theEntry )
754 LightApp_DataObject* aCurObj;
755 for ( SUIT_DataObjectIterator it( root(), SUIT_DataObjectIterator::DepthLeft ); it.current(); ++it ) {
756 aCurObj = dynamic_cast<LightApp_DataObject*>( it.current() );
757 if ( aCurObj && aCurObj->entry() == theEntry )