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