Salome HOME
e43410b4be30caec6ab3904c337c8d1b7401d45d
[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                                                       const bool           theIsInOperation )
668 {
669   if ( theIsBuildTree )
670   {
671     buildObjectTree( theParent, theObject, theParentEntry, theIsInOperation );
672   }
673   return theObject;
674 }
675
676 LightApp_DataObject* HYDROGUI_DataModel::createZone( SUIT_DataObject*       theParent,
677                                                      Handle(HYDROData_Zone) theModelObject,
678                                                      const QString&         theParentEntry,
679                                                      const bool             theIsBuildTree,
680                                                      const bool             theIsInOperation )
681 {
682   return buildObject( theParent, new HYDROGUI_Zone( theParent, theModelObject, theParentEntry, theIsInOperation ), 
683     theParentEntry, theIsBuildTree, theIsInOperation );
684 }
685
686 LightApp_DataObject* HYDROGUI_DataModel::createRegion( SUIT_DataObject*         theParent,
687                                                        Handle(HYDROData_Region) theModelObject,
688                                                        const QString&           theParentEntry,
689                                                        const bool               theIsBuildTree,
690                                                        const bool               theIsInOperation )
691 {
692   return buildObject( theParent, new HYDROGUI_Region( theParent, theModelObject, theParentEntry, theIsInOperation ), 
693     theParentEntry, theIsBuildTree, theIsInOperation );
694 }
695
696 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
697                                                        const QString&   theName,
698                                                        const QString&   theParentEntry )
699 {
700   return new HYDROGUI_NamedObject( theParent, theName, theParentEntry );
701 }
702
703 void HYDROGUI_DataModel::buildObjectTree( SUIT_DataObject* theParent,
704                                           SUIT_DataObject* theObject,
705                                           const QString&   theParentEntry,
706                                           const bool       theIsInOperation )
707 {
708   HYDROGUI_DataObject* aGuiObj = dynamic_cast<HYDROGUI_DataObject*>( theObject );
709   if ( !aGuiObj )
710     return;
711
712   Handle(HYDROData_Entity) aDataObj = aGuiObj->modelObject();
713   if ( aDataObj.IsNull() )
714     return;
715
716   if ( aDataObj->IsKind( STANDARD_TYPE(HYDROData_Object) ) )
717   {
718     Handle(HYDROData_Object) aGeomObj =
719       Handle(HYDROData_Object)::DownCast( aDataObj );
720
721     Handle(HYDROData_DummyObject3D) anObject3D = aGeomObj->GetObject3D();
722     if ( !anObject3D.IsNull() )
723       createObject( aGuiObj, anObject3D, aGuiObj->entry(), false );
724   }
725
726   ObjectKind anObjectKind = aDataObj->GetKind();
727
728   if ( anObjectKind == KIND_IMAGE )
729   {
730     Handle(HYDROData_Image) anImageObj =
731       Handle(HYDROData_Image)::DownCast( aDataObj );
732     for ( int anIndex = 0, aNbRef = anImageObj->NbReferences(); anIndex < aNbRef; anIndex++ )
733     {
734       Handle(HYDROData_Entity) aRefObj = anImageObj->Reference( anIndex );
735       if ( !aRefObj.IsNull() && !aRefObj->IsRemoved() )
736         createObject( aGuiObj, aRefObj, aGuiObj->entry(), false );
737     }
738   }
739   else if ( anObjectKind == KIND_IMMERSIBLE_ZONE )
740   {
741     Handle(HYDROData_ImmersibleZone) aZoneObj =
742       Handle(HYDROData_ImmersibleZone)::DownCast( aDataObj );
743
744     LightApp_DataObject* aPolylineSect = 
745       createObject( aGuiObj, tr( "ZONE_POLYLINE" ), aGuiObj->entry() );
746
747     Handle(HYDROData_PolylineXY) aPolyline = aZoneObj->GetPolyline();
748     if ( !aPolyline.IsNull() && !aPolyline->IsRemoved() )
749       createObject( aPolylineSect, aPolyline, aGuiObj->entry(), false );
750
751     LightApp_DataObject* aBathSect = 
752       createObject( aGuiObj, tr( "ZONE_BATHYMETRY" ), aGuiObj->entry() );
753
754     Handle(HYDROData_Bathymetry) aBathymetry = aZoneObj->GetBathymetry();
755     if ( !aBathymetry.IsNull() && !aBathymetry->IsRemoved() )
756       createObject( aBathSect, aBathymetry, aGuiObj->entry(), false );
757   }
758   else if ( anObjectKind == KIND_POLYLINE )
759   {
760     Handle(HYDROData_Polyline3D) aPolyline3D =
761       Handle(HYDROData_Polyline3D)::DownCast( aDataObj );
762
763     LightApp_DataObject* aPolylineSect = 
764       createObject( aGuiObj, tr( "POLYLINE3D_POLYLINE" ), aGuiObj->entry() );
765
766     Handle(HYDROData_PolylineXY) aPolylineXY = aPolyline3D->GetPolylineXY();
767     if ( !aPolylineXY.IsNull() && !aPolylineXY->IsRemoved() )
768       createObject( aPolylineSect, aPolylineXY, aGuiObj->entry(), false );
769
770     LightApp_DataObject* aProfileSect = 
771       createObject( aGuiObj, tr( "POLYLINE3D_PROFILE" ), aGuiObj->entry() );
772
773     Handle(HYDROData_ProfileUZ) aProfileUZ = aPolyline3D->GetProfileUZ();
774     if ( aProfileUZ.IsNull() || aProfileUZ->IsRemoved() )
775       aProfileUZ = aPolyline3D->GetChildProfileUZ( false );
776
777     if ( !aProfileUZ.IsNull() && !aProfileUZ->IsRemoved() )
778     {
779       Handle(HYDROData_Profile) aProfile = 
780         Handle(HYDROData_Profile)::DownCast( aProfileUZ->GetFatherObject() );
781       if ( !aProfile.IsNull() && !aProfile->IsRemoved() )
782         createObject( aProfileSect, aProfile, aGuiObj->entry(), false );
783     }
784
785     LightApp_DataObject* aBathSect = 
786       createObject( aGuiObj, tr( "POLYLINE3D_BATHYMETRY" ), aGuiObj->entry() );
787
788     Handle(HYDROData_Bathymetry) aBathymetry = aPolyline3D->GetBathymetry();
789     if ( !aBathymetry.IsNull() && !aBathymetry->IsRemoved() )
790       createObject( aBathSect, aBathymetry, aGuiObj->entry(), false );
791   }
792   else if ( anObjectKind == KIND_CALCULATION )
793   {
794     Handle(HYDROData_CalculationCase) aCaseObj =
795       Handle(HYDROData_CalculationCase)::DownCast( aDataObj );
796
797     LightApp_DataObject* aCaseRegionsSect = 
798       createObject( aGuiObj, tr( "CASE_REGIONS" ), aGuiObj->entry() );
799
800     HYDROData_SequenceOfObjects aCaseRegions = aCaseObj->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( aCaseRegionsSect, aCaseRegion, "", true, theIsInOperation );
808     }
809   }
810   else if ( anObjectKind == KIND_REGION )
811   {
812     Handle(HYDROData_Region) aRegionObj =
813       Handle(HYDROData_Region)::DownCast( aDataObj );
814
815     HYDROData_SequenceOfObjects aRegionZones = aRegionObj->GetZones();
816     HYDROData_SequenceOfObjects::Iterator anIter( aRegionZones );
817     for ( ; anIter.More(); anIter.Next() )
818     {
819       Handle(HYDROData_Zone) aRegionZone =
820         Handle(HYDROData_Zone)::DownCast( anIter.Value() );
821       if( !aRegionZone.IsNull() && !aRegionZone->IsRemoved() )
822         createZone( aGuiObj, aRegionZone, "", true, theIsInOperation );
823     }
824   }
825   else if ( anObjectKind == KIND_PROFILE )
826   {
827     Handle(HYDROData_Profile) aProfileObj =
828       Handle(HYDROData_Profile)::DownCast( aDataObj );
829
830     aGuiObj->setIsValid( aProfileObj->IsValid() );
831   }
832 }
833
834 void HYDROGUI_DataModel::buildCaseTree( SUIT_DataObject* theParent, Handle(HYDROData_CalculationCase) theCase )
835 {
836   if ( !theCase.IsNull() )
837   {
838     if ( theParent )
839     {
840       // Remove previous objects tree
841         DataObjectList aList;
842         theParent->children( aList );
843         QListIterator<SUIT_DataObject*> anIter( aList );
844         while( anIter.hasNext() )
845           removeChild( theParent, anIter.next() );
846     }
847
848     new HYDROGUI_DropTargetObject( theParent, tr( "NEW_REGION" ), "", true );
849
850     HYDROData_SequenceOfObjects aCaseRegions = theCase->GetRegions();
851     HYDROData_SequenceOfObjects::Iterator anIter( aCaseRegions );
852     for ( ; anIter.More(); anIter.Next() )
853     {
854       Handle(HYDROData_Region) aCaseRegion =
855         Handle(HYDROData_Region)::DownCast( anIter.Value() );
856       if( !aCaseRegion.IsNull() && !aCaseRegion->IsRemoved() )
857         createRegion( theParent, aCaseRegion, "", true, true );
858     }
859   }
860 }
861
862 void HYDROGUI_DataModel::updateObjectTree( Handle(HYDROData_Entity)& theObj )
863 {
864   if ( !theObj.IsNull() )
865   {
866     HYDROGUI_DataObject* aGuiObj = dynamic_cast<HYDROGUI_DataObject*>(
867       findObject( HYDROGUI_DataObject::dataObjectEntry( theObj ) ) );
868     if ( aGuiObj )
869     {
870       // Remove previous objects tree
871       DataObjectList aList;
872       aGuiObj->children( aList );
873       QListIterator<SUIT_DataObject*> anIter( aList );
874       while( anIter.hasNext() )
875         removeChild( aGuiObj, anIter.next() );
876
877       // Rebuild the subtree
878       QString aParentEntry;
879       HYDROGUI_DataObject* aParent = dynamic_cast<HYDROGUI_DataObject*>( aGuiObj->parent() );
880       if ( aParent )
881       {
882         aParentEntry = aParent->entry();
883       }
884       buildObjectTree( aParent, aGuiObj, aParentEntry, aGuiObj->isInOperation() );
885     } 
886     else
887     {
888       // workaround for the bug in SalomeApp_Study::findObjectByEntry - it can't find LightApp_DataObjects
889       HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
890       if( aModule )
891       {
892         aModule->getApp()->updateObjectBrowser();
893       }
894     }
895   }
896 }
897
898 void HYDROGUI_DataModel::removeChild( SUIT_DataObject* theParent,
899                                       SUIT_DataObject* theChild )
900 {
901   SUIT_DataObject* aSubChild = theChild->firstChild();
902   for( ; aSubChild; aSubChild = aSubChild->nextBrother() )
903     removeChild( theChild, aSubChild );
904   theParent->removeChild( theChild );
905 }
906
907 SUIT_DataObject* HYDROGUI_DataModel::findChildByName( const SUIT_DataObject* theFather,
908                                                       const QString& theName )
909 {
910   SUIT_DataObject* aChild = theFather->firstChild();
911   while( aChild )
912   {
913     if( aChild->name() == theName )
914       return aChild; // found
915     aChild = aChild->nextBrother();
916   }
917   return NULL; // not found
918 }
919
920 bool HYDROGUI_DataModel::createNewRegion( Handle(HYDROData_CalculationCase) theCase, 
921                                          const QList<HYDROGUI_Zone*>& theZonesList )
922 {
923   bool isOk = !theCase.IsNull();
924   if ( isOk )
925   {
926     Handle(HYDROData_Region) aRegion;
927     Handle(HYDROData_Zone) aZone;
928     for (int i = 0; i < theZonesList.length(); i++ )
929     {
930       aZone = Handle(HYDROData_Zone)::DownCast( theZonesList.at(i)->modelObject() );
931       if ( !aZone.IsNull() )
932       {
933         if ( aRegion.IsNull() )
934         {
935           aRegion = theCase->AddNewRegion( aZone );
936           isOk = !aRegion.IsNull();
937         }
938         else
939         {
940           if ( !( aRegion->AddZone( aZone ) ) )
941           {
942             isOk = false;
943           }
944         }
945       }
946     }
947   }
948   return isOk;
949 }
950
951 bool HYDROGUI_DataModel::rename( Handle(HYDROData_Entity) theEntity, const QString& theName )
952 {
953   if ( theName.isEmpty() )
954     return false;
955
956   try 
957   {
958     getDocument()->StartOperation();
959     theEntity->SetName( theName );
960     getDocument()->CommitOperation( HYDROGUI_Tool::ToExtString( tr("RENAME_TO").arg( theName ) ) );
961     module()->application()->activeStudy()->Modified();
962   }
963   catch ( Standard_Failure )
964   {
965     getDocument()->AbortOperation();
966     return false;
967   }
968   return true;
969 }
970