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()
25 int SalomeApp_Study::id() const
29 id = studyDS()->StudyId();
33 _PTR(Study) SalomeApp_Study::studyDS() const
38 void SalomeApp_Study::createDocument()
40 MESSAGE( "openDocument" );
42 // initialize myStudyDS, read HDF file
43 _PTR(Study) study ( SalomeApp_Application::studyMgr()->NewStudy( newStudyName().latin1() ) );
50 setRoot( new SalomeApp_RootObject( this ) );
52 CAM_Study::createDocument();
57 //=======================================================================
58 // name : openDocument
59 // Purpose : Open document
60 //=======================================================================
61 bool SalomeApp_Study::openDocument( const QString& theFileName )
63 MESSAGE( "openDocument" );
65 // initialize myStudyDS, read HDF file
66 _PTR(Study) study ( SalomeApp_Application::studyMgr()->Open( (char*) theFileName.latin1() ) );
72 // build a SUIT_DataObject-s tree under myRoot member field
74 setRoot( new SalomeApp_RootObject( this ) );
75 // 2. iterate through all components and create corresponding sub-trees under them
76 _PTR(SComponentIterator) it ( studyDS()->NewComponentIterator() );
77 for ( ; it->More(); it->Next() ) {
78 // don't use shared_ptr here, for Data Object will take
79 // ownership of this pointer
80 _PTR(SComponent) aComponent ( it->Value() );
82 if ( aComponent->ComponentDataType() == "Interface Applicative" )
83 continue; // skip the magic "Interface Applicative" component
85 SalomeApp_DataModel::BuildTree( aComponent, root(), this );
88 bool res = CAM_Study::openDocument( theFileName );
95 //=======================================================================
96 // name : loadDocument
97 // Purpose : Connects GUI study to SALOMEDS one already loaded into StudyManager
98 //=======================================================================
99 bool SalomeApp_Study::loadDocument( const QString& theStudyName )
101 MESSAGE( "loadDocument" );
103 // obtain myStudyDS from StudyManager
104 _PTR(Study) study ( SalomeApp_Application::studyMgr()->GetStudyByName( (char*) theStudyName.latin1() ) );
110 // build a SUIT_DataObject-s tree under myRoot member field
112 setRoot( new SalomeApp_RootObject( this ) );
113 // 2. iterate through all components and create corresponding sub-trees under them
114 _PTR(SComponentIterator) it ( studyDS()->NewComponentIterator() );
115 for ( ; it->More(); it->Next() ) {
116 // don't use shared_ptr here, for Data Object will take
117 // ownership of this pointer
118 _PTR(SComponent) aComponent ( it->Value() );
120 if ( aComponent->ComponentDataType() == "Interface Applicative" )
121 continue; // skip the magic "Interface Applicative" component
123 SalomeApp_DataModel::BuildTree( aComponent, root(), this );
126 // TODO: potentially unsafe call, since base study's openDocument() might try to access the file directly - to be improved
127 bool res = CAM_Study::openDocument( theStudyName );
134 //=======================================================================
135 // name : saveDocumentAs
136 // Purpose : Save document
137 //=======================================================================
138 bool SalomeApp_Study::saveDocumentAs( const QString& theFileName )
140 ModelList list; dataModels( list );
142 SalomeApp_DataModel* aModel = (SalomeApp_DataModel*)list.first();
143 for ( ; aModel; aModel = (SalomeApp_DataModel*)list.next() )
144 aModel->saveAs( theFileName, this );
146 bool res = CAM_Study::saveDocumentAs( theFileName );
148 // save SALOMEDS document
149 bool isMultiFile = false, isAscii = false;// TODO: This information should be taken from preferences afterwards!
150 isAscii ? SalomeApp_Application::studyMgr()->SaveAsASCII( theFileName.latin1(), studyDS(), isMultiFile ) :
151 SalomeApp_Application::studyMgr()->SaveAs ( theFileName.latin1(), studyDS(), isMultiFile );
158 //=======================================================================
159 // name : saveDocument
160 // Purpose : Save document
161 //=======================================================================
162 void SalomeApp_Study::saveDocument()
164 ModelList list; dataModels( list );
166 SalomeApp_DataModel* aModel = (SalomeApp_DataModel*)list.first();
167 for ( ; aModel; aModel = (SalomeApp_DataModel*)list.next() )
170 CAM_Study::saveDocument();
172 // save SALOMEDS document
173 bool isMultiFile = false, isAscii = false;// TODO: This information should be taken from preferences afterwards!
174 isAscii ? SalomeApp_Application::studyMgr()->SaveASCII( studyDS(), isMultiFile ) :
175 SalomeApp_Application::studyMgr()->Save ( studyDS(), isMultiFile );
180 //================================================================
181 // Function : closeDocument
183 //================================================================
184 void SalomeApp_Study::closeDocument()
186 // Inform everybody that this study is going to close when it's most safe to,
187 // i.e. in the very beginning
190 // close SALOMEDS document
191 SalomeApp_Application::studyMgr()->Close( studyDS() );
192 SALOMEDSClient_Study* aStudy = NULL;
193 setStudyDS( _PTR(Study)(aStudy) );
195 CAM_Study::closeDocument();
198 //================================================================
199 // Function : isModified
201 //================================================================
202 bool SalomeApp_Study::isModified() const
204 bool isAnyChanged = studyDS() && studyDS()->IsModified();
205 ModelList list; dataModels( list );
207 SalomeApp_DataModel* aModel = 0;
208 for ( QPtrListIterator<CAM_DataModel> it( list ); it.current() && !isAnyChanged; ++it ){
209 aModel = dynamic_cast<SalomeApp_DataModel*>( it.current() );
211 isAnyChanged = aModel->isModified();
216 //================================================================
217 // Function : isSaved
219 //================================================================
220 bool SalomeApp_Study::isSaved() const
222 bool isAllSaved = studyDS() && studyDS()->GetPersistentReference().size();
223 ModelList list; dataModels( list );
225 SalomeApp_DataModel* aModel = 0;
226 for ( QPtrListIterator<CAM_DataModel> it( list ); it.current() && isAllSaved; ++it ){
227 aModel = dynamic_cast<SalomeApp_DataModel*>( it.current() );
229 isAllSaved = aModel->isSaved();
234 void SalomeApp_Study::setStudyDS( const _PTR(Study)& s )
239 void SalomeApp_Study::dataModelInserted (const CAM_DataModel* dm)
241 MESSAGE("SalomeApp_Study::dataModelInserted() : module name() = " << dm->module()->name());
243 CAM_Study::dataModelInserted(dm);
245 // Create SComponent for module, using default engine (CORBAless)
246 SalomeApp_Module* aModule = (SalomeApp_Module*)(dm->module());
248 QString anEngineIOR = aModule->engineIOR();
249 if (anEngineIOR.isEmpty()) { // CORBAless module
250 // Check SComponent existance
251 _PTR(SComponent) aComp = studyDS()->FindComponent(dm->module()->name());
254 _PTR(StudyBuilder) aBuilder = studyDS()->NewBuilder();
255 aComp = aBuilder->NewComponent(dm->module()->name());
257 // Set default engine IOR
258 aBuilder->DefineComponentInstance(aComp, SalomeApp_Application::defaultEngineIOR());
264 bool SalomeApp_Study::openDataModel( const QString& studyName, CAM_DataModel* dm )
269 SalomeApp_DataModel* aDM = (SalomeApp_DataModel*)(dm);
270 if (aDM && aDM->open(studyName, this)) {
271 // Something has been read -> create data model tree
272 aDM->update(NULL, this);
279 void SalomeApp_Study::updateModelRoot( const CAM_DataModel* dm )
281 CAM_Study::updateModelRoot( dm );
282 ((SalomeApp_Application*)application())->objectBrowser()->updateTree();
285 QString SalomeApp_Study::newStudyName() const
287 std::vector<std::string> studies = SalomeApp_Application::studyMgr()->GetOpenStudies();
288 QString prefix( "Study%1" ), newName, curName;
289 int i = 1, j, n = studies.size();
290 while ( newName.isEmpty() ){
291 curName = prefix.arg( i );
292 for ( j = 0 ; j < n; j++ ){
293 if ( !strcmp( studies[j].c_str(), curName.latin1() ) )