1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/
19 #include "SalomeApp_Study.h"
21 #include "SalomeApp_Module.h"
22 #include "SalomeApp_DataModel.h"
23 #include "SalomeApp_DataObject.h"
24 #include "SalomeApp_Application.h"
25 #include "SalomeApp_Engine_i.hxx"
26 #include "SalomeApp_VisualState.h"
28 #include "LightApp_RootObject.h"
30 #include <OB_Browser.h>
32 #include <SUIT_ResourceMgr.h>
35 #include <qapplication.h>
38 #include "utilities.h"
43 #include <SUIT_Session.h>
45 #include "SALOMEDS_Tool.hxx"
47 #include "SALOMEDSClient_ClientFactory.hxx"
48 #include "SALOMEDSClient_IParameters.hxx"
50 #include <SALOMEconfig.h>
51 #include CORBA_SERVER_HEADER(SALOME_Exception)
58 SalomeApp_Study::SalomeApp_Study( SUIT_Application* app )
59 : LightApp_Study( app )
66 SalomeApp_Study::~SalomeApp_Study()
73 int SalomeApp_Study::id() const
77 id = studyDS()->StudyId();
84 _PTR(Study) SalomeApp_Study::studyDS() const
92 void SalomeApp_Study::createDocument()
94 MESSAGE( "openDocument" );
96 // initialize myStudyDS, read HDF file
97 QString aName = newStudyName();
98 _PTR(Study) study ( SalomeApp_Application::studyMgr()->NewStudy( aName.latin1() ) );
103 setStudyName( aName );
106 setRoot( new LightApp_RootObject( this ) );
108 CAM_Study::createDocument();
109 emit created( this );
114 \param theFileName - name of file
116 bool SalomeApp_Study::openDocument( const QString& theFileName )
118 MESSAGE( "openDocument" );
120 // initialize myStudyDS, read HDF file
121 _PTR(Study) study ( SalomeApp_Application::studyMgr()->Open( (char*) theFileName.latin1() ) );
127 setRoot( new LightApp_RootObject( this ) ); // create myRoot
129 // update loaded data models: call open() and update() on them.
132 for ( ModelListIterator it( dm_s ); it.current(); ++it )
133 openDataModel( studyName(), it.current() );
135 // this will build a SUIT_DataObject-s tree under myRoot member field
136 // passing "false" in order NOT to rebuild existing data models' trees - it was done in previous step
137 // but tree that corresponds to not-loaded data models will be updated any way.
138 ((SalomeApp_Application*)application())->updateObjectBrowser( false );
140 bool res = CAM_Study::openDocument( theFileName );
143 study->IsSaved(true);
145 bool restore = application()->resourceMgr()->booleanValue( "Study", "store_visual_state", true );
147 std::vector<int> savePoints = getSavePoints();
148 if ( savePoints.size() > 0 )
149 SalomeApp_VisualState( (SalomeApp_Application*)application() ).restoreState( savePoints[savePoints.size()-1] );
156 Connects GUI study to SALOMEDS one already loaded into StudyManager
157 \param theStudyName - name of study
159 bool SalomeApp_Study::loadDocument( const QString& theStudyName )
161 MESSAGE( "loadDocument" );
163 // obtain myStudyDS from StudyManager
164 _PTR(Study) study ( SalomeApp_Application::studyMgr()->GetStudyByName( (char*) theStudyName.latin1() ) );
170 setRoot( new LightApp_RootObject( this ) ); // create myRoot
172 //SRN: BugID IPAL9021, put there the same code as in a method openDocument
174 // update loaded data models: call open() and update() on them.
178 for ( ModelListIterator it( dm_s ); it.current(); ++it )
179 openDataModel( studyName(), it.current() );
181 // this will build a SUIT_DataObject-s tree under myRoot member field
182 // passing "false" in order NOT to rebuild existing data models' trees - it was done in previous step
183 // but tree that corresponds to not-loaded data models will be updated any way.
184 ((SalomeApp_Application*)application())->updateObjectBrowser( false );
186 bool res = CAM_Study::openDocument( theStudyName );
189 bool restore = application()->resourceMgr()->booleanValue( "Study", "store_visual_state", true );
191 std::vector<int> savePoints = getSavePoints();
192 if ( savePoints.size() > 0 )
193 SalomeApp_VisualState( (SalomeApp_Application*)application() ).restoreState( savePoints[savePoints.size()-1] );
196 //SRN: BugID IPAL9021: End
203 \param theFileName - name of file
205 bool SalomeApp_Study::saveDocumentAs( const QString& theFileName )
207 bool store = application()->resourceMgr()->booleanValue( "Study", "store_visual_state", true );
209 SalomeApp_VisualState( (SalomeApp_Application*)application() ).storeState();
211 ModelList list; dataModels( list );
213 SalomeApp_DataModel* aModel = (SalomeApp_DataModel*)list.first();
214 QStringList listOfFiles;
215 for ( ; aModel; aModel = (SalomeApp_DataModel*)list.next() ) {
217 aModel->saveAs( theFileName, this, listOfFiles );
218 if ( !listOfFiles.isEmpty() )
219 saveModuleData(aModel->module()->name(), listOfFiles);
222 // save SALOMEDS document
223 SUIT_ResourceMgr* resMgr = application()->resourceMgr();
227 bool isMultiFile = resMgr->booleanValue( "Study", "multi_file", false );
228 bool isAscii = resMgr->booleanValue( "Study", "ascii_file", false );
229 bool res = (isAscii ?
230 SalomeApp_Application::studyMgr()->SaveAsASCII( theFileName.latin1(), studyDS(), isMultiFile ) :
231 SalomeApp_Application::studyMgr()->SaveAs ( theFileName.latin1(), studyDS(), isMultiFile ))
232 && CAM_Study::saveDocumentAs( theFileName );
234 res = res && saveStudyData(theFileName);
243 Saves previously opened document
245 bool SalomeApp_Study::saveDocument()
247 bool store = application()->resourceMgr()->booleanValue( "Study", "store_visual_state", true );
249 SalomeApp_VisualState( (SalomeApp_Application*)application() ).storeState();
251 ModelList list; dataModels( list );
253 SalomeApp_DataModel* aModel = (SalomeApp_DataModel*)list.first();
254 QStringList listOfFiles;
255 for ( ; aModel; aModel = (SalomeApp_DataModel*)list.next() ) {
257 aModel->save(listOfFiles);
258 if ( !listOfFiles.isEmpty() )
259 saveModuleData(aModel->module()->name(), listOfFiles);
262 // save SALOMEDS document
263 SUIT_ResourceMgr* resMgr = application()->resourceMgr();
267 bool isMultiFile = resMgr->booleanValue( "Study", "multi_file", false );
268 bool isAscii = resMgr->booleanValue( "Study", "ascii_file", false );
269 bool res = (isAscii ?
270 SalomeApp_Application::studyMgr()->SaveASCII( studyDS(), isMultiFile ) :
271 SalomeApp_Application::studyMgr()->Save ( studyDS(), isMultiFile )) && CAM_Study::saveDocument();
273 res = res && saveStudyData(studyName());
283 void SalomeApp_Study::closeDocument(bool permanently)
285 LightApp_Study::closeDocument(permanently);
287 // close SALOMEDS document
288 _PTR(Study) studyPtr = studyDS();
291 if(permanently) SalomeApp_Application::studyMgr()->Close( studyPtr );
292 SALOMEDSClient_Study* aStudy = 0;
293 setStudyDS( _PTR(Study)(aStudy) );
298 \return true, if study is modified in comparison with last open/save
300 bool SalomeApp_Study::isModified() const
302 bool isAnyChanged = studyDS() && studyDS()->IsModified();
304 isAnyChanged = LightApp_Study::isModified();
310 \return if data model is saved
312 bool SalomeApp_Study::isSaved() const
314 bool isAllSaved = studyDS() && studyDS()->GetPersistentReference().size();
316 isAllSaved = LightApp_Study::isSaved();
323 \param theModuleName - name of module
324 \param theListOfFiles - list of files to be saved
326 void SalomeApp_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).latin1();
340 SetListOfFiles(theModuleName, aListOfFiles);
345 \param theModuleName - name of module
346 \param theListOfFiles - list of files to be loaded
348 void SalomeApp_Study::openModuleData( QString theModuleName, QStringList& theListOfFiles )
350 std::vector<std::string> aListOfFiles = GetListOfFiles( theModuleName );
352 int i, aLength = aListOfFiles.size() - 1;
356 //Get a temporary directory for saved a file
357 theListOfFiles.append(aListOfFiles[0].c_str());
359 for(i = 0; i < aLength; i++)
360 theListOfFiles.append(aListOfFiles[i+1].c_str());
364 Saves data from study
366 bool SalomeApp_Study::saveStudyData( const QString& theFileName )
368 ModelList list; dataModels( list );
369 SalomeApp_DataModel* aModel = (SalomeApp_DataModel*)list.first();
370 std::vector<std::string> listOfFiles(0);
371 for ( ; aModel; aModel = (SalomeApp_DataModel*)list.next() )
372 SetListOfFiles(aModel->module()->name(), listOfFiles);
379 bool SalomeApp_Study::openStudyData( const QString& theFileName )
387 void SalomeApp_Study::setStudyDS( const _PTR(Study)& s )
395 void SalomeApp_Study::dataModelInserted (const CAM_DataModel* dm)
397 MESSAGE("SalomeApp_Study::dataModelInserted() : module name() = " << dm->module()->name());
399 CAM_Study::dataModelInserted(dm);
405 Create SComponent for module, using default engine (CORBAless)
407 void SalomeApp_Study::addComponent(const CAM_DataModel* dm)
409 SalomeApp_Module* aModule = dynamic_cast<SalomeApp_Module*>( dm->module() );
410 // 1. aModule == 0 means that this is a light module (no CORBA enigine)
412 // Check SComponent existance
413 _PTR(Study) aStudy = studyDS();
416 _PTR(SComponent) aComp = aStudy->FindComponent(dm->module()->name());
419 _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
420 aComp = aBuilder->NewComponent(dm->module()->name());
421 aBuilder->SetName(aComp, dm->module()->moduleName().latin1());
422 QString anIconName = dm->module()->iconName();
423 if (!anIconName.isEmpty()) {
424 _PTR(AttributePixMap) anAttr = aBuilder->FindOrCreateAttribute(aComp, "AttributePixMap");
426 anAttr->SetPixMap(anIconName.latin1());
428 // Set default engine IOR
429 aBuilder->DefineComponentInstance(aComp, SalomeApp_Application::defaultEngineIOR().latin1());
430 //SalomeApp_DataModel::BuildTree( aComp, root(), this, /*skipExisitng=*/true );
431 SalomeApp_DataModel::synchronize( aComp, this );
439 bool SalomeApp_Study::openDataModel( const QString& studyName, CAM_DataModel* dm )
444 // SalomeApp_DataModel* aDM = (SalomeApp_DataModel*)(dm);
445 SalomeApp_Module* aModule = dynamic_cast<SalomeApp_Module*>( dm->module() );
446 _PTR(Study) aStudy = studyDS(); // shared_ptr cannot be used here
447 _PTR(SComponent) aSComp;
449 // 1. aModule == 0 means that this is a light module (no CORBA enigine)
451 anEngine = SalomeApp_Application::defaultEngineIOR();
452 aSComp = aStudy->FindComponent(dm->module()->name());
455 SalomeApp_DataModel* aDM = dynamic_cast<SalomeApp_DataModel*>( dm );
457 QString anId = aDM->getRootEntry( this );
458 if ( anId.isEmpty() )
459 return true; // Probably nothing to load
460 anEngine = aDM->getModule()->engineIOR();
461 if ( anEngine.isEmpty() )
463 aSComp = aStudy->FindComponentID( std::string( anId.latin1() ) );
467 _PTR(StudyBuilder) aBuilder( aStudy->NewBuilder() );
470 aBuilder->LoadWith( aSComp, std::string( anEngine.latin1() ) );
472 catch( const SALOME::SALOME_Exception& ) {
473 // Oops, something went wrong while loading -> return an error
476 // Something has been read -> create data model tree
477 //SalomeApp_DataModel* aDM = dynamic_cast<SalomeApp_DataModel*>( dm );
478 // aDM->buildTree( aSComp, 0, this );
481 // Don't return false here, for there might be no data
482 // for a given component in the study yet
484 QStringList listOfFiles;
485 openModuleData(dm->module()->name(), listOfFiles);
486 if (dm && dm->open(studyName, this, listOfFiles)) {
487 // Remove the files and temporary directory, created
488 // for this module by LightApp_Engine_i::Load()
489 bool isMultiFile = false; // TODO: decide, how to access this parameter
490 RemoveTemporaryFiles( dm->module()->name(), isMultiFile );
492 // Something has been read -> create data model tree
493 LightApp_DataModel* aDM = dynamic_cast<LightApp_DataModel*>( dm );
495 aDM->update(NULL, this);
502 Create new study name.
504 QString SalomeApp_Study::newStudyName() const
506 std::vector<std::string> studies = SalomeApp_Application::studyMgr()->GetOpenStudies();
507 QString prefix( "Study%1" ), newName, curName;
508 int i = 1, j, n = studies.size();
509 while ( newName.isEmpty() ){
510 curName = prefix.arg( i );
511 for ( j = 0 ; j < n; j++ ){
512 if ( !strcmp( studies[j].c_str(), curName.latin1() ) )
524 \return list of files used by module: to be used by CORBAless modules
525 \param theModuleName - name of module
527 std::vector<std::string> SalomeApp_Study::GetListOfFiles( const char* theModuleName ) const
529 SalomeApp_Engine_i* aDefaultEngine = SalomeApp_Engine_i::GetInstance();
531 return aDefaultEngine->GetListOfFiles(id(), theModuleName);
533 std::vector<std::string> aListOfFiles;
538 Sets list of files used by module: to be used by CORBAless modules
539 \param theModuleName - name of module
540 \param theListOfFiles - list of files
542 void SalomeApp_Study::SetListOfFiles ( const char* theModuleName,
543 const std::vector<std::string> theListOfFiles )
545 SalomeApp_Engine_i* aDefaultEngine = SalomeApp_Engine_i::GetInstance();
547 aDefaultEngine->SetListOfFiles(theListOfFiles, id(), theModuleName);
551 \return temporary directory for saving files of modules
553 std::string SalomeApp_Study::GetTmpDir ( const char* theURL, const bool isMultiFile )
555 std::string anURLDir = SALOMEDS_Tool::GetDirFromPath(theURL);
556 std::string aTmpDir = isMultiFile ? anURLDir : SALOMEDS_Tool::GetTmpDir();
561 Removes temporary files
563 void SalomeApp_Study::RemoveTemporaryFiles ( const char* theModuleName, const bool isMultiFile ) const
568 std::vector<std::string> aListOfFiles = GetListOfFiles( theModuleName );
569 if (aListOfFiles.size() > 0) {
570 std::string aTmpDir = aListOfFiles[0];
572 const int n = aListOfFiles.size() - 1;
573 SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
575 for (int i = 0; i < n; i++)
576 aSeq[i] = CORBA::string_dup(aListOfFiles[i + 1].c_str());
578 SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.c_str(), aSeq.in(), true);
583 Deletes all references to object
586 void SalomeApp_Study::deleteReferencesTo( _PTR( SObject ) obj )
588 _PTR(StudyBuilder) sb = studyDS()->NewBuilder();
589 std::vector<_PTR(SObject)> aRefs = studyDS()->FindDependances( obj );
590 for( int i=0, n=aRefs.size(); i<n; i++ )
592 _PTR( SObject ) o = aRefs[i];
593 if( o->GetFatherComponent()->ComponentDataType()==obj->GetFatherComponent()->ComponentDataType() )
595 sb->RemoveReference( o );
596 sb->RemoveObjectWithChildren( o );
602 \return real entry by entry of reference
603 \param entry - entry of reference object
605 QString SalomeApp_Study::referencedToEntry( const QString& entry ) const
607 _PTR(SObject) obj = studyDS()->FindObjectID( entry.latin1() );
608 _PTR(SObject) refobj;
610 if( obj && obj->ReferencedObject( refobj ) )
611 return refobj->GetID().c_str();
612 return LightApp_Study::referencedToEntry( entry );
616 \return component data type for entry
618 QString SalomeApp_Study::componentDataType( const QString& entry ) const
620 _PTR(SObject) obj( studyDS()->FindObjectID( entry.latin1() ) );
622 return LightApp_Study::componentDataType( entry );
623 return obj->GetFatherComponent()->ComponentDataType().c_str();
627 \return true if entry corresponds to component
629 bool SalomeApp_Study::isComponent( const QString& entry ) const
631 _PTR(SObject) obj( studyDS()->FindObjectID( entry.latin1() ) );
632 return obj && QString( obj->GetID().c_str() ) == obj->GetFatherComponent()->GetID().c_str();
636 \return entries of object children
638 void SalomeApp_Study::children( const QString& entry, QStringList& child_entries ) const
640 _PTR(SObject) SO = studyDS()->FindObjectID( entry.latin1() );
641 _PTR(ChildIterator) anIter ( studyDS()->NewChildIterator( SO ) );
642 anIter->InitEx( true );
643 while( anIter->More() )
645 _PTR(SObject) val( anIter->Value() );
646 child_entries.append( val->GetID().c_str() );
652 Fills list with components names
653 \param comp - list to be filled
655 void SalomeApp_Study::components( QStringList& comps ) const
657 for( _PTR(SComponentIterator) it ( studyDS()->NewComponentIterator() ); it->More(); it->Next() )
659 _PTR(SComponent) aComponent ( it->Value() );
660 if( aComponent && aComponent->ComponentDataType() == "Interface Applicative" )
661 continue; // skip the magic "Interface Applicative" component
662 comps.append( aComponent->ComponentDataType().c_str() );
667 \return a list of saved points' IDs
669 std::vector<int> SalomeApp_Study::getSavePoints()
673 _PTR(SObject) so = studyDS()->FindComponent("Interface Applicative");
676 _PTR(StudyBuilder) builder = studyDS()->NewBuilder();
677 _PTR(ChildIterator) anIter ( studyDS()->NewChildIterator( so ) );
678 for(; anIter->More(); anIter->Next())
680 _PTR(SObject) val( anIter->Value() );
681 _PTR(GenericAttribute) genAttr;
682 if(builder->FindAttribute(val, genAttr, "AttributeParameter")) v.push_back(val->Tag());
689 Removes a given save point
691 void SalomeApp_Study::removeSavePoint(int savePoint)
693 if(savePoint <= 0) return;
694 _PTR(AttributeParameter) AP = studyDS()->GetCommonParameters(getVisualComponentName(), savePoint);
695 _PTR(SObject) so = AP->GetSObject();
696 _PTR(StudyBuilder) builder = studyDS()->NewBuilder();
697 builder->RemoveObjectWithChildren(so);
701 \return a name of save point
703 QString SalomeApp_Study::getNameOfSavePoint(int savePoint)
705 _PTR(AttributeParameter) AP = studyDS()->GetCommonParameters(getVisualComponentName(), savePoint);
706 _PTR(IParameters) ip = ClientFactory::getIParameters(AP);
707 return ip->getProperty("AP_SAVEPOINT_NAME");
711 Sets a name of save point
713 void SalomeApp_Study::setNameOfSavePoint(int savePoint, const QString& nameOfSavePoint)
715 _PTR(AttributeParameter) AP = studyDS()->GetCommonParameters(getVisualComponentName(), savePoint);
716 _PTR(IParameters) ip = ClientFactory::getIParameters(AP);
717 ip->setProperty("AP_SAVEPOINT_NAME", nameOfSavePoint.latin1());
721 \return a name of the component where visual parameters are stored
723 std::string SalomeApp_Study::getVisualComponentName()
725 return "Interface Applicative";
729 Slot: called on change of a root of a data model. Redefined from CAM_Study
731 void SalomeApp_Study::updateModelRoot( const CAM_DataModel* dm )
733 LightApp_Study::updateModelRoot( dm );
735 // calling updateSavePointDataObjects in order to set correct order of "Gui states" object
736 // it must always be the last one.
737 ((SalomeApp_Application*)application())->updateSavePointDataObjects( this );