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