1 // File: XGUI_Displayer.cpp
2 // Created: 20 Apr 2014
3 // Author: Natalia ERMOLAEVA
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"
11 #include <ModelAPI_Document.h>
12 #include <ModelAPI_Data.h>
14 #include <AIS_InteractiveContext.hxx>
15 #include <AIS_LocalContext.hxx>
16 #include <AIS_ListOfInteractive.hxx>
17 #include <AIS_ListIteratorOfListOfInteractive.hxx>
19 #include <AIS_Shape.hxx>
23 const int MOUSE_SENSITIVITY_IN_PIXEL = 10; ///< defines the local context mouse selection sensitivity
25 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
27 myWorkshop = theWorkshop;
30 XGUI_Displayer::~XGUI_Displayer()
34 bool XGUI_Displayer::IsVisible(boost::shared_ptr<ModelAPI_Feature> theFeature)
36 return myFeature2AISObjectMap.find(theFeature) != myFeature2AISObjectMap.end();
39 void XGUI_Displayer::Display(boost::shared_ptr<ModelAPI_Feature> theFeature,
40 const bool isUpdateViewer)
44 /*void XGUI_Displayer::Display(boost::shared_ptr<ModelAPI_Feature> theFeature,
45 const TopoDS_Shape& theShape, const bool isUpdateViewer)
47 Handle(AIS_InteractiveContext) aContext = AISContext();
49 Handle(AIS_Shape) anAIS = new AIS_Shape(theShape);
50 myFeature2AISObjectMap[theFeature] = anAIS;
52 aContext->Display(anAIS, Standard_False);
54 aContext->UpdateCurrentViewer();
58 std::list<XGUI_ViewerPrs> XGUI_Displayer::GetSelected()
60 std::set<boost::shared_ptr<ModelAPI_Feature> > aPrsFeatures;
61 std::list<XGUI_ViewerPrs> aPresentations;
63 Handle(AIS_InteractiveContext) aContext = AISContext();
64 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
65 Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
66 TopoDS_Shape aShape = aContext->SelectedShape();
68 boost::shared_ptr<ModelAPI_Feature> aFeature = GetFeature(anIO);
69 if (aPrsFeatures.find(aFeature) != aPrsFeatures.end())
71 aPresentations.push_back(XGUI_ViewerPrs(aFeature, aShape));
72 aPrsFeatures.insert(aFeature);
74 return aPresentations;
77 std::list<XGUI_ViewerPrs> XGUI_Displayer::GetHighlighted()
79 std::set<boost::shared_ptr<ModelAPI_Feature> > aPrsFeatures;
80 std::list<XGUI_ViewerPrs> aPresentations;
82 Handle(AIS_InteractiveContext) aContext = AISContext();
83 for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
84 Handle(AIS_InteractiveObject) anIO = aContext->DetectedInteractive();
85 TopoDS_Shape aShape = aContext->DetectedShape();
87 boost::shared_ptr<ModelAPI_Feature> aFeature = GetFeature(anIO);
88 if (aPrsFeatures.find(aFeature) != aPrsFeatures.end())
90 aPresentations.push_back(XGUI_ViewerPrs(aFeature, aShape));
91 aPrsFeatures.insert(aFeature);
94 return aPresentations;
97 void XGUI_Displayer::Erase(boost::shared_ptr<ModelAPI_Feature> theFeature,
98 const bool isUpdateViewer)
100 if (myFeature2AISObjectMap.find(theFeature) == myFeature2AISObjectMap.end())
103 Handle(AIS_InteractiveContext) aContext = AISContext();
104 Handle(AIS_InteractiveObject) anAIS = myFeature2AISObjectMap[theFeature];
105 Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast(anAIS);
106 if (!anAISShape.IsNull())
108 aContext->Erase(anAISShape);
110 myFeature2AISObjectMap.erase(theFeature);
113 aContext->UpdateCurrentViewer();
116 void XGUI_Displayer::Redisplay(boost::shared_ptr<ModelAPI_Feature> theFeature,
117 const TopoDS_Shape& theShape, const bool isUpdateViewer)
119 Handle(AIS_InteractiveContext) aContext = AISContext();
120 // Open local context if there is no one
121 if (!aContext->HasOpenedContext()) {
122 aContext->ClearCurrents(false);
123 aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
124 // set mouse sensitivity
125 aContext->SetSensitivityMode(StdSelect_SM_WINDOW);
126 aContext->SetPixelTolerance(MOUSE_SENSITIVITY_IN_PIXEL);
128 // display or redisplay presentation
129 Handle(AIS_Shape) anAIS;
130 if (IsVisible(theFeature)) {
131 anAIS = Handle(AIS_Shape)::DownCast(myFeature2AISObjectMap[theFeature]);
132 if (!anAIS.IsNull()) {
133 // if the AIS object is displayed in the opened local context in some mode, additional
134 // AIS sub objects are created there. They should be rebuild for correct selecting.
135 // It is possible to correct it by closing local context before the shape set and opening
136 // after. Another workaround to thrown down the selection and reselecting the AIS.
137 // If there was a problem here, try the first solution with close/open local context.
138 anAIS->Set(theShape);
141 /*if (aContext->IsSelected(anAIS)) {
142 aContext->AddOrRemoveSelected(anAIS, false);
143 aContext->AddOrRemoveSelected(anAIS, false);
144 //aContext->SetSelected(anAIS, false);
149 anAIS = new AIS_Shape(theShape);
150 myFeature2AISObjectMap[theFeature] = anAIS;
151 aContext->Display(anAIS, false);
155 void XGUI_Displayer::ActivateInLocalContext(boost::shared_ptr<ModelAPI_Feature> theFeature,
156 const std::list<int>& theModes, const bool isUpdateViewer)
158 Handle(AIS_InteractiveContext) aContext = AISContext();
159 // Open local context if there is no one
160 if (!aContext->HasOpenedContext()) {
161 aContext->ClearCurrents(false);
162 aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
164 // display or redisplay presentation
165 Handle(AIS_Shape) anAIS;
166 if (IsVisible(theFeature))
167 anAIS = Handle(AIS_Shape)::DownCast(myFeature2AISObjectMap[theFeature]);
169 // Activate selection of objects from prs
170 if (!anAIS.IsNull()) {
171 aContext->Load(anAIS, -1, true/*allow decomposition*/);
172 aContext->Deactivate(anAIS);
174 std::list<int>::const_iterator anIt = theModes.begin(), aLast = theModes.end();
175 for (; anIt != aLast; anIt++)
177 aContext->Activate(anAIS, AIS_Shape::SelectionMode((TopAbs_ShapeEnum)*anIt));
182 aContext->UpdateCurrentViewer();
185 void XGUI_Displayer::StopSelection(const std::list<XGUI_ViewerPrs>& theFeatures, const bool isStop,
186 const bool isUpdateViewer)
188 Handle(AIS_InteractiveContext) aContext = AISContext();
190 Handle(AIS_Shape) anAIS;
191 std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
192 boost::shared_ptr<ModelAPI_Feature> aFeature;
193 for (; anIt != aLast; anIt++) {
194 aFeature = (*anIt).feature();
195 if (IsVisible(aFeature))
196 anAIS = Handle(AIS_Shape)::DownCast(myFeature2AISObjectMap[aFeature]);
201 QColor aColor(Qt::white);
202 anAIS->SetColor(Quantity_Color(aColor.red()/255., aColor.green()/255., aColor.blue()/255., Quantity_TOC_RGB));
206 QColor aColor(Qt::red);
207 anAIS->SetColor(Quantity_Color(aColor.red()/255., aColor.green()/255., aColor.blue()/255., Quantity_TOC_RGB));
212 aContext->UpdateCurrentViewer();
215 void XGUI_Displayer::SetSelected(const std::list<XGUI_ViewerPrs>& theFeatures, const bool isUpdateViewer)
217 Handle(AIS_InteractiveContext) aContext = AISContext();
219 std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
220 boost::shared_ptr<ModelAPI_Feature> aFeature;
222 Handle(AIS_Shape) anAIS;
223 // we need to unhighligth objects manually in the current local context
224 // in couple with the selection clear (TODO)
225 Handle(AIS_LocalContext) aLocalContext = aContext->LocalContext();
226 if (!aLocalContext.IsNull())
227 aLocalContext->UnhilightLastDetected(myWorkshop->viewer()->activeView());
228 aContext->ClearSelected(false);
230 for (; anIt != aLast; anIt++) {
231 aFeature = (*anIt).feature();
232 if (IsVisible(aFeature))
233 anAIS = Handle(AIS_Shape)::DownCast(myFeature2AISObjectMap[aFeature]);
236 aContext->AddOrRemoveSelected(anAIS, false);
240 aContext->UpdateCurrentViewer();
243 void XGUI_Displayer::EraseAll(const bool isUpdateViewer)
245 Handle(AIS_InteractiveContext) ic = AISContext();
247 AIS_ListOfInteractive aList;
248 ic->DisplayedObjects(aList);
249 AIS_ListIteratorOfListOfInteractive anIter(aList);
250 for (; anIter.More(); anIter.Next()) {
251 if ((anIter.Value()->DynamicType() == STANDARD_TYPE(AIS_Trihedron)))
255 Handle(AIS_InteractiveObject) anIO = anIter.Value();
256 ic->Erase(anIO, false);
258 myFeature2AISObjectMap.clear();
260 ic->UpdateCurrentViewer();
263 void XGUI_Displayer::EraseDeletedFeatures(const bool isUpdateViewer)
265 Handle(AIS_InteractiveContext) aContext = AISContext();
267 FeatureToAISMap::const_iterator aFIt = myFeature2AISObjectMap.begin(),
268 aFLast = myFeature2AISObjectMap.end();
269 std::list<boost::shared_ptr<ModelAPI_Feature>> aRemoved;
270 for (; aFIt != aFLast; aFIt++)
272 boost::shared_ptr<ModelAPI_Feature> aFeature = (*aFIt).first;
273 if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
274 Handle(AIS_InteractiveObject) anAIS = (*aFIt).second;
275 if (!anAIS.IsNull()) {
276 aContext->Erase(anAIS, false);
277 aRemoved.push_back(aFeature);
281 std::list<boost::shared_ptr<ModelAPI_Feature>>::const_iterator anIt = aRemoved.begin(),
282 aLast = aRemoved.end();
283 for (; anIt != aLast; anIt++) {
284 myFeature2AISObjectMap.erase(myFeature2AISObjectMap.find(*anIt));
288 aContext->UpdateCurrentViewer();
291 void XGUI_Displayer::CloseLocalContexts(const bool isUpdateViewer)
293 CloseAllContexts(true);
296 boost::shared_ptr<ModelAPI_Feature> XGUI_Displayer::GetFeature(Handle(AIS_InteractiveObject) theIO)
298 boost::shared_ptr<ModelAPI_Feature> aFeature;
299 FeatureToAISMap::const_iterator aFIt = myFeature2AISObjectMap.begin(),
300 aFLast = myFeature2AISObjectMap.end();
301 for (; aFIt != aFLast && !aFeature; aFIt++) {
302 Handle(AIS_InteractiveObject) anAIS = (*aFIt).second;
305 aFeature = (*aFIt).first;
310 void XGUI_Displayer::CloseAllContexts(const bool isUpdateViewer)
312 Handle(AIS_InteractiveContext) ic = AISContext();
314 ic->CloseAllContexts(false);
316 ic->UpdateCurrentViewer();
320 void XGUI_Displayer::UpdateViewer()
322 Handle(AIS_InteractiveContext) ic = AISContext();
324 ic->UpdateCurrentViewer();
327 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
329 return myWorkshop->viewer()->AISContext();