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