Salome HOME
92ae9d2a4b4b5ae98788c909f14474723c03e1ff
[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 #include "ModuleBase_Tools.h"
10
11 #include <ModelAPI_Document.h>
12 #include <ModelAPI_Data.h>
13 #include <ModelAPI_Object.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     ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
52     if (aResult) {
53       boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModuleBase_Tools::shape(aResult);
54       if (aShapePtr) {
55         anAIS = boost::shared_ptr<GeomAPI_AISObject>(new GeomAPI_AISObject());
56         anAIS->createShape(aShapePtr);
57       }
58     } else {
59       GeomPresentablePtr aPrs = boost::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
60       if (aPrs) {
61         anAIS = aPrs->getAISObject(boost::shared_ptr<GeomAPI_AISObject>());
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->Erase(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   ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
135   if (aResult) {
136     boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModuleBase_Tools::shape(aResult);
137     if (aShapePtr) {
138       boost::shared_ptr<GeomAPI_AISObject> aAISObj = getAISObject(theObject);
139       Handle(AIS_Shape) aAISShape = Handle(AIS_Shape)::DownCast(aAISObj->impl<Handle(AIS_InteractiveObject)>());
140       if (!aAISShape.IsNull()) {
141         aAISShape->Set(aShapePtr->impl<TopoDS_Shape>());
142         AISContext()->Redisplay(aAISShape, isUpdateViewer);
143       }
144     }
145   } else {
146     GeomPresentablePtr aPrs = boost::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
147     if (aPrs) {
148       boost::shared_ptr<GeomAPI_AISObject> aAISObj = aPrs->getAISObject(getAISObject(theObject));
149       Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
150       AISContext()->Redisplay(aAISIO, isUpdateViewer);
151     }
152   }
153 }
154
155 void XGUI_Displayer::activateInLocalContext(ResultPtr theResult,
156                                          const std::list<int>& theModes, const bool isUpdateViewer)
157 {
158   Handle(AIS_InteractiveContext) aContext = AISContext();
159   // Open local context if there is no one
160   if (!aContext->HasOpenedContext()) {
161     aContext->ClearCurrents(false);
162     aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
163   }
164   // display or redisplay presentation
165   Handle(AIS_InteractiveObject) anAIS;
166   if (isVisible(theResult))
167   {
168     boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theResult];
169     if (anObj)
170       anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
171   }
172
173   // Activate selection of objects from prs
174   if (!anAIS.IsNull()) {
175     aContext->Load(anAIS, -1, true/*allow decomposition*/);
176     aContext->Deactivate(anAIS);
177
178     std::list<int>::const_iterator anIt = theModes.begin(), aLast = theModes.end();
179     for (; anIt != aLast; anIt++)
180     {
181       aContext->Activate(anAIS, (*anIt));
182     }
183   }
184
185   if (isUpdateViewer)
186     updateViewer();
187 }
188
189 void XGUI_Displayer::stopSelection(const QResultList& theResults, const bool isStop,
190                                    const bool isUpdateViewer)
191 {
192   Handle(AIS_InteractiveContext) aContext = AISContext();
193
194   Handle(AIS_Shape) anAIS;
195   QResultList::const_iterator anIt = theResults.begin(), aLast = theResults.end();
196   ResultPtr aFeature;
197   for (; anIt != aLast; anIt++) {
198     aFeature = *anIt;
199     if (isVisible(aFeature))
200       anAIS = Handle(AIS_Shape)::DownCast(myResult2AISObjectMap[aFeature]->impl<Handle(AIS_InteractiveObject)>());
201     if (anAIS.IsNull())
202       continue;
203
204     if (isStop) {
205       QColor aColor(Qt::white);
206       anAIS->SetColor(Quantity_Color(aColor.red()/255., aColor.green()/255., aColor.blue()/255., Quantity_TOC_RGB));
207       anAIS->Redisplay();
208     }
209     else {
210       QColor aColor(Qt::red);
211       anAIS->SetColor(Quantity_Color(aColor.red()/255., aColor.green()/255., aColor.blue()/255., Quantity_TOC_RGB));
212       anAIS->Redisplay();
213     }
214   }
215   if (isUpdateViewer)
216     updateViewer();
217 }
218
219 void XGUI_Displayer::setSelected(const QResultList& theResults, const bool isUpdateViewer)
220 {
221   Handle(AIS_InteractiveContext) aContext = AISContext();
222   // we need to unhighligth objects manually in the current local context
223   // in couple with the selection clear (TODO)
224   Handle(AIS_LocalContext) aLocalContext = aContext->LocalContext();
225   if (!aLocalContext.IsNull())
226     aLocalContext->UnhilightLastDetected(myWorkshop->viewer()->activeView());
227
228   aContext->ClearSelected();
229   foreach(ResultPtr aResult, theResults) {
230     if (myResult2AISObjectMap.find(aResult) == myResult2AISObjectMap.end()) 
231       return;
232
233     boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[aResult];
234     if (anObj)
235     {
236       Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
237       if (!anAIS.IsNull())
238         aContext->AddOrRemoveSelected(anAIS, false);
239     }
240   }
241   if (isUpdateViewer)
242     updateViewer();
243 }
244
245
246 /*void XGUI_Displayer::EraseAll(const bool isUpdateViewer)
247 {
248   Handle(AIS_InteractiveContext) ic = AISContext();
249
250   AIS_ListOfInteractive aList;
251   ic->DisplayedObjects(aList);
252   AIS_ListIteratorOfListOfInteractive anIter(aList);
253   for (; anIter.More(); anIter.Next()) {
254     if ((anIter.Value()->DynamicType() == STANDARD_TYPE(AIS_Trihedron)))
255       continue;
256
257     // erase an object
258     Handle(AIS_InteractiveObject) anIO = anIter.Value();
259     ic->Erase(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
270   ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(),
271                                  aFLast = myResult2AISObjectMap.end();
272   std::list<ObjectPtr> aRemoved;
273   for (; aFIt != aFLast; aFIt++)
274   {
275     ObjectPtr aFeature = (*aFIt).first;
276     if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
277       boost::shared_ptr<GeomAPI_AISObject> anObj = (*aFIt).second;
278       if (!anObj) continue;
279       Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
280       if (!anAIS.IsNull()) {
281         aContext->Erase(anAIS, false);
282         aRemoved.push_back(aFeature);
283       }
284     }
285   }
286   std::list<ObjectPtr>::const_iterator anIt = aRemoved.begin(),
287                                                                  aLast = aRemoved.end();
288   for (; anIt != aLast; anIt++) {
289     myResult2AISObjectMap.erase(myResult2AISObjectMap.find(*anIt));
290   }
291
292   if (isUpdateViewer)
293     updateViewer();
294 }
295
296 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
297 {
298   closeAllContexts(true);
299 }
300
301 boost::shared_ptr<GeomAPI_AISObject> XGUI_Displayer::getAISObject(ObjectPtr theObject) const
302 {
303   boost::shared_ptr<GeomAPI_AISObject> anIO;
304   if (myResult2AISObjectMap.find(theObject) != myResult2AISObjectMap.end())
305     anIO = (myResult2AISObjectMap.find(theObject))->second;
306   return anIO;
307 }
308
309 ObjectPtr XGUI_Displayer::getObject(Handle(AIS_InteractiveObject) theIO) const
310 {
311   ObjectPtr aFeature;
312   ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(),
313                                  aFLast = myResult2AISObjectMap.end();
314   for (; aFIt != aFLast && !aFeature; aFIt++) {
315     boost::shared_ptr<GeomAPI_AISObject> anObj = (*aFIt).second;
316     if (!anObj) continue;
317     Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
318     if (anAIS != theIO)
319       continue;
320     aFeature = (*aFIt).first;
321   }
322   return aFeature;
323 }
324
325 void XGUI_Displayer::closeAllContexts(const bool isUpdateViewer)
326 {
327   Handle(AIS_InteractiveContext) ic = AISContext();
328   if (!ic.IsNull()) {
329     ic->CloseAllContexts(false);
330     if (isUpdateViewer)
331       updateViewer();
332   }
333 }
334
335 void XGUI_Displayer::updateViewer()
336 {
337   Handle(AIS_InteractiveContext) ic = AISContext();
338   if (!ic.IsNull())
339     ic->UpdateCurrentViewer();
340 }
341
342 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const 
343
344   return myWorkshop->viewer()->AISContext(); 
345 }
346
347
348 void XGUI_Displayer::display(boost::shared_ptr<GeomAPI_AISObject> theAIS, bool isUpdate)
349 {
350   Handle(AIS_InteractiveContext) aContext = AISContext();
351   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
352   if (!anAISIO.IsNull())
353     aContext->Display(anAISIO, isUpdate);
354 }
355
356 void XGUI_Displayer::erase(boost::shared_ptr<GeomAPI_AISObject> theAIS, const bool isUpdate)
357 {
358   Handle(AIS_InteractiveContext) aContext = AISContext();
359   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
360   if (!anAISIO.IsNull()) {
361     aContext->Remove(anAISIO, isUpdate);
362   }
363 }
364