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