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