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"
10 #include <ModelAPI_Document.h>
11 #include <ModelAPI_Data.h>
12 #include <ModelAPI_Object.h>
13 #include <ModelAPI_Tools.h>
15 #include <GeomAPI_Shape.h>
16 #include <GeomAPI_IPresentable.h>
18 #include <AIS_InteractiveContext.hxx>
19 #include <AIS_LocalContext.hxx>
20 #include <AIS_ListOfInteractive.hxx>
21 #include <AIS_ListIteratorOfListOfInteractive.hxx>
22 #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(ObjectPtr theObject) const
40 return myResult2AISObjectMap.find(theObject) != myResult2AISObjectMap.end();
43 void XGUI_Displayer::display(ObjectPtr theObject, bool isUpdateViewer)
45 if (isVisible(theObject)) {
46 redisplay(theObject, isUpdateViewer);
48 boost::shared_ptr<GeomAPI_AISObject> anAIS;
50 GeomPresentablePtr aPrs = boost::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
52 anAIS = aPrs->getAISObject(boost::shared_ptr<GeomAPI_AISObject>());
54 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
56 boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
58 anAIS = boost::shared_ptr<GeomAPI_AISObject>(new GeomAPI_AISObject());
59 anAIS->createShape(aShapePtr);
64 display(theObject, anAIS, isUpdateViewer);
68 void XGUI_Displayer::display(ObjectPtr theObject, boost::shared_ptr<GeomAPI_AISObject> theAIS,
71 Handle(AIS_InteractiveContext) aContext = AISContext();
72 if (aContext.IsNull())
75 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
76 if (!anAISIO.IsNull()) {
77 myResult2AISObjectMap[theObject] = theAIS;
78 aContext->Display(anAISIO, isUpdateViewer);
82 void XGUI_Displayer::erase(ObjectPtr theObject, const bool isUpdateViewer)
84 if (!isVisible(theObject))
87 Handle(AIS_InteractiveContext) aContext = AISContext();
88 if (aContext.IsNull())
90 boost::shared_ptr<GeomAPI_AISObject> anObject = myResult2AISObjectMap[theObject];
92 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
93 if (!anAIS.IsNull()) {
94 aContext->Remove(anAIS, isUpdateViewer);
97 myResult2AISObjectMap.erase(theObject);
100 /*bool XGUI_Displayer::redisplay(ObjectPtr theObject,
101 boost::shared_ptr<GeomAPI_AISObject> theAIS,
102 const bool isUpdateViewer)
104 bool isCreated = false;
105 Handle(AIS_InteractiveObject) anAIS =
106 theAIS ? theAIS->impl<Handle(AIS_InteractiveObject)>() : Handle(AIS_InteractiveObject)();
107 Handle(AIS_InteractiveContext) aContext = AISContext();
108 // Open local context if there is no one
109 if (!aContext->HasOpenedContext()) {
110 aContext->ClearCurrents(false);
111 aContext->OpenLocalContext(false /use displayed objects/, true /allow shape decomposition/);
112 // set mouse sensitivity
113 //aContext->SetSensitivityMode(StdSelect_SM_WINDOW);
114 //aContext->SetPixelTolerance(MOUSE_SENSITIVITY_IN_PIXEL);
116 // display or redisplay presentation
117 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theObject];
118 if (isVisible(theObject) && anObj && !anObj->empty()) {
119 aContext->Redisplay(anAIS, isUpdateViewer);
120 //aContext->RecomputeSelectionOnly(anAIS);
123 myResult2AISObjectMap[theObject] = theAIS;
124 aContext->Display(anAIS, isUpdateViewer);
130 void XGUI_Displayer::redisplay(ObjectPtr theObject, bool isUpdateViewer)
132 if (!isVisible(theObject))
135 Handle(AIS_InteractiveObject) aAISIO;
136 boost::shared_ptr<GeomAPI_AISObject> aAISObj = getAISObject(theObject);
137 GeomPresentablePtr aPrs = boost::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
139 boost::shared_ptr<GeomAPI_AISObject> aAIS_Obj = aPrs->getAISObject(aAISObj);
140 if (aAISObj && !aAIS_Obj) {
141 erase(theObject, isUpdateViewer);
144 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
146 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
148 boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
150 Handle(AIS_Shape) aAISShape = Handle(AIS_Shape)::DownCast(
151 aAISObj->impl<Handle(AIS_InteractiveObject)>());
152 if (!aAISShape.IsNull()) {
153 aAISShape->Set(aShapePtr->impl<TopoDS_Shape>());
159 if (!aAISIO.IsNull()) {
160 Handle(AIS_InteractiveContext) aContext = AISContext();
161 if (aContext.IsNull())
163 aContext->Redisplay(aAISIO, isUpdateViewer);
164 //if (aContext->HasOpenedContext()) {
165 // aContext->Load(aAISIO, -1, true/*allow decomposition*/);
170 void XGUI_Displayer::activateInLocalContext(ObjectPtr theResult, const std::list<int>& theModes,
171 const bool isUpdateViewer)
173 Handle(AIS_InteractiveContext) aContext = AISContext();
174 if (aContext.IsNull())
176 // Open local context if there is no one
177 if (!aContext->HasOpenedContext()) {
178 aContext->ClearCurrents(false);
179 //aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
180 aContext->OpenLocalContext();
181 aContext->NotUseDisplayedObjects();
183 // display or redisplay presentation
184 Handle(AIS_InteractiveObject) anAIS;
185 if (isVisible(theResult)) {
186 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theResult];
188 anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
191 // Activate selection of objects from prs
192 if (!anAIS.IsNull()) {
193 aContext->ClearSelected(false); // ToCheck
194 //aContext->upClearSelected(false); // ToCheck
195 aContext->Load(anAIS, -1, true/*allow decomposition*/);
196 aContext->Deactivate(anAIS);
198 std::list<int>::const_iterator anIt = theModes.begin(), aLast = theModes.end();
199 for (; anIt != aLast; anIt++) {
200 aContext->Activate(anAIS, (*anIt));
208 void XGUI_Displayer::deactivate(ObjectPtr theObject)
210 if (isVisible(theObject)) {
211 Handle(AIS_InteractiveContext) aContext = AISContext();
212 if (aContext.IsNull())
215 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theObject];
216 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
217 aContext->Deactivate(anAIS);
221 void XGUI_Displayer::activate(ObjectPtr theObject)
223 if (isVisible(theObject)) {
224 Handle(AIS_InteractiveContext) aContext = AISContext();
225 if (aContext.IsNull())
228 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theObject];
229 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
230 aContext->Activate(anAIS);
234 void XGUI_Displayer::stopSelection(const QList<ObjectPtr>& theResults, const bool isStop,
235 const bool isUpdateViewer)
237 Handle(AIS_InteractiveContext) aContext = AISContext();
238 if (aContext.IsNull())
241 Handle(AIS_Shape) anAIS;
242 QList<ObjectPtr>::const_iterator anIt = theResults.begin(), aLast = theResults.end();
244 for (; anIt != aLast; anIt++) {
246 if (isVisible(aFeature))
247 anAIS = Handle(AIS_Shape)::DownCast(
248 myResult2AISObjectMap[aFeature]->impl<Handle(AIS_InteractiveObject)>());
253 QColor aColor(Qt::white);
255 Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
259 QColor aColor(Qt::red);
261 Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
270 void XGUI_Displayer::setSelected(const QList<ObjectPtr>& theResults, const bool isUpdateViewer)
272 Handle(AIS_InteractiveContext) aContext = AISContext();
273 // we need to unhighligth objects manually in the current local context
274 // in couple with the selection clear (TODO)
275 Handle(AIS_LocalContext) aLocalContext = aContext->LocalContext();
276 if (!aLocalContext.IsNull())
277 aLocalContext->UnhilightLastDetected(myWorkshop->viewer()->activeView());
279 aContext->ClearSelected();
280 foreach(ObjectPtr aResult, theResults)
282 if (myResult2AISObjectMap.find(aResult) == myResult2AISObjectMap.end())
285 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[aResult];
287 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
289 aContext->AddOrRemoveSelected(anAIS, false);
296 void XGUI_Displayer::eraseAll(const bool isUpdateViewer)
298 Handle(AIS_InteractiveContext) ic = AISContext();
302 ResultToAISMap::iterator aIt;
303 for (aIt = myResult2AISObjectMap.begin(); aIt != myResult2AISObjectMap.end(); aIt++) {
305 boost::shared_ptr<GeomAPI_AISObject> aAISObj = (*aIt).second;
306 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
308 ic->Remove(anIO, false);
310 myResult2AISObjectMap.clear();
315 void XGUI_Displayer::eraseDeletedResults(const bool isUpdateViewer)
317 Handle(AIS_InteractiveContext) aContext = AISContext();
318 if (aContext.IsNull())
321 ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(), aFLast =
322 myResult2AISObjectMap.end();
323 std::list<ObjectPtr> aRemoved;
324 for (; aFIt != aFLast; aFIt++) {
325 ObjectPtr aFeature = (*aFIt).first;
326 if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
327 boost::shared_ptr<GeomAPI_AISObject> anObj = (*aFIt).second;
330 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
331 if (!anAIS.IsNull()) {
332 aContext->Remove(anAIS, false);
333 aRemoved.push_back(aFeature);
337 std::list<ObjectPtr>::const_iterator anIt = aRemoved.begin(), aLast = aRemoved.end();
338 for (; anIt != aLast; anIt++) {
339 myResult2AISObjectMap.erase(myResult2AISObjectMap.find(*anIt));
346 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
348 AISContext()->ClearSelected(false);
349 closeAllContexts(true);
352 boost::shared_ptr<GeomAPI_AISObject> XGUI_Displayer::getAISObject(ObjectPtr theObject) const
354 boost::shared_ptr<GeomAPI_AISObject> anIO;
355 if (myResult2AISObjectMap.find(theObject) != myResult2AISObjectMap.end())
356 anIO = (myResult2AISObjectMap.find(theObject))->second;
360 ObjectPtr XGUI_Displayer::getObject(Handle(AIS_InteractiveObject) theIO) const
363 ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(), aFLast =
364 myResult2AISObjectMap.end();
365 for (; aFIt != aFLast && !aFeature; aFIt++) {
366 boost::shared_ptr<GeomAPI_AISObject> anObj = (*aFIt).second;
369 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
372 aFeature = (*aFIt).first;
377 void XGUI_Displayer::closeAllContexts(const bool isUpdateViewer)
379 Handle(AIS_InteractiveContext) ic = AISContext();
381 ic->CloseAllContexts(false);
387 void XGUI_Displayer::updateViewer()
389 Handle(AIS_InteractiveContext) ic = AISContext();
391 ic->UpdateCurrentViewer();
394 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
396 return myWorkshop->viewer()->AISContext();
399 void XGUI_Displayer::display(boost::shared_ptr<GeomAPI_AISObject> theAIS, bool isUpdate)
401 Handle(AIS_InteractiveContext) aContext = AISContext();
402 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
403 if (!anAISIO.IsNull())
404 aContext->Display(anAISIO, isUpdate);
407 void XGUI_Displayer::erase(boost::shared_ptr<GeomAPI_AISObject> theAIS, const bool isUpdate)
409 Handle(AIS_InteractiveContext) aContext = AISContext();
410 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
411 if (!anAISIO.IsNull()) {
412 aContext->Remove(anAISIO, isUpdate);
416 void XGUI_Displayer::activateObjectsOutOfContext(const std::list<int>& theModes,
417 Handle(SelectMgr_Filter) theFilter)
419 Handle(AIS_InteractiveContext) aContext = AISContext();
420 // Open local context if there is no one
421 if (!aContext->HasOpenedContext())
424 aContext->UseDisplayedObjects();
425 std::list<int>::const_iterator anIt = theModes.begin(), aLast = theModes.end();
426 for (; anIt != aLast; anIt++) {
427 aContext->ActivateStandardMode((TopAbs_ShapeEnum)(*anIt));
430 if (!theFilter.IsNull())
431 aContext->AddFilter(theFilter);
435 void XGUI_Displayer::deactivateObjectsOutOfContext()
437 Handle(AIS_InteractiveContext) aContext = AISContext();
438 // Open local context if there is no one
439 if (!aContext->HasOpenedContext())
442 aContext->RemoveFilters();
443 aContext->NotUseDisplayedObjects();
447 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool toUpdate)
449 if (theMode == NoMode)
452 Handle(AIS_InteractiveContext) aContext = AISContext();
453 if (aContext.IsNull())
456 boost::shared_ptr<GeomAPI_AISObject> aAISObj = getAISObject(theObject);
460 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
461 aContext->SetDisplayMode(aAISIO, theMode, toUpdate);
465 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
467 Handle(AIS_InteractiveContext) aContext = AISContext();
468 if (aContext.IsNull())
471 boost::shared_ptr<GeomAPI_AISObject> aAISObj = getAISObject(theObject);
475 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
476 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();