]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_Displayer.cpp
Salome HOME
AIS presentation for result is 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
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   Handle(AIS_InteractiveObject) aAISIO;
114   AISObjectPtr aAISObj = getAISObject(theObject);
115   GeomPresentablePtr aPrs = boost::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
116   if (aPrs) {
117     AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
118     if (aAISObj && !aAIS_Obj) {
119       erase(theObject, isUpdateViewer);
120       return;
121     }
122     aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
123   } else {
124     ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
125     if (aResult) {
126       boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
127       if (aShapePtr) {
128         Handle(AIS_Shape) aAISShape = Handle(AIS_Shape)::DownCast(
129             aAISObj->impl<Handle(AIS_InteractiveObject)>());
130         if (!aAISShape.IsNull()) {
131           aAISShape->Set(aShapePtr->impl<TopoDS_Shape>());
132           aAISIO = aAISShape;
133         }
134       }
135     }
136   }
137   if (!aAISIO.IsNull()) {
138     Handle(AIS_InteractiveContext) aContext = AISContext();
139     if (aContext.IsNull())
140       return;
141     aContext->Redisplay(aAISIO, isUpdateViewer);
142     //if (aContext->HasOpenedContext()) {
143     //  aContext->Load(aAISIO, -1, true/*allow decomposition*/);
144     //}
145   }
146 }
147
148 void XGUI_Displayer::deactivate(ObjectPtr theObject)
149 {
150   if (isVisible(theObject)) {
151     Handle(AIS_InteractiveContext) aContext = AISContext();
152     if (aContext.IsNull())
153       return;
154
155     AISObjectPtr anObj = myResult2AISObjectMap[theObject];
156     Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
157     aContext->Deactivate(anAIS);
158   }
159 }
160
161 void XGUI_Displayer::activate(ObjectPtr theObject, const QIntList& theModes)
162 {
163   if (isVisible(theObject)) {
164     Handle(AIS_InteractiveContext) aContext = AISContext();
165     if (aContext.IsNull())
166       return;
167
168     AISObjectPtr anObj = myResult2AISObjectMap[theObject];
169     Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
170     if (aContext->HasOpenedContext()) {
171       aContext->Load(anAIS, -1, true);
172     }
173     if (theModes.size() > 0) {
174       foreach(int aMode, theModes) {
175         aContext->Activate(anAIS, aMode);
176       }
177     } else 
178       aContext->Activate(anAIS);
179   }
180 }
181
182 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
183 {
184   Handle(AIS_InteractiveContext) aContext = AISContext();
185   if (aContext.IsNull())
186     return false;
187   if (!isVisible(theObject))
188     return false;
189     
190   AISObjectPtr anObj = myResult2AISObjectMap.at(theObject);
191   Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
192
193   TColStd_ListOfInteger aModes;
194   aContext->ActivatedModes(anAIS, aModes);
195   return aModes.Extent() > 0;
196 }
197
198 void XGUI_Displayer::stopSelection(const QList<ObjectPtr>& theResults, const bool isStop,
199                                    const bool isUpdateViewer)
200 {
201   Handle(AIS_InteractiveContext) aContext = AISContext();
202   if (aContext.IsNull())
203     return;
204
205   Handle(AIS_Shape) anAIS;
206   QList<ObjectPtr>::const_iterator anIt = theResults.begin(), aLast = theResults.end();
207   ObjectPtr aFeature;
208   for (; anIt != aLast; anIt++) {
209     aFeature = *anIt;
210     if (isVisible(aFeature))
211       anAIS = Handle(AIS_Shape)::DownCast(
212           myResult2AISObjectMap[aFeature]->impl<Handle(AIS_InteractiveObject)>());
213     if (anAIS.IsNull())
214       continue;
215
216     if (isStop) {
217       QColor aColor(Qt::white);
218       anAIS->SetColor(
219           Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
220                          Quantity_TOC_RGB));
221       anAIS->Redisplay();
222     } else {
223       QColor aColor(Qt::red);
224       anAIS->SetColor(
225           Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
226                          Quantity_TOC_RGB));
227       anAIS->Redisplay();
228     }
229   }
230   if (isUpdateViewer)
231     updateViewer();
232 }
233
234 void XGUI_Displayer::setSelected(const QList<ObjectPtr>& theResults, const bool isUpdateViewer)
235 {
236   Handle(AIS_InteractiveContext) aContext = AISContext();
237   // we need to unhighligth objects manually in the current local context
238   // in couple with the selection clear (TODO)
239   Handle(AIS_LocalContext) aLocalContext = aContext->LocalContext();
240   if (!aLocalContext.IsNull())
241     aLocalContext->UnhilightLastDetected(myWorkshop->viewer()->activeView());
242
243   aContext->ClearSelected();
244   foreach(ObjectPtr aResult, theResults)
245   {
246     if (myResult2AISObjectMap.find(aResult) == myResult2AISObjectMap.end())
247       continue;
248
249     AISObjectPtr anObj = myResult2AISObjectMap[aResult];
250     if (anObj) {
251       Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
252       if (!anAIS.IsNull())
253         aContext->AddOrRemoveSelected(anAIS, false);
254     }
255   }
256   if (isUpdateViewer)
257     updateViewer();
258 }
259
260
261 void XGUI_Displayer::clearSelected()
262 {
263   Handle(AIS_InteractiveContext) aContext = AISContext();
264   if (aContext) {
265     aContext->UnhilightCurrents(false);
266     aContext->ClearSelected();
267   }
268 }
269
270 void XGUI_Displayer::eraseAll(const bool isUpdateViewer)
271 {
272   Handle(AIS_InteractiveContext) ic = AISContext();
273   if (ic.IsNull())
274     return;
275
276    ResultToAISMap::iterator aIt;
277    for (aIt = myResult2AISObjectMap.begin(); aIt != myResult2AISObjectMap.end(); aIt++) {
278      // erase an object
279      AISObjectPtr aAISObj = (*aIt).second;
280      Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
281      if (!anIO.IsNull())
282       ic->Remove(anIO, false);
283    }
284    myResult2AISObjectMap.clear();
285    if (isUpdateViewer)
286      updateViewer();
287  }
288
289 void XGUI_Displayer::eraseDeletedResults(const bool isUpdateViewer)
290 {
291   Handle(AIS_InteractiveContext) aContext = AISContext();
292   if (aContext.IsNull())
293     return;
294
295   ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(), aFLast =
296       myResult2AISObjectMap.end();
297   std::list<ObjectPtr> aRemoved;
298   for (; aFIt != aFLast; aFIt++) {
299     ObjectPtr aFeature = (*aFIt).first;
300     if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
301       AISObjectPtr anObj = (*aFIt).second;
302       if (!anObj)
303         continue;
304       Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
305       if (!anAIS.IsNull()) {
306         aContext->Remove(anAIS, false);
307         aRemoved.push_back(aFeature);
308       }
309     }
310   }
311   std::list<ObjectPtr>::const_iterator anIt = aRemoved.begin(), aLast = aRemoved.end();
312   for (; anIt != aLast; anIt++) {
313     myResult2AISObjectMap.erase(myResult2AISObjectMap.find(*anIt));
314   }
315
316   if (isUpdateViewer)
317     updateViewer();
318 }
319
320 void XGUI_Displayer::openLocalContext()
321 {
322   Handle(AIS_InteractiveContext) aContext = AISContext();
323   if (aContext.IsNull())
324     return;
325   // Open local context if there is no one
326   if (!aContext->HasOpenedContext()) {
327     aContext->ClearCurrents(false);
328     //aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
329     aContext->OpenLocalContext();
330     aContext->NotUseDisplayedObjects();
331   }
332 }
333
334 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
335 {
336   AISContext()->ClearSelected(false);
337   closeAllContexts(true);
338 }
339
340 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
341 {
342   AISObjectPtr anIO;
343   if (myResult2AISObjectMap.find(theObject) != myResult2AISObjectMap.end())
344     anIO = (myResult2AISObjectMap.find(theObject))->second;
345   return anIO;
346 }
347
348 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
349 {
350   Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
351   return getObject(aRefAIS);
352 }
353
354 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
355 {
356   ObjectPtr aFeature;
357   ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(), aFLast =
358       myResult2AISObjectMap.end();
359   for (; aFIt != aFLast && !aFeature; aFIt++) {
360     AISObjectPtr anObj = (*aFIt).second;
361     if (!anObj)
362       continue;
363     Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
364     if (anAIS != theIO)
365       continue;
366     aFeature = (*aFIt).first;
367   }
368   return aFeature;
369 }
370
371 void XGUI_Displayer::closeAllContexts(const bool isUpdateViewer)
372 {
373   Handle(AIS_InteractiveContext) ic = AISContext();
374   if (!ic.IsNull()) {
375     ic->CloseAllContexts(false);
376     if (isUpdateViewer)
377       updateViewer();
378   }
379 }
380
381 void XGUI_Displayer::updateViewer()
382 {
383   Handle(AIS_InteractiveContext) ic = AISContext();
384   if (!ic.IsNull())
385     ic->UpdateCurrentViewer();
386 }
387
388 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
389 {
390   return myWorkshop->viewer()->AISContext();
391 }
392
393 void XGUI_Displayer::display(AISObjectPtr theAIS, bool isUpdate)
394 {
395   Handle(AIS_InteractiveContext) aContext = AISContext();
396   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
397   if (!anAISIO.IsNull())
398     aContext->Display(anAISIO, isUpdate);
399 }
400
401 void XGUI_Displayer::erase(AISObjectPtr theAIS, const bool isUpdate)
402 {
403   Handle(AIS_InteractiveContext) aContext = AISContext();
404   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
405   if (!anAISIO.IsNull()) {
406     aContext->Remove(anAISIO, isUpdate);
407   }
408 }
409
410 void XGUI_Displayer::activateObjectsOutOfContext(const QIntList& theModes)
411 {
412   Handle(AIS_InteractiveContext) aContext = AISContext();
413   // Open local context if there is no one
414   if (!aContext->HasOpenedContext()) 
415     return;
416
417   aContext->UseDisplayedObjects();
418   ResultToAISMap::iterator aIt;
419   Handle(AIS_InteractiveObject) anAISIO;
420   for (aIt = myResult2AISObjectMap.begin(); aIt != myResult2AISObjectMap.end(); aIt++) {
421     anAISIO = (*aIt).second->impl<Handle(AIS_InteractiveObject)>();
422     aContext->Load(anAISIO, -1, true);
423     if (theModes.size() == 0)
424       aContext->Activate(anAISIO);
425     else {
426       foreach(int aMode, theModes) {
427         aContext->Activate(anAISIO, aMode);
428       }
429     }
430   }
431 }
432
433
434 void XGUI_Displayer::deactivateObjectsOutOfContext()
435 {
436   Handle(AIS_InteractiveContext) aContext = AISContext();
437   // Open local context if there is no one
438   if (!aContext->HasOpenedContext()) 
439     return;
440
441   aContext->NotUseDisplayedObjects();
442 }
443
444
445 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool toUpdate)
446 {
447   if (theMode == NoMode)
448     return;
449
450   Handle(AIS_InteractiveContext) aContext = AISContext();
451   if (aContext.IsNull())
452     return;
453
454   AISObjectPtr aAISObj = getAISObject(theObject);
455   if (!aAISObj)
456     return;
457
458   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
459   aContext->SetDisplayMode(aAISIO, theMode, toUpdate);
460 }
461
462 void XGUI_Displayer::setSelectionModes(const QIntList& theModes)
463 {
464   Handle(AIS_InteractiveContext) aContext = AISContext();
465   if (aContext.IsNull())
466     return;
467   if (!aContext->HasOpenedContext())
468     return;
469   // Clear previous mode
470   const TColStd_ListOfInteger& aModes = aContext->ActivatedStandardModes();
471   if (!aModes.IsEmpty()) {
472     TColStd_ListOfInteger aMModes;
473     aMModes.Assign(aModes);
474     TColStd_ListIteratorOfListOfInteger it(aMModes);
475     for(; it.More(); it.Next()) {
476       aContext->DeactivateStandardMode((TopAbs_ShapeEnum)it.Value());
477     }
478   }
479   foreach(int aMode, theModes) {
480     aContext->ActivateStandardMode((TopAbs_ShapeEnum)aMode);
481   }
482 }
483
484 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
485 {
486   Handle(AIS_InteractiveContext) aContext = AISContext();
487   if (aContext.IsNull())
488     return NoMode;
489
490   AISObjectPtr aAISObj = getAISObject(theObject);
491   if (!aAISObj)
492     return NoMode;
493
494   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
495   return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
496 }
497
498 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
499 {
500   Handle(AIS_InteractiveContext) aContext = AISContext();
501   if (aContext.IsNull())
502     return;
503   aContext->AddFilter(theFilter);
504 }
505
506 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
507 {
508   Handle(AIS_InteractiveContext) aContext = AISContext();
509   if (aContext.IsNull())
510     return;
511   aContext->RemoveFilter(theFilter);
512 }