Salome HOME
Make property panel as a GUI of an operation
[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 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
208 {
209   Handle(AIS_InteractiveContext) aContext = AISContext();
210   if (aContext.IsNull())
211     return false;
212   if (!isVisible(theObject))
213     return false;
214     
215   boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap.at(theObject);
216   Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
217
218   TColStd_ListOfInteger aModes;
219   aContext->ActivatedModes(anAIS, aModes);
220   return aModes.Extent() > 0;
221 }
222
223 void XGUI_Displayer::stopSelection(const QList<ObjectPtr>& theResults, const bool isStop,
224                                    const bool isUpdateViewer)
225 {
226   Handle(AIS_InteractiveContext) aContext = AISContext();
227   if (aContext.IsNull())
228     return;
229
230   Handle(AIS_Shape) anAIS;
231   QList<ObjectPtr>::const_iterator anIt = theResults.begin(), aLast = theResults.end();
232   ObjectPtr aFeature;
233   for (; anIt != aLast; anIt++) {
234     aFeature = *anIt;
235     if (isVisible(aFeature))
236       anAIS = Handle(AIS_Shape)::DownCast(
237           myResult2AISObjectMap[aFeature]->impl<Handle(AIS_InteractiveObject)>());
238     if (anAIS.IsNull())
239       continue;
240
241     if (isStop) {
242       QColor aColor(Qt::white);
243       anAIS->SetColor(
244           Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
245                          Quantity_TOC_RGB));
246       anAIS->Redisplay();
247     } else {
248       QColor aColor(Qt::red);
249       anAIS->SetColor(
250           Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
251                          Quantity_TOC_RGB));
252       anAIS->Redisplay();
253     }
254   }
255   if (isUpdateViewer)
256     updateViewer();
257 }
258
259 void XGUI_Displayer::setSelected(const QList<ObjectPtr>& theResults, const bool isUpdateViewer)
260 {
261   Handle(AIS_InteractiveContext) aContext = AISContext();
262   // we need to unhighligth objects manually in the current local context
263   // in couple with the selection clear (TODO)
264   Handle(AIS_LocalContext) aLocalContext = aContext->LocalContext();
265   if (!aLocalContext.IsNull())
266     aLocalContext->UnhilightLastDetected(myWorkshop->viewer()->activeView());
267
268   aContext->ClearSelected();
269   foreach(ObjectPtr aResult, theResults)
270   {
271     if (myResult2AISObjectMap.find(aResult) == myResult2AISObjectMap.end())
272       continue;
273
274     boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[aResult];
275     if (anObj) {
276       Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
277       if (!anAIS.IsNull())
278         aContext->AddOrRemoveSelected(anAIS, false);
279     }
280   }
281   if (isUpdateViewer)
282     updateViewer();
283 }
284
285 void XGUI_Displayer::eraseAll(const bool isUpdateViewer)
286 {
287   Handle(AIS_InteractiveContext) ic = AISContext();
288   if (ic.IsNull())
289     return;
290
291    ResultToAISMap::iterator aIt;
292    for (aIt = myResult2AISObjectMap.begin(); aIt != myResult2AISObjectMap.end(); aIt++) {
293      // erase an object
294      boost::shared_ptr<GeomAPI_AISObject> aAISObj = (*aIt).second;
295      Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
296      if (!anIO.IsNull())
297       ic->Remove(anIO, false);
298    }
299    myResult2AISObjectMap.clear();
300    if (isUpdateViewer)
301      updateViewer();
302  }
303
304 void XGUI_Displayer::eraseDeletedResults(const bool isUpdateViewer)
305 {
306   Handle(AIS_InteractiveContext) aContext = AISContext();
307   if (aContext.IsNull())
308     return;
309
310   ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(), aFLast =
311       myResult2AISObjectMap.end();
312   std::list<ObjectPtr> aRemoved;
313   for (; aFIt != aFLast; aFIt++) {
314     ObjectPtr aFeature = (*aFIt).first;
315     if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
316       boost::shared_ptr<GeomAPI_AISObject> anObj = (*aFIt).second;
317       if (!anObj)
318         continue;
319       Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
320       if (!anAIS.IsNull()) {
321         aContext->Remove(anAIS, false);
322         aRemoved.push_back(aFeature);
323       }
324     }
325   }
326   std::list<ObjectPtr>::const_iterator anIt = aRemoved.begin(), aLast = aRemoved.end();
327   for (; anIt != aLast; anIt++) {
328     myResult2AISObjectMap.erase(myResult2AISObjectMap.find(*anIt));
329   }
330
331   if (isUpdateViewer)
332     updateViewer();
333 }
334
335 void XGUI_Displayer::openLocalContext()
336 {
337   Handle(AIS_InteractiveContext) aContext = AISContext();
338   if (aContext.IsNull())
339     return;
340   // Open local context if there is no one
341   if (!aContext->HasOpenedContext()) {
342     aContext->ClearCurrents(false);
343     //aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
344     aContext->OpenLocalContext();
345     aContext->NotUseDisplayedObjects();
346   }
347 }
348
349 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
350 {
351   AISContext()->ClearSelected(false);
352   closeAllContexts(true);
353 }
354
355 boost::shared_ptr<GeomAPI_AISObject> XGUI_Displayer::getAISObject(ObjectPtr theObject) const
356 {
357   boost::shared_ptr<GeomAPI_AISObject> anIO;
358   if (myResult2AISObjectMap.find(theObject) != myResult2AISObjectMap.end())
359     anIO = (myResult2AISObjectMap.find(theObject))->second;
360   return anIO;
361 }
362
363 ObjectPtr XGUI_Displayer::getObject(Handle(AIS_InteractiveObject) theIO) const
364 {
365   ObjectPtr aFeature;
366   ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(), aFLast =
367       myResult2AISObjectMap.end();
368   for (; aFIt != aFLast && !aFeature; aFIt++) {
369     boost::shared_ptr<GeomAPI_AISObject> anObj = (*aFIt).second;
370     if (!anObj)
371       continue;
372     Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
373     if (anAIS != theIO)
374       continue;
375     aFeature = (*aFIt).first;
376   }
377   return aFeature;
378 }
379
380 void XGUI_Displayer::closeAllContexts(const bool isUpdateViewer)
381 {
382   Handle(AIS_InteractiveContext) ic = AISContext();
383   if (!ic.IsNull()) {
384     ic->CloseAllContexts(false);
385     if (isUpdateViewer)
386       updateViewer();
387   }
388 }
389
390 void XGUI_Displayer::updateViewer()
391 {
392   Handle(AIS_InteractiveContext) ic = AISContext();
393   if (!ic.IsNull())
394     ic->UpdateCurrentViewer();
395 }
396
397 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
398 {
399   return myWorkshop->viewer()->AISContext();
400 }
401
402 void XGUI_Displayer::display(boost::shared_ptr<GeomAPI_AISObject> theAIS, bool isUpdate)
403 {
404   Handle(AIS_InteractiveContext) aContext = AISContext();
405   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
406   if (!anAISIO.IsNull())
407     aContext->Display(anAISIO, isUpdate);
408 }
409
410 void XGUI_Displayer::erase(boost::shared_ptr<GeomAPI_AISObject> theAIS, const bool isUpdate)
411 {
412   Handle(AIS_InteractiveContext) aContext = AISContext();
413   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
414   if (!anAISIO.IsNull()) {
415     aContext->Remove(anAISIO, isUpdate);
416   }
417 }
418
419 void XGUI_Displayer::activateObjectsOutOfContext(const std::list<int>& theModes, 
420                                                  Handle(SelectMgr_Filter) theFilter)
421 {
422   Handle(AIS_InteractiveContext) aContext = AISContext();
423   // Open local context if there is no one
424   if (!aContext->HasOpenedContext()) 
425     return;
426
427   aContext->UseDisplayedObjects();
428   std::list<int>::const_iterator anIt = theModes.begin(), aLast = theModes.end();
429   for (; anIt != aLast; anIt++) {
430     aContext->ActivateStandardMode((TopAbs_ShapeEnum)(*anIt));
431   }
432
433   if (!theFilter.IsNull())
434     aContext->AddFilter(theFilter);
435 }
436
437
438 void XGUI_Displayer::deactivateObjectsOutOfContext()
439 {
440   Handle(AIS_InteractiveContext) aContext = AISContext();
441   // Open local context if there is no one
442   if (!aContext->HasOpenedContext()) 
443     return;
444
445   aContext->RemoveFilters();
446   aContext->NotUseDisplayedObjects();
447 }
448
449
450 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool toUpdate)
451 {
452   if (theMode == NoMode)
453     return;
454
455   Handle(AIS_InteractiveContext) aContext = AISContext();
456   if (aContext.IsNull())
457     return;
458
459   boost::shared_ptr<GeomAPI_AISObject> aAISObj = getAISObject(theObject);
460   if (!aAISObj)
461     return;
462
463   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
464   aContext->SetDisplayMode(aAISIO, theMode, toUpdate);
465 }
466
467
468 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
469 {
470   Handle(AIS_InteractiveContext) aContext = AISContext();
471   if (aContext.IsNull())
472     return NoMode;
473
474   boost::shared_ptr<GeomAPI_AISObject> aAISObj = getAISObject(theObject);
475   if (!aAISObj)
476     return NoMode;
477
478   Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
479   return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
480 }
481