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>
18 #include <AIS_DimensionSelectionMode.hxx>
20 #include <AIS_Shape.hxx>
24 const int MOUSE_SENSITIVITY_IN_PIXEL = 10; ///< defines the local context mouse selection sensitivity
26 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
28 myWorkshop = theWorkshop;
31 XGUI_Displayer::~XGUI_Displayer()
35 bool XGUI_Displayer::IsVisible(boost::shared_ptr<ModelAPI_Feature> theFeature)
37 return myFeature2AISObjectMap.find(theFeature) != myFeature2AISObjectMap.end();
40 void XGUI_Displayer::Display(boost::shared_ptr<ModelAPI_Feature> theFeature,
41 const bool isUpdateViewer)
45 /*void XGUI_Displayer::Display(boost::shared_ptr<ModelAPI_Feature> theFeature,
46 const TopoDS_Shape& theShape, const bool isUpdateViewer)
48 Handle(AIS_InteractiveContext) aContext = AISContext();
50 Handle(AIS_Shape) anAIS = new AIS_Shape(theShape);
51 myFeature2AISObjectMap[theFeature] = anAIS;
53 aContext->Display(anAIS, Standard_False);
59 std::list<XGUI_ViewerPrs> XGUI_Displayer::GetSelected(const int theShapeTypeToSkip)
61 std::set<boost::shared_ptr<ModelAPI_Feature> > aPrsFeatures;
62 std::list<XGUI_ViewerPrs> aPresentations;
64 Handle(AIS_InteractiveContext) aContext = AISContext();
65 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
66 Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
67 TopoDS_Shape aShape = aContext->SelectedShape();
69 if (theShapeTypeToSkip >= 0 && !aShape.IsNull() && aShape.ShapeType() == theShapeTypeToSkip)
72 boost::shared_ptr<ModelAPI_Feature> aFeature = GetFeature(anIO);
73 if (aPrsFeatures.find(aFeature) != aPrsFeatures.end())
75 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
76 aPresentations.push_back(XGUI_ViewerPrs(aFeature, aShape, anOwner));
77 aPrsFeatures.insert(aFeature);
79 return aPresentations;
82 std::list<XGUI_ViewerPrs> XGUI_Displayer::GetHighlighted(const int theShapeTypeToSkip)
84 std::set<boost::shared_ptr<ModelAPI_Feature> > aPrsFeatures;
85 std::list<XGUI_ViewerPrs> aPresentations;
87 Handle(AIS_InteractiveContext) aContext = AISContext();
88 for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
89 Handle(AIS_InteractiveObject) anIO = aContext->DetectedInteractive();
90 TopoDS_Shape aShape = aContext->DetectedShape();
91 if (theShapeTypeToSkip >= 0 && !aShape.IsNull() && aShape.ShapeType() == theShapeTypeToSkip)
94 boost::shared_ptr<ModelAPI_Feature> aFeature = GetFeature(anIO);
95 if (aPrsFeatures.find(aFeature) != aPrsFeatures.end())
97 aPresentations.push_back(XGUI_ViewerPrs(aFeature, aShape, NULL));
98 aPrsFeatures.insert(aFeature);
101 return aPresentations;
104 void XGUI_Displayer::Erase(boost::shared_ptr<ModelAPI_Feature> theFeature,
105 const bool isUpdateViewer)
107 if (myFeature2AISObjectMap.find(theFeature) == myFeature2AISObjectMap.end())
110 Handle(AIS_InteractiveContext) aContext = AISContext();
111 Handle(AIS_InteractiveObject) anAIS = myFeature2AISObjectMap[theFeature];
112 Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast(anAIS);
113 if (!anAISShape.IsNull())
115 aContext->Erase(anAISShape);
117 myFeature2AISObjectMap.erase(theFeature);
123 bool XGUI_Displayer::Redisplay(boost::shared_ptr<ModelAPI_Feature> theFeature,
124 Handle(AIS_InteractiveObject) theAIS,
125 const int theSelectionMode,
126 const bool isUpdateViewer)
128 bool isCreated = false;
129 Handle(AIS_InteractiveContext) aContext = AISContext();
130 // Open local context if there is no one
131 if (!aContext->HasOpenedContext()) {
132 aContext->ClearCurrents(false);
133 aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
134 // set mouse sensitivity
135 //aContext->SetSensitivityMode(StdSelect_SM_WINDOW);
136 //aContext->SetPixelTolerance(MOUSE_SENSITIVITY_IN_PIXEL);
138 // display or redisplay presentation
139 if (IsVisible(theFeature) && !myFeature2AISObjectMap[theFeature].IsNull()) {
140 aContext->RecomputeSelectionOnly(theAIS);
143 myFeature2AISObjectMap[theFeature] = theAIS;
144 if (theSelectionMode < 0)
146 aContext->Display(theAIS, false);
150 aContext->Display(theAIS, 0, theSelectionMode, false);
160 void XGUI_Displayer::ActivateInLocalContext(boost::shared_ptr<ModelAPI_Feature> theFeature,
161 const std::list<int>& theModes, const bool isUpdateViewer)
163 Handle(AIS_InteractiveContext) aContext = AISContext();
164 // Open local context if there is no one
165 if (!aContext->HasOpenedContext()) {
166 aContext->ClearCurrents(false);
167 aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
169 // display or redisplay presentation
170 Handle(AIS_Shape) anAIS;
171 if (IsVisible(theFeature))
172 anAIS = Handle(AIS_Shape)::DownCast(myFeature2AISObjectMap[theFeature]);
174 // Activate selection of objects from prs
175 if (!anAIS.IsNull()) {
176 aContext->Load(anAIS, -1, true/*allow decomposition*/);
177 aContext->Deactivate(anAIS);
179 std::list<int>::const_iterator anIt = theModes.begin(), aLast = theModes.end();
180 for (; anIt != aLast; anIt++)
182 aContext->Activate(anAIS, AIS_Shape::SelectionMode((TopAbs_ShapeEnum)*anIt));
190 void XGUI_Displayer::StopSelection(const std::list<XGUI_ViewerPrs>& theFeatures, const bool isStop,
191 const bool isUpdateViewer)
193 Handle(AIS_InteractiveContext) aContext = AISContext();
195 Handle(AIS_Shape) anAIS;
196 std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
197 boost::shared_ptr<ModelAPI_Feature> aFeature;
198 for (; anIt != aLast; anIt++) {
199 aFeature = (*anIt).feature();
200 if (IsVisible(aFeature))
201 anAIS = Handle(AIS_Shape)::DownCast(myFeature2AISObjectMap[aFeature]);
206 QColor aColor(Qt::white);
207 anAIS->SetColor(Quantity_Color(aColor.red()/255., aColor.green()/255., aColor.blue()/255., Quantity_TOC_RGB));
211 QColor aColor(Qt::red);
212 anAIS->SetColor(Quantity_Color(aColor.red()/255., aColor.green()/255., aColor.blue()/255., Quantity_TOC_RGB));
220 void XGUI_Displayer::SetSelected(const std::list<XGUI_ViewerPrs>& theFeatures, const bool isUpdateViewer)
222 Handle(AIS_InteractiveContext) aContext = AISContext();
224 std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
225 boost::shared_ptr<ModelAPI_Feature> aFeature;
227 Handle(AIS_Shape) anAIS;
228 // we need to unhighligth objects manually in the current local context
229 // in couple with the selection clear (TODO)
230 Handle(AIS_LocalContext) aLocalContext = aContext->LocalContext();
231 if (!aLocalContext.IsNull())
232 aLocalContext->UnhilightLastDetected(myWorkshop->viewer()->activeView());
233 aContext->ClearSelected(false);
235 for (; anIt != aLast; anIt++) {
236 aFeature = (*anIt).feature();
237 if (IsVisible(aFeature))
238 anAIS = Handle(AIS_Shape)::DownCast(myFeature2AISObjectMap[aFeature]);
241 aContext->AddOrRemoveSelected(anAIS, false);
248 void XGUI_Displayer::EraseAll(const bool isUpdateViewer)
250 Handle(AIS_InteractiveContext) ic = AISContext();
252 AIS_ListOfInteractive aList;
253 ic->DisplayedObjects(aList);
254 AIS_ListIteratorOfListOfInteractive anIter(aList);
255 for (; anIter.More(); anIter.Next()) {
256 if ((anIter.Value()->DynamicType() == STANDARD_TYPE(AIS_Trihedron)))
260 Handle(AIS_InteractiveObject) anIO = anIter.Value();
261 ic->Erase(anIO, false);
263 myFeature2AISObjectMap.clear();
268 void XGUI_Displayer::EraseDeletedFeatures(const bool isUpdateViewer)
270 Handle(AIS_InteractiveContext) aContext = AISContext();
272 FeatureToAISMap::const_iterator aFIt = myFeature2AISObjectMap.begin(),
273 aFLast = myFeature2AISObjectMap.end();
274 std::list<boost::shared_ptr<ModelAPI_Feature>> aRemoved;
275 for (; aFIt != aFLast; aFIt++)
277 boost::shared_ptr<ModelAPI_Feature> aFeature = (*aFIt).first;
278 if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
279 Handle(AIS_InteractiveObject) anAIS = (*aFIt).second;
280 if (!anAIS.IsNull()) {
281 aContext->Erase(anAIS, false);
282 aRemoved.push_back(aFeature);
286 std::list<boost::shared_ptr<ModelAPI_Feature>>::const_iterator anIt = aRemoved.begin(),
287 aLast = aRemoved.end();
288 for (; anIt != aLast; anIt++) {
289 myFeature2AISObjectMap.erase(myFeature2AISObjectMap.find(*anIt));
296 void XGUI_Displayer::CloseLocalContexts(const bool isUpdateViewer)
298 CloseAllContexts(true);
301 Handle(AIS_InteractiveObject) XGUI_Displayer::GetAISObject(
302 boost::shared_ptr<ModelAPI_Feature> theFeature) const
304 Handle(AIS_InteractiveObject) anIO;
305 if (myFeature2AISObjectMap.find(theFeature) != myFeature2AISObjectMap.end())
306 anIO = (myFeature2AISObjectMap.find(theFeature))->second;
310 boost::shared_ptr<ModelAPI_Feature> XGUI_Displayer::GetFeature(Handle(AIS_InteractiveObject) theIO) const
312 boost::shared_ptr<ModelAPI_Feature> aFeature;
313 FeatureToAISMap::const_iterator aFIt = myFeature2AISObjectMap.begin(),
314 aFLast = myFeature2AISObjectMap.end();
315 for (; aFIt != aFLast && !aFeature; aFIt++) {
316 Handle(AIS_InteractiveObject) anAIS = (*aFIt).second;
319 aFeature = (*aFIt).first;
324 void XGUI_Displayer::CloseAllContexts(const bool isUpdateViewer)
326 Handle(AIS_InteractiveContext) ic = AISContext();
328 ic->CloseAllContexts(false);
334 void XGUI_Displayer::UpdateViewer()
336 Handle(AIS_InteractiveContext) ic = AISContext();
338 ic->UpdateCurrentViewer();
341 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
343 return myWorkshop->viewer()->AISContext();