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