Salome HOME
Rolling back incorrect integration
[modules/gui.git] / src / SOCC / SOCC_ViewModel.cxx
1 #include "SOCC_ViewModel.h"
2
3 #include "SOCC_Prs.h"
4 #include "SOCC_ViewWindow.h"
5
6 #include "SUIT_Session.h"
7 #include "SUIT_Application.h"
8
9 #include "ToolsGUI.h"
10
11 #include <SALOMEconfig.h>
12 #include CORBA_CLIENT_HEADER(SALOMEDS)
13
14 #include <AIS_ListIteratorOfListOfInteractive.hxx>
15 #include <Visual3d_View.hxx>
16
17 #include <SALOME_AISShape.hxx>
18 #include <SALOME_AISObject.hxx>
19
20 #include <Utils_ORB_INIT.hxx>
21 #include <Utils_SINGLETON.hxx>
22 #include <SALOME_ModuleCatalog_impl.hxx>
23 #include <SALOME_NamingService.hxx>
24
25 #include "SALOMEDSClient.hxx"
26 #include "SALOMEDS_StudyManager.hxx"
27
28 #include <AIS_TypeOfIso.hxx>
29
30 // in order NOT TO link with SalomeApp, here the code returns SALOMEDS_Study.
31 // SalomeApp_Study::studyDS() does it as well, but -- here it is retrieved from 
32 // SALOMEDS::StudyManager - no linkage with SalomeApp. 
33
34 static _PTR(Study) getStudyDS() 
35 {
36   SALOMEDSClient_Study* aStudy = NULL;
37   _PTR(StudyManager) aMgr( new SALOMEDS_StudyManager() );
38
39   // get id of SUIT_Study, if it's a SalomeApp_Study, it will return
40   //    id of its underlying SALOMEDS::Study
41   SUIT_Application* app = SUIT_Session::session()->activeApplication();
42   if ( !app )  return _PTR(Study)(aStudy); 
43   SUIT_Study* stud = app->activeStudy();
44   if ( !stud ) return _PTR(Study)(aStudy);  
45   const int id = stud->id(); // virtual method, must return SALOMEDS_Study id
46   // get SALOMEDS_Study with this id from StudyMgr
47   return aMgr->GetStudyByID( id );
48 }
49
50 SOCC_Viewer::SOCC_Viewer( bool DisplayTrihedron )
51 : OCCViewer_Viewer( DisplayTrihedron )
52 {
53 }
54
55 SOCC_Viewer::~SOCC_Viewer()
56 {
57 }
58
59 bool SOCC_Viewer::highlight( const Handle(SALOME_InteractiveObject)& obj,
60                              bool hilight, bool upd )
61 {
62   bool isInLocal = getAISContext()->HasOpenedContext();
63   //SUIT_Study* ActiveStudy = SUIT_Application::getDesktop()->getActiveStudy();
64   //SALOME_Selection* Sel = SALOME_Selection::Selection( ActiveStudy->getSelection() );
65
66   AIS_ListOfInteractive List;
67   getAISContext()->DisplayedObjects(List);
68   
69   AIS_ListIteratorOfListOfInteractive ite(List);
70   for ( ; ite.More(); ite.Next() )
71   {
72     Handle(SALOME_InteractiveObject) anObj =
73       Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
74
75     if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
76     {
77       if ( !isInLocal )
78           OCCViewer_Viewer::highlight( ite.Value(), hilight, false );
79       // highlight subshapes only when local selection is active
80       else
81       {
82         /*if ( ite.Value()->IsKind( STANDARD_TYPE( SALOME_AISShape ) ) )
83         {
84           Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast( ite.Value() );
85           TColStd_IndexedMapOfInteger MapIndex;
86           Sel->GetIndex( IObject, MapIndex );
87           aSh->highlightSubShapes( MapIndex, highlight );
88         }*/
89       }
90       break;
91     }
92   }
93     
94   if( upd )
95     update();
96     
97   return false;
98 }
99
100 bool SOCC_Viewer::isInViewer( const Handle(SALOME_InteractiveObject)& obj,
101                               bool onlyInViewer )
102 {
103   AIS_ListOfInteractive List;
104   getAISContext()->DisplayedObjects(List);
105
106   if( !onlyInViewer ) {
107     AIS_ListOfInteractive List1;
108     getAISContext()->ObjectsInCollector(List1);
109     List.Append(List1);
110   }
111
112   AIS_ListIteratorOfListOfInteractive ite(List);
113   for ( ; ite.More(); ite.Next() )
114   {
115     Handle(SALOME_InteractiveObject) anObj =
116         Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
117
118     if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
119       return true;
120   }
121   return false;
122 }
123
124 bool SOCC_Viewer::isVisible( const Handle(SALOME_InteractiveObject)& obj )
125 {
126   AIS_ListOfInteractive List;
127   getAISContext()->DisplayedObjects( List );
128
129   AIS_ListIteratorOfListOfInteractive ite( List );
130   for ( ; ite.More(); ite.Next() )
131   {
132     Handle(SALOME_InteractiveObject) anObj =
133       Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
134
135     if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
136       return getAISContext()->IsDisplayed( ite.Value() );
137   }
138   
139   return false;
140 }
141
142 void SOCC_Viewer::setColor( const Handle(SALOME_InteractiveObject)& obj,
143                             const QColor& color, bool update )
144 {
145   AIS_ListOfInteractive List;
146   getAISContext()->DisplayedObjects(List);
147   
148   AIS_ListIteratorOfListOfInteractive ite(List);
149   for ( ; ite.More(); ite.Next() )
150   {
151     Handle(SALOME_InteractiveObject) anObj =
152         Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
153
154     if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
155     {
156       OCCViewer_Viewer::setColor( ite.Value(), color, update );
157       return;
158     }
159   }
160 }
161
162 void SOCC_Viewer::switchRepresentation( const Handle(SALOME_InteractiveObject)& obj,
163                                         int mode, bool update )
164 {
165   AIS_ListOfInteractive List;
166   getAISContext()->DisplayedObjects(List);
167   
168   AIS_ListIteratorOfListOfInteractive ite(List);
169   for ( ; ite.More(); ite.Next() )
170   {
171     Handle(SALOME_InteractiveObject) anObj =
172         Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
173
174     if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
175     {
176       OCCViewer_Viewer::switchRepresentation( ite.Value(), mode, update );
177       return;
178     }
179   }
180 }
181
182 void SOCC_Viewer::setTransparency( const Handle(SALOME_InteractiveObject)& obj,
183                                    float trans, bool update )
184 {
185   AIS_ListOfInteractive List;
186   getAISContext()->DisplayedObjects( List );
187   
188   AIS_ListIteratorOfListOfInteractive ite( List );
189   for ( ; ite.More(); ite.Next() )
190   {
191     Handle(SALOME_InteractiveObject) anObj =
192         Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
193
194     if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( obj ) )
195     {
196       OCCViewer_Viewer::setTransparency( ite.Value(), trans, update );
197       return;
198     }
199   }
200 }
201
202 void SOCC_Viewer::rename( const Handle(SALOME_InteractiveObject)& obj,
203                           const QString& name )
204 {
205   AIS_ListOfInteractive List;
206   getAISContext()->DisplayedObjects(List);
207   
208   AIS_ListIteratorOfListOfInteractive ite(List);
209   while (ite.More())
210   {
211     if (ite.Value()->IsKind(STANDARD_TYPE(SALOME_AISShape)))
212     {
213       Handle(SALOME_AISShape) aSh = Handle(SALOME_AISShape)::DownCast(ite.Value());
214       
215       if ( aSh->hasIO() )
216       {
217         Handle(SALOME_InteractiveObject) IO = aSh->getIO();
218         if ( IO->isSame( obj ) )
219         {
220           aSh->setName( (char*)name.latin1() );
221           break;
222         }
223       }
224     }
225     else if ( ite.Value()->IsKind( STANDARD_TYPE( SALOME_AISObject ) ) )
226     {
227       Handle(SALOME_AISObject) aSh = Handle(SALOME_AISObject)::DownCast( ite.Value() );
228
229       // Add code here, if someone create a MODULE_AISObject.
230     }
231     ite.Next();
232   }
233 }
234
235 //=======================================================================
236 // name    : Display
237 // Purpose : Display presentation
238 //=======================================================================
239 void SOCC_Viewer::Display( const SALOME_OCCPrs* prs )
240 {
241   // try do downcast object
242   const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( prs );
243   if ( !anOCCPrs || anOCCPrs->IsNull() )
244     return;
245
246   // get SALOMEDS Study
247   _PTR(Study) study(getStudyDS());
248
249   // get context
250   Handle (AIS_InteractiveContext) ic = getAISContext();
251
252   // get all displayed objects
253   AIS_ListOfInteractive List;
254   ic->DisplayedObjects( List );
255   // get objects in the collector
256   AIS_ListOfInteractive ListCollector;
257   ic->ObjectsInCollector( ListCollector );
258
259   // get objects to be displayed
260   AIS_ListOfInteractive anAISObjects;
261   anOCCPrs->GetObjects( anAISObjects );
262
263   AIS_ListIteratorOfListOfInteractive aIter( anAISObjects );
264   for ( ; aIter.More(); aIter.Next() )
265   {
266     Handle(AIS_InteractiveObject) anAIS = aIter.Value();
267     if ( !anAIS.IsNull() )
268     {
269       // try to find presentation in the viewer
270       bool bDisplayed = false;
271       AIS_ListIteratorOfListOfInteractive ite( List );
272       for ( ; ite.More(); ite.Next() )
273       {
274         // compare presentations by handles
275         // if the object is already displayed - nothing to do more
276         if ( ite.Value() == anAIS )
277         {
278           // Deactivate object if necessary
279           if ( !anOCCPrs->ToActivate() )
280             ic->Deactivate( anAIS );
281           bDisplayed = true;
282           break;
283         }
284       }
285
286       if ( bDisplayed )
287         continue;
288
289       // then try to find presentation in the collector
290       bDisplayed = false;
291       ite.Initialize( ListCollector );
292       for ( ; ite.More(); ite.Next() )
293       {
294         // compare presentations by handles
295         // if the object is in collector - display it
296         if ( ite.Value() == anAIS )
297         {
298           ic->DisplayFromCollector( anAIS, false );
299
300           // Deactivate object if necessary
301           if ( !anOCCPrs->ToActivate() )
302             ic->Deactivate( anAIS );
303           bDisplayed = true;
304
305           // Set visibility flag
306           Handle(SALOME_InteractiveObject) anObj =
307             Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
308           if ( !anObj.IsNull() && anObj->hasEntry() )
309           {
310             if ( study )
311               ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
312           }
313
314           break;
315         }
316       }
317       if ( bDisplayed )
318         continue;
319
320       // if object is not displayed and not found in the collector - display it
321       if ( anAIS->IsKind( STANDARD_TYPE(AIS_Trihedron) ) )
322       {
323         Handle(AIS_Trihedron) aTrh = Handle(AIS_Trihedron)::DownCast( anAIS );
324         double aNewSize = 100, aSize = 100;
325         getTrihedronSize( aNewSize, aSize );
326         aTrh->SetSize( aTrh == getTrihedron() ? aNewSize : 0.5 * aNewSize );
327       }
328
329       ic->Display( anAIS, false );
330
331       // Set visibility flag
332       Handle(SALOME_InteractiveObject) anObj =
333         Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
334       if ( !anObj.IsNull() && anObj->hasEntry() )
335       {
336         if ( study  )
337           ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
338       }
339
340       // Deactivate object if necessary
341       if ( !anOCCPrs->ToActivate() )
342         ic->Deactivate( anAIS );
343     }
344   }
345 }
346
347 //=======================================================================
348 // name    : Erase
349 // Purpose : Erase presentation
350 //=======================================================================
351 void SOCC_Viewer::Erase( const SALOME_OCCPrs* prs, const bool forced )
352 {
353   // try do downcast object
354   const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( prs );
355   if ( !anOCCPrs || anOCCPrs->IsNull() )
356     return;
357
358   // get SALOMEDS Study
359   _PTR(Study) study(getStudyDS());
360
361   // get context
362   Handle(AIS_InteractiveContext) ic = getAISContext();
363
364   // get objects to be erased
365   AIS_ListOfInteractive anAISObjects;
366   anOCCPrs->GetObjects( anAISObjects );
367
368   AIS_ListIteratorOfListOfInteractive aIter( anAISObjects );
369   for ( ; aIter.More(); aIter.Next() ) {
370     Handle(AIS_InteractiveObject) anAIS = aIter.Value();
371     if ( !anAIS.IsNull() ) {
372       // erase the object from context : move it to collector
373       ic->Erase( anAIS, false, forced ? false : true );
374
375       // Set visibility flag if necessary
376       if ( !forced )
377       {
378         Handle(SALOME_InteractiveObject) anObj =
379           Handle(SALOME_InteractiveObject)::DownCast( anAIS->GetOwner() );
380         if ( !anObj.IsNull() && anObj->hasEntry() )
381         {
382           if ( study )
383             ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
384         }
385       }
386     }
387   }
388 }
389
390 //=======================================================================
391 // name    : EraseAll
392 // Purpose : Erase all presentations
393 //=======================================================================
394 void SOCC_Viewer::EraseAll( const bool forced )
395 {
396   // get SALOMEDS Study
397   _PTR(Study) study(getStudyDS());
398
399   // get context
400   Handle(AIS_InteractiveContext) ic = getAISContext();
401
402   // check if trihedron is displayed
403   Standard_Boolean isTrihedronDisplayed = ic->IsDisplayed( getTrihedron() );
404
405   // get objects to be erased (all currently displayed objects)
406   AIS_ListOfInteractive aList;
407   ic->DisplayedObjects( aList );
408   AIS_ListIteratorOfListOfInteractive anIter( aList );
409   for ( ; anIter.More(); anIter.Next() ) {
410     if ( isTrihedronDisplayed && anIter.Value()->DynamicType() == STANDARD_TYPE( AIS_Trihedron ) )
411       continue;
412
413     // erase an object
414     Handle(AIS_InteractiveObject) anIO = anIter.Value();
415     ic->Erase( anIO, false, forced ? false : true );
416     
417     // Set visibility flag if necessary
418     if ( !forced ) {
419       Handle(SALOME_InteractiveObject) anObj =
420         Handle(SALOME_InteractiveObject)::DownCast( anIO->GetOwner() );
421
422       if ( !anObj.IsNull() && anObj->hasEntry() ) {
423         if ( study )
424           ToolsGUI::SetVisibility( study, anObj->getEntry(), true, this );
425       }
426     }
427   }
428
429   // display trihedron if necessary
430   if ( isTrihedronDisplayed )
431     getAISContext()->Display( getTrihedron() );
432   else
433     Repaint();
434 }
435
436 //=======================================================================
437 // name    : CreatePrs
438 // Purpose : Create presentation corresponding to the entry
439 //=======================================================================
440 SALOME_Prs* SOCC_Viewer::CreatePrs( const char* entry )
441 {
442   SOCC_Prs* prs = new SOCC_Prs();
443   if ( entry )
444   {
445     // get context
446     Handle(AIS_InteractiveContext) ic = getAISContext();
447
448     // get displayed objects
449     AIS_ListOfInteractive List;
450     ic->DisplayedObjects( List );
451     // get objects in the collector
452     AIS_ListOfInteractive ListCollector;
453     ic->ObjectsInCollector( ListCollector );
454     List.Append( ListCollector );
455
456     AIS_ListIteratorOfListOfInteractive ite( List );
457     for ( ; ite.More(); ite.Next() )
458     {
459       Handle(SALOME_InteractiveObject) anObj =
460         Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
461
462       if ( !anObj.IsNull() && anObj->hasEntry() && strcmp( anObj->getEntry(), entry ) == 0 )
463         prs->AddObject( ite.Value() );
464     }
465   }
466   return prs;
467 }
468
469 //=======================================================================
470 // name    : LocalSelection
471 // Purpose : Activates selection of sub shapes
472 //=======================================================================
473 void SOCC_Viewer::LocalSelection( const SALOME_OCCPrs* thePrs, const int theMode )
474 {
475   Handle(AIS_InteractiveContext) ic = getAISContext();
476   
477   const SOCC_Prs* anOCCPrs = dynamic_cast<const SOCC_Prs*>( thePrs );
478   if ( ic.IsNull() )
479     return;
480   
481   // Open local context if there is no one
482   bool allObjects = thePrs == 0 || thePrs->IsNull();
483   if ( !ic->HasOpenedContext() ) {
484     ic->ClearCurrents( false );
485     ic->OpenLocalContext( allObjects, true, true );
486   }
487
488   AIS_ListOfInteractive anObjs;
489   // Get objects to be activated
490   if ( allObjects ) 
491     ic->DisplayedObjects( anObjs );
492   else
493     anOCCPrs->GetObjects( anObjs );
494
495   // Activate selection of objects from prs
496   AIS_ListIteratorOfListOfInteractive aIter( anObjs );
497   for ( ; aIter.More(); aIter.Next() ) {
498     Handle(AIS_InteractiveObject) anAIS = aIter.Value();
499     if ( !anAIS.IsNull() )
500     {
501       if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
502       {
503         ic->Load( anAIS, -1, false );
504         ic->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)theMode ) );
505       }
506       else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
507       {
508         ic->Load( anAIS, -1, false );
509         ic->Activate( anAIS, theMode );
510       }
511     }
512   }
513 }
514
515 //=======================================================================
516 // name    : GlobalSelection
517 // Purpose : Deactivates selection of sub shapes
518 //=======================================================================
519 void SOCC_Viewer::GlobalSelection( const bool update ) const
520 {
521   Handle(AIS_InteractiveContext) ic = getAISContext();
522   if ( !ic.IsNull() )
523   {
524     ic->CloseAllContexts( false );
525     if ( update )
526       ic->CurrentViewer()->Redraw();
527   }
528 }
529
530 //=======================================================================
531 // name    : BeforeDisplay
532 // Purpose : Axiluary method called before displaying of objects
533 //=======================================================================
534 void  SOCC_Viewer::BeforeDisplay( SALOME_Displayer* d )
535 {
536   d->BeforeDisplay( this, SALOME_OCCViewType() );
537 }
538
539 //=======================================================================
540 // name    : AfterDisplay
541 // Purpose : Axiluary method called after displaying of objects
542 //=======================================================================
543 void SOCC_Viewer::AfterDisplay( SALOME_Displayer* d )
544 {
545   d->AfterDisplay( this, SALOME_OCCViewType() );
546 }
547
548 //=======================================================================
549 // name    : getTrihedronSize
550 // Purpose : Get new and current trihedron size corresponding to the
551 //           current model size
552 //=======================================================================
553 bool SOCC_Viewer::getTrihedronSize( double& theNewSize, double& theSize )
554 {
555   theNewSize = 100;
556   theSize = 100;
557
558   //SRN: BUG IPAL8996, a usage of method ActiveView without an initialization
559   Handle(V3d_Viewer) viewer = getViewer3d();
560   viewer->InitActiveViews();
561   if(!viewer->MoreActiveViews()) return false;
562
563   Handle(V3d_View) view3d = viewer->ActiveView();
564   //SRN: END of fix
565
566   if ( view3d.IsNull() )
567     return false;
568
569   double Xmin = 0, Ymin = 0, Zmin = 0, Xmax = 0, Ymax = 0, Zmax = 0;
570   double aMaxSide;
571
572   view3d->View()->MinMaxValues( Xmin, Ymin, Zmin, Xmax, Ymax, Zmax );
573
574   if ( Xmin == RealFirst() || Ymin == RealFirst() || Zmin == RealFirst() ||
575        Xmax == RealLast()  || Ymax == RealLast()  || Zmax == RealLast() )
576     return false;
577
578   aMaxSide = Xmax - Xmin;
579   if ( aMaxSide < Ymax -Ymin ) aMaxSide = Ymax -Ymin;
580   if ( aMaxSide < Zmax -Zmin ) aMaxSide = Zmax -Zmin;
581
582   float aSizeInPercents = SUIT_Session::session()->resourceMgr()->doubleValue("Viewer","TrihedronSize", 105.);
583
584   static float EPS = 5.0E-3;
585   theSize = getTrihedron()->Size();
586   theNewSize = aMaxSide*aSizeInPercents / 100.0;
587
588   return fabs( theNewSize - theSize ) > theSize * EPS ||
589          fabs( theNewSize - theSize) > theNewSize * EPS;
590 }
591
592 //=======================================================================
593 // name    : Repaint
594 // Purpose : 
595 //=======================================================================
596 void SOCC_Viewer::Repaint()
597 {
598 //  onAdjustTrihedron();
599   getViewer3d()->Update();
600 }
601
602 //=======================================================================
603 // name    : createView
604 // Purpose : create SOCC_ViewWindow
605 //=======================================================================
606 SUIT_ViewWindow* SOCC_Viewer::createView( SUIT_Desktop* theDesktop )
607 {
608   SOCC_ViewWindow* view = new SOCC_ViewWindow(theDesktop, this);
609   initView( view );
610   return view;
611 }