Salome HOME
Dump Image data to python script (Feature #13).
[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 #include <QApplication>
57 #include <QDir>
58
59 HYDROGUI_DataModel::HYDROGUI_DataModel( CAM_Module* theModule )
60 : LightApp_DataModel( theModule )
61 {
62   update( module()->application()->activeStudy()->id() );
63 }
64
65 HYDROGUI_DataModel::~HYDROGUI_DataModel()
66 {
67 }
68
69 bool HYDROGUI_DataModel::open( const QString& theURL,
70                                CAM_Study* theStudy,
71                                QStringList theFileList )
72 {
73   LightApp_DataModel::open( theURL, theStudy, theFileList );
74   const int aStudyId = theStudy->id();
75
76   Data_DocError res = DocError_UnknownProblem;
77   if( theFileList.count() == 2 )
78   {
79     QString aTmpDir = theFileList[0];
80     QString aFileName = theFileList[1];
81
82     myStudyURL = theURL;
83     QString aFullPath = SUIT_Tools::addSlash( aTmpDir ) + aFileName;
84
85     try
86     {
87       res = HYDROData_Document::Load( (char*)aFullPath.toLatin1().constData(), aStudyId );
88     }
89     catch(...)
90     {
91       res = DocError_UnknownProblem;
92     }
93     if( res != DocError_OK )
94     {
95       module()->application()->putInfo( tr( "LOAD_ERROR" ) );
96       return false;
97     }
98   }
99
100   // if the document open was successful, the data model update happens
101   // in the set mode of the module
102   if( res == DocError_OK )
103     update( aStudyId );
104
105   return true;
106 }
107
108 bool HYDROGUI_DataModel::save( QStringList& theFileList )
109 {
110   if( !module()->application()->activeStudy() )
111     return false;
112   
113   LightApp_DataModel::save( theFileList );
114
115   QString aTmpDir;
116   QString aFileName;
117   SUIT_ResourceMgr* resMgr = module()->application()->resourceMgr();
118   bool isMultiFile = false;
119   if( resMgr )
120     isMultiFile = resMgr->booleanValue( "Study", "multi_file", false );
121
122   // save data to temporary files
123   LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy() );
124   aTmpDir = aStudy->GetTmpDir( myStudyURL.toLatin1().constData(), isMultiFile ).c_str();
125   aFileName = SUIT_Tools::file( myStudyURL, false ) + "_HYDRO.cbf";
126
127   QString aFullPath = aTmpDir + aFileName;
128   Data_DocError res = getDocument()->Save( (char*)aFullPath.toLatin1().constData() );
129   if( res != DocError_OK )
130   {
131     module()->application()->putInfo( tr( "SAVE_ERROR" ) );
132     return false;
133   }
134
135   theFileList.append( aTmpDir );
136   theFileList.append( aFileName );
137
138   return true;
139 }
140
141 bool HYDROGUI_DataModel::saveAs( const QString& theURL,
142                                  CAM_Study*,
143                                  QStringList& theFileList )
144 {
145   myStudyURL = theURL;
146   return save( theFileList );
147 }
148
149 bool HYDROGUI_DataModel::close()
150 {
151   return true;
152 }
153
154 bool HYDROGUI_DataModel::dumpPython( const QString& theURL,
155                                      CAM_Study*     theStudy,
156                                      bool           isMultiFile,
157                                      QStringList&   theListOfFiles )
158 {
159   LightApp_DataModel::dumpPython( theURL, theStudy, isMultiFile, theListOfFiles );
160
161   int aStudyId = theStudy->id();
162
163   LightApp_Study* aStudy = ::qobject_cast<LightApp_Study*>( theStudy );
164   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId );
165   if ( aDocument.IsNull() || !aStudy )
166     return false;
167
168   QString aFileToExport = aStudy->GetTmpDir( theURL.toLatin1().constData(), isMultiFile ).c_str();
169   aFileToExport += QString( QDir::separator() ) + "HYDRO.py";
170
171   bool aRes = aDocument->DumpToPython( aFileToExport );
172
173   if ( aRes )
174   {
175     theListOfFiles.append( aFileToExport );
176   }
177
178   return aRes;
179 }
180
181 bool HYDROGUI_DataModel::isModified() const
182 {
183   return getDocument()->IsModified();
184 }
185
186 bool HYDROGUI_DataModel::isSaved() const
187 {
188   return true;
189 }
190
191 void HYDROGUI_DataModel::update( const int theStudyId )
192 {
193   LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
194   if( !anApp )
195     return;
196
197   SUIT_DataObject* aStudyRoot = anApp->activeStudy()->root();
198   if( !aStudyRoot )
199     return;
200
201   // create root object if not exist
202   CAM_DataObject* aRootObj = root();
203   if( !aRootObj )
204     aRootObj = createRootModuleObject( aStudyRoot );
205
206   if( !aRootObj )
207     return;
208
209   DataObjectList aList;
210   aRootObj->children( aList );
211   QListIterator<SUIT_DataObject*> anIter( aList );
212   while( anIter.hasNext() )
213     removeChild( aRootObj, anIter.next() );
214
215   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theStudyId );
216   if( aDocument.IsNull() )
217     return;
218
219   LightApp_DataObject* anImageRootObj = createObject( aRootObj, "IMAGES" );
220
221   HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
222   for( ; anIterator.More(); anIterator.Next() )
223   {
224     Handle(HYDROData_Image) anImageObj =
225       Handle(HYDROData_Image)::DownCast( anIterator.Current() );
226     if( !anImageObj.IsNull() )
227     {
228       if( LightApp_DataObject* anImageDataObj = createObject( anImageRootObj, anImageObj ) )
229       {
230         for( int anIndex = 0, aNbRef = anImageObj->NbReferences(); anIndex < aNbRef; anIndex++ )
231         {
232           Handle(HYDROData_Image) aRefImageObj = anImageObj->Reference( anIndex );
233           if( !aRefImageObj.IsNull() && !aRefImageObj->IsRemoved() )
234             createObject( anImageDataObj, aRefImageObj, anImageDataObj->entry() );
235         }
236       }
237     }
238   }
239
240   LightApp_DataObject* aBathymetryRootObj = createObject( aRootObj, "BATHYMETRIES" );
241
242   anIterator = HYDROData_Iterator( aDocument, KIND_BATHYMETRY );
243   for( ; anIterator.More(); anIterator.Next() )
244   {
245     Handle(HYDROData_Bathymetry) aBathymetryObj =
246       Handle(HYDROData_Bathymetry)::DownCast( anIterator.Current() );
247     if( !aBathymetryObj.IsNull() )
248       createObject( aBathymetryRootObj, aBathymetryObj );
249   }
250
251   LightApp_DataObject* aPolylineRootObj = createObject( aRootObj, "POLYLINES" );
252
253   anIterator = HYDROData_Iterator( aDocument, KIND_POLYLINE );
254   for( ; anIterator.More(); anIterator.Next() )
255   {
256     Handle(HYDROData_Polyline) aPolylineObj =
257       Handle(HYDROData_Polyline)::DownCast( anIterator.Current() );
258     if( !aPolylineObj.IsNull() )
259       createObject( aPolylineRootObj, aPolylineObj );
260   }
261
262   LightApp_DataObject* aVisualStateRootObj = createObject( aRootObj, "VISUAL_STATES" );
263
264   anIterator = HYDROData_Iterator( aDocument, KIND_VISUAL_STATE );
265   for( ; anIterator.More(); anIterator.Next() )
266   {
267     Handle(HYDROData_VisualState) aVisualStateObj =
268       Handle(HYDROData_VisualState)::DownCast( anIterator.Current() );
269     if( !aVisualStateObj.IsNull() )
270       createObject( aVisualStateRootObj, aVisualStateObj );
271   }
272
273   if( SUIT_DataBrowser* anObjectBrowser = anApp->objectBrowser() )
274   {
275     anObjectBrowser->setAutoOpenLevel( 3 );
276     anObjectBrowser->openLevels();
277   }
278 }
279
280 HYDROGUI_DataObject* HYDROGUI_DataModel::getDataObject( const Handle(HYDROData_Object)& theModelObject )
281 {
282   return NULL; // to do if necessary
283 }
284
285 HYDROGUI_DataObject* HYDROGUI_DataModel::getReferencedDataObject( HYDROGUI_DataObject* theObject )
286 {
287   return NULL; // to do if necessary
288 }
289
290 SUIT_DataObject* HYDROGUI_DataModel::findObject( const QString& theEntry ) const
291 {
292   LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
293   return anApp ? anApp->findObject( theEntry ) : 0;
294 }
295
296 void HYDROGUI_DataModel::update( LightApp_DataObject* theObject,
297                                  LightApp_Study* theStudy )
298 {
299   if( !theStudy )
300     theStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy()) ;
301   if( theStudy )
302     update( theStudy->id() );
303 }
304
305 CAM_DataObject* HYDROGUI_DataModel::createRootModuleObject( SUIT_DataObject* theParent )
306 {
307   CAM_DataObject* aRootObj = createModuleObject( theParent );
308   setRoot( aRootObj );
309   return aRootObj;
310 }
311
312 void HYDROGUI_DataModel::updateModel()
313 {
314   HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
315   if( aModule )
316     update( aModule->getStudyId() );
317 }
318
319 Handle(HYDROData_Object) HYDROGUI_DataModel::objectByEntry( const QString& theEntry,
320                                                             const ObjectKind theObjectKind )
321 {
322   QString anEntry = theEntry;
323   if( anEntry.indexOf( "_" ) != -1 ) // reference object
324     anEntry = anEntry.section( "_", -1 );
325
326   Handle(HYDROData_Document) aDocument = getDocument();
327   if( !aDocument.IsNull() )
328   {
329     HYDROData_Iterator anIterator( aDocument, theObjectKind );
330     for( ; anIterator.More(); anIterator.Next() )
331     {
332       Handle(HYDROData_Object) anObject = anIterator.Current();
333       if( !anObject.IsNull() )
334       {
335         QString anEntryRef = HYDROGUI_DataObject::dataObjectEntry( anObject );
336         if( anEntryRef == anEntry )
337           return anObject;
338       }
339     }
340   }
341   return NULL;
342 }
343
344 bool HYDROGUI_DataModel::canUndo() const
345 {
346   return getDocument()->CanUndo();
347 }
348
349 bool HYDROGUI_DataModel::canRedo() const
350 {
351   return getDocument()->CanRedo();
352 }
353
354 QStringList HYDROGUI_DataModel::undoNames() const
355 {
356   QStringList aNames;
357   for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetUndos() ); anIter.More(); anIter.Next() )
358     aNames.prepend( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
359   return aNames;
360 }
361
362 QStringList HYDROGUI_DataModel::redoNames() const
363 {
364   QStringList aNames;
365   for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetRedos() ); anIter.More(); anIter.Next() )
366     aNames.append( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
367   return aNames;
368 }
369
370 void HYDROGUI_DataModel::clearUndos()
371 {
372   getDocument()->ClearUndos();
373 }
374
375 void HYDROGUI_DataModel::clearRedos()
376 {
377   getDocument()->ClearRedos();
378 }
379
380 bool HYDROGUI_DataModel::undo()
381 {
382   try 
383   {
384     getDocument()->Undo();
385   }
386   catch ( Standard_Failure )
387   {
388     return false;
389   }
390   return true;
391 }
392
393 bool HYDROGUI_DataModel::redo()
394 {
395   try 
396   {
397     getDocument()->Redo();
398   }
399   catch ( Standard_Failure )
400   {
401     return false;
402   }
403   return true;
404 }
405
406 Handle(HYDROData_Document) HYDROGUI_DataModel::getDocument() const
407 {
408   int aStudyId = module()->application()->activeStudy()->id();
409   return HYDROData_Document::Document( aStudyId );
410 }
411
412 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
413                                                        Handle(HYDROData_Object) theModelObject,
414                                                        const QString& theParentEntry )
415 {
416   return new HYDROGUI_DataObject( theParent, theModelObject, theParentEntry );
417 }
418
419 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
420                                                        const QString& theName )
421 {
422   return new HYDROGUI_NamedObject( theParent, theName );
423 }
424
425 void HYDROGUI_DataModel::removeChild( SUIT_DataObject* theParent,
426                                       SUIT_DataObject* theChild )
427 {
428   SUIT_DataObject* aSubChild = theChild->firstChild();
429   for( ; aSubChild; aSubChild = aSubChild->nextBrother() )
430     removeChild( theChild, aSubChild );
431   theParent->removeChild( theChild );
432 }
433
434 SUIT_DataObject* HYDROGUI_DataModel::findChildByName( const SUIT_DataObject* theFather,
435                                                       const QString& theName )
436 {
437   SUIT_DataObject* aChild = theFather->firstChild();
438   while( aChild )
439   {
440     if( aChild->name() == theName )
441       return aChild; // found
442     aChild = aChild->nextBrother();
443   }
444   return NULL; // not found
445 }