Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle and arc
[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   if (!anAIS.IsNull())
138   {
139     aContext->Erase(anAIS, isUpdateViewer);
140   }
141   myFeature2AISObjectMap.erase(aFeature);
142 }
143
144
145 bool XGUI_Displayer::redisplay(FeaturePtr theFeature,
146                                Handle(AIS_InteractiveObject) theAIS,
147                                const int theSelectionMode,
148                                const bool isUpdateViewer)
149 {
150   bool isCreated = false;
151   Handle(AIS_InteractiveContext) aContext = AISContext();
152   // Open local context if there is no one
153   if (!aContext->HasOpenedContext()) {
154     aContext->ClearCurrents(false);
155     aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
156     // set mouse sensitivity
157     //aContext->SetSensitivityMode(StdSelect_SM_WINDOW);
158     //aContext->SetPixelTolerance(MOUSE_SENSITIVITY_IN_PIXEL);
159   }
160   // display or redisplay presentation
161   if (isVisible(theFeature) && !myFeature2AISObjectMap[theFeature].IsNull()) {
162       aContext->RecomputeSelectionOnly(theAIS);
163   }
164   else {
165     myFeature2AISObjectMap[theFeature] = theAIS;
166     if (theSelectionMode < 0)
167     {
168       aContext->Display(theAIS, false);
169     }
170     else
171     {
172       aContext->Display(theAIS, 0, theSelectionMode, false);
173     }
174     isCreated = true;
175   }
176   if (isUpdateViewer)
177     updateViewer();
178
179   return isCreated;
180 }
181
182 void XGUI_Displayer::redisplay(FeaturePtr theFeature, bool isUpdateViewer)
183 {
184   FeaturePtr aFeature = XGUI_Tools::realFeature(theFeature);
185   if (!isVisible(aFeature))
186     return;
187
188   boost::shared_ptr<GeomAPI_Shape> aShapePtr = aFeature->data()->shape();
189   if (aShapePtr) {
190     Handle(AIS_InteractiveObject) aAISObj = getAISObject(aFeature);
191     Handle(AIS_Shape) aAISShape = Handle(AIS_Shape)::DownCast(aAISObj);
192     aAISShape->Set(aShapePtr->impl<TopoDS_Shape>());
193
194     AISContext()->Redisplay(aAISShape);
195   }
196 }
197
198 void XGUI_Displayer::activateInLocalContext(FeaturePtr theFeature,
199                                          const std::list<int>& theModes, const bool isUpdateViewer)
200 {
201   Handle(AIS_InteractiveContext) aContext = AISContext();
202   // Open local context if there is no one
203   if (!aContext->HasOpenedContext()) {
204     aContext->ClearCurrents(false);
205     aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
206   }
207   // display or redisplay presentation
208   Handle(AIS_Shape) anAIS;
209   if (isVisible(theFeature))
210     anAIS = Handle(AIS_Shape)::DownCast(myFeature2AISObjectMap[theFeature]);
211
212   // Activate selection of objects from prs
213   if (!anAIS.IsNull()) {
214         aContext->Load(anAIS, -1, true/*allow decomposition*/);
215     aContext->Deactivate(anAIS);
216
217     std::list<int>::const_iterator anIt = theModes.begin(), aLast = theModes.end();
218     for (; anIt != aLast; anIt++)
219     {
220       aContext->Activate(anAIS, AIS_Shape::SelectionMode((TopAbs_ShapeEnum)*anIt));
221         }
222   }
223
224   if (isUpdateViewer)
225     updateViewer();
226 }
227
228 void XGUI_Displayer::stopSelection(const std::list<XGUI_ViewerPrs>& theFeatures, const bool isStop,
229                                    const bool isUpdateViewer)
230 {
231   Handle(AIS_InteractiveContext) aContext = AISContext();
232
233   Handle(AIS_Shape) anAIS;
234   std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
235   FeaturePtr aFeature;
236   for (; anIt != aLast; anIt++) {
237     aFeature = (*anIt).feature();
238     if (isVisible(aFeature))
239       anAIS = Handle(AIS_Shape)::DownCast(myFeature2AISObjectMap[aFeature]);
240     if (anAIS.IsNull())
241       continue;
242
243     if (isStop) {
244       QColor aColor(Qt::white);
245       anAIS->SetColor(Quantity_Color(aColor.red()/255., aColor.green()/255., aColor.blue()/255., Quantity_TOC_RGB));
246       anAIS->Redisplay();
247     }
248     else {
249       QColor aColor(Qt::red);
250       anAIS->SetColor(Quantity_Color(aColor.red()/255., aColor.green()/255., aColor.blue()/255., Quantity_TOC_RGB));
251       anAIS->Redisplay();
252     }
253   }
254   if (isUpdateViewer)
255     updateViewer();
256 }
257
258 void XGUI_Displayer::setSelected(const std::list<XGUI_ViewerPrs>& theFeatures, const bool isUpdateViewer)
259 {
260   Handle(AIS_InteractiveContext) aContext = AISContext();
261
262   std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
263   FeaturePtr aFeature;
264
265   Handle(AIS_Shape) anAIS;
266   // we need to unhighligth objects manually in the current local context
267   // in couple with the selection clear (TODO)
268   Handle(AIS_LocalContext) aLocalContext = aContext->LocalContext();
269   if (!aLocalContext.IsNull())
270     aLocalContext->UnhilightLastDetected(myWorkshop->viewer()->activeView());
271   aContext->ClearSelected(false);
272
273   for (; anIt != aLast; anIt++) {
274     aFeature = (*anIt).feature();
275     if (isVisible(aFeature))
276       anAIS = Handle(AIS_Shape)::DownCast(myFeature2AISObjectMap[aFeature]);
277     if (anAIS.IsNull())
278       continue;
279     aContext->AddOrRemoveSelected(anAIS, false);
280   }
281  
282   if (isUpdateViewer)
283     updateViewer();
284 }
285
286
287 void XGUI_Displayer::setSelected(const QFeatureList& theFeatures, const bool isUpdateViewer)
288 {
289   Handle(AIS_InteractiveContext) aContext = AISContext();
290   aContext->ClearSelected();
291   foreach(FeaturePtr aFeature, theFeatures) {
292     FeaturePtr aRFeature = XGUI_Tools::realFeature(aFeature);
293     if (myFeature2AISObjectMap.find(aRFeature) == myFeature2AISObjectMap.end()) 
294       return;
295
296     Handle(AIS_InteractiveObject) anAIS = myFeature2AISObjectMap[aRFeature];
297     if (!anAIS.IsNull())
298       aContext->AddOrRemoveSelected(anAIS, false);
299   }
300   if (isUpdateViewer)
301     updateViewer();
302 }
303
304
305 /*void XGUI_Displayer::EraseAll(const bool isUpdateViewer)
306 {
307   Handle(AIS_InteractiveContext) ic = AISContext();
308
309   AIS_ListOfInteractive aList;
310   ic->DisplayedObjects(aList);
311   AIS_ListIteratorOfListOfInteractive anIter(aList);
312   for (; anIter.More(); anIter.Next()) {
313     if ((anIter.Value()->DynamicType() == STANDARD_TYPE(AIS_Trihedron)))
314       continue;
315
316     // erase an object
317     Handle(AIS_InteractiveObject) anIO = anIter.Value();
318     ic->Erase(anIO, false);
319   }
320   myFeature2AISObjectMap.clear();
321   if (isUpdateViewer)
322     updateViewer();
323 }*/
324
325 void XGUI_Displayer::eraseDeletedFeatures(const bool isUpdateViewer)
326 {
327   Handle(AIS_InteractiveContext) aContext = AISContext();
328
329   FeatureToAISMap::const_iterator aFIt = myFeature2AISObjectMap.begin(),
330                                   aFLast = myFeature2AISObjectMap.end();
331   std::list<FeaturePtr> aRemoved;
332   for (; aFIt != aFLast; aFIt++)
333   {
334     FeaturePtr aFeature = (*aFIt).first;
335     if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
336       Handle(AIS_InteractiveObject) anAIS = (*aFIt).second;
337       if (!anAIS.IsNull()) {
338         aContext->Erase(anAIS, false);
339         aRemoved.push_back(aFeature);
340       }
341     }
342   }
343   std::list<FeaturePtr>::const_iterator anIt = aRemoved.begin(),
344                                                                  aLast = aRemoved.end();
345   for (; anIt != aLast; anIt++) {
346     myFeature2AISObjectMap.erase(myFeature2AISObjectMap.find(*anIt));
347   }
348
349   if (isUpdateViewer)
350     updateViewer();
351 }
352
353 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
354 {
355   closeAllContexts(true);
356 }
357
358 Handle(AIS_InteractiveObject) XGUI_Displayer::getAISObject(
359                                               FeaturePtr theFeature) const
360 {
361   Handle(AIS_InteractiveObject) anIO;
362   if (myFeature2AISObjectMap.find(theFeature) != myFeature2AISObjectMap.end())
363     anIO = (myFeature2AISObjectMap.find(theFeature))->second;
364   return anIO;
365 }
366
367 FeaturePtr XGUI_Displayer::getFeature(Handle(AIS_InteractiveObject) theIO) const
368 {
369   FeaturePtr aFeature;
370   FeatureToAISMap::const_iterator aFIt = myFeature2AISObjectMap.begin(),
371                                   aFLast = myFeature2AISObjectMap.end();
372   for (; aFIt != aFLast && !aFeature; aFIt++) {
373     Handle(AIS_InteractiveObject) anAIS = (*aFIt).second;
374     if (anAIS != theIO)
375       continue;
376     aFeature = (*aFIt).first;
377   }
378   return aFeature;
379 }
380
381 void XGUI_Displayer::closeAllContexts(const bool isUpdateViewer)
382 {
383   Handle(AIS_InteractiveContext) ic = AISContext();
384   if (!ic.IsNull()) {
385     ic->CloseAllContexts(false);
386     if (isUpdateViewer)
387       updateViewer();
388   }
389 }
390
391 void XGUI_Displayer::updateViewer()
392 {
393   Handle(AIS_InteractiveContext) ic = AISContext();
394   if (!ic.IsNull())
395     ic->UpdateCurrentViewer();
396 }
397
398 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const 
399
400   return myWorkshop->viewer()->AISContext(); 
401 }