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