]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_Displayer.cpp
Salome HOME
Sketcher bugfixing
[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 "XGUI_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(FeaturePtr theFeature)
39 {
40   FeaturePtr aFeature = XGUI_Tools::realFeature(theFeature);
41   return myFeature2AISObjectMap.find(aFeature) != myFeature2AISObjectMap.end();
42 }
43
44 bool XGUI_Displayer::display(FeaturePtr theFeature, bool isUpdateViewer)
45 {
46   FeaturePtr aFeature = XGUI_Tools::realFeature(theFeature);
47   boost::shared_ptr<GeomAPI_Shape> aShapePtr = aFeature->data()->shape();
48
49   if (aShapePtr) {
50     boost::shared_ptr<GeomAPI_AISObject> anAIS(new GeomAPI_AISObject());
51     anAIS->createShape(aShapePtr);
52     return display(aFeature, anAIS, isUpdateViewer);
53   }
54   return false;
55 }
56
57 bool XGUI_Displayer::display(FeaturePtr theFeature,
58                              boost::shared_ptr<GeomAPI_AISObject> theAIS, bool isUpdateViewer)
59 {
60   Handle(AIS_InteractiveContext) aContext = AISContext();
61
62   Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
63   if (!anAISIO.IsNull()) {
64     myFeature2AISObjectMap[theFeature] = theAIS;
65     aContext->Display(anAISIO, isUpdateViewer);
66     return true;
67   }
68   return false;
69 }
70
71
72 std::list<XGUI_ViewerPrs> XGUI_Displayer::getSelected(const int theShapeTypeToSkip)
73 {
74   std::set<FeaturePtr > aPrsFeatures;
75   std::list<XGUI_ViewerPrs> aPresentations;
76
77   Handle(AIS_InteractiveContext) aContext = AISContext();
78   for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
79     Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
80     TopoDS_Shape aShape = aContext->SelectedShape();
81
82     if (theShapeTypeToSkip >= 0 && !aShape.IsNull() && aShape.ShapeType() == theShapeTypeToSkip)
83       continue;
84
85     FeaturePtr aFeature = getFeature(anIO);
86     if (aPrsFeatures.find(aFeature) != aPrsFeatures.end())
87       continue;
88     Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
89     aPresentations.push_back(XGUI_ViewerPrs(aFeature, aShape, anOwner));
90     aPrsFeatures.insert(aFeature);
91   }
92   return aPresentations;
93 }
94
95 QFeatureList XGUI_Displayer::selectedFeatures() const
96 {
97   QFeatureList aSelectedList;
98
99   Handle(AIS_InteractiveContext) aContext = AISContext();
100   for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
101     Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
102     FeaturePtr aFeature = getFeature(anIO);
103     if (aFeature)
104       aSelectedList.append(aFeature);
105   }
106   return aSelectedList;
107 }
108
109
110 std::list<XGUI_ViewerPrs> XGUI_Displayer::getHighlighted(const int theShapeTypeToSkip)
111 {
112   std::set<FeaturePtr > aPrsFeatures;
113   std::list<XGUI_ViewerPrs> aPresentations;
114
115   Handle(AIS_InteractiveContext) aContext = AISContext();
116   for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
117     Handle(AIS_InteractiveObject) anIO = aContext->DetectedInteractive();
118     TopoDS_Shape aShape = aContext->DetectedShape();
119     if (theShapeTypeToSkip >= 0 && !aShape.IsNull() && aShape.ShapeType() == theShapeTypeToSkip)
120       continue;
121
122     FeaturePtr aFeature = getFeature(anIO);
123     if (aPrsFeatures.find(aFeature) != aPrsFeatures.end())
124       continue;
125     aPresentations.push_back(XGUI_ViewerPrs(aFeature, aShape, NULL));
126     aPrsFeatures.insert(aFeature);
127   }
128
129   return aPresentations;
130 }
131
132 void XGUI_Displayer::erase(FeaturePtr theFeature,
133                            const bool isUpdateViewer)
134 {
135   FeaturePtr aFeature = XGUI_Tools::realFeature(theFeature);
136
137   if (myFeature2AISObjectMap.find(aFeature) == myFeature2AISObjectMap.end())
138     return;
139
140   Handle(AIS_InteractiveContext) aContext = AISContext();
141   boost::shared_ptr<GeomAPI_AISObject> anObject = myFeature2AISObjectMap[aFeature];
142   if (anObject)  {
143     Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
144     if (!anAIS.IsNull()) {
145       aContext->Erase(anAIS, isUpdateViewer);
146       
147     }
148   }
149   myFeature2AISObjectMap.erase(aFeature);
150 }
151
152
153 bool XGUI_Displayer::redisplay(FeaturePtr theFeature,
154                                boost::shared_ptr<GeomAPI_AISObject> theAIS,
155                                const bool isUpdateViewer)
156 {
157   bool isCreated = false;
158   Handle(AIS_InteractiveObject) anAIS = 
159     theAIS ? theAIS->impl<Handle(AIS_InteractiveObject)>() : Handle(AIS_InteractiveObject)();
160   Handle(AIS_InteractiveContext) aContext = AISContext();
161   // Open local context if there is no one
162   if (!aContext->HasOpenedContext()) {
163     aContext->ClearCurrents(false);
164     aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
165     // set mouse sensitivity
166     //aContext->SetSensitivityMode(StdSelect_SM_WINDOW);
167     //aContext->SetPixelTolerance(MOUSE_SENSITIVITY_IN_PIXEL);
168   }
169   // display or redisplay presentation
170   boost::shared_ptr<GeomAPI_AISObject> anObj = myFeature2AISObjectMap[theFeature];
171   if (isVisible(theFeature) && anObj && !anObj->empty()) {
172       aContext->RecomputeSelectionOnly(anAIS);
173   }
174   else {
175     myFeature2AISObjectMap[theFeature] = theAIS;
176     aContext->Display(anAIS, false);
177     isCreated = true;
178   }
179   if (isUpdateViewer)
180     updateViewer();
181
182   return isCreated;
183 }
184
185 bool XGUI_Displayer::redisplay(FeaturePtr theFeature, bool isUpdateViewer)
186 {
187   FeaturePtr aFeature = XGUI_Tools::realFeature(theFeature);
188   if (!isVisible(aFeature))
189     return false;
190
191   boost::shared_ptr<GeomAPI_Shape> aShapePtr = aFeature->data()->shape();
192   if (aShapePtr) {
193     boost::shared_ptr<GeomAPI_AISObject> aAISObj = getAISObject(aFeature);
194     Handle(AIS_Shape) aAISShape = Handle(AIS_Shape)::DownCast(aAISObj->impl<Handle(AIS_InteractiveObject)>());
195     if (!aAISShape.IsNull()) {
196       aAISShape->Set(aShapePtr->impl<TopoDS_Shape>());
197       AISContext()->Redisplay(aAISShape);
198       return true;
199     }
200   }
201   return false;
202 }
203
204 void XGUI_Displayer::activateInLocalContext(FeaturePtr theFeature,
205                                          const std::list<int>& theModes, const bool isUpdateViewer)
206 {
207   Handle(AIS_InteractiveContext) aContext = AISContext();
208   // Open local context if there is no one
209   if (!aContext->HasOpenedContext()) {
210     aContext->ClearCurrents(false);
211     aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
212   }
213   // display or redisplay presentation
214   Handle(AIS_InteractiveObject) anAIS;
215   if (isVisible(theFeature))
216   {
217     boost::shared_ptr<GeomAPI_AISObject> anObj = myFeature2AISObjectMap[theFeature];
218     if (anObj)
219       anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
220   }
221
222   // Activate selection of objects from prs
223   if (!anAIS.IsNull()) {
224     aContext->Load(anAIS, -1, true/*allow decomposition*/);
225     aContext->Deactivate(anAIS);
226
227     std::list<int>::const_iterator anIt = theModes.begin(), aLast = theModes.end();
228     for (; anIt != aLast; anIt++)
229     {
230       aContext->Activate(anAIS, (*anIt));
231     }
232   }
233
234   if (isUpdateViewer)
235     updateViewer();
236 }
237
238 void XGUI_Displayer::stopSelection(const QFeatureList& theFeatures, const bool isStop,
239                                    const bool isUpdateViewer)
240 {
241   Handle(AIS_InteractiveContext) aContext = AISContext();
242
243   Handle(AIS_Shape) anAIS;
244   QFeatureList::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
245   FeaturePtr aFeature;
246   for (; anIt != aLast; anIt++) {
247     aFeature = *anIt;
248     if (isVisible(aFeature))
249       anAIS = Handle(AIS_Shape)::DownCast(myFeature2AISObjectMap[aFeature]->impl<Handle(AIS_InteractiveObject)>());
250     if (anAIS.IsNull())
251       continue;
252
253     if (isStop) {
254       QColor aColor(Qt::white);
255       anAIS->SetColor(Quantity_Color(aColor.red()/255., aColor.green()/255., aColor.blue()/255., Quantity_TOC_RGB));
256       anAIS->Redisplay();
257     }
258     else {
259       QColor aColor(Qt::red);
260       anAIS->SetColor(Quantity_Color(aColor.red()/255., aColor.green()/255., aColor.blue()/255., Quantity_TOC_RGB));
261       anAIS->Redisplay();
262     }
263   }
264   if (isUpdateViewer)
265     updateViewer();
266 }
267
268 void XGUI_Displayer::setSelected(const QFeatureList& theFeatures, const bool isUpdateViewer)
269 {
270   Handle(AIS_InteractiveContext) aContext = AISContext();
271   // we need to unhighligth objects manually in the current local context
272   // in couple with the selection clear (TODO)
273   Handle(AIS_LocalContext) aLocalContext = aContext->LocalContext();
274   if (!aLocalContext.IsNull())
275     aLocalContext->UnhilightLastDetected(myWorkshop->viewer()->activeView());
276
277   aContext->ClearSelected();
278   foreach(FeaturePtr aFeature, theFeatures) {
279     FeaturePtr aRFeature = XGUI_Tools::realFeature(aFeature);
280     if (myFeature2AISObjectMap.find(aRFeature) == myFeature2AISObjectMap.end()) 
281       return;
282
283     boost::shared_ptr<GeomAPI_AISObject> anObj = myFeature2AISObjectMap[aRFeature];
284     if (anObj)
285     {
286       Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
287       if (!anAIS.IsNull())
288         aContext->AddOrRemoveSelected(anAIS, false);
289     }
290   }
291   if (isUpdateViewer)
292     updateViewer();
293 }
294
295
296 /*void XGUI_Displayer::EraseAll(const bool isUpdateViewer)
297 {
298   Handle(AIS_InteractiveContext) ic = AISContext();
299
300   AIS_ListOfInteractive aList;
301   ic->DisplayedObjects(aList);
302   AIS_ListIteratorOfListOfInteractive anIter(aList);
303   for (; anIter.More(); anIter.Next()) {
304     if ((anIter.Value()->DynamicType() == STANDARD_TYPE(AIS_Trihedron)))
305       continue;
306
307     // erase an object
308     Handle(AIS_InteractiveObject) anIO = anIter.Value();
309     ic->Erase(anIO, false);
310   }
311   myFeature2AISObjectMap.clear();
312   if (isUpdateViewer)
313     updateViewer();
314 }*/
315
316 void XGUI_Displayer::eraseDeletedFeatures(const bool isUpdateViewer)
317 {
318   Handle(AIS_InteractiveContext) aContext = AISContext();
319
320   FeatureToAISMap::const_iterator aFIt = myFeature2AISObjectMap.begin(),
321                                   aFLast = myFeature2AISObjectMap.end();
322   std::list<FeaturePtr> aRemoved;
323   for (; aFIt != aFLast; aFIt++)
324   {
325     FeaturePtr aFeature = (*aFIt).first;
326     if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
327       boost::shared_ptr<GeomAPI_AISObject> anObj = (*aFIt).second;
328       if (!anObj) continue;
329       Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
330       if (!anAIS.IsNull()) {
331         aContext->Erase(anAIS, false);
332         aRemoved.push_back(aFeature);
333       }
334     }
335   }
336   std::list<FeaturePtr>::const_iterator anIt = aRemoved.begin(),
337                                                                  aLast = aRemoved.end();
338   for (; anIt != aLast; anIt++) {
339     myFeature2AISObjectMap.erase(myFeature2AISObjectMap.find(*anIt));
340   }
341
342   if (isUpdateViewer)
343     updateViewer();
344 }
345
346 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
347 {
348   closeAllContexts(true);
349 }
350
351 boost::shared_ptr<GeomAPI_AISObject> XGUI_Displayer::getAISObject(
352                                               FeaturePtr theFeature) const
353 {
354   boost::shared_ptr<GeomAPI_AISObject> anIO;
355   if (myFeature2AISObjectMap.find(theFeature) != myFeature2AISObjectMap.end())
356     anIO = (myFeature2AISObjectMap.find(theFeature))->second;
357   return anIO;
358 }
359
360 FeaturePtr XGUI_Displayer::getFeature(Handle(AIS_InteractiveObject) theIO) const
361 {
362   FeaturePtr aFeature;
363   FeatureToAISMap::const_iterator aFIt = myFeature2AISObjectMap.begin(),
364                                   aFLast = myFeature2AISObjectMap.end();
365   for (; aFIt != aFLast && !aFeature; aFIt++) {
366     boost::shared_ptr<GeomAPI_AISObject> anObj = (*aFIt).second;
367     if (!anObj) continue;
368     Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
369     if (anAIS != theIO)
370       continue;
371     aFeature = (*aFIt).first;
372   }
373   return aFeature;
374 }
375
376 void XGUI_Displayer::closeAllContexts(const bool isUpdateViewer)
377 {
378   Handle(AIS_InteractiveContext) ic = AISContext();
379   if (!ic.IsNull()) {
380     ic->CloseAllContexts(false);
381     if (isUpdateViewer)
382       updateViewer();
383   }
384 }
385
386 void XGUI_Displayer::updateViewer()
387 {
388   Handle(AIS_InteractiveContext) ic = AISContext();
389   if (!ic.IsNull())
390     ic->UpdateCurrentViewer();
391 }
392
393 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const 
394
395   return myWorkshop->viewer()->AISContext(); 
396 }