]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_Displayer.cpp
Salome HOME
68e8b68bdf16e7720c362e0da0c88edb7d904462
[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::activateInLocalContext(ObjectPtr theResult, const std::list<int>& theModes,
144                                             const bool isUpdateViewer)
145 {
146   Handle(AIS_InteractiveContext) aContext = AISContext();
147   if (aContext.IsNull())
148     return;
149   // Open local context if there is no one
150   if (!aContext->HasOpenedContext()) {
151     aContext->ClearCurrents(false);
152     //aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
153     aContext->OpenLocalContext();
154     aContext->NotUseDisplayedObjects();
155   }
156   // display or redisplay presentation
157   Handle(AIS_InteractiveObject) anAIS;
158   if (isVisible(theResult)) {
159     boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theResult];
160     if (anObj)
161       anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
162   }
163
164   // Activate selection of objects from prs
165   if (!anAIS.IsNull()) {
166     aContext->ClearSelected(false);  // ToCheck
167     //aContext->upClearSelected(false); // ToCheck
168     aContext->Load(anAIS, -1, true/*allow decomposition*/);
169     aContext->Deactivate(anAIS);
170
171     std::list<int>::const_iterator anIt = theModes.begin(), aLast = theModes.end();
172     for (; anIt != aLast; anIt++) {
173       aContext->Activate(anAIS, (*anIt));
174     }
175   }
176
177   if (isUpdateViewer)
178     updateViewer();
179 }
180
181 void XGUI_Displayer::deactivate(ObjectPtr theObject)
182 {
183   if (isVisible(theObject)) {
184     Handle(AIS_InteractiveContext) aContext = AISContext();
185     if (aContext.IsNull())
186       return;
187
188     boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theObject];
189     Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
190     aContext->Deactivate(anAIS);
191   }
192 }
193
194 void XGUI_Displayer::activate(ObjectPtr theObject)
195 {
196   if (isVisible(theObject)) {
197     Handle(AIS_InteractiveContext) aContext = AISContext();
198     if (aContext.IsNull())
199       return;
200
201     boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theObject];
202     Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
203     aContext->Activate(anAIS);
204   }
205 }
206
207 void XGUI_Displayer::stopSelection(const QList<ObjectPtr>& theResults, const bool isStop,
208                                    const bool isUpdateViewer)
209 {
210   Handle(AIS_InteractiveContext) aContext = AISContext();
211   if (aContext.IsNull())
212     return;
213
214   Handle(AIS_Shape) anAIS;
215   QList<ObjectPtr>::const_iterator anIt = theResults.begin(), aLast = theResults.end();
216   ObjectPtr aFeature;
217   for (; anIt != aLast; anIt++) {
218     aFeature = *anIt;
219     if (isVisible(aFeature))
220       anAIS = Handle(AIS_Shape)::DownCast(
221           myResult2AISObjectMap[aFeature]->impl<Handle(AIS_InteractiveObject)>());
222     if (anAIS.IsNull())
223       continue;
224
225     if (isStop) {
226       QColor aColor(Qt::white);
227       anAIS->SetColor(
228           Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
229                          Quantity_TOC_RGB));
230       anAIS->Redisplay();
231     } else {
232       QColor aColor(Qt::red);
233       anAIS->SetColor(
234           Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
235                          Quantity_TOC_RGB));
236       anAIS->Redisplay();
237     }
238   }
239   if (isUpdateViewer)
240     updateViewer();
241 }
242
243 void XGUI_Displayer::setSelected(const QList<ObjectPtr>& theResults, const bool isUpdateViewer)
244 {
245   Handle(AIS_InteractiveContext) aContext = AISContext();
246   // we need to unhighligth objects manually in the current local context
247   // in couple with the selection clear (TODO)
248   Handle(AIS_LocalContext) aLocalContext = aContext->LocalContext();
249   if (!aLocalContext.IsNull())
250     aLocalContext->UnhilightLastDetected(myWorkshop->viewer()->activeView());
251
252   aContext->ClearSelected();
253   foreach(ObjectPtr aResult, theResults)
254   {
255     if (myResult2AISObjectMap.find(aResult) == myResult2AISObjectMap.end())
256       continue;
257
258     boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[aResult];
259     if (anObj) {
260       Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
261       if (!anAIS.IsNull())
262         aContext->AddOrRemoveSelected(anAIS, false);
263     }
264   }
265   if (isUpdateViewer)
266     updateViewer();
267 }
268
269 void XGUI_Displayer::eraseAll(const bool isUpdateViewer)
270 {
271   Handle(AIS_InteractiveContext) ic = AISContext();
272   if (ic.IsNull())
273     return;
274
275    ResultToAISMap::iterator aIt;
276    for (aIt = myResult2AISObjectMap.begin(); aIt != myResult2AISObjectMap.end(); aIt++) {
277      // erase an object
278      boost::shared_ptr<GeomAPI_AISObject> aAISObj = (*aIt).second;
279      Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
280      if (!anIO.IsNull())
281       ic->Remove(anIO, false);
282    }
283    myResult2AISObjectMap.clear();
284    if (isUpdateViewer)
285      updateViewer();
286  }
287
288 void XGUI_Displayer::eraseDeletedResults(const bool isUpdateViewer)
289 {
290   Handle(AIS_InteractiveContext) aContext = AISContext();
291   if (aContext.IsNull())
292     return;
293
294   ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(), aFLast =
295       myResult2AISObjectMap.end();
296   std::list<ObjectPtr> aRemoved;
297   for (; aFIt != aFLast; aFIt++) {
298     ObjectPtr aFeature = (*aFIt).first;
299     if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
300       boost::shared_ptr<GeomAPI_AISObject> anObj = (*aFIt).second;
301       if (!anObj)
302         continue;
303       Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
304       if (!anAIS.IsNull()) {
305         aContext->Remove(anAIS, false);
306         aRemoved.push_back(aFeature);
307       }
308     }
309   }
310   std::list<ObjectPtr>::const_iterator anIt = aRemoved.begin(), aLast = aRemoved.end();
311   for (; anIt != aLast; anIt++) {
312     myResult2AISObjectMap.erase(myResult2AISObjectMap.find(*anIt));
313   }
314
315   if (isUpdateViewer)
316     updateViewer();
317 }
318
319 void XGUI_Displayer::openLocalContext()
320 {
321   Handle(AIS_InteractiveContext) aContext = AISContext();
322   if (aContext.IsNull())
323     return;
324   // Open local context if there is no one
325   if (!aContext->HasOpenedContext()) {
326     aContext->ClearCurrents(false);
327     //aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
328     aContext->OpenLocalContext();
329     aContext->NotUseDisplayedObjects();
330   }
331 }
332
333 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
334 {
335   AISContext()->ClearSelected(false);
336   closeAllContexts(true);
337 }
338
339 boost::shared_ptr<GeomAPI_AISObject> XGUI_Displayer::getAISObject(ObjectPtr theObject) const
340 {
341   boost::shared_ptr<GeomAPI_AISObject> anIO;
342   if (myResult2AISObjectMap.find(theObject) != myResult2AISObjectMap.end())
343     anIO = (myResult2AISObjectMap.find(theObject))->second;
344   return anIO;
345 }
346
347 ObjectPtr XGUI_Displayer::getObject(Handle(AIS_InteractiveObject) theIO) const
348 {
349   ObjectPtr aFeature;
350   ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(), aFLast =
351       myResult2AISObjectMap.end();
352   for (; aFIt != aFLast && !aFeature; aFIt++) {
353     boost::shared_ptr<GeomAPI_AISObject> anObj = (*aFIt).second;
354     if (!anObj)
355       continue;
356     Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
357     if (anAIS != theIO)
358       continue;
359     aFeature = (*aFIt).first;
360   }
361   return aFeature;
362 }
363
364 void XGUI_Displayer::closeAllContexts(const bool isUpdateViewer)
365 {
366   Handle(AIS_InteractiveContext) ic = AISContext();
367   if (!ic.IsNull()) {
368     ic->CloseAllContexts(false);
369     if (isUpdateViewer)
370       updateViewer();
371   }
372 }
373
374 void XGUI_Displayer::updateViewer()
375 {
376   Handle(AIS_InteractiveContext) ic = AISContext();
377   if (!ic.IsNull())
378     ic->UpdateCurrentViewer();
379 }
380
381 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
382 {
383   return myWorkshop->viewer()->AISContext();
384 }
385
386 void XGUI_Displayer::display(boost::shared_ptr<GeomAPI_AISObject> theAIS, bool isUpdate)
387 {
388   Handle(AIS_InteractiveContext) aContext = AISContext();
389   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
390   if (!anAISIO.IsNull())
391     aContext->Display(anAISIO, isUpdate);
392 }
393
394 void XGUI_Displayer::erase(boost::shared_ptr<GeomAPI_AISObject> theAIS, const bool isUpdate)
395 {
396   Handle(AIS_InteractiveContext) aContext = AISContext();
397   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
398   if (!anAISIO.IsNull()) {
399     aContext->Remove(anAISIO, isUpdate);
400   }
401 }
402
403 void XGUI_Displayer::activateObjectsOutOfContext(const std::list<int>& theModes, 
404                                                  Handle(SelectMgr_Filter) theFilter)
405 {
406   Handle(AIS_InteractiveContext) aContext = AISContext();
407   // Open local context if there is no one
408   if (!aContext->HasOpenedContext()) 
409     return;
410
411   aContext->UseDisplayedObjects();
412   std::list<int>::const_iterator anIt = theModes.begin(), aLast = theModes.end();
413   for (; anIt != aLast; anIt++) {
414     aContext->ActivateStandardMode((TopAbs_ShapeEnum)(*anIt));
415   }
416
417   if (!theFilter.IsNull())
418     aContext->AddFilter(theFilter);
419 }
420
421
422 void XGUI_Displayer::deactivateObjectsOutOfContext()
423 {
424   Handle(AIS_InteractiveContext) aContext = AISContext();
425   // Open local context if there is no one
426   if (!aContext->HasOpenedContext()) 
427     return;
428
429   aContext->RemoveFilters();
430   aContext->NotUseDisplayedObjects();
431 }
432
433
434 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool toUpdate)
435 {
436   if (theMode == NoMode)
437     return;
438
439   Handle(AIS_InteractiveContext) aContext = AISContext();
440   if (aContext.IsNull())
441     return;
442
443   boost::shared_ptr<GeomAPI_AISObject> aAISObj = getAISObject(theObject);
444   if (!aAISObj)
445     return;
446
447   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
448   aContext->SetDisplayMode(aAISIO, theMode, toUpdate);
449 }
450
451
452 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
453 {
454   Handle(AIS_InteractiveContext) aContext = AISContext();
455   if (aContext.IsNull())
456     return NoMode;
457
458   boost::shared_ptr<GeomAPI_AISObject> aAISObj = getAISObject(theObject);
459   if (!aAISObj)
460     return NoMode;
461
462   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
463   return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
464 }
465