1 #include "SalomeApp_Study.h"
3 #include "SalomeApp_Module.h"
4 #include "SalomeApp_DataModel.h"
5 #include "SalomeApp_RootObject.h"
6 #include "SalomeApp_DataObject.h"
7 #include "SalomeApp_Application.h"
9 #include <OB_Browser.h>
11 #include <SUIT_ResourceMgr.h>
13 #include "utilities.h"
15 SalomeApp_Study::SalomeApp_Study( SUIT_Application* app )
20 SalomeApp_Study::~SalomeApp_Study()
24 int SalomeApp_Study::id() const
28 id = studyDS()->StudyId();
32 _PTR(Study) SalomeApp_Study::studyDS() const
37 void SalomeApp_Study::createDocument()
39 MESSAGE( "openDocument" );
41 // initialize myStudyDS, read HDF file
42 QString aName = newStudyName();
43 _PTR(Study) study ( SalomeApp_Application::studyMgr()->NewStudy( aName.latin1() ) );
48 setStudyName( aName );
51 setRoot( new SalomeApp_RootObject( this ) );
53 CAM_Study::createDocument();
58 //=======================================================================
59 // name : openDocument
60 // Purpose : Open document
61 //=======================================================================
62 bool SalomeApp_Study::openDocument( const QString& theFileName )
64 MESSAGE( "openDocument" );
66 // initialize myStudyDS, read HDF file
67 _PTR(Study) study ( SalomeApp_Application::studyMgr()->Open( (char*) theFileName.latin1() ) );
73 setRoot( new SalomeApp_RootObject( this ) ); // create myRoot
75 // update loaded data models: call open() and update() on them.
78 for ( ModelListIterator it( dm_s ); it.current(); ++it )
79 openDataModel( studyName(), it.current() );
81 // this will build a SUIT_DataObject-s tree under myRoot member field
82 // passing "false" in order NOT to rebuild existing data models' trees - it was done in previous step
83 // but tree that corresponds to not-loaded data models will be updated any way.
84 ((SalomeApp_Application*)application())->updateObjectBrowser( false );
86 bool res = CAM_Study::openDocument( theFileName );
92 //=======================================================================
93 // name : loadDocument
94 // Purpose : Connects GUI study to SALOMEDS one already loaded into StudyManager
95 //=======================================================================
96 bool SalomeApp_Study::loadDocument( const QString& theStudyName )
98 MESSAGE( "loadDocument" );
100 // obtain myStudyDS from StudyManager
101 _PTR(Study) study ( SalomeApp_Application::studyMgr()->GetStudyByName( (char*) theStudyName.latin1() ) );
107 // build a SUIT_DataObject-s tree under myRoot member field
109 setRoot( new SalomeApp_RootObject( this ) );
110 // 2. iterate through all components and create corresponding sub-trees under them
111 _PTR(SComponentIterator) it ( studyDS()->NewComponentIterator() );
112 for ( ; it->More(); it->Next() ) {
113 // don't use shared_ptr here, for Data Object will take
114 // ownership of this pointer
115 _PTR(SComponent) aComponent ( it->Value() );
117 if ( aComponent->ComponentDataType() == "Interface Applicative" )
118 continue; // skip the magic "Interface Applicative" component
120 SalomeApp_DataModel::BuildTree( aComponent, root(), this );
123 // TODO: potentially unsafe call, since base study's openDocument() might try to access the file directly - to be improved
125 bool res = CAM_Study::openDocument( theStudyName );
127 //emit opened( this );
132 //=======================================================================
133 // name : saveDocumentAs
134 // Purpose : Save document
135 //=======================================================================
136 bool SalomeApp_Study::saveDocumentAs( const QString& theFileName )
138 ModelList list; dataModels( list );
140 SalomeApp_DataModel* aModel = (SalomeApp_DataModel*)list.first();
141 for ( ; aModel; aModel = (SalomeApp_DataModel*)list.next() )
142 aModel->saveAs( theFileName, this );
144 // save SALOMEDS document
145 bool isMultiFile = false, isAscii = false;// TODO: This information should be taken from preferences afterwards!
146 /* bool res = */isAscii ? SalomeApp_Application::studyMgr()->SaveAsASCII( theFileName.latin1(), studyDS(), isMultiFile ) :
147 SalomeApp_Application::studyMgr()->SaveAs ( theFileName.latin1(), studyDS(), isMultiFile );
149 bool res = CAM_Study::saveDocumentAs( theFileName ); //SRN: BugID IPAL9377, removed usage of uninitialized variable <res>
157 //=======================================================================
158 // name : saveDocument
159 // Purpose : Save document
160 //=======================================================================
161 void SalomeApp_Study::saveDocument()
163 ModelList list; dataModels( list );
165 SalomeApp_DataModel* aModel = (SalomeApp_DataModel*)list.first();
166 for ( ; aModel; aModel = (SalomeApp_DataModel*)list.next() )
169 CAM_Study::saveDocument();
171 // save SALOMEDS document
172 bool isMultiFile = false, isAscii = false;// TODO: This information should be taken from preferences afterwards!
173 isAscii ? SalomeApp_Application::studyMgr()->SaveASCII( studyDS(), isMultiFile ) :
174 SalomeApp_Application::studyMgr()->Save ( studyDS(), isMultiFile );
179 //================================================================
180 // Function : closeDocument
182 //================================================================
183 void SalomeApp_Study::closeDocument(bool permanently)
185 // Inform everybody that this study is going to close when it's most safe to,
186 // i.e. in the very beginning
189 // close SALOMEDS document
190 _PTR(Study) studyPtr = studyDS();
193 if(permanently) SalomeApp_Application::studyMgr()->Close( studyPtr );
194 SALOMEDSClient_Study* aStudy = 0;
195 setStudyDS( _PTR(Study)(aStudy) );
198 CAM_Study::closeDocument(permanently);
201 //================================================================
202 // Function : isModified
204 //================================================================
205 bool SalomeApp_Study::isModified() const
207 bool isAnyChanged = studyDS() && studyDS()->IsModified();
208 ModelList list; dataModels( list );
210 SalomeApp_DataModel* aModel = 0;
211 for ( QPtrListIterator<CAM_DataModel> it( list ); it.current() && !isAnyChanged; ++it ){
212 aModel = dynamic_cast<SalomeApp_DataModel*>( it.current() );
214 isAnyChanged = aModel->isModified();
219 //================================================================
220 // Function : isSaved
222 //================================================================
223 bool SalomeApp_Study::isSaved() const
225 bool isAllSaved = studyDS() && studyDS()->GetPersistentReference().size();
226 ModelList list; dataModels( list );
228 SalomeApp_DataModel* aModel = 0;
229 for ( QPtrListIterator<CAM_DataModel> it( list ); it.current() && isAllSaved; ++it ){
230 aModel = dynamic_cast<SalomeApp_DataModel*>( it.current() );
232 isAllSaved = aModel->isSaved();
237 void SalomeApp_Study::setStudyDS( const _PTR(Study)& s )
242 void SalomeApp_Study::dataModelInserted (const CAM_DataModel* dm)
244 MESSAGE("SalomeApp_Study::dataModelInserted() : module name() = " << dm->module()->name());
246 CAM_Study::dataModelInserted(dm);
248 // Create SComponent for module, using default engine (CORBAless)
249 SalomeApp_Module* aModule = (SalomeApp_Module*)(dm->module());
251 QString anEngineIOR = aModule->engineIOR();
252 if (anEngineIOR.isEmpty()) { // CORBAless module
253 // Check SComponent existance
254 _PTR(SComponent) aComp = studyDS()->FindComponent(dm->module()->name());
257 _PTR(StudyBuilder) aBuilder = studyDS()->NewBuilder();
258 aComp = aBuilder->NewComponent(dm->module()->name());
260 // Set default engine IOR
261 aBuilder->DefineComponentInstance(aComp, SalomeApp_Application::defaultEngineIOR().latin1());
267 bool SalomeApp_Study::openDataModel( const QString& studyName, CAM_DataModel* dm )
272 SalomeApp_DataModel* aDM = (SalomeApp_DataModel*)(dm);
273 if (aDM && aDM->open(studyName, this)) {
274 // Something has been read -> create data model tree
275 aDM->update(NULL, this);
282 QString SalomeApp_Study::newStudyName() const
284 std::vector<std::string> studies = SalomeApp_Application::studyMgr()->GetOpenStudies();
285 QString prefix( "Study%1" ), newName, curName;
286 int i = 1, j, n = studies.size();
287 while ( newName.isEmpty() ){
288 curName = prefix.arg( i );
289 for ( j = 0 ; j < n; j++ ){
290 if ( !strcmp( studies[j].c_str(), curName.latin1() ) )