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