]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_Displayer.cpp
Salome HOME
afc04003cc954e6fe3c3acc092138325c5337873
[modules/shaper.git] / src / XGUI / XGUI_Displayer.cpp
1 // File:        XGUI_Displayer.cpp
2 // Created:     20 Apr 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include "XGUI_Displayer.h"
6 #include "XGUI_Viewer.h"
7 #include "XGUI_Workshop.h"
8 #include "XGUI_ViewerProxy.h"
9
10 #include <ModelAPI_Document.h>
11 #include <ModelAPI_Data.h>
12 #include <ModelAPI_Object.h>
13 #include <ModelAPI_Tools.h>
14
15 #include <ModuleBase_ResultPrs.h>
16
17 #include <GeomAPI_Shape.h>
18 #include <GeomAPI_IPresentable.h>
19
20 #include <AIS_InteractiveContext.hxx>
21 #include <AIS_LocalContext.hxx>
22 #include <AIS_ListOfInteractive.hxx>
23 #include <AIS_ListIteratorOfListOfInteractive.hxx>
24 #include <AIS_DimensionSelectionMode.hxx>
25 #include <AIS_Shape.hxx>
26 #include <AIS_Dimension.hxx>
27 #include <TColStd_ListIteratorOfListOfInteger.hxx>
28 #include <SelectMgr_ListOfFilter.hxx>
29 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
30
31 #include <set>
32
33 const int MOUSE_SENSITIVITY_IN_PIXEL = 10;  ///< defines the local context mouse selection sensitivity
34
35 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
36 {
37   myWorkshop = theWorkshop;
38 }
39
40 XGUI_Displayer::~XGUI_Displayer()
41 {
42 }
43
44 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
45 {
46   return myResult2AISObjectMap.find(theObject) != myResult2AISObjectMap.end();
47 }
48
49 void XGUI_Displayer::display(ObjectPtr theObject, bool isUpdateViewer)
50 {
51   if (isVisible(theObject)) {
52     redisplay(theObject, isUpdateViewer);
53   } else {
54     AISObjectPtr anAIS;
55
56     GeomPresentablePtr aPrs = boost::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
57     bool isShading = false;
58     if (aPrs) {
59       anAIS = aPrs->getAISObject(AISObjectPtr());
60     } else {
61       ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
62       if (aResult) {
63         boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
64         if (aShapePtr) {
65           anAIS = AISObjectPtr(new GeomAPI_AISObject());
66           anAIS->setImpl(new Handle(AIS_InteractiveObject)(new ModuleBase_ResultPrs(aResult)));
67           //anAIS->createShape(aShapePtr);
68           isShading = true;
69         }
70       }
71     }
72     if (anAIS)
73       display(theObject, anAIS, isShading, isUpdateViewer);
74   }
75 }
76
77 void XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS, 
78                              bool isShading, bool isUpdateViewer)
79 {
80   Handle(AIS_InteractiveContext) aContext = AISContext();
81   if (aContext.IsNull())
82     return;
83
84   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
85   if (!anAISIO.IsNull()) {
86     myResult2AISObjectMap[theObject] = theAIS;
87     aContext->Display(anAISIO, false);
88     aContext->SetDisplayMode(anAISIO, isShading? Shading : Wireframe, isUpdateViewer);
89   }
90 }
91
92 void XGUI_Displayer::erase(ObjectPtr theObject, const bool isUpdateViewer)
93 {
94   if (!isVisible(theObject))
95     return;
96
97   Handle(AIS_InteractiveContext) aContext = AISContext();
98   if (aContext.IsNull())
99     return;
100   AISObjectPtr anObject = myResult2AISObjectMap[theObject];
101   if (anObject) {
102     Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
103     if (!anAIS.IsNull()) {
104       aContext->Remove(anAIS, isUpdateViewer);
105     }
106   }
107   myResult2AISObjectMap.erase(theObject);
108 }
109
110 void XGUI_Displayer::redisplay(ObjectPtr theObject, bool isUpdateViewer)
111 {
112   if (!isVisible(theObject))
113     return;
114
115   AISObjectPtr aAISObj = getAISObject(theObject);
116   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
117
118   GeomPresentablePtr aPrs = boost::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
119   if (aPrs) {
120     AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
121     if (!aAIS_Obj) {
122       erase(theObject, isUpdateViewer);
123       return;
124     }
125     if (aAIS_Obj != aAISObj) {
126       myResult2AISObjectMap[theObject] = aAIS_Obj;
127     }
128     aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
129   }
130
131   if (!aAISIO.IsNull()) {
132     Handle(AIS_InteractiveContext) aContext = AISContext();
133     if (aContext.IsNull())
134       return;
135     aContext->Redisplay(aAISIO, isUpdateViewer);
136   }
137 }
138
139 void XGUI_Displayer::deactivate(ObjectPtr theObject)
140 {
141   if (isVisible(theObject)) {
142     Handle(AIS_InteractiveContext) aContext = AISContext();
143     if (aContext.IsNull())
144       return;
145
146     AISObjectPtr anObj = myResult2AISObjectMap[theObject];
147     Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
148     aContext->Deactivate(anAIS);
149   }
150 }
151
152 void XGUI_Displayer::activate(ObjectPtr theObject, const QIntList& theModes)
153 {
154   if (isVisible(theObject)) {
155     Handle(AIS_InteractiveContext) aContext = AISContext();
156     if (aContext.IsNull())
157       return;
158
159     AISObjectPtr anObj = myResult2AISObjectMap[theObject];
160     Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
161     if (aContext->HasOpenedContext()) {
162       aContext->Load(anAIS, -1, true);
163     }
164     if (theModes.size() > 0) {
165       foreach(int aMode, theModes) {
166         aContext->Activate(anAIS, aMode);
167       }
168     } else 
169       aContext->Activate(anAIS);
170   }
171 }
172
173 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
174 {
175   Handle(AIS_InteractiveContext) aContext = AISContext();
176   if (aContext.IsNull())
177     return false;
178   if (!isVisible(theObject))
179     return false;
180     
181   AISObjectPtr anObj = myResult2AISObjectMap.at(theObject);
182   Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
183
184   TColStd_ListOfInteger aModes;
185   aContext->ActivatedModes(anAIS, aModes);
186   return aModes.Extent() > 0;
187 }
188
189 void XGUI_Displayer::stopSelection(const QList<ObjectPtr>& theResults, const bool isStop,
190                                    const bool isUpdateViewer)
191 {
192   Handle(AIS_InteractiveContext) aContext = AISContext();
193   if (aContext.IsNull())
194     return;
195
196   Handle(AIS_Shape) anAIS;
197   QList<ObjectPtr>::const_iterator anIt = theResults.begin(), aLast = theResults.end();
198   ObjectPtr aFeature;
199   for (; anIt != aLast; anIt++) {
200     aFeature = *anIt;
201     if (isVisible(aFeature))
202       anAIS = Handle(AIS_Shape)::DownCast(
203           myResult2AISObjectMap[aFeature]->impl<Handle(AIS_InteractiveObject)>());
204     if (anAIS.IsNull())
205       continue;
206
207     if (isStop) {
208       QColor aColor(Qt::white);
209       anAIS->SetColor(
210           Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
211                          Quantity_TOC_RGB));
212       anAIS->Redisplay();
213     } else {
214       QColor aColor(Qt::red);
215       anAIS->SetColor(
216           Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
217                          Quantity_TOC_RGB));
218       anAIS->Redisplay();
219     }
220   }
221   if (isUpdateViewer)
222     updateViewer();
223 }
224
225 void XGUI_Displayer::setSelected(const QList<ObjectPtr>& theResults, const bool isUpdateViewer)
226 {
227   Handle(AIS_InteractiveContext) aContext = AISContext();
228   // we need to unhighligth objects manually in the current local context
229   // in couple with the selection clear (TODO)
230   Handle(AIS_LocalContext) aLocalContext = aContext->LocalContext();
231   if (!aLocalContext.IsNull())
232     aLocalContext->UnhilightLastDetected(myWorkshop->viewer()->activeView());
233
234   aContext->ClearSelected();
235   foreach(ObjectPtr aResult, theResults)
236   {
237     if (isVisible(aResult)) {
238       AISObjectPtr anObj = myResult2AISObjectMap[aResult];
239       Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
240       if (!anAIS.IsNull())
241         aContext->SetSelected(anAIS, false);
242     }
243   }
244   if (isUpdateViewer)
245     updateViewer();
246 }
247
248
249 void XGUI_Displayer::clearSelected()
250 {
251   Handle(AIS_InteractiveContext) aContext = AISContext();
252   if (aContext) {
253     aContext->UnhilightCurrents(false);
254     aContext->ClearSelected();
255   }
256 }
257
258 void XGUI_Displayer::eraseAll(const bool isUpdateViewer)
259 {
260   Handle(AIS_InteractiveContext) ic = AISContext();
261   if (ic.IsNull())
262     return;
263
264    ResultToAISMap::iterator aIt;
265    for (aIt = myResult2AISObjectMap.begin(); aIt != myResult2AISObjectMap.end(); aIt++) {
266      // erase an object
267      AISObjectPtr aAISObj = (*aIt).second;
268      Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
269      if (!anIO.IsNull())
270       ic->Remove(anIO, false);
271    }
272    myResult2AISObjectMap.clear();
273    if (isUpdateViewer)
274      updateViewer();
275  }
276
277 void XGUI_Displayer::eraseDeletedResults(const bool isUpdateViewer)
278 {
279   Handle(AIS_InteractiveContext) aContext = AISContext();
280   if (aContext.IsNull())
281     return;
282
283   ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(), aFLast =
284       myResult2AISObjectMap.end();
285   std::list<ObjectPtr> aRemoved;
286   for (; aFIt != aFLast; aFIt++) {
287     ObjectPtr aFeature = (*aFIt).first;
288     if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
289       AISObjectPtr anObj = (*aFIt).second;
290       if (!anObj)
291         continue;
292       Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
293       if (!anAIS.IsNull()) {
294         aContext->Remove(anAIS, false);
295         aRemoved.push_back(aFeature);
296       }
297     }
298   }
299   std::list<ObjectPtr>::const_iterator anIt = aRemoved.begin(), aLast = aRemoved.end();
300   for (; anIt != aLast; anIt++) {
301     myResult2AISObjectMap.erase(myResult2AISObjectMap.find(*anIt));
302   }
303
304   if (isUpdateViewer)
305     updateViewer();
306 }
307
308 void XGUI_Displayer::openLocalContext()
309 {
310   Handle(AIS_InteractiveContext) aContext = AISContext();
311   if (aContext.IsNull())
312     return;
313   // Open local context if there is no one
314   if (!aContext->HasOpenedContext()) {
315     aContext->ClearCurrents(false);
316     //aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
317     aContext->OpenLocalContext();
318     aContext->NotUseDisplayedObjects();
319
320     // Deactivate trihedron which can be activated in local selector
321     //AIS_ListOfInteractive aPrsList;
322     //aContext->DisplayedObjects(aPrsList, true);
323
324     //Handle(AIS_Trihedron) aTrihedron;
325     //AIS_ListIteratorOfListOfInteractive aIt(aPrsList);
326     //for(; aIt.More(); aIt.Next()){
327     //  aTrihedron = Handle(AIS_Trihedron)::DownCast(aIt.Value());
328     //  if (!aTrihedron.IsNull()) {
329     //    aContext->Deactivate(aTrihedron);
330     //    break;
331     //  }
332     //}
333   }
334 }
335
336 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
337 {
338   AISContext()->ClearSelected(false);
339   closeAllContexts(true);
340 }
341
342 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
343 {
344   AISObjectPtr anIO;
345   if (myResult2AISObjectMap.find(theObject) != myResult2AISObjectMap.end())
346     anIO = (myResult2AISObjectMap.find(theObject))->second;
347   return anIO;
348 }
349
350 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
351 {
352   Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
353   return getObject(aRefAIS);
354 }
355
356 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
357 {
358   ObjectPtr aFeature;
359   ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(), aFLast =
360       myResult2AISObjectMap.end();
361   for (; aFIt != aFLast && !aFeature; aFIt++) {
362     AISObjectPtr anObj = (*aFIt).second;
363     if (!anObj)
364       continue;
365     Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
366     if (anAIS != theIO)
367       continue;
368     aFeature = (*aFIt).first;
369   }
370   return aFeature;
371 }
372
373 void XGUI_Displayer::closeAllContexts(const bool isUpdateViewer)
374 {
375   Handle(AIS_InteractiveContext) ic = AISContext();
376   if (!ic.IsNull()) {
377     ic->CloseAllContexts(false);
378     if (isUpdateViewer)
379       updateViewer();
380   }
381 }
382
383 void XGUI_Displayer::updateViewer()
384 {
385   Handle(AIS_InteractiveContext) ic = AISContext();
386   if (!ic.IsNull())
387     ic->UpdateCurrentViewer();
388 }
389
390 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
391 {
392   return myWorkshop->viewer()->AISContext();
393 }
394
395 void XGUI_Displayer::display(AISObjectPtr theAIS, bool isUpdate)
396 {
397   Handle(AIS_InteractiveContext) aContext = AISContext();
398   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
399   if (!anAISIO.IsNull())
400     aContext->Display(anAISIO, isUpdate);
401 }
402
403 void XGUI_Displayer::erase(AISObjectPtr theAIS, const bool isUpdate)
404 {
405   Handle(AIS_InteractiveContext) aContext = AISContext();
406   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
407   if (!anAISIO.IsNull()) {
408     aContext->Remove(anAISIO, isUpdate);
409   }
410 }
411
412 void XGUI_Displayer::activateObjectsOutOfContext(const QIntList& theModes)
413 {
414   Handle(AIS_InteractiveContext) aContext = AISContext();
415   // Open local context if there is no one
416   if (!aContext->HasOpenedContext()) 
417     return;
418
419   aContext->UseDisplayedObjects();
420   ResultToAISMap::iterator aIt;
421   Handle(AIS_InteractiveObject) anAISIO;
422   for (aIt = myResult2AISObjectMap.begin(); aIt != myResult2AISObjectMap.end(); aIt++) {
423   anAISIO = (*aIt).second->impl<Handle(AIS_InteractiveObject)>();
424     aContext->Load(anAISIO, -1, true);
425     if (theModes.size() == 0)
426       aContext->Activate(anAISIO);
427     else {
428       foreach(int aMode, theModes) {
429         aContext->Activate(anAISIO, aMode);
430       }
431     }
432   }
433 }
434
435
436 void XGUI_Displayer::deactivateObjectsOutOfContext()
437 {
438   Handle(AIS_InteractiveContext) aContext = AISContext();
439   // Open local context if there is no one
440   if (!aContext->HasOpenedContext()) 
441     return;
442
443   aContext->NotUseDisplayedObjects();
444 }
445
446
447 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool toUpdate)
448 {
449   if (theMode == NoMode)
450     return;
451
452   Handle(AIS_InteractiveContext) aContext = AISContext();
453   if (aContext.IsNull())
454     return;
455
456   AISObjectPtr aAISObj = getAISObject(theObject);
457   if (!aAISObj)
458     return;
459
460   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
461   aContext->SetDisplayMode(aAISIO, theMode, toUpdate);
462 }
463
464 void XGUI_Displayer::setSelectionModes(const QIntList& theModes)
465 {
466   Handle(AIS_InteractiveContext) aContext = AISContext();
467   if (aContext.IsNull())
468     return;
469   if (!aContext->HasOpenedContext())
470     return;
471   // Clear previous mode
472   const TColStd_ListOfInteger& aModes = aContext->ActivatedStandardModes();
473   if (!aModes.IsEmpty()) {
474     TColStd_ListOfInteger aMModes;
475     aMModes.Assign(aModes);
476     TColStd_ListIteratorOfListOfInteger it(aMModes);
477     for(; it.More(); it.Next()) {
478       aContext->DeactivateStandardMode((TopAbs_ShapeEnum)it.Value());
479     }
480   }
481   foreach(int aMode, theModes) {
482     aContext->ActivateStandardMode((TopAbs_ShapeEnum)aMode);
483   }
484 }
485
486 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
487 {
488   Handle(AIS_InteractiveContext) aContext = AISContext();
489   if (aContext.IsNull())
490     return NoMode;
491
492   AISObjectPtr aAISObj = getAISObject(theObject);
493   if (!aAISObj)
494     return NoMode;
495
496   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
497   return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
498 }
499
500 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
501 {
502   Handle(AIS_InteractiveContext) aContext = AISContext();
503   if (aContext.IsNull())
504     return;
505   const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
506   SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
507   for (; aIt.More(); aIt.Next()) {
508     if (theFilter.Access() == aIt.Value().Access())
509       return;
510   }
511   aContext->AddFilter(theFilter);
512 }
513
514 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
515 {
516   Handle(AIS_InteractiveContext) aContext = AISContext();
517   if (aContext.IsNull())
518     return;
519   aContext->RemoveFilter(theFilter);
520 }