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