Salome HOME
Feature #86: The hierarchy in the Object Browser (T 19).
[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 #include "HYDROGUI_Zone.h"
29 #include "HYDROGUI_Region.h"
30
31 #include <HYDROData_Bathymetry.h>
32 #include <HYDROData_CalculationCase.h>
33 #include <HYDROData_Document.h>
34 #include <HYDROData_Image.h>
35 #include <HYDROData_ImmersibleZone.h>
36 #include <HYDROData_Iterator.h>
37 #include <HYDROData_Polyline.h>
38 #include <HYDROData_Profile.h>
39 #include <HYDROData_VisualState.h>
40 #include <HYDROData_Region.h>
41 #include <HYDROData_Zone.h>
42 #include <HYDROData_Obstacle.h>
43 #include <HYDROData_Channel.h>
44 #include <HYDROData_Digue.h>
45 #include <HYDROData_River.h>
46
47 #include <CAM_Application.h>
48 #include <CAM_DataObject.h>
49 #include <CAM_Module.h>
50 #include <CAM_Study.h>
51
52 #include <LightApp_Application.h>
53 #include <LightApp_DataObject.h>
54 #include <LightApp_Study.h>
55
56 #include <SUIT_DataObject.h>
57 #include <SUIT_DataBrowser.h>
58 #include <SUIT_ResourceMgr.h>
59 #include <SUIT_Study.h>
60 #include <SUIT_Tools.h>
61
62 #include <HYDROData_Document.h>
63
64 #include <TDF_Delta.hxx>
65 #include <TDF_ListIteratorOfDeltaList.hxx>
66
67 #include <QApplication>
68 #include <QDir>
69
70 static HYDROData_SequenceOfObjects myCopyingObjects;
71
72 HYDROGUI_DataModel::HYDROGUI_DataModel( CAM_Module* theModule )
73 : LightApp_DataModel( theModule )
74 {
75   update( module()->application()->activeStudy()->id() );
76 }
77
78 HYDROGUI_DataModel::~HYDROGUI_DataModel()
79 {
80 }
81
82 bool HYDROGUI_DataModel::open( const QString& theURL,
83                                CAM_Study* theStudy,
84                                QStringList theFileList )
85 {
86   LightApp_DataModel::open( theURL, theStudy, theFileList );
87   const int aStudyId = theStudy->id();
88
89   Data_DocError res = DocError_UnknownProblem;
90   if( theFileList.count() == 2 )
91   {
92     QString aTmpDir = theFileList[0];
93     QString aFileName = theFileList[1];
94
95     myStudyURL = theURL;
96     QString aFullPath = SUIT_Tools::addSlash( aTmpDir ) + aFileName;
97
98     try
99     {
100       res = HYDROData_Document::Load( (char*)aFullPath.toLatin1().constData(), aStudyId );
101     }
102     catch(...)
103     {
104       res = DocError_UnknownProblem;
105     }
106     if( res != DocError_OK )
107     {
108       module()->application()->putInfo( tr( "LOAD_ERROR" ) );
109       return false;
110     }
111   }
112
113   // if the document open was successful, the data model update happens
114   // in the set mode of the module
115   if( res == DocError_OK )
116     update( aStudyId );
117
118   return true;
119 }
120
121 bool HYDROGUI_DataModel::save( QStringList& theFileList )
122 {
123   if( !module()->application()->activeStudy() )
124     return false;
125   
126   LightApp_DataModel::save( theFileList );
127
128   QString aTmpDir;
129   QString aFileName;
130   SUIT_ResourceMgr* resMgr = module()->application()->resourceMgr();
131   bool isMultiFile = false;
132   if( resMgr )
133     isMultiFile = resMgr->booleanValue( "Study", "multi_file", false );
134
135   // save data to temporary files
136   LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy() );
137   aTmpDir = aStudy->GetTmpDir( myStudyURL.toLatin1().constData(), isMultiFile ).c_str();
138   aFileName = SUIT_Tools::file( myStudyURL, false ) + "_HYDRO.cbf";
139
140   QString aFullPath = aTmpDir + aFileName;
141   Data_DocError res = getDocument()->Save( (char*)aFullPath.toLatin1().constData() );
142   if( res != DocError_OK )
143   {
144     module()->application()->putInfo( tr( "SAVE_ERROR" ) );
145     return false;
146   }
147
148   theFileList.append( aTmpDir );
149   theFileList.append( aFileName );
150
151   return true;
152 }
153
154 bool HYDROGUI_DataModel::saveAs( const QString& theURL,
155                                  CAM_Study*,
156                                  QStringList& theFileList )
157 {
158   myStudyURL = theURL;
159   return save( theFileList );
160 }
161
162 bool HYDROGUI_DataModel::close()
163 {
164   return true;
165 }
166
167 bool HYDROGUI_DataModel::dumpPython( const QString& theURL,
168                                      CAM_Study*     theStudy,
169                                      bool           isMultiFile,
170                                      QStringList&   theListOfFiles )
171 {
172   LightApp_DataModel::dumpPython( theURL, theStudy, isMultiFile, theListOfFiles );
173
174   int aStudyId = theStudy->id();
175
176   LightApp_Study* aStudy = ::qobject_cast<LightApp_Study*>( theStudy );
177   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId );
178   if ( aDocument.IsNull() || !aStudy )
179     return false;
180
181   QString aFileToExport = aStudy->GetTmpDir( theURL.toLatin1().constData(), isMultiFile ).c_str();
182   aFileToExport += QString( QDir::separator() ) + "HYDRO.py";
183
184   bool aRes = aDocument->DumpToPython( aFileToExport );
185
186   if ( aRes )
187   {
188     theListOfFiles.append( aFileToExport );
189   }
190
191   return aRes;
192 }
193
194 bool HYDROGUI_DataModel::isModified() const
195 {
196   return getDocument()->IsModified();
197 }
198
199 bool HYDROGUI_DataModel::isSaved() const
200 {
201   return true;
202 }
203
204 void HYDROGUI_DataModel::update( const int theStudyId )
205 {
206   LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
207   if( !anApp )
208     return;
209
210   SUIT_DataObject* aStudyRoot = anApp->activeStudy()->root();
211   if( !aStudyRoot )
212     return;
213
214   // create root object if not exist
215   CAM_DataObject* aRootObj = root();
216   if( !aRootObj )
217     aRootObj = createRootModuleObject( aStudyRoot );
218
219   if( !aRootObj )
220     return;
221
222   DataObjectList aList;
223   aRootObj->children( aList );
224   QListIterator<SUIT_DataObject*> anIter( aList );
225   while( anIter.hasNext() )
226     removeChild( aRootObj, anIter.next() );
227
228   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theStudyId );
229   if( aDocument.IsNull() )
230     return;
231
232   // Create root objects:
233
234   // IMAGES
235   LightApp_DataObject* anImageRootObj = createObject( aRootObj, tr( partitionName( KIND_IMAGE ).toAscii() ) );
236
237   // BATHYMETRY
238   LightApp_DataObject* aBathymetryRootObj = createObject( aRootObj, tr( partitionName( KIND_BATHYMETRY ).toAscii() ) );
239
240   // ARTIFICIAL OBJECTS
241   LightApp_DataObject* anArtificialObjectsRootObj = createObject( aRootObj, tr( partitionName( KIND_ARTIFICIAL_OBJECT ).toAscii() ) );
242
243   // NATURAL OBJECTS
244   LightApp_DataObject* aNaturalObjectsRootObj = createObject( aRootObj, tr( partitionName( KIND_NATURAL_OBJECT ).toAscii() ) );
245
246   // OBSTACLES
247   LightApp_DataObject* anObstaclesRootObj = createObject( aRootObj, tr( partitionName( KIND_OBSTACLE ).toAscii() ) );
248
249   // CALCULATION CASES
250   LightApp_DataObject* aCalculRootObj = createObject( aRootObj, tr( partitionName( KIND_CALCULATION ).toAscii() ) );
251
252   // POLYLINES
253   LightApp_DataObject* aPolylineRootObj = createObject( aRootObj, tr( partitionName( KIND_POLYLINE ).toAscii() ) );
254
255   // PROFILES
256   LightApp_DataObject* aProfileRootObj = createObject( aRootObj, tr( partitionName( KIND_PROFILE ).toAscii() ) );
257
258   // VISUAL STATES
259   LightApp_DataObject* aVisualStateRootObj = createObject( aRootObj, tr( partitionName( KIND_VISUAL_STATE ).toAscii() ) );
260
261   HYDROData_Iterator anIterator( aDocument, KIND_UNKNOWN );
262   for( ; anIterator.More(); anIterator.Next() ) {
263     Handle(HYDROData_Entity) anObj = anIterator.Current();
264
265     switch ( anObj->GetKind() ) {
266       case KIND_IMAGE:
267       {
268         Handle(HYDROData_Image) anImageObj =
269           Handle(HYDROData_Image)::DownCast( anObj );
270         if( !anImageObj.IsNull() ) {
271           createObject( anImageRootObj, anImageObj );
272         }
273
274         break;
275       }
276       case KIND_BATHYMETRY:
277       {
278         Handle(HYDROData_Bathymetry) aBathymetryObj =
279           Handle(HYDROData_Bathymetry)::DownCast( anObj );
280         if( !aBathymetryObj.IsNull() ) {
281           createObject( aBathymetryRootObj, aBathymetryObj );
282         }
283
284         break;
285       }
286       case KIND_CHANNEL:
287       {
288         Handle(HYDROData_Channel) aChannelObj =
289           Handle(HYDROData_Channel)::DownCast( anObj );
290         if( !aChannelObj.IsNull() ) {
291           createObject( anArtificialObjectsRootObj, aChannelObj );
292         }
293
294         break;
295       }
296       case KIND_DIGUE:
297       {
298         Handle(HYDROData_Digue) aDigueObj =
299           Handle(HYDROData_Digue)::DownCast( anObj );
300         if( !aDigueObj.IsNull() ) {
301           createObject( anArtificialObjectsRootObj, aDigueObj );
302         }
303
304         break;
305       }
306       case KIND_IMMERSIBLE_ZONE:
307       {
308         Handle(HYDROData_ImmersibleZone) anImmersibleZoneObj =
309           Handle(HYDROData_ImmersibleZone)::DownCast( anObj );
310         if( !anImmersibleZoneObj.IsNull() ) {
311           createObject( aNaturalObjectsRootObj, anImmersibleZoneObj );
312         }
313
314         break;
315       }
316       case KIND_RIVER:
317       {
318         Handle(HYDROData_River) aRiverObj =
319           Handle(HYDROData_River)::DownCast( anObj );
320         if( !aRiverObj.IsNull() ) {
321           createObject( aNaturalObjectsRootObj, aRiverObj );
322         }
323
324         break;
325       }
326       case KIND_OBSTACLE:
327       {
328         Handle(HYDROData_Obstacle) anObstacleObj =
329           Handle(HYDROData_Obstacle)::DownCast( anObj );
330         if( !anObstacleObj.IsNull() ) {
331           createObject( anObstaclesRootObj, anObstacleObj );
332         }
333
334         break;
335       }
336       case KIND_CALCULATION:
337       {
338         Handle(HYDROData_CalculationCase) aCalculObj =
339           Handle(HYDROData_CalculationCase)::DownCast( anObj );
340         if( !aCalculObj.IsNull() ) {
341           createObject( aCalculRootObj, aCalculObj );
342         }
343
344         break;
345       }
346       case KIND_POLYLINE:
347       {
348         Handle(HYDROData_Polyline) aPolylineObj =
349           Handle(HYDROData_Polyline)::DownCast( anObj );
350         if( !aPolylineObj.IsNull() ) {
351           createObject( aPolylineRootObj, aPolylineObj );
352         }
353
354         break;
355       }
356       case KIND_PROFILE:
357       {
358         Handle(HYDROData_Profile) aProfileObj =
359           Handle(HYDROData_Profile)::DownCast( anObj );
360         if( !aProfileObj.IsNull() ) {
361           createObject( aProfileRootObj, aProfileObj );
362         }
363
364         break;
365       }
366       case KIND_VISUAL_STATE:
367       {
368         Handle(HYDROData_VisualState) aVisualStateObj =
369           Handle(HYDROData_VisualState)::DownCast( anObj );
370         if( !aVisualStateObj.IsNull() ) {
371           createObject( aVisualStateRootObj, aVisualStateObj );
372         }
373
374         break;
375       }
376     }
377   }
378
379   if( SUIT_DataBrowser* anObjectBrowser = anApp->objectBrowser() )
380   {
381     anObjectBrowser->setAutoOpenLevel( 3 );
382     anObjectBrowser->openLevels();
383   }
384 }
385
386 HYDROGUI_DataObject* HYDROGUI_DataModel::getDataObject( const Handle(HYDROData_Entity)& theModelObject )
387 {
388   return NULL; // to do if necessary
389 }
390
391 HYDROGUI_DataObject* HYDROGUI_DataModel::getReferencedDataObject( HYDROGUI_DataObject* theObject )
392 {
393   return NULL; // to do if necessary
394 }
395
396 SUIT_DataObject* HYDROGUI_DataModel::findObject( const QString& theEntry ) const
397 {
398   LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
399   return anApp ? anApp->findObject( theEntry ) : 0;
400 }
401
402 void HYDROGUI_DataModel::update( LightApp_DataObject* theObject,
403                                  LightApp_Study* theStudy )
404 {
405   if( !theStudy )
406     theStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy()) ;
407   if( theStudy )
408     update( theStudy->id() );
409 }
410
411 CAM_DataObject* HYDROGUI_DataModel::createRootModuleObject( SUIT_DataObject* theParent )
412 {
413   CAM_DataObject* aRootObj = createModuleObject( theParent );
414   setRoot( aRootObj );
415   return aRootObj;
416 }
417
418 void HYDROGUI_DataModel::updateModel()
419 {
420   HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
421   if( aModule )
422     update( aModule->getStudyId() );
423 }
424
425 Handle(HYDROData_Entity) HYDROGUI_DataModel::objectByEntry( const QString& theEntry,
426                                                             const ObjectKind theObjectKind )
427 {
428   QString anEntry = theEntry;
429   if( anEntry.indexOf( "_" ) != -1 ) // reference object
430     anEntry = anEntry.section( "_", -1 );
431
432   Handle(HYDROData_Document) aDocument = getDocument();
433   if( !aDocument.IsNull() )
434   {
435     HYDROData_Iterator anIterator( aDocument, theObjectKind );
436     for( ; anIterator.More(); anIterator.Next() )
437     {
438       Handle(HYDROData_Entity) anObject = anIterator.Current();
439       if( !anObject.IsNull() )
440       {
441         QString anEntryRef = HYDROGUI_DataObject::dataObjectEntry( anObject );
442         if( anEntryRef == anEntry )
443           return anObject;
444       }
445     }
446   }
447   return NULL;
448 }
449
450 bool HYDROGUI_DataModel::canUndo() const
451 {
452   return getDocument()->CanUndo();
453 }
454
455 bool HYDROGUI_DataModel::canRedo() const
456 {
457   return getDocument()->CanRedo();
458 }
459
460 QStringList HYDROGUI_DataModel::undoNames() const
461 {
462   QStringList aNames;
463   for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetUndos() ); anIter.More(); anIter.Next() )
464     aNames.prepend( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
465   return aNames;
466 }
467
468 QStringList HYDROGUI_DataModel::redoNames() const
469 {
470   QStringList aNames;
471   for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetRedos() ); anIter.More(); anIter.Next() )
472     aNames.append( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
473   return aNames;
474 }
475
476 void HYDROGUI_DataModel::clearUndos()
477 {
478   getDocument()->ClearUndos();
479 }
480
481 void HYDROGUI_DataModel::clearRedos()
482 {
483   getDocument()->ClearRedos();
484 }
485
486 bool HYDROGUI_DataModel::undo()
487 {
488   try 
489   {
490     getDocument()->Undo();
491   }
492   catch ( Standard_Failure )
493   {
494     return false;
495   }
496   return true;
497 }
498
499 bool HYDROGUI_DataModel::redo()
500 {
501   try 
502   {
503     getDocument()->Redo();
504   }
505   catch ( Standard_Failure )
506   {
507     return false;
508   }
509   return true;
510 }
511
512 bool HYDROGUI_DataModel::canCopy()
513 {
514   HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( (HYDROGUI_Module*)module() );
515   if( aSeq.Length() != 1 )
516     return false;
517
518   Handle(HYDROData_Entity) anObject = aSeq.First();
519   if( anObject.IsNull() )
520     return false;
521
522   ObjectKind aKind = anObject->GetKind();
523   if( aKind == KIND_IMAGE ||
524       aKind == KIND_POLYLINE ||
525       aKind == KIND_PROFILE ||
526       aKind == KIND_CALCULATION )
527     return true;
528
529   return false;
530 }
531
532 bool HYDROGUI_DataModel::canPaste()
533 {
534   for( int anIndex = 1, aLength = myCopyingObjects.Length(); anIndex <= aLength; anIndex++ )
535   {
536     Handle(HYDROData_Entity) anObject = myCopyingObjects.Value( anIndex );
537     if( !anObject.IsNull() && !anObject->IsRemoved() )
538       return true;
539   }
540   return false;
541 }
542
543 bool HYDROGUI_DataModel::copy()
544 {
545   HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( (HYDROGUI_Module*)module() );
546   changeCopyingObjects( aSeq );
547   return true;
548 }
549
550 bool HYDROGUI_DataModel::paste()
551 {
552   bool anIsChanged = false;
553   for( int anIndex = 1, aLength = myCopyingObjects.Length(); anIndex <= aLength; anIndex++ )
554   {
555     Handle(HYDROData_Entity) anObject = myCopyingObjects.Value( anIndex );
556     if( !anObject.IsNull() && !anObject->IsRemoved() )
557     {
558       ObjectKind aKind = anObject->GetKind();
559       Handle(HYDROData_Entity) aClone = getDocument()->CreateObject( aKind );
560       if( !aClone.IsNull() )
561       {
562         anObject->CopyTo( aClone );
563         anIsChanged = true;
564
565         // generate a new unique name for the clone object:
566         // case 1: Image_1 -> Image_2
567         // case 2: ImageObj -> ImageObj_1
568         QString aName = aClone->GetName();
569         QString aPrefix = aName;
570         if( aName.contains( '_' ) ) // case 1
571         {
572           QString aSuffix = aName.section( '_', -1 );
573           bool anIsInteger = false;
574           aSuffix.toInt( &anIsInteger );
575           if( anIsInteger )
576             aPrefix = aName.section( '_', 0, -2 );
577         }
578         else // case 2
579           aPrefix = aName;
580         aName = HYDROGUI_Tool::GenerateObjectName( (HYDROGUI_Module*)module(), aPrefix );
581         aClone->SetName( aName );
582       }
583     }
584   }
585   return anIsChanged;
586 }
587
588 void HYDROGUI_DataModel::changeCopyingObjects( const HYDROData_SequenceOfObjects& theSeq )
589 {
590   myCopyingObjects.Assign( theSeq );
591 }
592
593 QString HYDROGUI_DataModel::partitionName( const ObjectKind theObjectKind )
594 {
595   switch( theObjectKind )
596   {
597     case KIND_IMAGE:             return "IMAGES";
598     case KIND_POLYLINE:          return "POLYLINES";
599     case KIND_PROFILE:           return "PROFILES";
600     case KIND_VISUAL_STATE:      return "VISUAL_STATES";
601     case KIND_BATHYMETRY:        return "BATHYMETRIES";
602     case KIND_CALCULATION:       return "CALCULATION_CASES";
603     case KIND_OBSTACLE:          return "OBSTACLES";
604     case KIND_ARTIFICIAL_OBJECT: return "ARTIFICIAL_OBJECTS";
605     case KIND_NATURAL_OBJECT:    return "NATURAL_OBJECTS";
606     default: break;
607   }
608   return QString();
609 }
610
611 Handle(HYDROData_Document) HYDROGUI_DataModel::getDocument() const
612 {
613   int aStudyId = module()->application()->activeStudy()->id();
614   return HYDROData_Document::Document( aStudyId );
615 }
616
617 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject*         theParent,
618                                                        Handle(HYDROData_Entity) theModelObject,
619                                                        const QString&           theParentEntry,
620                                                        const bool               theIsBuildTree )
621 {
622   HYDROGUI_DataObject* aResObj = new HYDROGUI_DataObject( theParent, theModelObject, theParentEntry );
623   
624   if ( theIsBuildTree )
625   {
626     buildObjectTree( theParent, aResObj, theParentEntry );
627   }
628
629   return aResObj;
630 }
631
632 LightApp_DataObject* HYDROGUI_DataModel::buildObject( SUIT_DataObject*     theParent,
633                                                       HYDROGUI_DataObject* theObject,
634                                                       const QString&       theParentEntry,
635                                                       const bool           theIsBuildTree )
636 {
637   if ( theIsBuildTree )
638   {
639     buildObjectTree( theParent, theObject, theParentEntry );
640   }
641   return theObject;
642 }
643
644 LightApp_DataObject* HYDROGUI_DataModel::createZone( SUIT_DataObject*       theParent,
645                                                      Handle(HYDROData_Zone) theModelObject,
646                                                      const QString&         theParentEntry,
647                                                      const bool             theIsBuildTree )
648 {
649   return buildObject( theParent, new HYDROGUI_Zone( theParent, theModelObject, theParentEntry ), theParentEntry, theIsBuildTree );
650 }
651
652 LightApp_DataObject* HYDROGUI_DataModel::createRegion( SUIT_DataObject*         theParent,
653                                                        Handle(HYDROData_Region) theModelObject,
654                                                        const QString&           theParentEntry,
655                                                        const bool               theIsBuildTree )
656 {
657   return buildObject( theParent, new HYDROGUI_Region( theParent, theModelObject, theParentEntry ), theParentEntry, theIsBuildTree );
658 }
659
660 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
661                                                        const QString&   theName,
662                                                        const QString&   theParentEntry )
663 {
664   return new HYDROGUI_NamedObject( theParent, theName, theParentEntry );
665 }
666
667 void HYDROGUI_DataModel::buildObjectTree( SUIT_DataObject* theParent,
668                                           SUIT_DataObject* theObject,
669                                           const QString&   theParentEntry )
670 {
671   HYDROGUI_DataObject* aGuiObj = dynamic_cast<HYDROGUI_DataObject*>( theObject );
672   if ( !aGuiObj )
673     return;
674
675   Handle(HYDROData_Entity) aDataObj = aGuiObj->modelObject();
676   if ( aDataObj.IsNull() )
677     return;
678
679   ObjectKind anObjectKind = aDataObj->GetKind();
680
681   if ( anObjectKind == KIND_IMAGE )
682   {
683     Handle(HYDROData_Image) anImageObj =
684       Handle(HYDROData_Image)::DownCast( aDataObj );
685     for ( int anIndex = 0, aNbRef = anImageObj->NbReferences(); anIndex < aNbRef; anIndex++ )
686     {
687       Handle(HYDROData_Entity) aRefObj = anImageObj->Reference( anIndex );
688       if ( !aRefObj.IsNull() && !aRefObj->IsRemoved() )
689         createObject( aGuiObj, aRefObj, aGuiObj->entry(), false );
690     }
691   }
692   else if ( anObjectKind == KIND_IMMERSIBLE_ZONE )
693   {
694     Handle(HYDROData_ImmersibleZone) aZoneObj =
695       Handle(HYDROData_ImmersibleZone)::DownCast( aDataObj );
696
697     LightApp_DataObject* aPolylineSect = 
698       createObject( aGuiObj, tr( "ZONE_POLYLINE" ), aGuiObj->entry() );
699
700     Handle(HYDROData_Polyline) aPolyline = aZoneObj->GetPolyline();
701     if ( !aPolyline.IsNull() && !aPolyline->IsRemoved() )
702       createObject( aPolylineSect, aPolyline, aGuiObj->entry(), false );
703
704     LightApp_DataObject* aBathSect = 
705       createObject( aGuiObj, tr( "ZONE_BATHYMETRY" ), aGuiObj->entry() );
706
707     Handle(HYDROData_Bathymetry) aBathymetry = aZoneObj->GetBathymetry();
708     if ( !aBathymetry.IsNull() && !aBathymetry->IsRemoved() )
709       createObject( aBathSect, aBathymetry, aGuiObj->entry(), false );
710   }
711   else if ( anObjectKind == KIND_CALCULATION )
712   {
713     Handle(HYDROData_CalculationCase) aCaseObj =
714       Handle(HYDROData_CalculationCase)::DownCast( aDataObj );
715
716     LightApp_DataObject* aCaseRegionsSect = 
717       createObject( aGuiObj, tr( "CASE_REGIONS" ), aGuiObj->entry() );
718
719     HYDROData_SequenceOfObjects aCaseRegions = aCaseObj->GetRegions();
720     HYDROData_SequenceOfObjects::Iterator anIter( aCaseRegions );
721     for ( ; anIter.More(); anIter.Next() )
722     {
723       Handle(HYDROData_Region) aCaseRegion =
724         Handle(HYDROData_Region)::DownCast( anIter.Value() );
725       if( !aCaseRegion.IsNull() && !aCaseRegion->IsRemoved() )
726         createRegion( aCaseRegionsSect, aCaseRegion, "", true );
727     }
728   }
729   else if ( anObjectKind == KIND_REGION )
730   {
731     Handle(HYDROData_Region) aRegionObj =
732       Handle(HYDROData_Region)::DownCast( aDataObj );
733
734     HYDROData_SequenceOfObjects aRegionZones = aRegionObj->GetZones();
735     HYDROData_SequenceOfObjects::Iterator anIter( aRegionZones );
736     for ( ; anIter.More(); anIter.Next() )
737     {
738       Handle(HYDROData_Zone) aRegionZone =
739         Handle(HYDROData_Zone)::DownCast( anIter.Value() );
740       if( !aRegionZone.IsNull() && !aRegionZone->IsRemoved() )
741         createZone( aGuiObj, aRegionZone, "", true );
742     }
743   }
744 }
745
746 void HYDROGUI_DataModel::buildCaseTree( SUIT_DataObject* theParent, Handle(HYDROData_CalculationCase) theCase )
747 {
748   if ( !theCase.IsNull() )
749   {
750     new HYDROGUI_DropTargetObject( theParent, tr( "NEW_REGION" ), "" );
751
752     HYDROData_SequenceOfObjects aCaseRegions = theCase->GetRegions();
753     HYDROData_SequenceOfObjects::Iterator anIter( aCaseRegions );
754     for ( ; anIter.More(); anIter.Next() )
755     {
756       Handle(HYDROData_Region) aCaseRegion =
757         Handle(HYDROData_Region)::DownCast( anIter.Value() );
758       if( !aCaseRegion.IsNull() && !aCaseRegion->IsRemoved() )
759         createRegion( theParent, aCaseRegion, "", true );
760     }
761   }
762 }
763
764 void HYDROGUI_DataModel::removeChild( SUIT_DataObject* theParent,
765                                       SUIT_DataObject* theChild )
766 {
767   SUIT_DataObject* aSubChild = theChild->firstChild();
768   for( ; aSubChild; aSubChild = aSubChild->nextBrother() )
769     removeChild( theChild, aSubChild );
770   theParent->removeChild( theChild );
771 }
772
773 SUIT_DataObject* HYDROGUI_DataModel::findChildByName( const SUIT_DataObject* theFather,
774                                                       const QString& theName )
775 {
776   SUIT_DataObject* aChild = theFather->firstChild();
777   while( aChild )
778   {
779     if( aChild->name() == theName )
780       return aChild; // found
781     aChild = aChild->nextBrother();
782   }
783   return NULL; // not found
784 }
785
786 bool HYDROGUI_DataModel::createNewRegion( Handle(HYDROData_CalculationCase) theCase, 
787                                          const QList<HYDROGUI_Zone*>& theZonesList )
788 {
789   bool isOk = !theCase.IsNull();
790   if ( isOk )
791   {
792     Handle(HYDROData_Region) aRegion;
793     Handle(HYDROData_Zone) aZone;
794     for (int i = 0; i < theZonesList.length(); i++ )
795     {
796       aZone = Handle(HYDROData_Zone)::DownCast( theZonesList.at(i)->modelObject() );
797       if ( !aZone.IsNull() )
798       {
799         if ( aRegion.IsNull() )
800         {
801           aRegion = theCase->AddNewRegion( aZone );
802           isOk = !aRegion.IsNull();
803         }
804         else
805         {
806           if ( !( aRegion->AddZone( aZone ) ) )
807           {
808             isOk = false;
809           }
810         }
811       }
812     }
813   }
814   return isOk;
815 }