]> SALOME platform Git repositories - modules/gui.git/blob - src/SalomeApp/SalomeApp_Study.cxx
Salome HOME
bd38efc32facd226b34673be5346d6e39ae9d866
[modules/gui.git] / src / SalomeApp / SalomeApp_Study.cxx
1 #include "SalomeApp_Study.h"
2
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"
8
9 #include <OB_Browser.h>
10
11 #include <SUIT_ResourceMgr.h>
12
13 #include "utilities.h"
14
15 SalomeApp_Study::SalomeApp_Study( SUIT_Application* app )
16 : CAM_Study( app )
17 {}  
18
19
20 SalomeApp_Study::~SalomeApp_Study()
21 {
22   closeDocument();
23 }
24
25 int SalomeApp_Study::id() const
26 {
27   int id = -1;
28   if ( myStudyDS )
29     id = studyDS()->StudyId();
30   return id;
31 }
32
33 _PTR(Study) SalomeApp_Study::studyDS() const
34 {
35   return myStudyDS;
36 }
37
38 void SalomeApp_Study::createDocument()
39 {
40   MESSAGE( "openDocument" );
41
42   // initialize myStudyDS, read HDF file
43   _PTR(Study) study ( SalomeApp_Application::studyMgr()->NewStudy( newStudyName().latin1() ) );
44   if ( !study )
45     return;
46
47   setStudyDS( study );
48
49   // create myRoot
50   setRoot( new SalomeApp_RootObject( this ) );
51
52   CAM_Study::createDocument();
53
54   emit created( this );
55 }
56
57 //=======================================================================
58 // name    : openDocument
59 // Purpose : Open document
60 //=======================================================================
61 bool SalomeApp_Study::openDocument( const QString& theFileName )
62 {
63   MESSAGE( "openDocument" );
64
65   // initialize myStudyDS, read HDF file
66   _PTR(Study) study ( SalomeApp_Application::studyMgr()->Open( (char*) theFileName.latin1() ) );
67   if ( !study )
68     return false;
69
70   setStudyDS( study );
71
72   // build a SUIT_DataObject-s tree under myRoot member field
73   // 1. create myRoot
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() ); 
81
82     if ( aComponent->ComponentDataType() == "Interface Applicative" )
83       continue; // skip the magic "Interface Applicative" component
84     
85     SalomeApp_DataModel::BuildTree( aComponent, root(), this );
86   }
87
88   bool res = CAM_Study::openDocument( theFileName );
89
90   emit opened( this );
91
92   return res;
93 }
94
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 )
100 {
101   MESSAGE( "loadDocument" );
102
103   // obtain myStudyDS from StudyManager
104   _PTR(Study) study ( SalomeApp_Application::studyMgr()->GetStudyByName( (char*) theStudyName.latin1() ) );
105   if ( !study )
106     return false;
107
108   setStudyDS( study );
109
110   // build a SUIT_DataObject-s tree under myRoot member field
111   // 1. create myRoot
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() ); 
119
120     if ( aComponent->ComponentDataType() == "Interface Applicative" )
121       continue; // skip the magic "Interface Applicative" component
122     
123     SalomeApp_DataModel::BuildTree( aComponent, root(), this );
124   }
125
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 );
128
129   emit opened( this );
130
131   return res;  
132 }
133
134 //=======================================================================
135 // name    : saveDocumentAs
136 // Purpose : Save document
137 //=======================================================================
138 bool SalomeApp_Study::saveDocumentAs( const QString& theFileName )
139 {
140   ModelList list; dataModels( list );
141
142   SalomeApp_DataModel* aModel = (SalomeApp_DataModel*)list.first();
143   for ( ; aModel; aModel = (SalomeApp_DataModel*)list.next() )
144     aModel->saveAs( theFileName, this );
145
146   bool res = CAM_Study::saveDocumentAs( theFileName );
147
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 );
152
153   emit saved( this );
154
155   return res;
156 }
157
158 //=======================================================================
159 // name    : saveDocument
160 // Purpose : Save document
161 //=======================================================================
162 void SalomeApp_Study::saveDocument()
163 {
164   ModelList list; dataModels( list );
165
166   SalomeApp_DataModel* aModel = (SalomeApp_DataModel*)list.first();
167   for ( ; aModel; aModel = (SalomeApp_DataModel*)list.next() )
168     aModel->save();
169
170   CAM_Study::saveDocument();
171
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 );
176
177   emit saved( this );
178 }
179
180 //================================================================
181 // Function : closeDocument
182 // Purpose  : 
183 //================================================================
184 void SalomeApp_Study::closeDocument()
185 {
186   // Inform everybody that this study is going to close when it's most safe to,
187   // i.e. in the very beginning
188   emit closed( this );
189
190   // close SALOMEDS document
191   SalomeApp_Application::studyMgr()->Close( studyDS() );
192   SALOMEDSClient_Study* aStudy = NULL;
193   setStudyDS( _PTR(Study)(aStudy) );
194
195   CAM_Study::closeDocument();
196 }
197
198 //================================================================
199 // Function : isModified
200 // Purpose  : 
201 //================================================================
202 bool SalomeApp_Study::isModified() const
203 {
204   bool isAnyChanged = studyDS() && studyDS()->IsModified();
205   ModelList list; dataModels( list );
206
207   SalomeApp_DataModel* aModel = 0;
208   for ( QPtrListIterator<CAM_DataModel> it( list ); it.current() && !isAnyChanged; ++it ){
209     aModel = dynamic_cast<SalomeApp_DataModel*>( it.current() );
210     if ( aModel )
211       isAnyChanged = aModel->isModified();
212   }
213   return isAnyChanged; 
214 }
215
216 //================================================================
217 // Function : isSaved
218 // Purpose  : 
219 //================================================================
220 bool SalomeApp_Study::isSaved() const
221 {
222   bool isAllSaved = studyDS() && studyDS()->GetPersistentReference().size();
223   ModelList list; dataModels( list );
224
225   SalomeApp_DataModel* aModel = 0;
226   for ( QPtrListIterator<CAM_DataModel> it( list ); it.current() && isAllSaved; ++it ){
227     aModel = dynamic_cast<SalomeApp_DataModel*>( it.current() );
228     if ( aModel )
229       isAllSaved = aModel->isSaved();
230   }
231   return isAllSaved; 
232 }
233
234 void SalomeApp_Study::setStudyDS( const _PTR(Study)& s )
235 {
236   myStudyDS = s;
237 }
238
239 void SalomeApp_Study::dataModelInserted (const CAM_DataModel* dm)
240 {
241   MESSAGE("SalomeApp_Study::dataModelInserted() : module name() = " << dm->module()->name());
242
243   CAM_Study::dataModelInserted(dm);
244
245   // Create SComponent for module, using default engine (CORBAless)
246   SalomeApp_Module* aModule = (SalomeApp_Module*)(dm->module());
247   if (aModule) {
248     QString anEngineIOR = aModule->engineIOR();
249     if (anEngineIOR.isEmpty()) { // CORBAless module
250       // Check SComponent existance
251       _PTR(SComponent) aComp = studyDS()->FindComponent(dm->module()->name());
252       if (!aComp) {
253         // Create SComponent
254         _PTR(StudyBuilder) aBuilder = studyDS()->NewBuilder();
255         aComp = aBuilder->NewComponent(dm->module()->name());
256
257         // Set default engine IOR
258         aBuilder->DefineComponentInstance(aComp, SalomeApp_Application::defaultEngineIOR());
259       }
260     }
261   }
262 }
263
264 bool SalomeApp_Study::openDataModel( const QString& studyName, CAM_DataModel* dm )
265 {
266   if (!dm)
267     return false;
268
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);
273     return true;
274   }
275
276   return false;
277 }
278
279 void SalomeApp_Study::updateModelRoot( const CAM_DataModel* dm )
280 {
281   CAM_Study::updateModelRoot( dm );
282   ((SalomeApp_Application*)application())->objectBrowser()->updateTree();
283 }
284
285 QString SalomeApp_Study::newStudyName() const
286 {
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() ) )
294         break;
295     }
296     if ( j == n )
297       newName = curName;
298     else
299       i++;
300   }
301   return newName;
302 }