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
129 bool res = CAM_Study::openDocument( theStudyName );
131 //emit opened( this );
136 //=======================================================================
137 // name : saveDocumentAs
138 // Purpose : Save document
139 //=======================================================================
140 bool SalomeApp_Study::saveDocumentAs( const QString& theFileName )
142 ModelList list; dataModels( list );
144 SalomeApp_DataModel* aModel = (SalomeApp_DataModel*)list.first();
145 for ( ; aModel; aModel = (SalomeApp_DataModel*)list.next() )
146 aModel->saveAs( theFileName, this );
148 // save SALOMEDS document
149 bool isMultiFile = false, isAscii = false;// TODO: This information should be taken from preferences afterwards!
150 /* bool res = */isAscii ? SalomeApp_Application::studyMgr()->SaveAsASCII( theFileName.latin1(), studyDS(), isMultiFile ) :
151 SalomeApp_Application::studyMgr()->SaveAs ( theFileName.latin1(), studyDS(), isMultiFile );
153 bool res = res && CAM_Study::saveDocumentAs( theFileName );
161 //=======================================================================
162 // name : saveDocument
163 // Purpose : Save document
164 //=======================================================================
165 void SalomeApp_Study::saveDocument()
167 ModelList list; dataModels( list );
169 SalomeApp_DataModel* aModel = (SalomeApp_DataModel*)list.first();
170 for ( ; aModel; aModel = (SalomeApp_DataModel*)list.next() )
173 CAM_Study::saveDocument();
175 // save SALOMEDS document
176 bool isMultiFile = false, isAscii = false;// TODO: This information should be taken from preferences afterwards!
177 isAscii ? SalomeApp_Application::studyMgr()->SaveASCII( studyDS(), isMultiFile ) :
178 SalomeApp_Application::studyMgr()->Save ( studyDS(), isMultiFile );
183 //================================================================
184 // Function : closeDocument
186 //================================================================
187 void SalomeApp_Study::closeDocument()
189 // Inform everybody that this study is going to close when it's most safe to,
190 // i.e. in the very beginning
193 // close SALOMEDS document
194 _PTR(Study) studyPtr = studyDS();
197 SalomeApp_Application::studyMgr()->Close( studyPtr );
198 SALOMEDSClient_Study* aStudy = 0;
199 setStudyDS( _PTR(Study)(aStudy) );
202 CAM_Study::closeDocument();
205 //================================================================
206 // Function : isModified
208 //================================================================
209 bool SalomeApp_Study::isModified() const
211 bool isAnyChanged = studyDS() && studyDS()->IsModified();
212 ModelList list; dataModels( list );
214 SalomeApp_DataModel* aModel = 0;
215 for ( QPtrListIterator<CAM_DataModel> it( list ); it.current() && !isAnyChanged; ++it ){
216 aModel = dynamic_cast<SalomeApp_DataModel*>( it.current() );
218 isAnyChanged = aModel->isModified();
223 //================================================================
224 // Function : isSaved
226 //================================================================
227 bool SalomeApp_Study::isSaved() const
229 bool isAllSaved = studyDS() && studyDS()->GetPersistentReference().size();
230 ModelList list; dataModels( list );
232 SalomeApp_DataModel* aModel = 0;
233 for ( QPtrListIterator<CAM_DataModel> it( list ); it.current() && isAllSaved; ++it ){
234 aModel = dynamic_cast<SalomeApp_DataModel*>( it.current() );
236 isAllSaved = aModel->isSaved();
241 void SalomeApp_Study::setStudyDS( const _PTR(Study)& s )
246 void SalomeApp_Study::dataModelInserted (const CAM_DataModel* dm)
248 MESSAGE("SalomeApp_Study::dataModelInserted() : module name() = " << dm->module()->name());
250 CAM_Study::dataModelInserted(dm);
252 // Create SComponent for module, using default engine (CORBAless)
253 SalomeApp_Module* aModule = (SalomeApp_Module*)(dm->module());
255 QString anEngineIOR = aModule->engineIOR();
256 if (anEngineIOR.isEmpty()) { // CORBAless module
257 // Check SComponent existance
258 _PTR(SComponent) aComp = studyDS()->FindComponent(dm->module()->name());
261 _PTR(StudyBuilder) aBuilder = studyDS()->NewBuilder();
262 aComp = aBuilder->NewComponent(dm->module()->name());
264 // Set default engine IOR
265 aBuilder->DefineComponentInstance(aComp, SalomeApp_Application::defaultEngineIOR().latin1());
271 bool SalomeApp_Study::openDataModel( const QString& studyName, CAM_DataModel* dm )
276 SalomeApp_DataModel* aDM = (SalomeApp_DataModel*)(dm);
277 if (aDM && aDM->open(studyName, this)) {
278 // Something has been read -> create data model tree
279 aDM->update(NULL, this);
286 void SalomeApp_Study::updateModelRoot( const CAM_DataModel* dm )
288 CAM_Study::updateModelRoot( dm );
289 ((SalomeApp_Application*)application())->objectBrowser()->updateTree();
292 QString SalomeApp_Study::newStudyName() const
294 std::vector<std::string> studies = SalomeApp_Application::studyMgr()->GetOpenStudies();
295 QString prefix( "Study%1" ), newName, curName;
296 int i = 1, j, n = studies.size();
297 while ( newName.isEmpty() ){
298 curName = prefix.arg( i );
299 for ( j = 0 ; j < n; j++ ){
300 if ( !strcmp( studies[j].c_str(), curName.latin1() ) )