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 boost::shared_ptr<GeomAPI_AISObject> anAIS(new GeomAPI_AISObject());
51 anAIS->createShape(aShapePtr);
52 display(aFeature, anAIS, isUpdateViewer);
56 void XGUI_Displayer::display(FeaturePtr theFeature,
57 boost::shared_ptr<GeomAPI_AISObject> theAIS, bool isUpdateViewer)
59 Handle(AIS_InteractiveContext) aContext = AISContext();
61 myFeature2AISObjectMap[theFeature] = theAIS;
63 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
64 if (!anAISIO.IsNull())
65 aContext->Display(anAISIO, isUpdateViewer);
69 std::list<XGUI_ViewerPrs> XGUI_Displayer::getSelected(const int theShapeTypeToSkip)
71 std::set<FeaturePtr > aPrsFeatures;
72 std::list<XGUI_ViewerPrs> aPresentations;
74 Handle(AIS_InteractiveContext) aContext = AISContext();
75 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
76 Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
77 TopoDS_Shape aShape = aContext->SelectedShape();
79 if (theShapeTypeToSkip >= 0 && !aShape.IsNull() && aShape.ShapeType() == theShapeTypeToSkip)
82 FeaturePtr aFeature = getFeature(anIO);
83 if (aPrsFeatures.find(aFeature) != aPrsFeatures.end())
85 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
86 aPresentations.push_back(XGUI_ViewerPrs(aFeature, aShape, anOwner));
87 aPrsFeatures.insert(aFeature);
89 return aPresentations;
92 QFeatureList XGUI_Displayer::selectedFeatures() const
94 QFeatureList aSelectedList;
96 Handle(AIS_InteractiveContext) aContext = AISContext();
97 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
98 Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
99 FeaturePtr aFeature = getFeature(anIO);
101 aSelectedList.append(aFeature);
103 return aSelectedList;
107 std::list<XGUI_ViewerPrs> XGUI_Displayer::getHighlighted(const int theShapeTypeToSkip)
109 std::set<FeaturePtr > aPrsFeatures;
110 std::list<XGUI_ViewerPrs> aPresentations;
112 Handle(AIS_InteractiveContext) aContext = AISContext();
113 for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
114 Handle(AIS_InteractiveObject) anIO = aContext->DetectedInteractive();
115 TopoDS_Shape aShape = aContext->DetectedShape();
116 if (theShapeTypeToSkip >= 0 && !aShape.IsNull() && aShape.ShapeType() == theShapeTypeToSkip)
119 FeaturePtr aFeature = getFeature(anIO);
120 if (aPrsFeatures.find(aFeature) != aPrsFeatures.end())
122 aPresentations.push_back(XGUI_ViewerPrs(aFeature, aShape, NULL));
123 aPrsFeatures.insert(aFeature);
126 return aPresentations;
129 void XGUI_Displayer::erase(FeaturePtr theFeature,
130 const bool isUpdateViewer)
132 FeaturePtr aFeature = XGUI_Tools::realFeature(theFeature);
134 if (myFeature2AISObjectMap.find(aFeature) == myFeature2AISObjectMap.end())
137 Handle(AIS_InteractiveContext) aContext = AISContext();
138 boost::shared_ptr<GeomAPI_AISObject> anObject = myFeature2AISObjectMap[aFeature];
141 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
143 aContext->Erase(anAIS, isUpdateViewer);
145 myFeature2AISObjectMap.erase(aFeature);
149 bool XGUI_Displayer::redisplay(FeaturePtr theFeature,
150 boost::shared_ptr<GeomAPI_AISObject> theAIS,
151 const bool isUpdateViewer)
153 bool isCreated = false;
154 Handle(AIS_InteractiveObject) anAIS =
155 theAIS ? theAIS->impl<Handle(AIS_InteractiveObject)>() : Handle(AIS_InteractiveObject)();
156 Handle(AIS_InteractiveContext) aContext = AISContext();
157 // Open local context if there is no one
158 if (!aContext->HasOpenedContext()) {
159 aContext->ClearCurrents(false);
160 aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
161 // set mouse sensitivity
162 //aContext->SetSensitivityMode(StdSelect_SM_WINDOW);
163 //aContext->SetPixelTolerance(MOUSE_SENSITIVITY_IN_PIXEL);
165 // display or redisplay presentation
166 boost::shared_ptr<GeomAPI_AISObject> anObj = myFeature2AISObjectMap[theFeature];
167 if (isVisible(theFeature) && anObj && !anObj->empty()) {
168 aContext->RecomputeSelectionOnly(anAIS);
171 myFeature2AISObjectMap[theFeature] = theAIS;
172 aContext->Display(anAIS, false);
181 void XGUI_Displayer::redisplay(FeaturePtr theFeature, bool isUpdateViewer)
183 FeaturePtr aFeature = XGUI_Tools::realFeature(theFeature);
184 if (!isVisible(aFeature))
187 boost::shared_ptr<GeomAPI_Shape> aShapePtr = aFeature->data()->shape();
189 boost::shared_ptr<GeomAPI_AISObject> aAISObj = getAISObject(aFeature);
190 Handle(AIS_Shape) aAISShape = Handle(AIS_Shape)::DownCast(aAISObj->impl<Handle(AIS_InteractiveObject)>());
191 if (aAISShape.IsNull())
194 aAISShape->Set(aShapePtr->impl<TopoDS_Shape>());
195 AISContext()->Redisplay(aAISShape);
199 void XGUI_Displayer::activateInLocalContext(FeaturePtr theFeature,
200 const std::list<int>& theModes, const bool isUpdateViewer)
202 Handle(AIS_InteractiveContext) aContext = AISContext();
203 // Open local context if there is no one
204 if (!aContext->HasOpenedContext()) {
205 aContext->ClearCurrents(false);
206 aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
208 // display or redisplay presentation
209 Handle(AIS_InteractiveObject) anAIS;
210 if (isVisible(theFeature))
212 boost::shared_ptr<GeomAPI_AISObject> anObj = myFeature2AISObjectMap[theFeature];
214 anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
217 // Activate selection of objects from prs
218 if (!anAIS.IsNull()) {
219 aContext->Load(anAIS, -1, true/*allow decomposition*/);
220 aContext->Deactivate(anAIS);
222 std::list<int>::const_iterator anIt = theModes.begin(), aLast = theModes.end();
223 for (; anIt != aLast; anIt++)
225 aContext->Activate(anAIS, (*anIt));
233 void XGUI_Displayer::stopSelection(const QFeatureList& theFeatures, const bool isStop,
234 const bool isUpdateViewer)
236 Handle(AIS_InteractiveContext) aContext = AISContext();
238 Handle(AIS_Shape) anAIS;
239 QFeatureList::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
241 for (; anIt != aLast; anIt++) {
243 if (isVisible(aFeature))
244 anAIS = Handle(AIS_Shape)::DownCast(myFeature2AISObjectMap[aFeature]->impl<Handle(AIS_InteractiveObject)>());
249 QColor aColor(Qt::white);
250 anAIS->SetColor(Quantity_Color(aColor.red()/255., aColor.green()/255., aColor.blue()/255., Quantity_TOC_RGB));
254 QColor aColor(Qt::red);
255 anAIS->SetColor(Quantity_Color(aColor.red()/255., aColor.green()/255., aColor.blue()/255., Quantity_TOC_RGB));
263 void XGUI_Displayer::setSelected(const QFeatureList& theFeatures, const bool isUpdateViewer)
265 Handle(AIS_InteractiveContext) aContext = AISContext();
266 // we need to unhighligth objects manually in the current local context
267 // in couple with the selection clear (TODO)
268 Handle(AIS_LocalContext) aLocalContext = aContext->LocalContext();
269 if (!aLocalContext.IsNull())
270 aLocalContext->UnhilightLastDetected(myWorkshop->viewer()->activeView());
272 aContext->ClearSelected();
273 foreach(FeaturePtr aFeature, theFeatures) {
274 FeaturePtr aRFeature = XGUI_Tools::realFeature(aFeature);
275 if (myFeature2AISObjectMap.find(aRFeature) == myFeature2AISObjectMap.end())
278 boost::shared_ptr<GeomAPI_AISObject> anObj = myFeature2AISObjectMap[aRFeature];
281 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
283 aContext->AddOrRemoveSelected(anAIS, false);
291 /*void XGUI_Displayer::EraseAll(const bool isUpdateViewer)
293 Handle(AIS_InteractiveContext) ic = AISContext();
295 AIS_ListOfInteractive aList;
296 ic->DisplayedObjects(aList);
297 AIS_ListIteratorOfListOfInteractive anIter(aList);
298 for (; anIter.More(); anIter.Next()) {
299 if ((anIter.Value()->DynamicType() == STANDARD_TYPE(AIS_Trihedron)))
303 Handle(AIS_InteractiveObject) anIO = anIter.Value();
304 ic->Erase(anIO, false);
306 myFeature2AISObjectMap.clear();
311 void XGUI_Displayer::eraseDeletedFeatures(const bool isUpdateViewer)
313 Handle(AIS_InteractiveContext) aContext = AISContext();
315 FeatureToAISMap::const_iterator aFIt = myFeature2AISObjectMap.begin(),
316 aFLast = myFeature2AISObjectMap.end();
317 std::list<FeaturePtr> aRemoved;
318 for (; aFIt != aFLast; aFIt++)
320 FeaturePtr aFeature = (*aFIt).first;
321 if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
322 boost::shared_ptr<GeomAPI_AISObject> anObj = (*aFIt).second;
323 if (!anObj) continue;
324 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
325 if (!anAIS.IsNull()) {
326 aContext->Erase(anAIS, false);
327 aRemoved.push_back(aFeature);
331 std::list<FeaturePtr>::const_iterator anIt = aRemoved.begin(),
332 aLast = aRemoved.end();
333 for (; anIt != aLast; anIt++) {
334 myFeature2AISObjectMap.erase(myFeature2AISObjectMap.find(*anIt));
341 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
343 closeAllContexts(true);
346 boost::shared_ptr<GeomAPI_AISObject> XGUI_Displayer::getAISObject(
347 FeaturePtr theFeature) const
349 boost::shared_ptr<GeomAPI_AISObject> anIO;
350 if (myFeature2AISObjectMap.find(theFeature) != myFeature2AISObjectMap.end())
351 anIO = (myFeature2AISObjectMap.find(theFeature))->second;
355 FeaturePtr XGUI_Displayer::getFeature(Handle(AIS_InteractiveObject) theIO) const
358 FeatureToAISMap::const_iterator aFIt = myFeature2AISObjectMap.begin(),
359 aFLast = myFeature2AISObjectMap.end();
360 for (; aFIt != aFLast && !aFeature; aFIt++) {
361 boost::shared_ptr<GeomAPI_AISObject> anObj = (*aFIt).second;
362 if (!anObj) continue;
363 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
366 aFeature = (*aFIt).first;
371 void XGUI_Displayer::closeAllContexts(const bool isUpdateViewer)
373 Handle(AIS_InteractiveContext) ic = AISContext();
375 ic->CloseAllContexts(false);
381 void XGUI_Displayer::updateViewer()
383 Handle(AIS_InteractiveContext) ic = AISContext();
385 ic->UpdateCurrentViewer();
388 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
390 return myWorkshop->viewer()->AISContext();