Salome HOME
Base implementation of dumping study document in to Python script (Feature #14).
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_DataModel.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_DataModel.h"
24
25 #include "HYDROGUI_DataObject.h"
26 #include "HYDROGUI_Module.h"
27 #include "HYDROGUI_Tool.h"
28
29 #include <HYDROData_Bathymetry.h>
30 #include <HYDROData_Document.h>
31 #include <HYDROData_Image.h>
32 #include <HYDROData_Iterator.h>
33 #include <HYDROData_Polyline.h>
34 #include <HYDROData_VisualState.h>
35
36 #include <CAM_Application.h>
37 #include <CAM_DataObject.h>
38 #include <CAM_Module.h>
39 #include <CAM_Study.h>
40
41 #include <LightApp_Application.h>
42 #include <LightApp_DataObject.h>
43 #include <LightApp_Study.h>
44
45 #include <SUIT_DataObject.h>
46 #include <SUIT_DataBrowser.h>
47 #include <SUIT_ResourceMgr.h>
48 #include <SUIT_Study.h>
49 #include <SUIT_Tools.h>
50
51 #include <HYDROData_Document.h>
52
53 #include <TDF_Delta.hxx>
54 #include <TDF_ListIteratorOfDeltaList.hxx>
55
56 HYDROGUI_DataModel::HYDROGUI_DataModel( CAM_Module* theModule )
57 : LightApp_DataModel( theModule )
58 {
59   update( module()->application()->activeStudy()->id() );
60 }
61
62 HYDROGUI_DataModel::~HYDROGUI_DataModel()
63 {
64 }
65
66 bool HYDROGUI_DataModel::open( const QString& theURL,
67                                CAM_Study* theStudy,
68                                QStringList theFileList )
69 {
70   LightApp_DataModel::open( theURL, theStudy, theFileList );
71   const int aStudyId = theStudy->id();
72
73   Data_DocError res = DocError_UnknownProblem;
74   if( theFileList.count() == 2 )
75   {
76     QString aTmpDir = theFileList[0];
77     QString aFileName = theFileList[1];
78
79     myStudyURL = theURL;
80     QString aFullPath = SUIT_Tools::addSlash( aTmpDir ) + aFileName;
81
82     try
83     {
84       res = HYDROData_Document::Load( (char*)aFullPath.toLatin1().constData(), aStudyId );
85     }
86     catch(...)
87     {
88       res = DocError_UnknownProblem;
89     }
90     if( res != DocError_OK )
91     {
92       module()->application()->putInfo( tr( "LOAD_ERROR" ) );
93       return false;
94     }
95   }
96
97   // if the document open was successful, the data model update happens
98   // in the set mode of the module
99   if( res == DocError_OK )
100     update( aStudyId );
101
102   return true;
103 }
104
105 bool HYDROGUI_DataModel::save( QStringList& theFileList )
106 {
107   if( !module()->application()->activeStudy() )
108     return false;
109   
110   LightApp_DataModel::save( theFileList );
111
112   QString aTmpDir;
113   QString aFileName;
114   SUIT_ResourceMgr* resMgr = module()->application()->resourceMgr();
115   bool isMultiFile = false;
116   if( resMgr )
117     isMultiFile = resMgr->booleanValue( "Study", "multi_file", false );
118
119   // save data to temporary files
120   LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy() );
121   aTmpDir = aStudy->GetTmpDir( myStudyURL.toLatin1().constData(), isMultiFile ).c_str();
122   aFileName = SUIT_Tools::file( myStudyURL, false ) + "_HYDRO.cbf";
123
124   QString aFullPath = aTmpDir + aFileName;
125   Data_DocError res = getDocument()->Save( (char*)aFullPath.toLatin1().constData() );
126   if( res != DocError_OK )
127   {
128     module()->application()->putInfo( tr( "SAVE_ERROR" ) );
129     return false;
130   }
131
132   theFileList.append( aTmpDir );
133   theFileList.append( aFileName );
134
135   return true;
136 }
137
138 bool HYDROGUI_DataModel::saveAs( const QString& theURL,
139                                  CAM_Study*,
140                                  QStringList& theFileList )
141 {
142   myStudyURL = theURL;
143   return save( theFileList );
144 }
145
146 bool HYDROGUI_DataModel::close()
147 {
148   return true;
149 }
150
151 bool HYDROGUI_DataModel::isModified() const
152 {
153   return getDocument()->IsModified();
154 }
155
156 bool HYDROGUI_DataModel::isSaved() const
157 {
158   return true;
159 }
160
161 void HYDROGUI_DataModel::update( const int theStudyId )
162 {
163   LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
164   if( !anApp )
165     return;
166
167   SUIT_DataObject* aStudyRoot = anApp->activeStudy()->root();
168   if( !aStudyRoot )
169     return;
170
171   // create root object if not exist
172   CAM_DataObject* aRootObj = root();
173   if( !aRootObj )
174     aRootObj = createRootModuleObject( aStudyRoot );
175
176   if( !aRootObj )
177     return;
178
179   DataObjectList aList;
180   aRootObj->children( aList );
181   QListIterator<SUIT_DataObject*> anIter( aList );
182   while( anIter.hasNext() )
183     removeChild( aRootObj, anIter.next() );
184
185   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theStudyId );
186   if( aDocument.IsNull() )
187     return;
188
189   LightApp_DataObject* anImageRootObj = createObject( aRootObj, "IMAGES" );
190
191   HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
192   for( ; anIterator.More(); anIterator.Next() )
193   {
194     Handle(HYDROData_Image) anImageObj =
195       Handle(HYDROData_Image)::DownCast( anIterator.Current() );
196     if( !anImageObj.IsNull() )
197     {
198       if( LightApp_DataObject* anImageDataObj = createObject( anImageRootObj, anImageObj ) )
199       {
200         for( int anIndex = 0, aNbRef = anImageObj->NbReferences(); anIndex < aNbRef; anIndex++ )
201         {
202           Handle(HYDROData_Image) aRefImageObj = anImageObj->Reference( anIndex );
203           if( !aRefImageObj.IsNull() && !aRefImageObj->IsRemoved() )
204             createObject( anImageDataObj, aRefImageObj, anImageDataObj->entry() );
205         }
206       }
207     }
208   }
209
210   LightApp_DataObject* aBathymetryRootObj = createObject( aRootObj, "BATHYMETRIES" );
211
212   anIterator = HYDROData_Iterator( aDocument, KIND_BATHYMETRY );
213   for( ; anIterator.More(); anIterator.Next() )
214   {
215     Handle(HYDROData_Bathymetry) aBathymetryObj =
216       Handle(HYDROData_Bathymetry)::DownCast( anIterator.Current() );
217     if( !aBathymetryObj.IsNull() )
218       createObject( aBathymetryRootObj, aBathymetryObj );
219   }
220
221   LightApp_DataObject* aPolylineRootObj = createObject( aRootObj, "POLYLINES" );
222
223   anIterator = HYDROData_Iterator( aDocument, KIND_POLYLINE );
224   for( ; anIterator.More(); anIterator.Next() )
225   {
226     Handle(HYDROData_Polyline) aPolylineObj =
227       Handle(HYDROData_Polyline)::DownCast( anIterator.Current() );
228     if( !aPolylineObj.IsNull() )
229       createObject( aPolylineRootObj, aPolylineObj );
230   }
231
232   LightApp_DataObject* aVisualStateRootObj = createObject( aRootObj, "VISUAL_STATES" );
233
234   anIterator = HYDROData_Iterator( aDocument, KIND_VISUAL_STATE );
235   for( ; anIterator.More(); anIterator.Next() )
236   {
237     Handle(HYDROData_VisualState) aVisualStateObj =
238       Handle(HYDROData_VisualState)::DownCast( anIterator.Current() );
239     if( !aVisualStateObj.IsNull() )
240       createObject( aVisualStateRootObj, aVisualStateObj );
241   }
242
243   if( SUIT_DataBrowser* anObjectBrowser = anApp->objectBrowser() )
244   {
245     anObjectBrowser->setAutoOpenLevel( 3 );
246     anObjectBrowser->openLevels();
247   }
248 }
249
250 HYDROGUI_DataObject* HYDROGUI_DataModel::getDataObject( const Handle(HYDROData_Object)& theModelObject )
251 {
252   return NULL; // to do if necessary
253 }
254
255 HYDROGUI_DataObject* HYDROGUI_DataModel::getReferencedDataObject( HYDROGUI_DataObject* theObject )
256 {
257   return NULL; // to do if necessary
258 }
259
260 SUIT_DataObject* HYDROGUI_DataModel::findObject( const QString& theEntry ) const
261 {
262   LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
263   return anApp ? anApp->findObject( theEntry ) : 0;
264 }
265
266 void HYDROGUI_DataModel::update( LightApp_DataObject* theObject,
267                                  LightApp_Study* theStudy )
268 {
269   if( !theStudy )
270     theStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy()) ;
271   if( theStudy )
272     update( theStudy->id() );
273 }
274
275 CAM_DataObject* HYDROGUI_DataModel::createRootModuleObject( SUIT_DataObject* theParent )
276 {
277   CAM_DataObject* aRootObj = createModuleObject( theParent );
278   setRoot( aRootObj );
279   return aRootObj;
280 }
281
282 void HYDROGUI_DataModel::updateModel()
283 {
284   HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
285   if( aModule )
286     update( aModule->getStudyId() );
287 }
288
289 Handle(HYDROData_Object) HYDROGUI_DataModel::objectByEntry( const QString& theEntry,
290                                                             const ObjectKind theObjectKind )
291 {
292   QString anEntry = theEntry;
293   if( anEntry.indexOf( "_" ) != -1 ) // reference object
294     anEntry = anEntry.section( "_", -1 );
295
296   Handle(HYDROData_Document) aDocument = getDocument();
297   if( !aDocument.IsNull() )
298   {
299     HYDROData_Iterator anIterator( aDocument, theObjectKind );
300     for( ; anIterator.More(); anIterator.Next() )
301     {
302       Handle(HYDROData_Object) anObject = anIterator.Current();
303       if( !anObject.IsNull() )
304       {
305         QString anEntryRef = HYDROGUI_DataObject::dataObjectEntry( anObject );
306         if( anEntryRef == anEntry )
307           return anObject;
308       }
309     }
310   }
311   return NULL;
312 }
313
314 bool HYDROGUI_DataModel::canUndo() const
315 {
316   return getDocument()->CanUndo();
317 }
318
319 bool HYDROGUI_DataModel::canRedo() const
320 {
321   return getDocument()->CanRedo();
322 }
323
324 QStringList HYDROGUI_DataModel::undoNames() const
325 {
326   QStringList aNames;
327   for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetUndos() ); anIter.More(); anIter.Next() )
328     aNames.prepend( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
329   return aNames;
330 }
331
332 QStringList HYDROGUI_DataModel::redoNames() const
333 {
334   QStringList aNames;
335   for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetRedos() ); anIter.More(); anIter.Next() )
336     aNames.append( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
337   return aNames;
338 }
339
340 void HYDROGUI_DataModel::clearUndos()
341 {
342   getDocument()->ClearUndos();
343 }
344
345 void HYDROGUI_DataModel::clearRedos()
346 {
347   getDocument()->ClearRedos();
348 }
349
350 bool HYDROGUI_DataModel::undo()
351 {
352   try 
353   {
354     getDocument()->Undo();
355   }
356   catch ( Standard_Failure )
357   {
358     return false;
359   }
360   return true;
361 }
362
363 bool HYDROGUI_DataModel::redo()
364 {
365   try 
366   {
367     getDocument()->Redo();
368   }
369   catch ( Standard_Failure )
370   {
371     return false;
372   }
373   return true;
374 }
375
376 Handle(HYDROData_Document) HYDROGUI_DataModel::getDocument() const
377 {
378   int aStudyId = module()->application()->activeStudy()->id();
379   return HYDROData_Document::Document( aStudyId );
380 }
381
382 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
383                                                        Handle(HYDROData_Object) theModelObject,
384                                                        const QString& theParentEntry )
385 {
386   return new HYDROGUI_DataObject( theParent, theModelObject, theParentEntry );
387 }
388
389 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
390                                                        const QString& theName )
391 {
392   return new HYDROGUI_NamedObject( theParent, theName );
393 }
394
395 void HYDROGUI_DataModel::removeChild( SUIT_DataObject* theParent,
396                                       SUIT_DataObject* theChild )
397 {
398   SUIT_DataObject* aSubChild = theChild->firstChild();
399   for( ; aSubChild; aSubChild = aSubChild->nextBrother() )
400     removeChild( theChild, aSubChild );
401   theParent->removeChild( theChild );
402 }
403
404 SUIT_DataObject* HYDROGUI_DataModel::findChildByName( const SUIT_DataObject* theFather,
405                                                       const QString& theName )
406 {
407   SUIT_DataObject* aChild = theFather->firstChild();
408   while( aChild )
409   {
410     if( aChild->name() == theName )
411       return aChild; // found
412     aChild = aChild->nextBrother();
413   }
414   return NULL; // not found
415 }