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