]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_Displayer.cpp
Salome HOME
Initial iteration for storage the back references and making concealment only on...
[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 <ModuleBase_ResultPrs.h>
16
17 #include <GeomAPI_Shape.h>
18 #include <GeomAPI_IPresentable.h>
19
20 #include <AIS_InteractiveContext.hxx>
21 #include <AIS_LocalContext.hxx>
22 #include <AIS_ListOfInteractive.hxx>
23 #include <AIS_ListIteratorOfListOfInteractive.hxx>
24 #include <AIS_DimensionSelectionMode.hxx>
25 #include <AIS_Shape.hxx>
26 #include <AIS_Dimension.hxx>
27 #include <TColStd_ListIteratorOfListOfInteger.hxx>
28
29 #include <set>
30
31 const int MOUSE_SENSITIVITY_IN_PIXEL = 10;  ///< defines the local context mouse selection sensitivity
32
33 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
34 {
35   myWorkshop = theWorkshop;
36 }
37
38 XGUI_Displayer::~XGUI_Displayer()
39 {
40 }
41
42 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
43 {
44   return myResult2AISObjectMap.find(theObject) != myResult2AISObjectMap.end();
45 }
46
47 void XGUI_Displayer::display(ObjectPtr theObject, bool isUpdateViewer)
48 {
49   if (isVisible(theObject)) {
50     redisplay(theObject, isUpdateViewer);
51   } else {
52     AISObjectPtr anAIS;
53
54     GeomPresentablePtr aPrs = boost::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
55     bool isShading = false;
56     if (aPrs) {
57       anAIS = aPrs->getAISObject(AISObjectPtr());
58     } else {
59       ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
60       if (aResult) {
61         boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
62         if (aShapePtr) {
63           anAIS = AISObjectPtr(new GeomAPI_AISObject());
64           anAIS->setImpl(new Handle(AIS_InteractiveObject)(new ModuleBase_ResultPrs(aResult)));
65           //anAIS->createShape(aShapePtr);
66           isShading = true;
67         }
68       }
69     }
70     if (anAIS)
71       display(theObject, anAIS, isShading, isUpdateViewer);
72   }
73 }
74
75 void XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS, 
76                              bool isShading, bool isUpdateViewer)
77 {
78   Handle(AIS_InteractiveContext) aContext = AISContext();
79   if (aContext.IsNull())
80     return;
81
82   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
83   if (!anAISIO.IsNull()) {
84     myResult2AISObjectMap[theObject] = theAIS;
85     aContext->Display(anAISIO, false);
86     aContext->SetDisplayMode(anAISIO, isShading? Shading : Wireframe, isUpdateViewer);
87   }
88 }
89
90 void XGUI_Displayer::erase(ObjectPtr theObject, const bool isUpdateViewer)
91 {
92   if (!isVisible(theObject))
93     return;
94
95   Handle(AIS_InteractiveContext) aContext = AISContext();
96   if (aContext.IsNull())
97     return;
98   AISObjectPtr anObject = myResult2AISObjectMap[theObject];
99   if (anObject) {
100     Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
101     if (!anAIS.IsNull()) {
102       aContext->Remove(anAIS, isUpdateViewer);
103     }
104   }
105   myResult2AISObjectMap.erase(theObject);
106 }
107
108 void XGUI_Displayer::redisplay(ObjectPtr theObject, bool isUpdateViewer)
109 {
110   if (!isVisible(theObject))
111     return;
112
113   AISObjectPtr aAISObj = getAISObject(theObject);
114   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
115
116   GeomPresentablePtr aPrs = boost::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
117   if (aPrs) {
118     AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
119     if (!aAIS_Obj) {
120       erase(theObject, isUpdateViewer);
121       return;
122     }
123     if (aAIS_Obj != aAISObj) {
124       myResult2AISObjectMap[theObject] = aAIS_Obj;
125     }
126     aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
127   }
128
129   if (!aAISIO.IsNull()) {
130     Handle(AIS_InteractiveContext) aContext = AISContext();
131     if (aContext.IsNull())
132       return;
133     aContext->Redisplay(aAISIO, isUpdateViewer);
134   }
135 }
136
137 void XGUI_Displayer::deactivate(ObjectPtr theObject)
138 {
139   if (isVisible(theObject)) {
140     Handle(AIS_InteractiveContext) aContext = AISContext();
141     if (aContext.IsNull())
142       return;
143
144     AISObjectPtr anObj = myResult2AISObjectMap[theObject];
145     Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
146     aContext->Deactivate(anAIS);
147   }
148 }
149
150 void XGUI_Displayer::activate(ObjectPtr theObject, const QIntList& theModes)
151 {
152   if (isVisible(theObject)) {
153     Handle(AIS_InteractiveContext) aContext = AISContext();
154     if (aContext.IsNull())
155       return;
156
157     AISObjectPtr anObj = myResult2AISObjectMap[theObject];
158     Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
159     if (aContext->HasOpenedContext()) {
160       aContext->Load(anAIS, -1, true);
161     }
162     if (theModes.size() > 0) {
163       foreach(int aMode, theModes) {
164         aContext->Activate(anAIS, aMode);
165       }
166     } else 
167       aContext->Activate(anAIS);
168   }
169 }
170
171 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
172 {
173   Handle(AIS_InteractiveContext) aContext = AISContext();
174   if (aContext.IsNull())
175     return false;
176   if (!isVisible(theObject))
177     return false;
178     
179   AISObjectPtr anObj = myResult2AISObjectMap.at(theObject);
180   Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
181
182   TColStd_ListOfInteger aModes;
183   aContext->ActivatedModes(anAIS, aModes);
184   return aModes.Extent() > 0;
185 }
186
187 void XGUI_Displayer::stopSelection(const QList<ObjectPtr>& theResults, const bool isStop,
188                                    const bool isUpdateViewer)
189 {
190   Handle(AIS_InteractiveContext) aContext = AISContext();
191   if (aContext.IsNull())
192     return;
193
194   Handle(AIS_Shape) anAIS;
195   QList<ObjectPtr>::const_iterator anIt = theResults.begin(), aLast = theResults.end();
196   ObjectPtr aFeature;
197   for (; anIt != aLast; anIt++) {
198     aFeature = *anIt;
199     if (isVisible(aFeature))
200       anAIS = Handle(AIS_Shape)::DownCast(
201           myResult2AISObjectMap[aFeature]->impl<Handle(AIS_InteractiveObject)>());
202     if (anAIS.IsNull())
203       continue;
204
205     if (isStop) {
206       QColor aColor(Qt::white);
207       anAIS->SetColor(
208           Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
209                          Quantity_TOC_RGB));
210       anAIS->Redisplay();
211     } else {
212       QColor aColor(Qt::red);
213       anAIS->SetColor(
214           Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
215                          Quantity_TOC_RGB));
216       anAIS->Redisplay();
217     }
218   }
219   if (isUpdateViewer)
220     updateViewer();
221 }
222
223 void XGUI_Displayer::setSelected(const QList<ObjectPtr>& theResults, const bool isUpdateViewer)
224 {
225   Handle(AIS_InteractiveContext) aContext = AISContext();
226   // we need to unhighligth objects manually in the current local context
227   // in couple with the selection clear (TODO)
228   Handle(AIS_LocalContext) aLocalContext = aContext->LocalContext();
229   if (!aLocalContext.IsNull())
230     aLocalContext->UnhilightLastDetected(myWorkshop->viewer()->activeView());
231
232   aContext->ClearSelected();
233   foreach(ObjectPtr aResult, theResults)
234   {
235     if (myResult2AISObjectMap.find(aResult) == myResult2AISObjectMap.end())
236       continue;
237
238     AISObjectPtr anObj = myResult2AISObjectMap[aResult];
239     if (anObj) {
240       Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
241       if (!anAIS.IsNull())
242         aContext->AddOrRemoveSelected(anAIS, false);
243     }
244   }
245   if (isUpdateViewer)
246     updateViewer();
247 }
248
249
250 void XGUI_Displayer::clearSelected()
251 {
252   Handle(AIS_InteractiveContext) aContext = AISContext();
253   if (aContext) {
254     aContext->UnhilightCurrents(false);
255     aContext->ClearSelected();
256   }
257 }
258
259 void XGUI_Displayer::eraseAll(const bool isUpdateViewer)
260 {
261   Handle(AIS_InteractiveContext) ic = AISContext();
262   if (ic.IsNull())
263     return;
264
265    ResultToAISMap::iterator aIt;
266    for (aIt = myResult2AISObjectMap.begin(); aIt != myResult2AISObjectMap.end(); aIt++) {
267      // erase an object
268      AISObjectPtr aAISObj = (*aIt).second;
269      Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
270      if (!anIO.IsNull())
271       ic->Remove(anIO, false);
272    }
273    myResult2AISObjectMap.clear();
274    if (isUpdateViewer)
275      updateViewer();
276  }
277
278 void XGUI_Displayer::eraseDeletedResults(const bool isUpdateViewer)
279 {
280   Handle(AIS_InteractiveContext) aContext = AISContext();
281   if (aContext.IsNull())
282     return;
283
284   ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(), aFLast =
285       myResult2AISObjectMap.end();
286   std::list<ObjectPtr> aRemoved;
287   for (; aFIt != aFLast; aFIt++) {
288     ObjectPtr aFeature = (*aFIt).first;
289     if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
290       AISObjectPtr anObj = (*aFIt).second;
291       if (!anObj)
292         continue;
293       Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
294       if (!anAIS.IsNull()) {
295         aContext->Remove(anAIS, false);
296         aRemoved.push_back(aFeature);
297       }
298     }
299   }
300   std::list<ObjectPtr>::const_iterator anIt = aRemoved.begin(), aLast = aRemoved.end();
301   for (; anIt != aLast; anIt++) {
302     myResult2AISObjectMap.erase(myResult2AISObjectMap.find(*anIt));
303   }
304
305   if (isUpdateViewer)
306     updateViewer();
307 }
308
309 void XGUI_Displayer::openLocalContext()
310 {
311   Handle(AIS_InteractiveContext) aContext = AISContext();
312   if (aContext.IsNull())
313     return;
314   // Open local context if there is no one
315   if (!aContext->HasOpenedContext()) {
316     aContext->ClearCurrents(false);
317     //aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
318     aContext->OpenLocalContext();
319     aContext->NotUseDisplayedObjects();
320   }
321 }
322
323 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
324 {
325   AISContext()->ClearSelected(false);
326   closeAllContexts(true);
327 }
328
329 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
330 {
331   AISObjectPtr anIO;
332   if (myResult2AISObjectMap.find(theObject) != myResult2AISObjectMap.end())
333     anIO = (myResult2AISObjectMap.find(theObject))->second;
334   return anIO;
335 }
336
337 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
338 {
339   Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
340   return getObject(aRefAIS);
341 }
342
343 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
344 {
345   ObjectPtr aFeature;
346   ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(), aFLast =
347       myResult2AISObjectMap.end();
348   for (; aFIt != aFLast && !aFeature; aFIt++) {
349     AISObjectPtr anObj = (*aFIt).second;
350     if (!anObj)
351       continue;
352     Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
353     if (anAIS != theIO)
354       continue;
355     aFeature = (*aFIt).first;
356   }
357   return aFeature;
358 }
359
360 void XGUI_Displayer::closeAllContexts(const bool isUpdateViewer)
361 {
362   Handle(AIS_InteractiveContext) ic = AISContext();
363   if (!ic.IsNull()) {
364     ic->CloseAllContexts(false);
365     if (isUpdateViewer)
366       updateViewer();
367   }
368 }
369
370 void XGUI_Displayer::updateViewer()
371 {
372   Handle(AIS_InteractiveContext) ic = AISContext();
373   if (!ic.IsNull())
374     ic->UpdateCurrentViewer();
375 }
376
377 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
378 {
379   return myWorkshop->viewer()->AISContext();
380 }
381
382 void XGUI_Displayer::display(AISObjectPtr theAIS, bool isUpdate)
383 {
384   Handle(AIS_InteractiveContext) aContext = AISContext();
385   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
386   if (!anAISIO.IsNull())
387     aContext->Display(anAISIO, isUpdate);
388 }
389
390 void XGUI_Displayer::erase(AISObjectPtr theAIS, const bool isUpdate)
391 {
392   Handle(AIS_InteractiveContext) aContext = AISContext();
393   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
394   if (!anAISIO.IsNull()) {
395     aContext->Remove(anAISIO, isUpdate);
396   }
397 }
398
399 void XGUI_Displayer::activateObjectsOutOfContext(const QIntList& theModes)
400 {
401   Handle(AIS_InteractiveContext) aContext = AISContext();
402   // Open local context if there is no one
403   if (!aContext->HasOpenedContext()) 
404     return;
405
406   aContext->UseDisplayedObjects();
407   ResultToAISMap::iterator aIt;
408   Handle(AIS_InteractiveObject) anAISIO;
409   for (aIt = myResult2AISObjectMap.begin(); aIt != myResult2AISObjectMap.end(); aIt++) {
410     anAISIO = (*aIt).second->impl<Handle(AIS_InteractiveObject)>();
411     aContext->Load(anAISIO, -1, true);
412     if (theModes.size() == 0)
413       aContext->Activate(anAISIO);
414     else {
415       foreach(int aMode, theModes) {
416         aContext->Activate(anAISIO, aMode);
417       }
418     }
419   }
420 }
421
422
423 void XGUI_Displayer::deactivateObjectsOutOfContext()
424 {
425   Handle(AIS_InteractiveContext) aContext = AISContext();
426   // Open local context if there is no one
427   if (!aContext->HasOpenedContext()) 
428     return;
429
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   AISObjectPtr 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 void XGUI_Displayer::setSelectionModes(const QIntList& theModes)
452 {
453   Handle(AIS_InteractiveContext) aContext = AISContext();
454   if (aContext.IsNull())
455     return;
456   if (!aContext->HasOpenedContext())
457     return;
458   // Clear previous mode
459   const TColStd_ListOfInteger& aModes = aContext->ActivatedStandardModes();
460   if (!aModes.IsEmpty()) {
461     TColStd_ListOfInteger aMModes;
462     aMModes.Assign(aModes);
463     TColStd_ListIteratorOfListOfInteger it(aMModes);
464     for(; it.More(); it.Next()) {
465       aContext->DeactivateStandardMode((TopAbs_ShapeEnum)it.Value());
466     }
467   }
468   foreach(int aMode, theModes) {
469     aContext->ActivateStandardMode((TopAbs_ShapeEnum)aMode);
470   }
471 }
472
473 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
474 {
475   Handle(AIS_InteractiveContext) aContext = AISContext();
476   if (aContext.IsNull())
477     return NoMode;
478
479   AISObjectPtr aAISObj = getAISObject(theObject);
480   if (!aAISObj)
481     return NoMode;
482
483   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
484   return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
485 }
486
487 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
488 {
489   Handle(AIS_InteractiveContext) aContext = AISContext();
490   if (aContext.IsNull())
491     return;
492   aContext->AddFilter(theFilter);
493 }
494
495 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
496 {
497   Handle(AIS_InteractiveContext) aContext = AISContext();
498   if (aContext.IsNull())
499     return;
500   aContext->RemoveFilter(theFilter);
501 }