]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_Displayer.cpp
Salome HOME
57befeb24cb8958c3aac8acd0b1f935a1452cf8d
[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
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     GeomPresentablePtr aPrs = boost::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
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         }
62       }
63     }
64     if (anAIS)
65       display(theObject, anAIS, isUpdateViewer);
66   }
67 }
68
69 void XGUI_Displayer::display(ObjectPtr theObject, boost::shared_ptr<GeomAPI_AISObject> theAIS,
70                              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 void XGUI_Displayer::erase(ObjectPtr theObject, const bool isUpdateViewer)
82 {
83   if (!isVisible(theObject))
84     return;
85
86   Handle(AIS_InteractiveContext) aContext = AISContext();
87   boost::shared_ptr<GeomAPI_AISObject> anObject = myResult2AISObjectMap[theObject];
88   if (anObject) {
89     Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
90     if (!anAIS.IsNull()) {
91       aContext->Remove(anAIS, isUpdateViewer);
92     }
93   }
94   myResult2AISObjectMap.erase(theObject);
95 }
96
97 /*bool XGUI_Displayer::redisplay(ObjectPtr theObject,
98  boost::shared_ptr<GeomAPI_AISObject> theAIS,
99  const bool isUpdateViewer)
100  {
101  bool isCreated = false;
102  Handle(AIS_InteractiveObject) anAIS = 
103  theAIS ? theAIS->impl<Handle(AIS_InteractiveObject)>() : Handle(AIS_InteractiveObject)();
104  Handle(AIS_InteractiveContext) aContext = AISContext();
105  // Open local context if there is no one
106  if (!aContext->HasOpenedContext()) {
107  aContext->ClearCurrents(false);
108  aContext->OpenLocalContext(false /use displayed objects/, true /allow shape decomposition/);
109  // set mouse sensitivity
110  //aContext->SetSensitivityMode(StdSelect_SM_WINDOW);
111  //aContext->SetPixelTolerance(MOUSE_SENSITIVITY_IN_PIXEL);
112  }
113  // display or redisplay presentation
114  boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theObject];
115  if (isVisible(theObject) && anObj && !anObj->empty()) {
116  aContext->Redisplay(anAIS, isUpdateViewer);
117  //aContext->RecomputeSelectionOnly(anAIS);
118  }
119  else {
120  myResult2AISObjectMap[theObject] = theAIS;
121  aContext->Display(anAIS, isUpdateViewer);
122  isCreated = true;
123  }
124  return isCreated;
125  }*/
126
127 void XGUI_Displayer::redisplay(ObjectPtr theObject, bool isUpdateViewer)
128 {
129   if (!isVisible(theObject))
130     return;
131
132   Handle(AIS_InteractiveObject) aAISIO;
133   boost::shared_ptr<GeomAPI_AISObject> aAISObj = getAISObject(theObject);
134   GeomPresentablePtr aPrs = boost::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
135   if (aPrs) {
136     boost::shared_ptr<GeomAPI_AISObject> aAIS_Obj = aPrs->getAISObject(aAISObj);
137     if (aAISObj && !aAIS_Obj) {
138       erase(theObject, isUpdateViewer);
139       return;
140     }
141     aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
142   } else {
143     ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
144     if (aResult) {
145       boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
146       if (aShapePtr) {
147         Handle(AIS_Shape) aAISShape = Handle(AIS_Shape)::DownCast(
148             aAISObj->impl<Handle(AIS_InteractiveObject)>());
149         if (!aAISShape.IsNull()) {
150           aAISShape->Set(aShapePtr->impl<TopoDS_Shape>());
151           aAISIO = aAISShape;
152         }
153       }
154     }
155   }
156   if (!aAISIO.IsNull()) {
157     Handle(AIS_InteractiveContext) aContext = AISContext();
158     aContext->Redisplay(aAISIO, isUpdateViewer);
159     //if (aContext->HasOpenedContext()) {
160     //  aContext->Load(aAISIO, -1, true/*allow decomposition*/);
161     //}
162   }
163 }
164
165 void XGUI_Displayer::activateInLocalContext(ObjectPtr theResult, const std::list<int>& theModes,
166                                             const bool isUpdateViewer)
167 {
168   Handle(AIS_InteractiveContext) aContext = AISContext();
169   // Open local context if there is no one
170   if (!aContext->HasOpenedContext()) {
171     aContext->ClearCurrents(false);
172     aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
173   }
174   // display or redisplay presentation
175   Handle(AIS_InteractiveObject) anAIS;
176   if (isVisible(theResult)) {
177     boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theResult];
178     if (anObj)
179       anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
180   }
181
182   // Activate selection of objects from prs
183   if (!anAIS.IsNull()) {
184     aContext->ClearSelected(false);  // ToCheck
185     //aContext->upClearSelected(false); // ToCheck
186     aContext->Load(anAIS, -1, true/*allow decomposition*/);
187     aContext->Deactivate(anAIS);
188
189     std::list<int>::const_iterator anIt = theModes.begin(), aLast = theModes.end();
190     for (; anIt != aLast; anIt++) {
191       aContext->Activate(anAIS, (*anIt));
192     }
193   }
194
195   if (isUpdateViewer)
196     updateViewer();
197 }
198
199 void XGUI_Displayer::deactivate(ObjectPtr theObject)
200 {
201   if (isVisible(theObject)) {
202     Handle(AIS_InteractiveContext) aContext = AISContext();
203
204     boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theObject];
205     Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
206     aContext->Deactivate(anAIS);
207   }
208 }
209
210 void XGUI_Displayer::activate(ObjectPtr theObject)
211 {
212   if (isVisible(theObject)) {
213     Handle(AIS_InteractiveContext) aContext = AISContext();
214
215     boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theObject];
216     Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
217     aContext->Activate(anAIS);
218   }
219 }
220
221 void XGUI_Displayer::stopSelection(const QList<ObjectPtr>& theResults, const bool isStop,
222                                    const bool isUpdateViewer)
223 {
224   Handle(AIS_InteractiveContext) aContext = AISContext();
225
226   Handle(AIS_Shape) anAIS;
227   QList<ObjectPtr>::const_iterator anIt = theResults.begin(), aLast = theResults.end();
228   ObjectPtr aFeature;
229   for (; anIt != aLast; anIt++) {
230     aFeature = *anIt;
231     if (isVisible(aFeature))
232       anAIS = Handle(AIS_Shape)::DownCast(
233           myResult2AISObjectMap[aFeature]->impl<Handle(AIS_InteractiveObject)>());
234     if (anAIS.IsNull())
235       continue;
236
237     if (isStop) {
238       QColor aColor(Qt::white);
239       anAIS->SetColor(
240           Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
241                          Quantity_TOC_RGB));
242       anAIS->Redisplay();
243     } else {
244       QColor aColor(Qt::red);
245       anAIS->SetColor(
246           Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
247                          Quantity_TOC_RGB));
248       anAIS->Redisplay();
249     }
250   }
251   if (isUpdateViewer)
252     updateViewer();
253 }
254
255 void XGUI_Displayer::setSelected(const QList<ObjectPtr>& theResults, const bool isUpdateViewer)
256 {
257   Handle(AIS_InteractiveContext) aContext = AISContext();
258   // we need to unhighligth objects manually in the current local context
259   // in couple with the selection clear (TODO)
260   Handle(AIS_LocalContext) aLocalContext = aContext->LocalContext();
261   if (!aLocalContext.IsNull())
262     aLocalContext->UnhilightLastDetected(myWorkshop->viewer()->activeView());
263
264   aContext->ClearSelected();
265   foreach(ObjectPtr aResult, theResults)
266   {
267     if (myResult2AISObjectMap.find(aResult) == myResult2AISObjectMap.end())
268       continue;
269
270     boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[aResult];
271     if (anObj) {
272       Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
273       if (!anAIS.IsNull())
274         aContext->AddOrRemoveSelected(anAIS, false);
275     }
276   }
277   if (isUpdateViewer)
278     updateViewer();
279 }
280
281 /*void XGUI_Displayer::EraseAll(const bool isUpdateViewer)
282  {
283  Handle(AIS_InteractiveContext) ic = AISContext();
284
285  AIS_ListOfInteractive aList;
286  ic->DisplayedObjects(aList);
287  AIS_ListIteratorOfListOfInteractive anIter(aList);
288  for (; anIter.More(); anIter.Next()) {
289  if ((anIter.Value()->DynamicType() == STANDARD_TYPE(AIS_Trihedron)))
290  continue;
291
292  // erase an object
293  Handle(AIS_InteractiveObject) anIO = anIter.Value();
294  ic->Erase(anIO, false);
295  }
296  myResult2AISObjectMap.clear();
297  if (isUpdateViewer)
298  updateViewer();
299  }*/
300
301 void XGUI_Displayer::eraseDeletedResults(const bool isUpdateViewer)
302 {
303   Handle(AIS_InteractiveContext) aContext = AISContext();
304
305   ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(), aFLast =
306       myResult2AISObjectMap.end();
307   std::list<ObjectPtr> aRemoved;
308   for (; aFIt != aFLast; aFIt++) {
309     ObjectPtr aFeature = (*aFIt).first;
310     if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
311       boost::shared_ptr<GeomAPI_AISObject> anObj = (*aFIt).second;
312       if (!anObj)
313         continue;
314       Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
315       if (!anAIS.IsNull()) {
316         aContext->Remove(anAIS, false);
317         aRemoved.push_back(aFeature);
318       }
319     }
320   }
321   std::list<ObjectPtr>::const_iterator anIt = aRemoved.begin(), aLast = aRemoved.end();
322   for (; anIt != aLast; anIt++) {
323     myResult2AISObjectMap.erase(myResult2AISObjectMap.find(*anIt));
324   }
325
326   if (isUpdateViewer)
327     updateViewer();
328 }
329
330 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
331 {
332   closeAllContexts(true);
333 }
334
335 boost::shared_ptr<GeomAPI_AISObject> XGUI_Displayer::getAISObject(ObjectPtr theObject) const
336 {
337   boost::shared_ptr<GeomAPI_AISObject> anIO;
338   if (myResult2AISObjectMap.find(theObject) != myResult2AISObjectMap.end())
339     anIO = (myResult2AISObjectMap.find(theObject))->second;
340   return anIO;
341 }
342
343 ObjectPtr XGUI_Displayer::getObject(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     boost::shared_ptr<GeomAPI_AISObject> 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(boost::shared_ptr<GeomAPI_AISObject> 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(boost::shared_ptr<GeomAPI_AISObject> 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