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