]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_Displayer.cpp
Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[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
14 #include <AIS_InteractiveContext.hxx>
15 #include <AIS_LocalContext.hxx>
16 #include <AIS_ListOfInteractive.hxx>
17 #include <AIS_ListIteratorOfListOfInteractive.hxx>
18
19 #include <AIS_Shape.hxx>
20
21 #include <set>
22
23 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
24 {
25   myWorkshop = theWorkshop;
26 }
27
28 XGUI_Displayer::~XGUI_Displayer()
29 {
30 }
31
32 bool XGUI_Displayer::IsVisible(boost::shared_ptr<ModelAPI_Feature> theFeature)
33 {
34   return myFeature2AISObjectMap.find(theFeature) != myFeature2AISObjectMap.end();
35 }
36
37 void XGUI_Displayer::Display(boost::shared_ptr<ModelAPI_Feature> theFeature,
38                              const bool isUpdateViewer)
39 {
40 }
41
42 /*void XGUI_Displayer::Display(boost::shared_ptr<ModelAPI_Feature> theFeature,
43                              const TopoDS_Shape& theShape, const bool isUpdateViewer)
44 {
45   Handle(AIS_InteractiveContext) aContext = AISContext();
46
47   Handle(AIS_Shape) anAIS = new AIS_Shape(theShape);
48   myFeature2AISObjectMap[theFeature] = anAIS;
49
50   aContext->Display(anAIS, Standard_False);
51   if (isUpdateViewer)
52     aContext->UpdateCurrentViewer();
53 }*/
54
55
56 std::list<XGUI_ViewerPrs> XGUI_Displayer::GetSelected()
57 {
58   std::set<boost::shared_ptr<ModelAPI_Feature> > aPrsFeatures;
59   std::list<XGUI_ViewerPrs> aPresentations;
60
61   Handle(AIS_InteractiveContext) aContext = AISContext();
62   for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
63     Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
64     TopoDS_Shape aShape = aContext->SelectedShape();
65
66     boost::shared_ptr<ModelAPI_Feature> aFeature = GetFeature(anIO);
67     if (aPrsFeatures.find(aFeature) != aPrsFeatures.end())
68       continue;
69     aPresentations.push_back(XGUI_ViewerPrs(aFeature, aShape));
70     aPrsFeatures.insert(aFeature);
71   }
72   return aPresentations;
73 }
74
75 std::list<XGUI_ViewerPrs> XGUI_Displayer::GetHighlighted()
76 {
77   std::set<boost::shared_ptr<ModelAPI_Feature> > aPrsFeatures;
78   std::list<XGUI_ViewerPrs> aPresentations;
79
80   Handle(AIS_InteractiveContext) aContext = AISContext();
81   for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
82     Handle(AIS_InteractiveObject) anIO = aContext->DetectedInteractive();
83     TopoDS_Shape aShape = aContext->DetectedShape();
84
85     boost::shared_ptr<ModelAPI_Feature> aFeature = GetFeature(anIO);
86     if (aPrsFeatures.find(aFeature) != aPrsFeatures.end())
87       continue;
88     aPresentations.push_back(XGUI_ViewerPrs(aFeature, aShape));
89     aPrsFeatures.insert(aFeature);
90   }
91
92   return aPresentations;
93 }
94
95 void XGUI_Displayer::Erase(boost::shared_ptr<ModelAPI_Feature> theFeature,
96                            const bool isUpdateViewer)
97 {
98   if (myFeature2AISObjectMap.find(theFeature) == myFeature2AISObjectMap.end())
99     return;
100
101   Handle(AIS_InteractiveContext) aContext = AISContext();
102   Handle(AIS_InteractiveObject) anAIS = myFeature2AISObjectMap[theFeature];
103   Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast(anAIS);
104   if (!anAISShape.IsNull())
105   {
106     aContext->Erase(anAISShape);
107   }
108   myFeature2AISObjectMap.erase(theFeature);
109
110   if (isUpdateViewer)
111     aContext->UpdateCurrentViewer();
112 }
113
114 void XGUI_Displayer::Redisplay(boost::shared_ptr<ModelAPI_Feature> theFeature,
115                                const TopoDS_Shape& theShape, const bool isUpdateViewer)
116 {
117   Handle(AIS_InteractiveContext) aContext = AISContext();
118   // Open local context if there is no one
119   if (!aContext->HasOpenedContext()) {
120     aContext->ClearCurrents(false);
121     aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
122   }
123   // display or redisplay presentation
124   Handle(AIS_Shape) anAIS;
125   if (IsVisible(theFeature)) {
126     anAIS = Handle(AIS_Shape)::DownCast(myFeature2AISObjectMap[theFeature]);
127     if (!anAIS.IsNull()) {
128       // if the AIS object is displayed in the opened local context in some mode, additional
129       // AIS sub objects are created there. They should be rebuild for correct selecting.
130       // It is possible to correct it by closing local context before the shape set and opening
131       // after. Another workaround to thrown down the selection and reselecting the AIS.
132       // If there was a problem here, try the first solution with close/open local context.
133       anAIS->Set(theShape);
134       anAIS->Redisplay();
135
136       /*if (aContext->IsSelected(anAIS)) {
137         aContext->AddOrRemoveSelected(anAIS, false);
138         aContext->AddOrRemoveSelected(anAIS, false);
139         //aContext->SetSelected(anAIS, false);
140       }*/
141     }
142   }
143   else {
144     anAIS = new AIS_Shape(theShape);
145     myFeature2AISObjectMap[theFeature] = anAIS;
146     aContext->Display(anAIS, false);
147   }
148 }
149
150 void XGUI_Displayer::ActivateInLocalContext(boost::shared_ptr<ModelAPI_Feature> theFeature,
151                                          const std::list<int>& theModes, const bool isUpdateViewer)
152 {
153   Handle(AIS_InteractiveContext) aContext = AISContext();
154   // Open local context if there is no one
155   if (!aContext->HasOpenedContext()) {
156     aContext->ClearCurrents(false);
157     aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
158   }
159   // display or redisplay presentation
160   Handle(AIS_Shape) anAIS;
161   if (IsVisible(theFeature))
162     anAIS = Handle(AIS_Shape)::DownCast(myFeature2AISObjectMap[theFeature]);
163
164   // Activate selection of objects from prs
165   if (!anAIS.IsNull()) {
166         aContext->Load(anAIS, -1, true/*allow decomposition*/);
167     aContext->Deactivate(anAIS);
168
169     std::list<int>::const_iterator anIt = theModes.begin(), aLast = theModes.end();
170     for (; anIt != aLast; anIt++)
171     {
172       aContext->Activate(anAIS, AIS_Shape::SelectionMode((TopAbs_ShapeEnum)*anIt));
173         }
174   }
175
176   if (isUpdateViewer)
177     aContext->UpdateCurrentViewer();
178 }
179
180 void XGUI_Displayer::StopSelection(const std::list<XGUI_ViewerPrs>& theFeatures, const bool isStop,
181                                    const bool isUpdateViewer)
182 {
183   Handle(AIS_InteractiveContext) aContext = AISContext();
184
185   Handle(AIS_Shape) anAIS;
186   std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
187   boost::shared_ptr<ModelAPI_Feature> aFeature;
188   for (; anIt != aLast; anIt++) {
189     aFeature = (*anIt).feature();
190     if (IsVisible(aFeature))
191       anAIS = Handle(AIS_Shape)::DownCast(myFeature2AISObjectMap[aFeature]);
192     if (anAIS.IsNull())
193       continue;
194
195     if (isStop) {
196       QColor aColor(Qt::white);
197       anAIS->SetColor(Quantity_Color(aColor.red()/255., aColor.green()/255., aColor.blue()/255., Quantity_TOC_RGB));
198       anAIS->Redisplay();
199     }
200     else {
201       QColor aColor(Qt::red);
202       anAIS->SetColor(Quantity_Color(aColor.red()/255., aColor.green()/255., aColor.blue()/255., Quantity_TOC_RGB));
203       anAIS->Redisplay();
204     }
205   }
206   if (isUpdateViewer)
207     aContext->UpdateCurrentViewer();
208 }
209
210 void XGUI_Displayer::SetSelected(const std::list<XGUI_ViewerPrs>& theFeatures, const bool isUpdateViewer)
211 {
212   Handle(AIS_InteractiveContext) aContext = AISContext();
213
214   std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
215   boost::shared_ptr<ModelAPI_Feature> aFeature;
216
217   Handle(AIS_Shape) anAIS;
218   // we need to unhighligth objects manually in the current local context
219   // in couple with the selection clear (TODO)
220   Handle(AIS_LocalContext) aLocalContext = aContext->LocalContext();
221   if (!aLocalContext.IsNull())
222     aLocalContext->UnhilightLastDetected(myWorkshop->viewer()->activeView());
223   aContext->ClearSelected(false);
224
225   for (; anIt != aLast; anIt++) {
226     aFeature = (*anIt).feature();
227     if (IsVisible(aFeature))
228       anAIS = Handle(AIS_Shape)::DownCast(myFeature2AISObjectMap[aFeature]);
229     if (anAIS.IsNull())
230       continue;
231     aContext->AddOrRemoveSelected(anAIS, false);
232   }
233  
234   if (isUpdateViewer)
235     aContext->UpdateCurrentViewer();
236 }
237
238 void XGUI_Displayer::EraseAll(const bool isUpdateViewer)
239 {
240   Handle(AIS_InteractiveContext) ic = AISContext();
241
242   AIS_ListOfInteractive aList;
243   ic->DisplayedObjects(aList);
244   AIS_ListIteratorOfListOfInteractive anIter(aList);
245   for (; anIter.More(); anIter.Next()) {
246     if ((anIter.Value()->DynamicType() == STANDARD_TYPE(AIS_Trihedron)))
247       continue;
248
249     // erase an object
250     Handle(AIS_InteractiveObject) anIO = anIter.Value();
251     ic->Erase(anIO, false);
252   }
253   myFeature2AISObjectMap.clear();
254   if (isUpdateViewer)
255     ic->UpdateCurrentViewer();
256 }
257
258 void XGUI_Displayer::EraseDeletedFeatures(const bool isUpdateViewer)
259 {
260   Handle(AIS_InteractiveContext) aContext = AISContext();
261
262   FeatureToAISMap::const_iterator aFIt = myFeature2AISObjectMap.begin(),
263                                   aFLast = myFeature2AISObjectMap.end();
264   std::list<boost::shared_ptr<ModelAPI_Feature>> aRemoved;
265   for (; aFIt != aFLast; aFIt++)
266   {
267     boost::shared_ptr<ModelAPI_Feature> aFeature = (*aFIt).first;
268     if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
269       Handle(AIS_InteractiveObject) anAIS = (*aFIt).second;
270       if (!anAIS.IsNull()) {
271         aContext->Erase(anAIS, false);
272         aRemoved.push_back(aFeature);
273       }
274     }
275   }
276   std::list<boost::shared_ptr<ModelAPI_Feature>>::const_iterator anIt = aRemoved.begin(),
277                                                                  aLast = aRemoved.end();
278   for (; anIt != aLast; anIt++) {
279     myFeature2AISObjectMap.erase(myFeature2AISObjectMap.find(*anIt));
280   }
281
282   if (isUpdateViewer)
283     aContext->UpdateCurrentViewer();
284 }
285
286 void XGUI_Displayer::CloseLocalContexts(const bool isUpdateViewer)
287 {
288   CloseAllContexts(true);
289 }
290
291 boost::shared_ptr<ModelAPI_Feature> XGUI_Displayer::GetFeature(Handle(AIS_InteractiveObject) theIO)
292 {
293   boost::shared_ptr<ModelAPI_Feature> aFeature;
294   FeatureToAISMap::const_iterator aFIt = myFeature2AISObjectMap.begin(),
295                                   aFLast = myFeature2AISObjectMap.end();
296   for (; aFIt != aFLast && !aFeature; aFIt++) {
297     Handle(AIS_InteractiveObject) anAIS = (*aFIt).second;
298     if (anAIS != theIO)
299       continue;
300     aFeature = (*aFIt).first;
301   }
302   return aFeature;
303 }
304
305 void XGUI_Displayer::CloseAllContexts(const bool isUpdateViewer)
306 {
307   Handle(AIS_InteractiveContext) ic = AISContext();
308   if (!ic.IsNull()) {
309     ic->CloseAllContexts(false);
310     if (isUpdateViewer)
311       ic->UpdateCurrentViewer();
312   }
313 }
314
315 void XGUI_Displayer::UpdateViewer()
316 {
317   Handle(AIS_InteractiveContext) ic = AISContext();
318   if (!ic.IsNull())
319     ic->UpdateCurrentViewer();
320 }
321
322 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const 
323
324   return myWorkshop->viewer()->AISContext(); 
325 }