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