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