Salome HOME
Merge branch 'BR_LAND_COVER_MAP' of ssh://git.salome-platform.org/modules/hydro into...
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_DataModel.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_DataModel.h"
20
21 #include "HYDROGUI_DataObject.h"
22 #include "HYDROGUI_Module.h"
23 #include "HYDROGUI_Tool.h"
24 #include "HYDROGUI_Tool2.h"
25 #include "HYDROGUI_Zone.h"
26 #include "HYDROGUI_Region.h"
27
28 #include <HYDROData_Bathymetry.h>
29 #include <HYDROData_CalculationCase.h>
30 #include <HYDROGUI_DataModelSync.h>
31 #include <HYDROData_Document.h>
32 #include <HYDROData_DummyObject3D.h>
33 #include <HYDROData_Image.h>
34 #include <HYDROData_ImmersibleZone.h>
35 #include <HYDROData_Iterator.h>
36 #include <HYDROData_Polyline3D.h>
37 #include <HYDROData_PolylineXY.h>
38 #include <HYDROData_Profile.h>
39 #include <HYDROData_VisualState.h>
40 #include <HYDROData_Region.h>
41 #include <HYDROData_Zone.h>
42 #include <HYDROData_Obstacle.h>
43 #include <HYDROData_Channel.h>
44 #include <HYDROData_Digue.h>
45 #include <HYDROData_River.h>
46 #include <HYDROData_Stream.h>
47 #include <HYDROData_StricklerTable.h>
48 #include <HYDROData_LandCoverMap.h>
49
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_DataBrowser.h>
58 #include <SUIT_ResourceMgr.h>
59 #include <SUIT_Study.h>
60 #include <SUIT_Tools.h>
61 #include <SUIT_TreeSync.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 // #define DEB_GROUPS 1
72 #ifdef DEB_GROUPS
73 #include <HYDROData_ShapesGroup.h>
74 #endif
75
76 static HYDROData_SequenceOfObjects myCopyingObjects;
77
78 const int ENTRY_COLUMN = 2;
79
80
81 HYDROGUI_DataModel::HYDROGUI_DataModel( CAM_Module* theModule )
82 : LightApp_DataModel( theModule )
83 {
84   update( module()->application()->activeStudy()->id() );
85 }
86
87 HYDROGUI_DataModel::~HYDROGUI_DataModel()
88 {
89 }
90
91 bool HYDROGUI_DataModel::create( CAM_Study* theStudy )
92 {
93     bool status = LightApp_DataModel::create( theStudy );
94     if ( status )
95         updateDocument();
96     return status;
97 }
98
99 bool HYDROGUI_DataModel::open( const QString& theURL,
100                                CAM_Study* theStudy,
101                                QStringList theFileList )
102 {
103   LightApp_DataModel::open( theURL, theStudy, theFileList );
104   const int aStudyId = theStudy->id();
105
106   Data_DocError res = DocError_UnknownProblem;
107   if( theFileList.count() >= 2 )
108   {
109     QString aTmpDir = theFileList[0];
110     QString aDataFileName = theFileList[1];
111     QString aStatesFileName = theFileList.count() == 3 ? theFileList[2] : "";
112
113     myStudyURL = theURL;
114     QString aDataFullPath = SUIT_Tools::addSlash( aTmpDir ) + aDataFileName;
115     QString aStatesFullPath = aStatesFileName.isEmpty() ? "" : SUIT_Tools::addSlash( aTmpDir ) + aStatesFileName;
116
117     try
118     {
119       res = HYDROData_Document::Load( (char*)aDataFullPath.toLatin1().constData(), aStudyId );
120     }
121     catch(...)
122     {
123       res = DocError_UnknownProblem;
124     }
125     
126     if ( res != DocError_OK )
127     {
128       module()->application()->putInfo( tr( "LOAD_ERROR" ) );
129       return false;
130     }
131
132     if ( !aStatesFullPath.isEmpty() )
133     {
134       QFile aFile( aStatesFullPath );
135       if( aFile.open( QFile::ReadOnly ) )
136       {
137         myStates = aFile.readAll();
138         aFile.close();
139       }
140     }
141
142     updateDocument();
143   }
144
145   // if the document open was successful, the data model update happens
146   // in the set mode of the module
147   if ( res == DocError_OK )
148     update( aStudyId );
149
150   return true;
151 }
152
153 bool HYDROGUI_DataModel::save( QStringList& theFileList )
154 {
155   if( !module()->application()->activeStudy() )
156     return false;
157   
158   LightApp_DataModel::save( theFileList );
159
160   QString aTmpDir;
161   SUIT_ResourceMgr* resMgr = module()->application()->resourceMgr();
162   bool isMultiFile = false;
163   if ( resMgr )
164     isMultiFile = resMgr->booleanValue( "Study", "multi_file", false );
165
166   // save module data to temporary files
167   LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy() );
168   aTmpDir = aStudy->GetTmpDir( myStudyURL.toLatin1().constData(), isMultiFile ).c_str();
169   
170   // save OCAF data to a temporary file
171   QString aDataFileName = SUIT_Tools::file( myStudyURL, false ) + "_HYDRO.cbf";
172   QString aDataFullPath = aTmpDir + aDataFileName;
173   Data_DocError res = getDocument()->Save( (char*)aDataFullPath.toLatin1().constData() );
174   if( res != DocError_OK )
175   {
176     module()->application()->putInfo( tr( "SAVE_ERROR" ) );
177     return false;
178   }
179
180   // save tree state data to a temporary file
181   LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
182   QByteArray aStatesData = anApp->objectBrowser()->getOpenStates( ENTRY_COLUMN );
183   QString aStatesFileName = SUIT_Tools::file( myStudyURL, false ) + "_HYDRO_tree_states.txt";
184   QString aStatesFullPath = aTmpDir + aStatesFileName;
185   QFile aFile( aStatesFullPath );
186   if( aFile.open( QFile::WriteOnly ) )
187   {
188     aFile.write( aStatesData );
189     aFile.close();
190   }
191
192   // add temporary files to the list
193   theFileList.append( aTmpDir );
194   theFileList.append( aDataFileName );
195   theFileList.append( aStatesFileName );
196
197   return true;
198 }
199
200 bool HYDROGUI_DataModel::saveAs( const QString& theURL,
201                                  CAM_Study*,
202                                  QStringList& theFileList )
203 {
204   myStudyURL = theURL;
205   return save( theFileList );
206 }
207
208 bool HYDROGUI_DataModel::close()
209 {
210   HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
211   if ( aModule )
212       aModule->clearCache();
213   return true;
214 }
215
216 bool HYDROGUI_DataModel::dumpPython( const QString& theURL,
217                                      CAM_Study*     theStudy,
218                                      bool           isMultiFile,
219                                      QStringList&   theListOfFiles )
220 {
221   LightApp_DataModel::dumpPython( theURL, theStudy, isMultiFile, theListOfFiles );
222
223   int aStudyId = theStudy->id();
224
225   LightApp_Study* aStudy = ::qobject_cast<LightApp_Study*>( theStudy );
226   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId );
227   if ( aDocument.IsNull() || !aStudy )
228     return false;
229
230   QString aDir = aStudy->GetTmpDir( theURL.toLatin1().constData(), isMultiFile ).c_str();
231   QString aFileToExport = aDir + QString( QDir::separator() ) + "HYDRO.py";
232
233   bool aRes = aDocument->DumpToPython( aFileToExport, isMultiFile );
234   if ( aRes )
235   {
236     theListOfFiles.append( aDir );
237     theListOfFiles.append( aFileToExport );
238   }
239
240   return aRes;
241 }
242
243 bool HYDROGUI_DataModel::isModified() const
244 {
245   return getDocument()->IsModified();
246 }
247
248 bool HYDROGUI_DataModel::isSaved() const
249 {
250   return true;
251 }
252
253 void HYDROGUI_DataModel::update( const int theStudyId )
254 {
255   LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
256   if( !anApp )
257     return;
258
259   SUIT_DataObject* aStudyRoot = anApp->activeStudy()->root();
260   if( !aStudyRoot )
261     return;
262
263   // create a new root object
264   CAM_DataObject* aNewRootObj = new CAM_DataObject();
265
266   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theStudyId );
267   if( aDocument.IsNull() )
268     return;
269
270   // Create root objects:
271
272   // IMAGES
273   LightApp_DataObject* anImageRootObj = createObject( aNewRootObj, tr( partitionName( KIND_IMAGE ).toAscii() ) );
274
275   // BATHYMETRY
276   LightApp_DataObject* aBathymetryRootObj = createObject( aNewRootObj, tr( partitionName( KIND_BATHYMETRY ).toAscii() ) );
277
278   // ARTIFICIAL OBJECTS
279   LightApp_DataObject* anArtificialObjectsRootObj = createObject( aNewRootObj, tr( partitionName( KIND_ARTIFICIAL_OBJECT ).toAscii() ) );
280
281   // NATURAL OBJECTS
282   LightApp_DataObject* aNaturalObjectsRootObj = createObject( aNewRootObj, tr( partitionName( KIND_NATURAL_OBJECT ).toAscii() ) );
283
284   // OBSTACLES
285   LightApp_DataObject* anObstaclesRootObj = createObject( aNewRootObj, tr( partitionName( KIND_OBSTACLE ).toAscii() ) );
286
287   // STRICKLER TABLES
288   LightApp_DataObject* aStricklerTablesRootObj = createObject( aNewRootObj, tr( partitionName( KIND_STRICKLER_TABLE ).toAscii() ) );
289
290   // LAND COVER MAPS
291   LightApp_DataObject* aLandCoversRootObj = createObject( aNewRootObj, tr( partitionName( KIND_LAND_COVER_MAP ).toAscii() ) );
292
293   // CALCULATION CASES
294   LightApp_DataObject* aCalculRootObj = createObject( aNewRootObj, tr( partitionName( KIND_CALCULATION ).toAscii() ) );
295
296   // POLYLINES
297   LightApp_DataObject* aPolylineRootObj = createObject( aNewRootObj, tr( partitionName( KIND_POLYLINEXY ).toAscii() ) );
298
299   // POLYLINES
300   LightApp_DataObject* aPolyline3DRootObj = createObject( aNewRootObj, tr( partitionName( KIND_POLYLINE ).toAscii() ) );
301
302   // PROFILES
303   LightApp_DataObject* aProfileRootObj = createObject( aNewRootObj, tr( partitionName( KIND_PROFILE ).toAscii() ) );
304
305   // VISUAL STATES
306   LightApp_DataObject* aVisualStateRootObj = createObject( aNewRootObj, tr( partitionName( KIND_VISUAL_STATE ).toAscii() ) );
307
308   int aNoStricklerTableObj = 0;
309   HYDROData_Iterator anIterator( aDocument, KIND_UNKNOWN );
310   for( ; anIterator.More(); anIterator.Next() ) {
311     LightApp_DataObject* obj = 0;
312     Handle(HYDROData_Entity) anObj = anIterator.Current();
313
314     if ( !anObj.IsNull() )
315     {
316       switch ( anObj->GetKind() ) {
317         case KIND_IMAGE:
318         {
319           Handle(HYDROData_Image) anImageObj =
320             Handle(HYDROData_Image)::DownCast( anObj );
321           if( !anImageObj.IsNull() ) {
322             obj = createObject( anImageRootObj, anImageObj );
323           }
324
325           break;
326         }
327         case KIND_BATHYMETRY:
328         {
329           Handle(HYDROData_Bathymetry) aBathymetryObj =
330             Handle(HYDROData_Bathymetry)::DownCast( anObj );
331           if( !aBathymetryObj.IsNull() ) {
332             obj = createObject( aBathymetryRootObj, aBathymetryObj );
333           }
334
335           break;
336         }
337         case KIND_CHANNEL:
338         {
339           Handle(HYDROData_Channel) aChannelObj =
340             Handle(HYDROData_Channel)::DownCast( anObj );
341           if( !aChannelObj.IsNull() ) {
342             obj = createObject( anArtificialObjectsRootObj, aChannelObj );
343           }
344
345           break;
346         }
347         case KIND_DIGUE:
348         {
349           Handle(HYDROData_Digue) aDigueObj =
350             Handle(HYDROData_Digue)::DownCast( anObj );
351           if( !aDigueObj.IsNull() ) {
352             obj = createObject( anArtificialObjectsRootObj, aDigueObj );
353           }
354
355           break;
356         }
357         case KIND_IMMERSIBLE_ZONE:
358         {
359           Handle(HYDROData_ImmersibleZone) anImmersibleZoneObj =
360             Handle(HYDROData_ImmersibleZone)::DownCast( anObj );
361           if( !anImmersibleZoneObj.IsNull() ) {
362             obj = createObject( aNaturalObjectsRootObj, anImmersibleZoneObj );
363           }
364
365           break;
366         }
367         case KIND_RIVER:
368         {
369           Handle(HYDROData_River) aRiverObj =
370             Handle(HYDROData_River)::DownCast( anObj );
371           if( !aRiverObj.IsNull() ) {
372             obj = createObject( aNaturalObjectsRootObj, aRiverObj );
373           }
374
375           break;
376         }
377         case KIND_STREAM:
378         {
379           Handle(HYDROData_Stream) aStreamObj =
380             Handle(HYDROData_Stream)::DownCast( anObj );
381           if( !aStreamObj.IsNull() ) {
382             obj = createObject( aNaturalObjectsRootObj, aStreamObj );
383           }
384
385           break;
386         }
387         case KIND_OBSTACLE:
388         {
389           Handle(HYDROData_Obstacle) anObstacleObj =
390             Handle(HYDROData_Obstacle)::DownCast( anObj );
391           if( !anObstacleObj.IsNull() ) {
392             obj = createObject( anObstaclesRootObj, anObstacleObj );
393           }
394
395           break;
396         }
397                 case KIND_STRICKLER_TABLE:
398         {
399           Handle(HYDROData_StricklerTable) aStricklerTableObj =
400             Handle(HYDROData_StricklerTable)::DownCast( anObj );
401           if( !aStricklerTableObj.IsNull() ) {
402             obj = createObject( aStricklerTablesRootObj, aStricklerTableObj );
403           }
404                   aNoStricklerTableObj++;
405
406           break;
407         }
408         case KIND_LAND_COVER_MAP:
409         {
410           Handle(HYDROData_LandCoverMap) aLandCoverMapObj =
411             Handle(HYDROData_LandCoverMap)::DownCast( anObj );
412           if( !aLandCoverMapObj.IsNull() ) {
413             obj = createObject( aLandCoversRootObj, aLandCoverMapObj );
414           }
415
416           break;
417         }
418         case KIND_CALCULATION:
419         {
420           Handle(HYDROData_CalculationCase) aCalculObj =
421             Handle(HYDROData_CalculationCase)::DownCast( anObj );
422           if( !aCalculObj.IsNull() ) {
423             obj = createObject( aCalculRootObj, aCalculObj );
424           }
425
426           break;
427         }
428         case KIND_POLYLINEXY:
429         {
430           Handle(HYDROData_PolylineXY) aPolylineObj =
431             Handle(HYDROData_PolylineXY)::DownCast( anObj );
432           if( !aPolylineObj.IsNull() ) {
433             obj = createObject( aPolylineRootObj, aPolylineObj );
434           }
435
436           break;
437         }
438         case KIND_POLYLINE:
439         {
440           Handle(HYDROData_Polyline3D) aPolylineObj =
441             Handle(HYDROData_Polyline3D)::DownCast( anObj );
442           if( !aPolylineObj.IsNull() ) {
443             obj = createObject( aPolyline3DRootObj, aPolylineObj );
444           }
445
446           break;
447         }
448         case KIND_PROFILE:
449         {
450           Handle(HYDROData_Profile) aProfileObj =
451             Handle(HYDROData_Profile)::DownCast( anObj );
452           if( !aProfileObj.IsNull() ) {
453             obj = createObject( aProfileRootObj, aProfileObj );
454           }
455
456           break;
457         }
458         case KIND_VISUAL_STATE:
459         {
460           Handle(HYDROData_VisualState) aVisualStateObj =
461             Handle(HYDROData_VisualState)::DownCast( anObj );
462           if( !aVisualStateObj.IsNull() ) {
463             obj = createObject( aVisualStateRootObj, aVisualStateObj );
464           }
465
466           break;
467         }
468       }
469     }
470   }
471
472   // Create default Strickler table object
473   if ( aNoStricklerTableObj == 0 )
474     createDefaultStricklerTable( aDocument, aStricklerTablesRootObj );
475
476   //if( SUIT_DataBrowser* anObjectBrowser = anApp->objectBrowser() )
477   //{
478   //  anObjectBrowser->setAutoOpenLevel( 3 );
479   //  anObjectBrowser->openLevels();
480   //}
481
482   HYDROGUI_DataModelSync aSync( aNewRootObj );
483   SUIT_DataObject* aRoot = root();
484   bool isNewDoc = aRoot==0;
485   if( isNewDoc )
486     aRoot = createRootModuleObject( aStudyRoot );
487   ::synchronize < suitPtr, suitPtr, HYDROGUI_DataModelSync >
488     ( aNewRootObj, aRoot, aSync );
489
490   SUIT_DataBrowser* ob = anApp->objectBrowser();
491
492   if ( !myStates.isEmpty() )
493   {
494     ob->updateTree();
495     ob->setOpenStates( myStates, ENTRY_COLUMN );
496     myStates.clear();
497   }
498 }
499
500 HYDROGUI_DataObject* HYDROGUI_DataModel::getDataObject( const Handle(HYDROData_Entity)& theModelObject )
501 {
502   HYDROGUI_DataObject* aGuiObj = dynamic_cast<HYDROGUI_DataObject*>(
503     findObject( HYDROGUI_DataObject::dataObjectEntry( theModelObject ) ) );
504   return aGuiObj;
505 }
506
507 HYDROGUI_DataObject* HYDROGUI_DataModel::getReferencedDataObject( HYDROGUI_DataObject* theObject )
508 {
509   return NULL; // to do if necessary
510 }
511
512 SUIT_DataObject* HYDROGUI_DataModel::findObject( const QString& theEntry ) const
513 {
514   LightApp_Application* anApp = dynamic_cast<LightApp_Application*>( module()->application() );
515   return anApp ? anApp->findObject( theEntry ) : 0;
516 }
517
518 void HYDROGUI_DataModel::update( LightApp_DataObject* theObject,
519                                  LightApp_Study* theStudy )
520 {
521   if( !theStudy )
522     theStudy = dynamic_cast<LightApp_Study*>( module()->application()->activeStudy()) ;
523   if( theStudy )
524     update( theStudy->id() );
525 }
526
527 CAM_DataObject* HYDROGUI_DataModel::createRootModuleObject( SUIT_DataObject* theParent )
528 {
529   CAM_ModuleObject* aRootObj = createModuleObject( theParent );
530   aRootObj->setDataModel( this );
531   setRoot( aRootObj );
532   return aRootObj;
533 }
534
535 void HYDROGUI_DataModel::updateModel()
536 {
537   HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
538   if( aModule )
539     update( aModule->getStudyId() );
540 }
541
542 Handle(HYDROData_Entity) HYDROGUI_DataModel::objectByEntry( const QString& theEntry,
543                                                             const ObjectKind theObjectKind )
544 {
545   QString anEntry = theEntry;
546   if( anEntry.indexOf( "_" ) != -1 ) // reference object
547     anEntry = anEntry.section( "_", -1 );
548
549   Handle(HYDROData_Document) aDocument = getDocument();
550   if( !aDocument.IsNull() )
551   {
552     HYDROData_Iterator anIterator( aDocument, theObjectKind );
553     for( ; anIterator.More(); anIterator.Next() )
554     {
555       Handle(HYDROData_Entity) anObject = anIterator.Current();
556       if( !anObject.IsNull() )
557       {
558         QString anEntryRef = HYDROGUI_DataObject::dataObjectEntry( anObject );
559         if( anEntryRef == anEntry )
560           return anObject;
561       }
562     }
563   }
564   return NULL;
565 }
566
567 bool HYDROGUI_DataModel::canUndo() const
568 {
569   return getDocument()->CanUndo();
570 }
571
572 bool HYDROGUI_DataModel::canRedo() const
573 {
574   return getDocument()->CanRedo();
575 }
576
577 QStringList HYDROGUI_DataModel::undoNames() const
578 {
579   QStringList aNames;
580   for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetUndos() ); anIter.More(); anIter.Next() )
581     aNames.prepend( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
582   return aNames;
583 }
584
585 QStringList HYDROGUI_DataModel::redoNames() const
586 {
587   QStringList aNames;
588   for( TDF_ListIteratorOfDeltaList anIter( getDocument()->GetRedos() ); anIter.More(); anIter.Next() )
589     aNames.append( HYDROGUI_Tool::ToQString( anIter.Value()->Name() ) );
590   return aNames;
591 }
592
593 void HYDROGUI_DataModel::clearUndos()
594 {
595   getDocument()->ClearUndos();
596 }
597
598 void HYDROGUI_DataModel::clearRedos()
599 {
600   getDocument()->ClearRedos();
601 }
602
603 bool HYDROGUI_DataModel::undo()
604 {
605   try 
606   {
607     getDocument()->Undo();
608   }
609   catch ( Standard_Failure )
610   {
611     return false;
612   }
613   return true;
614 }
615
616 bool HYDROGUI_DataModel::redo()
617 {
618   try 
619   {
620     getDocument()->Redo();
621   }
622   catch ( Standard_Failure )
623   {
624     return false;
625   }
626   return true;
627 }
628
629 bool HYDROGUI_DataModel::canCopy()
630 {
631   HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( (HYDROGUI_Module*)module() );
632   bool isCanCopy = !aSeq.IsEmpty();
633
634   for ( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ ) {
635     Handle(HYDROData_Entity) anObject = aSeq.Value( anIndex );
636     if( !anObject.IsNull() ) {
637       ObjectKind aKind = anObject->GetKind();
638       bool isUnrecognized = aKind <= KIND_UNKNOWN || aKind > KIND_LAST;
639       bool isChildObject = aKind == KIND_DUMMY_3D || 
640                            aKind == KIND_ZONE ||
641                            aKind == KIND_SHAPES_GROUP || 
642                            aKind == KIND_SPLITTED_GROUP;
643       if ( isUnrecognized || isChildObject ) {
644         isCanCopy = false;
645         break;
646       }
647     }
648   }
649   
650   return isCanCopy;
651 }
652
653 bool HYDROGUI_DataModel::canPaste()
654 {
655   for( int anIndex = 1, aLength = myCopyingObjects.Length(); anIndex <= aLength; anIndex++ )
656   {
657     Handle(HYDROData_Entity) anObject = myCopyingObjects.Value( anIndex );
658     if( !anObject.IsNull() && !anObject->IsRemoved() )
659       return true;
660   }
661   return false;
662 }
663
664 bool HYDROGUI_DataModel::copy()
665 {
666   HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( (HYDROGUI_Module*)module() );
667   changeCopyingObjects( aSeq );
668   return true;
669 }
670
671 bool HYDROGUI_DataModel::paste()
672 {
673   bool anIsChanged = false;
674   for( int anIndex = 1, aLength = myCopyingObjects.Length(); anIndex <= aLength; anIndex++ )
675   {
676     Handle(HYDROData_Entity) anObject = myCopyingObjects.Value( anIndex );
677     if( !anObject.IsNull() && !anObject->IsRemoved() )
678     {
679       ObjectKind aKind = anObject->GetKind();
680       Handle(HYDROData_Entity) aClone = getDocument()->CreateObject( aKind );
681       if( !aClone.IsNull() )
682       {
683         anObject->CopyTo( aClone, true );
684         anIsChanged = true;
685
686         // remove Z layer
687         aClone->RemoveZLevel();
688       }
689     }
690   }
691   return anIsChanged;
692 }
693
694 void HYDROGUI_DataModel::changeCopyingObjects( const HYDROData_SequenceOfObjects& theSeq )
695 {
696   myCopyingObjects.Assign( theSeq );
697 }
698
699 QString HYDROGUI_DataModel::partitionName( const ObjectKind theObjectKind )
700 {
701   switch( theObjectKind )
702   {
703     case KIND_IMAGE:             return "IMAGES";
704     case KIND_POLYLINE:          return "POLYLINES_3D";
705     case KIND_POLYLINEXY:        return "POLYLINES";
706     case KIND_PROFILE:           return "PROFILES";
707     case KIND_VISUAL_STATE:      return "VISUAL_STATES";
708     case KIND_BATHYMETRY:        return "BATHYMETRIES";
709     case KIND_CALCULATION:       return "CALCULATION_CASES";
710     case KIND_OBSTACLE:          return "OBSTACLES";
711     case KIND_ARTIFICIAL_OBJECT: return "ARTIFICIAL_OBJECTS";
712     case KIND_NATURAL_OBJECT:    return "NATURAL_OBJECTS";
713     case KIND_STRICKLER_TABLE:   return "STRICKLER_TABLES";
714     case KIND_LAND_COVER_MAP:    return "LAND_COVER_MAPS";
715     default: break;
716   }
717   return QString();
718 }
719
720 Handle(HYDROData_Document) HYDROGUI_DataModel::getDocument() const
721 {
722   int aStudyId = module()->application()->activeStudy()->id();
723   return HYDROData_Document::Document( aStudyId );
724 }
725
726 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject*         theParent,
727                                                        Handle(HYDROData_Entity) theModelObject,
728                                                        const QString&           theParentEntry,
729                                                        const bool               theIsBuildTree )
730 {
731   HYDROGUI_DataObject* aResObj = new HYDROGUI_DataObject( theParent, theModelObject, theParentEntry );
732
733   const ObjectKind aKind = theModelObject->GetKind();
734   bool visibility = aKind == KIND_IMAGE || aKind == KIND_POLYLINEXY || aKind == KIND_POLYLINE ||
735                     aKind == KIND_SHAPES_GROUP || aKind == KIND_SPLITTED_GROUP || aKind == KIND_ZONE ||
736                     aKind == KIND_IMMERSIBLE_ZONE || aKind == KIND_REGION || aKind == KIND_BATHYMETRY ||
737                     aKind == KIND_OBSTACLE || aKind == KIND_STREAM || aKind == KIND_CHANNEL ||
738                     aKind == KIND_DIGUE || aKind == KIND_DUMMY_3D || aKind == KIND_LAND_COVER_MAP;
739   if ( !visibility )
740   {
741     Handle(HYDROData_Profile) aProfObj = Handle(HYDROData_Profile)::DownCast( theModelObject );
742     visibility = !aProfObj.IsNull() && aProfObj->IsValid();
743   }
744
745   if ( aKind == KIND_REGION )
746   {
747       QString an = aResObj->name();
748       int a = 0;
749   }
750
751   if ( visibility )
752   {
753     setObjectVisibilityState( theModelObject, aResObj );
754   }
755
756   if ( theIsBuildTree )
757   {
758     buildObjectTree( theParent, aResObj, theParentEntry );
759   }
760
761   return aResObj;
762 }
763
764 LightApp_DataObject* HYDROGUI_DataModel::buildObject( SUIT_DataObject*     theParent,
765                                                       HYDROGUI_DataObject* theObject,
766                                                       const QString&       theParentEntry,
767                                                       const bool           theIsBuildTree,
768                                                       const bool           theIsInOperation )
769 {
770   if ( theIsBuildTree )
771   {
772     buildObjectTree( theParent, theObject, theParentEntry, theIsInOperation );
773   }
774   return theObject;
775 }
776
777 LightApp_DataObject* HYDROGUI_DataModel::createZone( SUIT_DataObject*       theParent,
778                                                      Handle(HYDROData_Zone) theModelObject,
779                                                      const QString&         theParentEntry,
780                                                      const bool             theIsBuildTree,
781                                                      const bool             theIsInOperation )
782 {
783   HYDROGUI_Zone* aZone = new HYDROGUI_Zone( theParent, theModelObject, theParentEntry, theIsInOperation );
784   LightApp_DataObject* aDataObj = buildObject( theParent, aZone, theParentEntry, theIsBuildTree, theIsInOperation );
785
786   setObjectVisibilityState( theModelObject, aZone );
787
788   return aDataObj;
789 }
790
791 LightApp_DataObject* HYDROGUI_DataModel::createRegion( SUIT_DataObject*         theParent,
792                                                        Handle(HYDROData_Region) theModelObject,
793                                                        const QString&           theParentEntry,
794                                                        const bool               theIsBuildTree,
795                                                        const bool               theIsInOperation )
796 {
797   return buildObject( theParent, new HYDROGUI_Region( theParent, theModelObject, theParentEntry, theIsInOperation ), 
798     theParentEntry, theIsBuildTree, theIsInOperation );
799 }
800
801 void HYDROGUI_DataModel::createDefaultStricklerTable( const Handle(HYDROData_Document)& theDocument,
802                                                       LightApp_DataObject*              theParent )
803 {
804   // Create default Strickler table object
805   Handle(HYDROData_StricklerTable) aStricklerTableObj =
806     Handle(HYDROData_StricklerTable)::DownCast( theDocument->CreateObject(KIND_STRICKLER_TABLE) );      
807   if ( !aStricklerTableObj.IsNull() )
808   {
809     SUIT_ResourceMgr* resMgr = module()->application()->resourceMgr();
810     QString defTablePath = resMgr->path( "resources", module()->name(), tr( "DEFAULT_STRICKLER_TABLE_FILE" ) );
811     aStricklerTableObj->Import( defTablePath );
812         // Set name
813     QString aStricklerTableName;
814     if ( aStricklerTableObj->GetName().isEmpty() )
815     {
816       HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
817       if ( aModule )
818         aStricklerTableName = HYDROGUI_Tool::GenerateObjectName( aModule, tr( "DEFAULT_STRICKLER_TABLE_NAME" ) );
819     }
820     if ( aStricklerTableObj->GetName() != aStricklerTableName )
821       aStricklerTableObj->SetName( aStricklerTableName );
822
823     aStricklerTableObj->Update();
824
825     LightApp_DataObject* obj = createObject( theParent, aStricklerTableObj );
826   }
827 }
828
829 LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject* theParent,
830                                                        const QString&   theName,
831                                                        const QString&   theParentEntry )
832 {
833   return new HYDROGUI_NamedObject( theParent, theName, theParentEntry );
834 }
835
836 void HYDROGUI_DataModel::buildObjectPartition( SUIT_DataObject*                   theObject,
837                                                const HYDROData_SequenceOfObjects& theObjects,
838                                                const QString&                     thePartName,
839                                                const bool                         theIsCreateEmpty )
840 {
841   if ( theObjects.IsEmpty() && !theIsCreateEmpty )
842     return;
843
844   HYDROGUI_DataObject* aGuiObj = dynamic_cast<HYDROGUI_DataObject*>( theObject );
845   if ( !aGuiObj )
846     return;
847
848   LightApp_DataObject* aPartSect = 
849     createObject( aGuiObj, thePartName, aGuiObj->entry() );
850
851   HYDROData_SequenceOfObjects::Iterator anIter( theObjects );
852   for ( ; anIter.More(); anIter.Next() )
853   {
854     Handle(HYDROData_Entity) anObj = anIter.Value();
855     if( !anObj.IsNull() && !anObj->IsRemoved() )
856       createObject( aPartSect, anObj, aGuiObj->entry(), false );
857   }
858 }
859
860 void HYDROGUI_DataModel::buildObjectTree( SUIT_DataObject* theParent,
861                                           SUIT_DataObject* theObject,
862                                           const QString&   theParentEntry,
863                                           const bool       theIsInOperation )
864 {
865   HYDROGUI_DataObject* aGuiObj = dynamic_cast<HYDROGUI_DataObject*>( theObject );
866   if ( !aGuiObj )
867     return;
868
869   Handle(HYDROData_Entity) aDataObj = aGuiObj->modelObject();
870   if ( aDataObj.IsNull() )
871     return;
872
873   if ( aDataObj->IsKind( STANDARD_TYPE(HYDROData_Object) ) )
874   {
875     Handle(HYDROData_Object) aGeomObj =
876       Handle(HYDROData_Object)::DownCast( aDataObj );
877
878     Handle(HYDROData_DummyObject3D) anObject3D = aGeomObj->GetObject3D();
879     if ( !anObject3D.IsNull() )
880       createObject( aGuiObj, anObject3D, "", false );
881
882 #ifdef DEB_GROUPS
883     HYDROData_SequenceOfObjects anObjGroups = aGeomObj->GetGroups();
884     buildObjectPartition( aGuiObj, anObjGroups, tr( "OBJECT_GROUPS" ), false );
885 #endif
886   }
887
888   ObjectKind anObjectKind = aDataObj->GetKind();
889
890   if ( anObjectKind == KIND_IMAGE )
891   {
892     Handle(HYDROData_Image) anImageObj =
893       Handle(HYDROData_Image)::DownCast( aDataObj );
894     for ( int anIndex = 0, aNbRef = anImageObj->NbReferences(); anIndex < aNbRef; anIndex++ )
895     {
896       Handle(HYDROData_Entity) aRefObj = anImageObj->Reference( anIndex );
897       if ( !aRefObj.IsNull() && !aRefObj->IsRemoved() )
898         createObject( aGuiObj, aRefObj, aGuiObj->entry(), false );
899     }
900   }
901   else if ( anObjectKind == KIND_IMMERSIBLE_ZONE )
902   {
903     Handle(HYDROData_ImmersibleZone) aZoneObj =
904       Handle(HYDROData_ImmersibleZone)::DownCast( aDataObj );
905
906     LightApp_DataObject* aPolylineSect = 
907       createObject( aGuiObj, tr( "ZONE_POLYLINE" ), aGuiObj->entry() );
908
909     Handle(HYDROData_PolylineXY) aPolyline = aZoneObj->GetPolyline();
910     if ( !aPolyline.IsNull() && !aPolyline->IsRemoved() )
911       createObject( aPolylineSect, aPolyline, aGuiObj->entry(), false );
912
913     LightApp_DataObject* aBathSect = 
914       createObject( aGuiObj, tr( "ZONE_BATHYMETRY" ), aGuiObj->entry() );
915
916     Handle(HYDROData_IAltitudeObject) anAltitudeObj = aZoneObj->GetAltitudeObject();
917     if ( !anAltitudeObj.IsNull() && !anAltitudeObj->IsRemoved() )
918       createObject( aBathSect, anAltitudeObj, aGuiObj->entry(), false );
919   }
920   else if ( anObjectKind == KIND_POLYLINE )
921   {
922     Handle(HYDROData_Polyline3D) aPolyline3D =
923       Handle(HYDROData_Polyline3D)::DownCast( aDataObj );
924
925     LightApp_DataObject* aPolylineSect = 
926       createObject( aGuiObj, tr( "POLYLINE3D_POLYLINE" ), aGuiObj->entry() );
927
928     Handle(HYDROData_PolylineXY) aPolylineXY = aPolyline3D->GetPolylineXY();
929     if ( !aPolylineXY.IsNull() && !aPolylineXY->IsRemoved() )
930       createObject( aPolylineSect, aPolylineXY, aGuiObj->entry(), false );
931
932     LightApp_DataObject* aProfileSect = 
933       createObject( aGuiObj, tr( "POLYLINE3D_PROFILE" ), aGuiObj->entry() );
934
935     Handle(HYDROData_ProfileUZ) aProfileUZ = aPolyline3D->GetProfileUZ();
936     if ( aProfileUZ.IsNull() || aProfileUZ->IsRemoved() )
937       aProfileUZ = aPolyline3D->GetChildProfileUZ( false );
938
939     if ( !aProfileUZ.IsNull() && !aProfileUZ->IsRemoved() )
940     {
941       Handle(HYDROData_Profile) aProfile = 
942         Handle(HYDROData_Profile)::DownCast( aProfileUZ->GetFatherObject() );
943       if ( !aProfile.IsNull() && !aProfile->IsRemoved() )
944         createObject( aProfileSect, aProfile, aGuiObj->entry(), false );
945     }
946
947     LightApp_DataObject* aBathSect = 
948       createObject( aGuiObj, tr( "POLYLINE3D_BATHYMETRY" ), aGuiObj->entry() );
949
950     Handle(HYDROData_IAltitudeObject) anAltitudeObj = aPolyline3D->GetAltitudeObject();
951     if ( !anAltitudeObj.IsNull() && !anAltitudeObj->IsRemoved() )
952       createObject( aBathSect, anAltitudeObj, aGuiObj->entry(), false );
953   }
954   else if ( anObjectKind == KIND_CALCULATION )
955   {
956     Handle(HYDROData_CalculationCase) aCaseObj =
957       Handle(HYDROData_CalculationCase)::DownCast( aDataObj );
958
959     LightApp_DataObject* aPolylineSect = 
960       createObject( aGuiObj, tr( "CASE_BOUNDARY" ), aGuiObj->entry() );
961
962     Handle(HYDROData_PolylineXY) aPolyline = aCaseObj->GetBoundaryPolyline();
963     if ( !aPolyline.IsNull() && !aPolyline->IsRemoved() )
964       createObject( aPolylineSect, aPolyline, aGuiObj->entry(), false );
965
966     LightApp_DataObject* aCaseAOSect = 
967       createObject( aGuiObj, tr( partitionName( KIND_ARTIFICIAL_OBJECT ).toAscii() ),
968                     aGuiObj->entry() );
969     LightApp_DataObject* aCaseNOSect = 
970       createObject( aGuiObj, tr( partitionName( KIND_NATURAL_OBJECT ).toAscii() ),
971                     aGuiObj->entry() );
972
973     HYDROData_SequenceOfObjects aSeq = aCaseObj->GetGeometryObjects();
974     HYDROData_SequenceOfObjects::Iterator aGOIter( aSeq );
975     Handle(HYDROData_Entity) anEntity;
976     Handle(HYDROData_ArtificialObject) anAObject;
977     Handle(HYDROData_NaturalObject) aNObject;
978     for ( ; aGOIter.More(); aGOIter.Next() )
979     {
980       anEntity = aGOIter.Value();
981       if ( anEntity.IsNull() )
982         continue;
983       anAObject = Handle(HYDROData_ArtificialObject)::DownCast( anEntity );
984       if ( !anAObject.IsNull() )
985         createObject( aCaseAOSect, anAObject, aGuiObj->entry(), false );
986       else
987       {
988         aNObject = Handle(HYDROData_NaturalObject)::DownCast( anEntity );
989         if ( !aNObject.IsNull() )
990           createObject( aCaseNOSect, aNObject, aGuiObj->entry(), false );
991       }
992     }
993
994     LightApp_DataObject* aLandCoverMapSect = 
995       createObject( aGuiObj, tr( "CASE_LAND_COVER_MAP" ), aGuiObj->entry() );
996
997     Handle(HYDROData_LandCoverMap) aLandCoverMap = aCaseObj->GetLandCoverMap();
998     if ( !aLandCoverMap.IsNull() && !aLandCoverMap->IsRemoved() )
999       createObject( aLandCoverMapSect, aLandCoverMap, aGuiObj->entry(), false );
1000
1001     LightApp_DataObject* aCaseRegionsSect = 
1002       createObject( aGuiObj, tr( "CASE_REGIONS" ), aGuiObj->entry() );
1003
1004     HYDROData_SequenceOfObjects aCaseRegions = aCaseObj->GetRegions();
1005     HYDROData_SequenceOfObjects::Iterator anIter( aCaseRegions );
1006     for ( ; anIter.More(); anIter.Next() )
1007     {
1008       Handle(HYDROData_Region) aCaseRegion =
1009         Handle(HYDROData_Region)::DownCast( anIter.Value() );
1010       if( !aCaseRegion.IsNull() && !aCaseRegion->IsRemoved() )
1011         createRegion( aCaseRegionsSect, aCaseRegion, "", true, theIsInOperation );
1012     }
1013    
1014 #ifdef DEB_GROUPS
1015     HYDROData_SequenceOfObjects aCalcGroups = aCaseObj->GetGeometryGroups();
1016     buildObjectPartition( aGuiObj, aCalcGroups, tr( "OBJECT_GROUPS" ), false );
1017
1018     HYDROData_SequenceOfObjects aCalcSplitGroups = aCaseObj->GetSplittedGroups();
1019     buildObjectPartition( aGuiObj, aCalcSplitGroups, tr( "CASE_SPLITTED_GROUPS" ), false );
1020 #endif
1021
1022   }
1023   else if ( anObjectKind == KIND_REGION )
1024   {
1025     Handle(HYDROData_Region) aRegionObj =
1026       Handle(HYDROData_Region)::DownCast( aDataObj );
1027
1028     HYDROData_SequenceOfObjects aRegionZones = aRegionObj->GetZones();
1029     HYDROData_SequenceOfObjects::Iterator anIter( aRegionZones );
1030     for ( ; anIter.More(); anIter.Next() )
1031     {
1032       Handle(HYDROData_Zone) aRegionZone =
1033         Handle(HYDROData_Zone)::DownCast( anIter.Value() );
1034       if( !aRegionZone.IsNull() && !aRegionZone->IsRemoved() )
1035         createZone( aGuiObj, aRegionZone, "", true, theIsInOperation );
1036     }
1037   }
1038   else if ( anObjectKind == KIND_PROFILE )
1039   {
1040     Handle(HYDROData_Profile) aProfileObj =
1041       Handle(HYDROData_Profile)::DownCast( aDataObj );
1042
1043     aGuiObj->setIsValid( aProfileObj->IsValid() );
1044   }
1045   else if ( anObjectKind == KIND_CHANNEL || anObjectKind == KIND_DIGUE )
1046   {
1047     Handle(HYDROData_Channel) aChannelObj =
1048       Handle(HYDROData_Channel)::DownCast( aDataObj );
1049
1050     LightApp_DataObject* aGuideLineSect = 
1051       createObject( aGuiObj, tr( "CHANNEL_GUIDE_LINE" ), aGuiObj->entry() );
1052     Handle(HYDROData_Polyline3D) aGuideLine = aChannelObj->GetGuideLine();
1053     if ( !aGuideLine.IsNull() && !aGuideLine->IsRemoved() ) {
1054       createObject( aGuideLineSect, aGuideLine, aGuiObj->entry(), false );
1055     }
1056
1057     LightApp_DataObject* aProfileSect = 
1058       createObject( aGuiObj, tr( "CHANNEL_PROFILE" ), aGuiObj->entry() );
1059     Handle(HYDROData_Profile) aProfile = aChannelObj->GetProfile();
1060     if ( !aProfile.IsNull() && !aProfile->IsRemoved() ) {
1061       createObject( aProfileSect, aProfile, aGuiObj->entry(), false );
1062     }
1063   }
1064   else if ( anObjectKind == KIND_STREAM )
1065   {
1066     Handle(HYDROData_Stream) aStreamObj =
1067       Handle(HYDROData_Stream)::DownCast( aDataObj );
1068
1069     LightApp_DataObject* aHydraulicAxisSect = 
1070       createObject( aGuiObj, tr( "STREAM_HYDRAULIC_AXIS" ), aGuiObj->entry() );
1071     Handle(HYDROData_PolylineXY) aHydraulicAxis = aStreamObj->GetHydraulicAxis();
1072     if ( !aHydraulicAxis.IsNull() && !aHydraulicAxis->IsRemoved() ) {
1073       createObject( aHydraulicAxisSect, aHydraulicAxis, aGuiObj->entry(), false );
1074     }
1075
1076     HYDROData_SequenceOfObjects aProfiles = aStreamObj->GetProfiles();
1077     buildObjectPartition( aGuiObj, aProfiles, tr( "STREAM_PROFILES" ), true );
1078
1079     Handle(HYDROData_Polyline3D) aBottomPolyline = aStreamObj->GetBottomPolyline();
1080     if ( !aBottomPolyline.IsNull() && !aBottomPolyline->IsRemoved() ) {
1081       createObject( aGuiObj, aBottomPolyline, aGuiObj->entry(), false );
1082     }
1083   }
1084   else if ( anObjectKind == KIND_LAND_COVER_MAP )
1085   {
1086     Handle(HYDROData_LandCoverMap) aLandCoverMapObj =
1087       Handle(HYDROData_LandCoverMap)::DownCast( aDataObj );
1088
1089     /*TODO: reference objects of the land cover map 
1090     HYDROData_SequenceOfObjects aPolylines = aLandCoverMapObj->GetPolylines();
1091     buildObjectPartition( aGuiObj, aPolylines, tr( "LAND_COVER_POLYLINES" ), true );*/
1092   }
1093   
1094   HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
1095   if( aModule )
1096     aModule->enableLCMActions();
1097   
1098 }
1099
1100 void HYDROGUI_DataModel::buildCaseTree( SUIT_DataObject* theParent, Handle(HYDROData_CalculationCase) theCase )
1101 {
1102   if ( !theCase.IsNull() )
1103   {
1104     if ( theParent )
1105     {
1106       // Remove previous objects tree
1107         DataObjectList aList;
1108         theParent->children( aList );
1109         QListIterator<SUIT_DataObject*> anIter( aList );
1110         while( anIter.hasNext() )
1111           removeChild( theParent, anIter.next() );
1112     }
1113
1114     new HYDROGUI_DropTargetObject( theParent, tr( "NEW_REGION" ), "", true );
1115
1116     HYDROData_SequenceOfObjects aCaseRegions = theCase->GetRegions();
1117     HYDROData_SequenceOfObjects::Iterator anIter( aCaseRegions );
1118     for ( ; anIter.More(); anIter.Next() )
1119     {
1120       Handle(HYDROData_Region) aCaseRegion =
1121         Handle(HYDROData_Region)::DownCast( anIter.Value() );
1122       if( !aCaseRegion.IsNull() && !aCaseRegion->IsRemoved() )
1123         createRegion( theParent, aCaseRegion, "", true, true );
1124     }
1125   }
1126 }
1127
1128 void HYDROGUI_DataModel::updateObjectTree( Handle(HYDROData_Entity)& theObj )
1129 {
1130   if ( !theObj.IsNull() )
1131   {
1132     HYDROGUI_DataObject* aGuiObj = dynamic_cast<HYDROGUI_DataObject*>(
1133       findObject( HYDROGUI_DataObject::dataObjectEntry( theObj ) ) );
1134     if ( aGuiObj )
1135     {
1136       // Remove previous objects tree
1137       DataObjectList aList;
1138       aGuiObj->children( aList );
1139       QListIterator<SUIT_DataObject*> anIter( aList );
1140       while( anIter.hasNext() )
1141         removeChild( aGuiObj, anIter.next() );
1142
1143       // Rebuild the subtree
1144       QString aParentEntry;
1145       HYDROGUI_DataObject* aParent = dynamic_cast<HYDROGUI_DataObject*>( aGuiObj->parent() );
1146       if ( aParent )
1147       {
1148         aParentEntry = aParent->entry();
1149       }
1150       buildObjectTree( aParent, aGuiObj, aParentEntry, aGuiObj->isInOperation() );
1151     } 
1152     else
1153     {
1154       // workaround for the bug in SalomeApp_Study::findObjectByEntry - it can't find LightApp_DataObjects
1155       HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
1156       if( aModule )
1157       {
1158         aModule->getApp()->updateObjectBrowser();
1159       }
1160     }
1161   }
1162 }
1163
1164 void HYDROGUI_DataModel::removeChild( SUIT_DataObject* theParent,
1165                                       SUIT_DataObject* theChild )
1166 {
1167   SUIT_DataObject* aSubChild = theChild->firstChild();
1168   for( ; aSubChild; aSubChild = aSubChild->nextBrother() )
1169     removeChild( theChild, aSubChild );
1170   theParent->removeChild( theChild );
1171 }
1172
1173 SUIT_DataObject* HYDROGUI_DataModel::findChildByName( const SUIT_DataObject* theFather,
1174                                                       const QString& theName )
1175 {
1176   SUIT_DataObject* aChild = theFather->firstChild();
1177   while( aChild )
1178   {
1179     if( aChild->name() == theName )
1180       return aChild; // found
1181     aChild = aChild->nextBrother();
1182   }
1183   return NULL; // not found
1184 }
1185
1186 bool HYDROGUI_DataModel::createNewRegion( Handle(HYDROData_CalculationCase) theCase, 
1187                                          const QList<HYDROGUI_Zone*>& theZonesList )
1188 {
1189   bool isOk = !theCase.IsNull();
1190   if ( isOk )
1191   {
1192     Handle(HYDROData_Region) aRegion;
1193     Handle(HYDROData_Zone) aZone;
1194     for (int i = 0; i < theZonesList.length(); i++ )
1195     {
1196       aZone = Handle(HYDROData_Zone)::DownCast( theZonesList.at(i)->modelObject() );
1197       if ( !aZone.IsNull() )
1198       {
1199         if ( aRegion.IsNull() )
1200         {
1201           aRegion = theCase->AddNewRegion( aZone );
1202           isOk = !aRegion.IsNull();
1203         }
1204         else
1205         {
1206           if ( !( aRegion->AddZone( aZone ) ) )
1207           {
1208             isOk = false;
1209           }
1210         }
1211       }
1212     }
1213   }
1214   return isOk;
1215 }
1216
1217 bool HYDROGUI_DataModel::rename( Handle(HYDROData_Entity) theEntity, const QString& theName )
1218 {
1219   if ( theName.isEmpty() )
1220     return false;
1221
1222   try 
1223   {
1224     getDocument()->StartOperation();
1225     theEntity->SetName( theName );
1226     getDocument()->CommitOperation( HYDROGUI_Tool::ToExtString( tr("RENAME_TO").arg( theName ) ) );
1227     module()->application()->activeStudy()->Modified();
1228   }
1229   catch ( Standard_Failure )
1230   {
1231     getDocument()->AbortOperation();
1232     return false;
1233   }
1234   return true;
1235 }
1236
1237 void HYDROGUI_DataModel::updateDocument()
1238 {
1239     // Sets the default strickler coefficient from preferences to document.
1240     Handle(HYDROData_Document) aDoc = getDocument();
1241     SUIT_ResourceMgr* resMgr = module()->application()->resourceMgr();
1242     if ( resMgr && !aDoc.IsNull() )
1243       aDoc->SetDefaultStricklerCoefficient( resMgr->doubleValue( "preferences", "default_strickler_coefficient", 0 ) );
1244 }
1245
1246 void HYDROGUI_DataModel::setObjectVisibilityState( Handle(HYDROData_Entity) theModelObject,
1247                                                    HYDROGUI_DataObject* theObject )
1248 {
1249   SUIT_AbstractModel* treeModel = 0;
1250   LightApp_Application* app = dynamic_cast<LightApp_Application*>( module()->application() );
1251   if ( app )
1252     treeModel = dynamic_cast<SUIT_AbstractModel*>( app->objectBrowser()->model() );
1253
1254   if ( treeModel )
1255   {
1256     HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
1257     bool isVisible = aModule->isObjectVisible( -1, theModelObject );
1258     Qtx::VisibilityState aVisState = isVisible ? Qtx::ShownState : Qtx::HiddenState;
1259     treeModel->setVisibilityState( theObject->text( theObject->customData( Qtx::IdType ).toInt() ), aVisState, false );
1260   }
1261 }