Salome HOME
Profile dialog backgound like it is in OCC viewer.
[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_PolylineXY.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_POLYLINEXY ).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     if ( !anObj.IsNull() )
266     {
267       switch ( anObj->GetKind() ) {
268         case KIND_IMAGE:
269         {
270           Handle(HYDROData_Image) anImageObj =
271             Handle(HYDROData_Image)::DownCast( anObj );
272           if( !anImageObj.IsNull() ) {
273             createObject( anImageRootObj, anImageObj );
274           }
275
276           break;
277         }
278         case KIND_BATHYMETRY:
279         {
280           Handle(HYDROData_Bathymetry) aBathymetryObj =
281             Handle(HYDROData_Bathymetry)::DownCast( anObj );
282           if( !aBathymetryObj.IsNull() ) {
283             createObject( aBathymetryRootObj, aBathymetryObj );
284           }
285
286           break;
287         }
288         case KIND_CHANNEL:
289         {
290           Handle(HYDROData_Channel) aChannelObj =
291             Handle(HYDROData_Channel)::DownCast( anObj );
292           if( !aChannelObj.IsNull() ) {
293             createObject( anArtificialObjectsRootObj, aChannelObj );
294           }
295
296           break;
297         }
298         case KIND_DIGUE:
299         {
300           Handle(HYDROData_Digue) aDigueObj =
301             Handle(HYDROData_Digue)::DownCast( anObj );
302           if( !aDigueObj.IsNull() ) {
303             createObject( anArtificialObjectsRootObj, aDigueObj );
304           }
305
306           break;
307         }
308         case KIND_IMMERSIBLE_ZONE:
309         {
310           Handle(HYDROData_ImmersibleZone) anImmersibleZoneObj =
311             Handle(HYDROData_ImmersibleZone)::DownCast( anObj );
312           if( !anImmersibleZoneObj.IsNull() ) {
313             createObject( aNaturalObjectsRootObj, anImmersibleZoneObj );
314           }
315
316           break;
317         }
318         case KIND_RIVER:
319         {
320           Handle(HYDROData_River) aRiverObj =
321             Handle(HYDROData_River)::DownCast( anObj );
322           if( !aRiverObj.IsNull() ) {
323             createObject( aNaturalObjectsRootObj, aRiverObj );
324           }
325
326           break;
327         }
328         case KIND_OBSTACLE:
329         {
330           Handle(HYDROData_Obstacle) anObstacleObj =
331             Handle(HYDROData_Obstacle)::DownCast( anObj );
332           if( !anObstacleObj.IsNull() ) {
333             createObject( anObstaclesRootObj, anObstacleObj );
334           }
335
336           break;
337         }
338         case KIND_CALCULATION:
339         {
340           Handle(HYDROData_CalculationCase) aCalculObj =
341             Handle(HYDROData_CalculationCase)::DownCast( anObj );
342           if( !aCalculObj.IsNull() ) {
343             createObject( aCalculRootObj, aCalculObj );
344           }
345
346           break;
347         }
348         case KIND_POLYLINEXY:
349         {
350           Handle(HYDROData_PolylineXY) aPolylineObj =
351             Handle(HYDROData_PolylineXY)::DownCast( anObj );
352           if( !aPolylineObj.IsNull() ) {
353             createObject( aPolylineRootObj, aPolylineObj );
354           }
355
356           break;
357         }
358         case KIND_PROFILE:
359         {
360           Handle(HYDROData_Profile) aProfileObj =
361             Handle(HYDROData_Profile)::DownCast( anObj );
362           if( !aProfileObj.IsNull() ) {
363             createObject( aProfileRootObj, aProfileObj );
364           }
365
366           break;
367         }
368         case KIND_VISUAL_STATE:
369         {
370           Handle(HYDROData_VisualState) aVisualStateObj =
371             Handle(HYDROData_VisualState)::DownCast( anObj );
372           if( !aVisualStateObj.IsNull() ) {
373             createObject( aVisualStateRootObj, aVisualStateObj );
374           }
375
376           break;
377         }
378       }
379     }
380   }
381
382   if( SUIT_DataBrowser* anObjectBrowser = anApp->objectBrowser() )
383   {
384     anObjectBrowser->setAutoOpenLevel( 3 );
385     anObjectBrowser->openLevels();
386   }
387 }
388
389 HYDROGUI_DataObject* HYDROGUI_DataModel::getDataObject( const Handle(HYDROData_Entity)& theModelObject )
390 {
391   return NULL; // to do if necessary
392 }
393
394 HYDROGUI_DataObject* HYDROGUI_DataModel::getReferencedDataObject( HYDROGUI_DataObject* theObject )
395 {
396   return NULL; // to do if necessary
397 }
398
399 SUIT_DataObject* HYDROGUI_DataModel::findObject( const QString& theEntry ) const
400 {
401   LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
402   return anApp ? anApp->findObject( theEntry ) : 0;
403 }
404
405 void HYDROGUI_DataModel::update( LightApp_DataObject* theObject,
406                                  LightApp_Study* theStudy )
407 {
408   if( !theStudy )
409     theStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy()) ;
410   if( theStudy )
411     update( theStudy->id() );
412 }
413
414 CAM_DataObject* HYDROGUI_DataModel::createRootModuleObject( SUIT_DataObject* theParent )
415 {
416   CAM_DataObject* aRootObj = createModuleObject( theParent );
417   setRoot( aRootObj );
418   return aRootObj;
419 }
420
421 void HYDROGUI_DataModel::updateModel()
422 {
423   HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
424   if( aModule )
425     update( aModule->getStudyId() );
426 }
427
428 Handle(HYDROData_Entity) HYDROGUI_DataModel::objectByEntry( const QString& theEntry,
429                                                             const ObjectKind theObjectKind )
430 {
431   QString anEntry = theEntry;
432   if( anEntry.indexOf( "_" ) != -1 ) // reference object
433     anEntry = anEntry.section( "_", -1 );
434
435   Handle(HYDROData_Document) aDocument = getDocument();
436   if( !aDocument.IsNull() )
437   {
438     HYDROData_Iterator anIterator( aDocument, theObjectKind );
439     for( ; anIterator.More(); anIterator.Next() )
440     {
441       Handle(HYDROData_Entity) anObject = anIterator.Current();
442       if( !anObject.IsNull() )
443       {
444         QString anEntryRef = HYDROGUI_DataObject::dataObjectEntry( anObject );
445         if( anEntryRef == anEntry )
446           return anObject;
447       }
448     }
449   }
450   return NULL;
451 }
452
453 bool HYDROGUI_DataModel::canUndo() const
454 {
455   return getDocument()->CanUndo();
456 }
457
458 bool HYDROGUI_DataModel::canRedo() const
459 {
460   return getDocument()->CanRedo();
461 }
462
463 QStringList HYDROGUI_DataModel::undoNames() const
464 {
465   QStringList aNames;
466   for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetUndos() ); anIter.More(); anIter.Next() )
467     aNames.prepend( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
468   return aNames;
469 }
470
471 QStringList HYDROGUI_DataModel::redoNames() const
472 {
473   QStringList aNames;
474   for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetRedos() ); anIter.More(); anIter.Next() )
475     aNames.append( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
476   return aNames;
477 }
478
479 void HYDROGUI_DataModel::clearUndos()
480 {
481   getDocument()->ClearUndos();
482 }
483
484 void HYDROGUI_DataModel::clearRedos()
485 {
486   getDocument()->ClearRedos();
487 }
488
489 bool HYDROGUI_DataModel::undo()
490 {
491   try 
492   {
493     getDocument()->Undo();
494   }
495   catch ( Standard_Failure )
496   {
497     return false;
498   }
499   return true;
500 }
501
502 bool HYDROGUI_DataModel::redo()
503 {
504   try 
505   {
506     getDocument()->Redo();
507   }
508   catch ( Standard_Failure )
509   {
510     return false;
511   }
512   return true;
513 }
514
515 bool HYDROGUI_DataModel::canCopy()
516 {
517   HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( (HYDROGUI_Module*)module() );
518   if( aSeq.Length() != 1 )
519     return false;
520
521   Handle(HYDROData_Entity) anObject = aSeq.First();
522   if( anObject.IsNull() )
523     return false;
524
525   ObjectKind aKind = anObject->GetKind();
526   if( aKind == KIND_IMAGE ||
527       aKind == KIND_POLYLINE ||
528       aKind == KIND_PROFILE ||
529       aKind == KIND_CALCULATION )
530     return true;
531
532   return false;
533 }
534
535 bool HYDROGUI_DataModel::canPaste()
536 {
537   for( int anIndex = 1, aLength = myCopyingObjects.Length(); anIndex <= aLength; anIndex++ )
538   {
539     Handle(HYDROData_Entity) anObject = myCopyingObjects.Value( anIndex );
540     if( !anObject.IsNull() && !anObject->IsRemoved() )
541       return true;
542   }
543   return false;
544 }
545
546 bool HYDROGUI_DataModel::copy()
547 {
548   HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( (HYDROGUI_Module*)module() );
549   changeCopyingObjects( aSeq );
550   return true;
551 }
552
553 bool HYDROGUI_DataModel::paste()
554 {
555   bool anIsChanged = false;
556   for( int anIndex = 1, aLength = myCopyingObjects.Length(); anIndex <= aLength; anIndex++ )
557   {
558     Handle(HYDROData_Entity) anObject = myCopyingObjects.Value( anIndex );
559     if( !anObject.IsNull() && !anObject->IsRemoved() )
560     {
561       ObjectKind aKind = anObject->GetKind();
562       Handle(HYDROData_Entity) aClone = getDocument()->CreateObject( aKind );
563       if( !aClone.IsNull() )
564       {
565         anObject->CopyTo( aClone );
566         anIsChanged = true;
567
568         // generate a new unique name for the clone object:
569         // case 1: Image_1 -> Image_2
570         // case 2: ImageObj -> ImageObj_1
571         QString aName = aClone->GetName();
572         QString aPrefix = aName;
573         if( aName.contains( '_' ) ) // case 1
574         {
575           QString aSuffix = aName.section( '_', -1 );
576           bool anIsInteger = false;
577           aSuffix.toInt( &anIsInteger );
578           if( anIsInteger )
579             aPrefix = aName.section( '_', 0, -2 );
580         }
581         else // case 2
582           aPrefix = aName;
583         aName = HYDROGUI_Tool::GenerateObjectName( (HYDROGUI_Module*)module(), aPrefix );
584         aClone->SetName( aName );
585       }
586     }
587   }
588   return anIsChanged;
589 }
590
591 void HYDROGUI_DataModel::changeCopyingObjects( const HYDROData_SequenceOfObjects& theSeq )
592 {
593   myCopyingObjects.Assign( theSeq );
594 }
595
596 QString HYDROGUI_DataModel::partitionName( const ObjectKind theObjectKind )
597 {
598   switch( theObjectKind )
599   {
600     case KIND_IMAGE:             return "IMAGES";
601     case KIND_POLYLINEXY:        return "POLYLINES";
602     case KIND_PROFILE:           return "PROFILES";
603     case KIND_VISUAL_STATE:      return "VISUAL_STATES";
604     case KIND_BATHYMETRY:        return "BATHYMETRIES";
605     case KIND_CALCULATION:       return "CALCULATION_CASES";
606     case KIND_OBSTACLE:          return "OBSTACLES";
607     case KIND_ARTIFICIAL_OBJECT: return "ARTIFICIAL_OBJECTS";
608     case KIND_NATURAL_OBJECT:    return "NATURAL_OBJECTS";
609     default: break;
610   }
611   return QString();
612 }
613
614 Handle(HYDROData_Document) HYDROGUI_DataModel::getDocument() const
615 {
616   int aStudyId = module()->application()->activeStudy()->id();
617   return HYDROData_Document::Document( aStudyId );
618 }
619
620 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject*         theParent,
621                                                        Handle(HYDROData_Entity) theModelObject,
622                                                        const QString&           theParentEntry,
623                                                        const bool               theIsBuildTree )
624 {
625   HYDROGUI_DataObject* aResObj = new HYDROGUI_DataObject( theParent, theModelObject, theParentEntry );
626   
627   if ( theIsBuildTree )
628   {
629     buildObjectTree( theParent, aResObj, theParentEntry );
630   }
631
632   return aResObj;
633 }
634
635 LightApp_DataObject* HYDROGUI_DataModel::buildObject( SUIT_DataObject*     theParent,
636                                                       HYDROGUI_DataObject* theObject,
637                                                       const QString&       theParentEntry,
638                                                       const bool           theIsBuildTree )
639 {
640   if ( theIsBuildTree )
641   {
642     buildObjectTree( theParent, theObject, theParentEntry );
643   }
644   return theObject;
645 }
646
647 LightApp_DataObject* HYDROGUI_DataModel::createZone( SUIT_DataObject*       theParent,
648                                                      Handle(HYDROData_Zone) theModelObject,
649                                                      const QString&         theParentEntry,
650                                                      const bool             theIsBuildTree )
651 {
652   return buildObject( theParent, new HYDROGUI_Zone( theParent, theModelObject, theParentEntry ), theParentEntry, theIsBuildTree );
653 }
654
655 LightApp_DataObject* HYDROGUI_DataModel::createRegion( SUIT_DataObject*         theParent,
656                                                        Handle(HYDROData_Region) theModelObject,
657                                                        const QString&           theParentEntry,
658                                                        const bool               theIsBuildTree )
659 {
660   return buildObject( theParent, new HYDROGUI_Region( theParent, theModelObject, theParentEntry ), theParentEntry, theIsBuildTree );
661 }
662
663 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
664                                                        const QString&   theName,
665                                                        const QString&   theParentEntry )
666 {
667   return new HYDROGUI_NamedObject( theParent, theName, theParentEntry );
668 }
669
670 void HYDROGUI_DataModel::buildObjectTree( SUIT_DataObject* theParent,
671                                           SUIT_DataObject* theObject,
672                                           const QString&   theParentEntry )
673 {
674   HYDROGUI_DataObject* aGuiObj = dynamic_cast<HYDROGUI_DataObject*>( theObject );
675   if ( !aGuiObj )
676     return;
677
678   Handle(HYDROData_Entity) aDataObj = aGuiObj->modelObject();
679   if ( aDataObj.IsNull() )
680     return;
681
682   ObjectKind anObjectKind = aDataObj->GetKind();
683
684   if ( anObjectKind == KIND_IMAGE )
685   {
686     Handle(HYDROData_Image) anImageObj =
687       Handle(HYDROData_Image)::DownCast( aDataObj );
688     for ( int anIndex = 0, aNbRef = anImageObj->NbReferences(); anIndex < aNbRef; anIndex++ )
689     {
690       Handle(HYDROData_Entity) aRefObj = anImageObj->Reference( anIndex );
691       if ( !aRefObj.IsNull() && !aRefObj->IsRemoved() )
692         createObject( aGuiObj, aRefObj, aGuiObj->entry(), false );
693     }
694   }
695   else if ( anObjectKind == KIND_IMMERSIBLE_ZONE )
696   {
697     Handle(HYDROData_ImmersibleZone) aZoneObj =
698       Handle(HYDROData_ImmersibleZone)::DownCast( aDataObj );
699
700     LightApp_DataObject* aPolylineSect = 
701       createObject( aGuiObj, tr( "ZONE_POLYLINE" ), aGuiObj->entry() );
702
703     Handle(HYDROData_PolylineXY) aPolyline = aZoneObj->GetPolyline();
704     if ( !aPolyline.IsNull() && !aPolyline->IsRemoved() )
705       createObject( aPolylineSect, aPolyline, aGuiObj->entry(), false );
706
707     LightApp_DataObject* aBathSect = 
708       createObject( aGuiObj, tr( "ZONE_BATHYMETRY" ), aGuiObj->entry() );
709
710     Handle(HYDROData_Bathymetry) aBathymetry = aZoneObj->GetBathymetry();
711     if ( !aBathymetry.IsNull() && !aBathymetry->IsRemoved() )
712       createObject( aBathSect, aBathymetry, aGuiObj->entry(), false );
713   }
714   else if ( anObjectKind == KIND_CALCULATION )
715   {
716     Handle(HYDROData_CalculationCase) aCaseObj =
717       Handle(HYDROData_CalculationCase)::DownCast( aDataObj );
718
719     LightApp_DataObject* aCaseRegionsSect = 
720       createObject( aGuiObj, tr( "CASE_REGIONS" ), aGuiObj->entry() );
721
722     HYDROData_SequenceOfObjects aCaseRegions = aCaseObj->GetRegions();
723     HYDROData_SequenceOfObjects::Iterator anIter( aCaseRegions );
724     for ( ; anIter.More(); anIter.Next() )
725     {
726       Handle(HYDROData_Region) aCaseRegion =
727         Handle(HYDROData_Region)::DownCast( anIter.Value() );
728       if( !aCaseRegion.IsNull() && !aCaseRegion->IsRemoved() )
729         createRegion( aCaseRegionsSect, aCaseRegion, "", true );
730     }
731   }
732   else if ( anObjectKind == KIND_REGION )
733   {
734     Handle(HYDROData_Region) aRegionObj =
735       Handle(HYDROData_Region)::DownCast( aDataObj );
736
737     HYDROData_SequenceOfObjects aRegionZones = aRegionObj->GetZones();
738     HYDROData_SequenceOfObjects::Iterator anIter( aRegionZones );
739     for ( ; anIter.More(); anIter.Next() )
740     {
741       Handle(HYDROData_Zone) aRegionZone =
742         Handle(HYDROData_Zone)::DownCast( anIter.Value() );
743       if( !aRegionZone.IsNull() && !aRegionZone->IsRemoved() )
744         createZone( aGuiObj, aRegionZone, "", true );
745     }
746   }
747 }
748
749 void HYDROGUI_DataModel::buildCaseTree( SUIT_DataObject* theParent, Handle(HYDROData_CalculationCase) theCase )
750 {
751   if ( !theCase.IsNull() )
752   {
753     new HYDROGUI_DropTargetObject( theParent, tr( "NEW_REGION" ), "" );
754
755     HYDROData_SequenceOfObjects aCaseRegions = theCase->GetRegions();
756     HYDROData_SequenceOfObjects::Iterator anIter( aCaseRegions );
757     for ( ; anIter.More(); anIter.Next() )
758     {
759       Handle(HYDROData_Region) aCaseRegion =
760         Handle(HYDROData_Region)::DownCast( anIter.Value() );
761       if( !aCaseRegion.IsNull() && !aCaseRegion->IsRemoved() )
762         createRegion( theParent, aCaseRegion, "", true );
763     }
764   }
765 }
766
767 void HYDROGUI_DataModel::removeChild( SUIT_DataObject* theParent,
768                                       SUIT_DataObject* theChild )
769 {
770   SUIT_DataObject* aSubChild = theChild->firstChild();
771   for( ; aSubChild; aSubChild = aSubChild->nextBrother() )
772     removeChild( theChild, aSubChild );
773   theParent->removeChild( theChild );
774 }
775
776 SUIT_DataObject* HYDROGUI_DataModel::findChildByName( const SUIT_DataObject* theFather,
777                                                       const QString& theName )
778 {
779   SUIT_DataObject* aChild = theFather->firstChild();
780   while( aChild )
781   {
782     if( aChild->name() == theName )
783       return aChild; // found
784     aChild = aChild->nextBrother();
785   }
786   return NULL; // not found
787 }
788
789 bool HYDROGUI_DataModel::createNewRegion( Handle(HYDROData_CalculationCase) theCase, 
790                                          const QList<HYDROGUI_Zone*>& theZonesList )
791 {
792   bool isOk = !theCase.IsNull();
793   if ( isOk )
794   {
795     Handle(HYDROData_Region) aRegion;
796     Handle(HYDROData_Zone) aZone;
797     for (int i = 0; i < theZonesList.length(); i++ )
798     {
799       aZone = Handle(HYDROData_Zone)::DownCast( theZonesList.at(i)->modelObject() );
800       if ( !aZone.IsNull() )
801       {
802         if ( aRegion.IsNull() )
803         {
804           aRegion = theCase->AddNewRegion( aZone );
805           isOk = !aRegion.IsNull();
806         }
807         else
808         {
809           if ( !( aRegion->AddZone( aZone ) ) )
810           {
811             isOk = false;
812           }
813         }
814       }
815     }
816   }
817   return isOk;
818 }