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>
13 #include <ModelAPI_Object.h>
15 #include <GeomAPI_Shape.h>
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>
23 #include <AIS_Shape.hxx>
27 const int MOUSE_SENSITIVITY_IN_PIXEL = 10; ///< defines the local context mouse selection sensitivity
29 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
31 myWorkshop = theWorkshop;
34 XGUI_Displayer::~XGUI_Displayer()
38 bool XGUI_Displayer::isVisible(FeaturePtr theFeature)
40 FeaturePtr aFeature = XGUI_Tools::realFeature(theFeature);
41 return myFeature2AISObjectMap.find(aFeature) != myFeature2AISObjectMap.end();
44 void XGUI_Displayer::display(FeaturePtr theFeature, bool isUpdateViewer)
46 FeaturePtr aFeature = XGUI_Tools::realFeature(theFeature);
47 boost::shared_ptr<GeomAPI_Shape> aShapePtr = aFeature->data()->shape();
50 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
51 display(aFeature, aShape, isUpdateViewer);
55 void XGUI_Displayer::display(FeaturePtr theFeature,
56 const TopoDS_Shape& theShape, bool isUpdateViewer)
58 Handle(AIS_InteractiveContext) aContext = AISContext();
60 Handle(AIS_Shape) anAIS = new AIS_Shape(theShape);
61 myFeature2AISObjectMap[theFeature] = anAIS;
63 aContext->Display(anAIS, isUpdateViewer);
67 std::list<XGUI_ViewerPrs> XGUI_Displayer::getSelected(const int theShapeTypeToSkip)
69 std::set<FeaturePtr > aPrsFeatures;
70 std::list<XGUI_ViewerPrs> aPresentations;
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();
77 if (theShapeTypeToSkip >= 0 && !aShape.IsNull() && aShape.ShapeType() == theShapeTypeToSkip)
80 FeaturePtr aFeature = getFeature(anIO);
81 if (aPrsFeatures.find(aFeature) != aPrsFeatures.end())
83 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
84 aPresentations.push_back(XGUI_ViewerPrs(aFeature, aShape, anOwner));
85 aPrsFeatures.insert(aFeature);
87 return aPresentations;
90 QFeatureList XGUI_Displayer::selectedFeatures() const
92 QFeatureList aSelectedList;
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);
99 aSelectedList.append(aFeature);
101 return aSelectedList;
105 std::list<XGUI_ViewerPrs> XGUI_Displayer::getHighlighted(const int theShapeTypeToSkip)
107 std::set<FeaturePtr > aPrsFeatures;
108 std::list<XGUI_ViewerPrs> aPresentations;
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)
117 FeaturePtr aFeature = getFeature(anIO);
118 if (aPrsFeatures.find(aFeature) != aPrsFeatures.end())
120 aPresentations.push_back(XGUI_ViewerPrs(aFeature, aShape, NULL));
121 aPrsFeatures.insert(aFeature);
124 return aPresentations;
127 void XGUI_Displayer::erase(FeaturePtr theFeature,
128 const bool isUpdateViewer)
130 FeaturePtr aFeature = XGUI_Tools::realFeature(theFeature);
132 if (myFeature2AISObjectMap.find(aFeature) == myFeature2AISObjectMap.end())
135 Handle(AIS_InteractiveContext) aContext = AISContext();
136 Handle(AIS_InteractiveObject) anAIS = myFeature2AISObjectMap[aFeature];
139 aContext->Erase(anAIS, isUpdateViewer);
141 myFeature2AISObjectMap.erase(aFeature);
145 bool XGUI_Displayer::redisplay(FeaturePtr theFeature,
146 Handle(AIS_InteractiveObject) theAIS,
147 const bool isUpdateViewer)
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);
159 // display or redisplay presentation
160 if (isVisible(theFeature) && !myFeature2AISObjectMap[theFeature].IsNull()) {
161 aContext->RecomputeSelectionOnly(theAIS);
164 myFeature2AISObjectMap[theFeature] = theAIS;
165 aContext->Display(theAIS, false);
174 void XGUI_Displayer::redisplay(FeaturePtr theFeature, bool isUpdateViewer)
176 FeaturePtr aFeature = XGUI_Tools::realFeature(theFeature);
177 if (!isVisible(aFeature))
180 boost::shared_ptr<GeomAPI_Shape> aShapePtr = aFeature->data()->shape();
182 Handle(AIS_InteractiveObject) aAISObj = getAISObject(aFeature);
183 Handle(AIS_Shape) aAISShape = Handle(AIS_Shape)::DownCast(aAISObj);
184 aAISShape->Set(aShapePtr->impl<TopoDS_Shape>());
186 AISContext()->Redisplay(aAISShape);
190 void XGUI_Displayer::activateInLocalContext(FeaturePtr theFeature,
191 const std::list<int>& theModes, const bool isUpdateViewer)
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*/);
199 // display or redisplay presentation
200 Handle(AIS_InteractiveObject) anAIS;
201 if (isVisible(theFeature))
202 anAIS = Handle(AIS_InteractiveObject)::DownCast(myFeature2AISObjectMap[theFeature]);
204 // Activate selection of objects from prs
205 if (!anAIS.IsNull()) {
206 aContext->Load(anAIS, -1, true/*allow decomposition*/);
207 aContext->Deactivate(anAIS);
209 std::list<int>::const_iterator anIt = theModes.begin(), aLast = theModes.end();
210 for (; anIt != aLast; anIt++)
212 aContext->Activate(anAIS, (*anIt));
220 void XGUI_Displayer::stopSelection(const QFeatureList& theFeatures, const bool isStop,
221 const bool isUpdateViewer)
223 Handle(AIS_InteractiveContext) aContext = AISContext();
225 Handle(AIS_Shape) anAIS;
226 QFeatureList::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
228 for (; anIt != aLast; anIt++) {
230 if (isVisible(aFeature))
231 anAIS = Handle(AIS_Shape)::DownCast(myFeature2AISObjectMap[aFeature]);
236 QColor aColor(Qt::white);
237 anAIS->SetColor(Quantity_Color(aColor.red()/255., aColor.green()/255., aColor.blue()/255., Quantity_TOC_RGB));
241 QColor aColor(Qt::red);
242 anAIS->SetColor(Quantity_Color(aColor.red()/255., aColor.green()/255., aColor.blue()/255., Quantity_TOC_RGB));
250 void XGUI_Displayer::setSelected(const QFeatureList& theFeatures, const bool isUpdateViewer)
252 Handle(AIS_InteractiveContext) aContext = AISContext();
253 // we need to unhighligth objects manually in the current local context
254 // in couple with the selection clear (TODO)
255 Handle(AIS_LocalContext) aLocalContext = aContext->LocalContext();
256 if (!aLocalContext.IsNull())
257 aLocalContext->UnhilightLastDetected(myWorkshop->viewer()->activeView());
259 aContext->ClearSelected();
260 foreach(FeaturePtr aFeature, theFeatures) {
261 FeaturePtr aRFeature = XGUI_Tools::realFeature(aFeature);
262 if (myFeature2AISObjectMap.find(aRFeature) == myFeature2AISObjectMap.end())
265 Handle(AIS_InteractiveObject) anAIS = myFeature2AISObjectMap[aRFeature];
267 aContext->AddOrRemoveSelected(anAIS, false);
274 /*void XGUI_Displayer::EraseAll(const bool isUpdateViewer)
276 Handle(AIS_InteractiveContext) ic = AISContext();
278 AIS_ListOfInteractive aList;
279 ic->DisplayedObjects(aList);
280 AIS_ListIteratorOfListOfInteractive anIter(aList);
281 for (; anIter.More(); anIter.Next()) {
282 if ((anIter.Value()->DynamicType() == STANDARD_TYPE(AIS_Trihedron)))
286 Handle(AIS_InteractiveObject) anIO = anIter.Value();
287 ic->Erase(anIO, false);
289 myFeature2AISObjectMap.clear();
294 void XGUI_Displayer::eraseDeletedFeatures(const bool isUpdateViewer)
296 Handle(AIS_InteractiveContext) aContext = AISContext();
298 FeatureToAISMap::const_iterator aFIt = myFeature2AISObjectMap.begin(),
299 aFLast = myFeature2AISObjectMap.end();
300 std::list<FeaturePtr> aRemoved;
301 for (; aFIt != aFLast; aFIt++)
303 FeaturePtr aFeature = (*aFIt).first;
304 if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
305 Handle(AIS_InteractiveObject) anAIS = (*aFIt).second;
306 if (!anAIS.IsNull()) {
307 aContext->Erase(anAIS, false);
308 aRemoved.push_back(aFeature);
312 std::list<FeaturePtr>::const_iterator anIt = aRemoved.begin(),
313 aLast = aRemoved.end();
314 for (; anIt != aLast; anIt++) {
315 myFeature2AISObjectMap.erase(myFeature2AISObjectMap.find(*anIt));
322 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
324 closeAllContexts(true);
327 Handle(AIS_InteractiveObject) XGUI_Displayer::getAISObject(
328 FeaturePtr theFeature) const
330 Handle(AIS_InteractiveObject) anIO;
331 if (myFeature2AISObjectMap.find(theFeature) != myFeature2AISObjectMap.end())
332 anIO = (myFeature2AISObjectMap.find(theFeature))->second;
336 FeaturePtr XGUI_Displayer::getFeature(Handle(AIS_InteractiveObject) theIO) const
339 FeatureToAISMap::const_iterator aFIt = myFeature2AISObjectMap.begin(),
340 aFLast = myFeature2AISObjectMap.end();
341 for (; aFIt != aFLast && !aFeature; aFIt++) {
342 Handle(AIS_InteractiveObject) anAIS = (*aFIt).second;
345 aFeature = (*aFIt).first;
350 void XGUI_Displayer::closeAllContexts(const bool isUpdateViewer)
352 Handle(AIS_InteractiveContext) ic = AISContext();
354 ic->CloseAllContexts(false);
360 void XGUI_Displayer::updateViewer()
362 Handle(AIS_InteractiveContext) ic = AISContext();
364 ic->UpdateCurrentViewer();
367 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
369 return myWorkshop->viewer()->AISContext();