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