Salome HOME
PAL9391 - display/erase objects from other modules
[modules/gui.git] / src / LightApp / LightApp_Study.cxx
1 #include "LightApp_Study.h"
2
3 #include "CAM_DataModel.h"
4 #include "LightApp_Application.h"
5 #include "LightApp_DataModel.h"
6 #include "LightApp_DataObject.h"
7 #include "LightApp_RootObject.h"
8 #include "LightApp_Driver.h"
9
10 #include "SUIT_ResourceMgr.h"
11 #include "SUIT_DataObjectIterator.h"
12
13 #include <OB_Browser.h>
14
15 #include <TCollection_AsciiString.hxx> 
16
17 #include <OSD_Path.hxx>
18 #include <OSD_File.hxx>
19 #include <OSD_Directory.hxx>
20 #include <OSD_Process.hxx>
21 #include <OSD_Directory.hxx>
22 #include <OSD_Protection.hxx>
23 #include <OSD_SingleProtection.hxx>
24 #include <OSD_FileIterator.hxx>
25
26 #include <qstring.h>
27
28 /*!
29   Constructor.
30 */
31 LightApp_Study::LightApp_Study( SUIT_Application* app )
32 : CAM_Study( app )
33 {
34   myDriver = new LightApp_Driver();
35 }
36  
37 /*!
38   Destructor.
39 */
40 LightApp_Study::~LightApp_Study()
41 {
42 }
43
44 /*!
45   Create document.
46 */
47 void LightApp_Study::createDocument()
48 {
49   // create myRoot
50   setRoot( new LightApp_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 LightApp_Study::openDocument( const QString& theFileName )
62 {
63   myDriver->ClearDriverContents();
64   // create files for models from theFileName
65   if( !openStudyData(theFileName))
66     return false;
67
68   setRoot( new LightApp_RootObject( this ) ); // create myRoot
69
70   // update loaded data models: call open() and update() on them.
71   ModelList dm_s;
72   dataModels( dm_s );
73   for ( ModelListIterator it( dm_s ); it.current(); ++it )
74     openDataModel( studyName(), it.current() );
75   // this will build a SUIT_DataObject-s tree under myRoot member field
76   // passing "false" in order NOT to rebuild existing data models' trees - it was done in previous step
77   // but tree that corresponds to not-loaded data models will be updated any way. 
78   ((LightApp_Application*)application())->updateObjectBrowser( false ); 
79
80   bool res = CAM_Study::openDocument( theFileName );
81
82   emit opened( this );
83   return res;
84 }
85
86 //=======================================================================
87 // name    : loadDocument
88 /*! Purpose : Load document */
89 //=======================================================================
90 bool LightApp_Study::loadDocument( const QString& theStudyName )
91 {
92   myDriver->ClearDriverContents();
93   if( !openStudyData(theStudyName))
94     return false;
95
96   setRoot( new LightApp_RootObject( this ) ); // create myRoot
97
98   //SRN: BugID IPAL9021, put there the same code as in a method openDocument
99
100   // update loaded data models: call open() and update() on them.
101   ModelList dm_s;
102   dataModels( dm_s );
103
104   for ( ModelListIterator it( dm_s ); it.current(); ++it )
105     openDataModel( studyName(), it.current() );
106
107   // this will build a SUIT_DataObject-s tree under myRoot member field
108   // passing "false" in order NOT to rebuild existing data models' trees - it was done in previous step
109   // but tree that corresponds to not-loaded data models will be updated any way. 
110   ((LightApp_Application*)application())->updateObjectBrowser( false ); 
111
112   bool res = CAM_Study::openDocument( theStudyName );
113   emit opened( this );
114   //SRN: BugID IPAL9021: End
115   return res;
116 }
117
118 //=======================================================================
119 // name    : saveDocumentAs
120 /*! Purpose : Save document */
121 //=======================================================================
122 bool LightApp_Study::saveDocumentAs( const QString& theFileName )
123 {
124   ModelList list; dataModels( list );
125
126   LightApp_DataModel* aModel = (LightApp_DataModel*)list.first();
127
128   myDriver->ClearDriverContents();
129   QStringList listOfFiles;
130   for ( ; aModel; aModel = (LightApp_DataModel*)list.next() ) {
131     listOfFiles.clear();
132     aModel->saveAs( theFileName, this, listOfFiles );
133     if ( !listOfFiles.isEmpty() )
134       saveModuleData(aModel->module()->name(), listOfFiles);
135   }
136
137   bool res = saveStudyData(theFileName);
138   res = res && CAM_Study::saveDocumentAs( theFileName );
139   //SRN: BugID IPAL9377, removed usage of uninitialized variable <res>
140   if ( res )
141     emit saved( this );
142
143   return res;
144 }
145
146 //=======================================================================
147 // name    : saveDocument
148 /*! Purpose : Save document */
149 //=======================================================================
150 bool LightApp_Study::saveDocument()
151 {
152   ModelList list; dataModels( list );
153
154   LightApp_DataModel* aModel = (LightApp_DataModel*)list.first();
155
156   myDriver->ClearDriverContents();
157   QStringList listOfFiles;
158   for ( ; aModel; aModel = (LightApp_DataModel*)list.next() ) {
159     listOfFiles.clear();
160     aModel->save( listOfFiles );
161     saveModuleData(aModel->module()->name(), listOfFiles);
162   }
163
164   bool res = saveStudyData(studyName());
165   res = res && CAM_Study::saveDocument();
166   if (res)
167     emit saved( this );
168
169   return res;
170 }
171
172 //================================================================
173 // Function : closeDocument
174 /*! Purpose  : Close document */
175 //================================================================
176 void LightApp_Study::closeDocument(bool permanently)
177 {
178   // Inform everybody that this study is going to close when it's most safe to,
179   // i.e. in the very beginning
180   emit closed( this );
181
182   CAM_Study::closeDocument(permanently);
183 }
184
185 //================================================================
186 // Function : referencedToEntry
187 /*! Purpose  : Return current entry*/
188 //================================================================
189 QString LightApp_Study::referencedToEntry( const QString& entry ) const
190 {
191   return entry;
192 }
193
194 //================================================================
195 // Function : children
196 /*! Purpose : Return entries of children of object*/
197 //================================================================
198 void LightApp_Study::children( const QString&, QStringList& ) const
199 {
200 }
201
202 //================================================================
203 // Function : isComponent
204 /*! Purpose : Return true if entry corresponds to component*/
205 //================================================================
206 bool LightApp_Study::isComponent( const QString& entry ) const
207 {
208   return false;
209 }
210
211 //================================================================
212 // Function : componentDataType
213 /*! Purpose  : Return component data type from entry*/
214 //================================================================
215 QString LightApp_Study::componentDataType( const QString& entry ) const
216 {
217   LightApp_DataObject* aCurObj;
218   for ( SUIT_DataObjectIterator it( root(), SUIT_DataObjectIterator::DepthLeft ); it.current(); ++it ) {
219     aCurObj = dynamic_cast<LightApp_DataObject*>( it.current() );
220     if ( aCurObj && aCurObj->entry() == entry ) {
221       return aCurObj->componentDataType();
222     }
223   }
224   return "";
225 }
226
227 //================================================================
228 // Function : isModified
229 // Purpose  : 
230 //================================================================
231 bool LightApp_Study::isModified() const
232 {
233   bool isAnyChanged = CAM_Study::isModified();
234   ModelList list; dataModels( list );
235
236   LightApp_DataModel* aModel = 0;
237   for ( QPtrListIterator<CAM_DataModel> it( list ); it.current() && !isAnyChanged; ++it ){
238     aModel = dynamic_cast<LightApp_DataModel*>( it.current() );
239     if ( aModel )
240       isAnyChanged = aModel->isModified();
241   }
242   return isAnyChanged; 
243 }
244
245 //================================================================
246 // Function : isSaved
247 /*! Purpose  : Check: data model is saved?*/
248 //================================================================
249 bool LightApp_Study::isSaved() const
250 {
251   bool isAllSaved = CAM_Study::isSaved();
252   ModelList list; dataModels( list );
253
254   LightApp_DataModel* aModel = 0;
255   for ( QPtrListIterator<CAM_DataModel> it( list ); it.current() && isAllSaved; ++it ){
256     aModel = dynamic_cast<LightApp_DataModel*>( it.current() );
257     if ( aModel )
258       isAllSaved = aModel->isSaved();
259   }
260   return isAllSaved; 
261 }
262
263 //=======================================================================
264 // name    : saveModuleData
265 /*! Purpose :  Create SComponent for module, necessary for SalomeApp study */
266 //=======================================================================
267 void LightApp_Study::addComponent(const CAM_DataModel* dm)
268 {
269 }
270
271 //=======================================================================
272 // name    : saveModuleData
273 /*! Purpose : save list file for module 'theModuleName' */
274 //=======================================================================
275 void LightApp_Study::saveModuleData(QString theModuleName, QStringList theListOfFiles)
276 {
277   int aNb = theListOfFiles.count();
278   if ( aNb == 0 )
279     return;
280
281   std::vector<std::string> aListOfFiles ( aNb );
282   int anIndex = 0;
283   for ( QStringList::Iterator it = theListOfFiles.begin(); it != theListOfFiles.end(); ++it ) {
284     if ( (*it).isEmpty() )
285       continue;
286     aListOfFiles[anIndex] = (*it).latin1();
287     anIndex++;
288   }
289   myDriver->SetListOfFiles(theModuleName, aListOfFiles);
290 }
291
292 //=======================================================================
293 // name    : openModuleData
294 /*! Purpose : gets list of file for module 'theModuleNam' */
295 //=======================================================================
296 void LightApp_Study::openModuleData(QString theModuleName, QStringList& theListOfFiles)
297 {
298   std::vector<std::string> aListOfFiles =  myDriver->GetListOfFiles(theModuleName);
299   int i, aLength = aListOfFiles.size() - 1;
300   if (aLength < 0)
301     return;
302
303   //Get a temporary directory for saved a file
304   theListOfFiles.append(aListOfFiles[0].c_str());
305   for(i = 0; i < aLength; i++)
306     theListOfFiles.append(aListOfFiles[i+1].c_str());
307 }
308
309 //=======================================================================
310 // name    : saveStudyData
311 /*! Purpose : save data from study */
312 //=======================================================================
313 bool LightApp_Study::saveStudyData( const QString& theFileName )
314 {
315   ModelList list; dataModels( list );
316   SUIT_ResourceMgr* resMgr = application()->resourceMgr();
317   if( !resMgr )
318     return false;
319   bool isMultiFile = resMgr->booleanValue( "Study", "multi_file", false );
320
321   bool aRes = myDriver->SaveDatasInFile(theFileName.latin1(), isMultiFile);
322   // clear map
323   std::vector<std::string> aList(0);
324   for ( ModelListIterator it( list ); it.current(); ++it )
325     myDriver->SetListOfFiles(it.current()->module()->name(), aList);
326
327   return aRes;
328 }
329
330 //=======================================================================
331 // name    : openStudyData
332 /*! Purpose : open data for study */
333 //=======================================================================
334 bool LightApp_Study::openStudyData( const QString& theFileName )
335 {
336   SUIT_ResourceMgr* resMgr = application()->resourceMgr();
337   if( !resMgr )
338     return false;
339   bool isMultiFile = resMgr->booleanValue( "Study", "multi_file", false );
340
341   bool aRes = myDriver->ReadDatasFromFile(theFileName.latin1(), isMultiFile);
342   return aRes;
343 }
344
345 //================================================================
346 // Function : openDataModel
347 /*! Purpose  : Open data model */
348 //================================================================
349 bool LightApp_Study::openDataModel( const QString& studyName, CAM_DataModel* dm )
350 {
351   if (!dm)
352     return false;
353
354   QStringList listOfFiles;
355   openModuleData(dm->module()->name(), listOfFiles);
356   if (dm && dm->open(studyName, this, listOfFiles)) {
357     // Remove the files and temporary directory, created
358     // for this module by LightApp_Driver::OpenStudyData()
359     bool isMultiFile = false; // TODO: decide, how to access this parameter
360     RemoveTemporaryFiles( dm->module()->name(), isMultiFile );
361
362      // Something has been read -> create data model tree
363     LightApp_DataModel* aDM = dynamic_cast<LightApp_DataModel*>( dm );
364     if ( aDM )
365       aDM->update(NULL, this);
366     return true;
367   }
368   return false;
369 }
370
371 //================================================================
372 // Function : GetTmpDir
373 /*! Purpose  : to be used by modules*/
374 //================================================================
375 std::string LightApp_Study::GetTmpDir (const char* theURL,
376                                        const bool  isMultiFile)
377 {
378   return myDriver->GetTmpDir(theURL, isMultiFile);
379 }
380
381 //================================================================
382 // Function : GetListOfFiles
383 /*! Purpose  : to be used by modules*/
384 //================================================================
385 std::vector<std::string> LightApp_Study::GetListOfFiles(const char* theModuleName) const
386 {
387   std::vector<std::string> aListOfFiles;
388   aListOfFiles = myDriver->GetListOfFiles(theModuleName);
389   return aListOfFiles;
390 }
391
392 //================================================================
393 // Function : SetListOfFiles
394 /*! Purpose  : to be used by modules*/
395 //================================================================
396 void LightApp_Study::SetListOfFiles (const char* theModuleName, const std::vector<std::string> theListOfFiles)
397 {
398   myDriver->SetListOfFiles(theModuleName, theListOfFiles);
399 }
400
401 //================================================================
402 // Function : RemoveTemporaryFiles
403 /*! Purpose  : to be used by modules*/
404 //================================================================
405 void LightApp_Study::RemoveTemporaryFiles (const char* theModuleName, const bool isMultiFile) const
406 {
407   if (isMultiFile)
408     return;
409   bool isDirDeleted = true;
410   myDriver->RemoveTemporaryFiles(theModuleName, isDirDeleted);
411 }
412
413 //================================================================
414 // Function : RemoveTemporaryFiles
415 /*! Purpose  : to be used by modules*/
416 //================================================================
417 void LightApp_Study::components( QStringList& comp ) const
418 {
419   DataObjectList children = root()->children();
420   DataObjectList::const_iterator anIt = children.begin(), aLast = children.end();
421   for( ; anIt!=aLast; anIt++ )
422   {
423     LightApp_DataObject* obj = dynamic_cast<LightApp_DataObject*>( *anIt );
424     if( obj && obj->entry()!="Interface Applicative" )
425       comp.append( obj->entry() );
426   }
427 }