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