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