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 // build a SUIT_DataObject-s tree under myRoot member field
75 setRoot( new SalomeApp_RootObject( this ) );
76 // 2. iterate through all components and create corresponding sub-trees under them
77 _PTR(SComponentIterator) it ( studyDS()->NewComponentIterator() );
78 for ( ; it->More(); it->Next() ) {
79 // don't use shared_ptr here, for Data Object will take
80 // ownership of this pointer
81 _PTR(SComponent) aComponent ( it->Value() );
83 if ( aComponent->ComponentDataType() == "Interface Applicative" )
84 continue; // skip the magic "Interface Applicative" component
86 SalomeApp_DataModel::BuildTree( aComponent, root(), this );
89 bool res = CAM_Study::openDocument( theFileName );
96 //=======================================================================
97 // name : loadDocument
98 // Purpose : Connects GUI study to SALOMEDS one already loaded into StudyManager
99 //=======================================================================
100 bool SalomeApp_Study::loadDocument( const QString& theStudyName )
102 MESSAGE( "loadDocument" );
104 // obtain myStudyDS from StudyManager
105 _PTR(Study) study ( SalomeApp_Application::studyMgr()->GetStudyByName( (char*) theStudyName.latin1() ) );
111 // build a SUIT_DataObject-s tree under myRoot member field
113 setRoot( new SalomeApp_RootObject( this ) );
114 // 2. iterate through all components and create corresponding sub-trees under them
115 _PTR(SComponentIterator) it ( studyDS()->NewComponentIterator() );
116 for ( ; it->More(); it->Next() ) {
117 // don't use shared_ptr here, for Data Object will take
118 // ownership of this pointer
119 _PTR(SComponent) aComponent ( it->Value() );
121 if ( aComponent->ComponentDataType() == "Interface Applicative" )
122 continue; // skip the magic "Interface Applicative" component
124 SalomeApp_DataModel::BuildTree( aComponent, root(), this );
127 // TODO: potentially unsafe call, since base study's openDocument() might try to access the file directly - to be improved
128 bool res = CAM_Study::openDocument( theStudyName );
135 //=======================================================================
136 // name : saveDocumentAs
137 // Purpose : Save document
138 //=======================================================================
139 bool SalomeApp_Study::saveDocumentAs( const QString& theFileName )
141 ModelList list; dataModels( list );
143 SalomeApp_DataModel* aModel = (SalomeApp_DataModel*)list.first();
144 for ( ; aModel; aModel = (SalomeApp_DataModel*)list.next() )
145 aModel->saveAs( theFileName, this );
147 // save SALOMEDS document
148 bool isMultiFile = false, isAscii = false;// TODO: This information should be taken from preferences afterwards!
149 /* bool res = */isAscii ? SalomeApp_Application::studyMgr()->SaveAsASCII( theFileName.latin1(), studyDS(), isMultiFile ) :
150 SalomeApp_Application::studyMgr()->SaveAs ( theFileName.latin1(), studyDS(), isMultiFile );
152 bool res = res && CAM_Study::saveDocumentAs( theFileName );
160 //=======================================================================
161 // name : saveDocument
162 // Purpose : Save document
163 //=======================================================================
164 void SalomeApp_Study::saveDocument()
166 ModelList list; dataModels( list );
168 SalomeApp_DataModel* aModel = (SalomeApp_DataModel*)list.first();
169 for ( ; aModel; aModel = (SalomeApp_DataModel*)list.next() )
172 CAM_Study::saveDocument();
174 // save SALOMEDS document
175 bool isMultiFile = false, isAscii = false;// TODO: This information should be taken from preferences afterwards!
176 isAscii ? SalomeApp_Application::studyMgr()->SaveASCII( studyDS(), isMultiFile ) :
177 SalomeApp_Application::studyMgr()->Save ( studyDS(), isMultiFile );
182 //================================================================
183 // Function : closeDocument
185 //================================================================
186 void SalomeApp_Study::closeDocument()
188 // Inform everybody that this study is going to close when it's most safe to,
189 // i.e. in the very beginning
192 // close SALOMEDS document
193 _PTR(Study) studyPtr = studyDS();
196 SalomeApp_Application::studyMgr()->Close( studyPtr );
197 SALOMEDSClient_Study* aStudy = 0;
198 setStudyDS( _PTR(Study)(aStudy) );
201 CAM_Study::closeDocument();
204 //================================================================
205 // Function : isModified
207 //================================================================
208 bool SalomeApp_Study::isModified() const
210 bool isAnyChanged = studyDS() && studyDS()->IsModified();
211 ModelList list; dataModels( list );
213 SalomeApp_DataModel* aModel = 0;
214 for ( QPtrListIterator<CAM_DataModel> it( list ); it.current() && !isAnyChanged; ++it ){
215 aModel = dynamic_cast<SalomeApp_DataModel*>( it.current() );
217 isAnyChanged = aModel->isModified();
222 //================================================================
223 // Function : isSaved
225 //================================================================
226 bool SalomeApp_Study::isSaved() const
228 bool isAllSaved = studyDS() && studyDS()->GetPersistentReference().size();
229 ModelList list; dataModels( list );
231 SalomeApp_DataModel* aModel = 0;
232 for ( QPtrListIterator<CAM_DataModel> it( list ); it.current() && isAllSaved; ++it ){
233 aModel = dynamic_cast<SalomeApp_DataModel*>( it.current() );
235 isAllSaved = aModel->isSaved();
240 void SalomeApp_Study::setStudyDS( const _PTR(Study)& s )
245 void SalomeApp_Study::dataModelInserted (const CAM_DataModel* dm)
247 MESSAGE("SalomeApp_Study::dataModelInserted() : module name() = " << dm->module()->name());
249 CAM_Study::dataModelInserted(dm);
251 // Create SComponent for module, using default engine (CORBAless)
252 SalomeApp_Module* aModule = (SalomeApp_Module*)(dm->module());
254 QString anEngineIOR = aModule->engineIOR();
255 if (anEngineIOR.isEmpty()) { // CORBAless module
256 // Check SComponent existance
257 _PTR(SComponent) aComp = studyDS()->FindComponent(dm->module()->name());
260 _PTR(StudyBuilder) aBuilder = studyDS()->NewBuilder();
261 aComp = aBuilder->NewComponent(dm->module()->name());
263 // Set default engine IOR
264 aBuilder->DefineComponentInstance(aComp, SalomeApp_Application::defaultEngineIOR().latin1());
270 bool SalomeApp_Study::openDataModel( const QString& studyName, CAM_DataModel* dm )
275 SalomeApp_DataModel* aDM = (SalomeApp_DataModel*)(dm);
276 if (aDM && aDM->open(studyName, this)) {
277 // Something has been read -> create data model tree
278 aDM->update(NULL, this);
285 void SalomeApp_Study::updateModelRoot( const CAM_DataModel* dm )
287 CAM_Study::updateModelRoot( dm );
288 ((SalomeApp_Application*)application())->objectBrowser()->updateTree();
291 QString SalomeApp_Study::newStudyName() const
293 std::vector<std::string> studies = SalomeApp_Application::studyMgr()->GetOpenStudies();
294 QString prefix( "Study%1" ), newName, curName;
295 int i = 1, j, n = studies.size();
296 while ( newName.isEmpty() ){
297 curName = prefix.arg( i );
298 for ( j = 0 ; j < n; j++ ){
299 if ( !strcmp( studies[j].c_str(), curName.latin1() ) )