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