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);
51 bool isShading = false;
53 anAIS = aPrs->getAISObject(boost::shared_ptr<GeomAPI_AISObject>());
55 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
57 boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
59 anAIS = boost::shared_ptr<GeomAPI_AISObject>(new GeomAPI_AISObject());
60 anAIS->createShape(aShapePtr);
66 display(theObject, anAIS, isShading, isUpdateViewer);
70 void XGUI_Displayer::display(ObjectPtr theObject, boost::shared_ptr<GeomAPI_AISObject> theAIS,
71 bool isShading, bool isUpdateViewer)
73 Handle(AIS_InteractiveContext) aContext = AISContext();
74 if (aContext.IsNull())
77 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
78 if (!anAISIO.IsNull()) {
79 myResult2AISObjectMap[theObject] = theAIS;
80 aContext->Display(anAISIO, false);
81 aContext->SetDisplayMode(anAISIO, isShading? Shading : Wireframe, isUpdateViewer);
85 void XGUI_Displayer::erase(ObjectPtr theObject, const bool isUpdateViewer)
87 if (!isVisible(theObject))
90 Handle(AIS_InteractiveContext) aContext = AISContext();
91 if (aContext.IsNull())
93 boost::shared_ptr<GeomAPI_AISObject> anObject = myResult2AISObjectMap[theObject];
95 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
96 if (!anAIS.IsNull()) {
97 aContext->Remove(anAIS, isUpdateViewer);
100 myResult2AISObjectMap.erase(theObject);
103 void XGUI_Displayer::redisplay(ObjectPtr theObject, bool isUpdateViewer)
105 if (!isVisible(theObject))
108 Handle(AIS_InteractiveObject) aAISIO;
109 boost::shared_ptr<GeomAPI_AISObject> aAISObj = getAISObject(theObject);
110 GeomPresentablePtr aPrs = boost::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
112 boost::shared_ptr<GeomAPI_AISObject> aAIS_Obj = aPrs->getAISObject(aAISObj);
113 if (aAISObj && !aAIS_Obj) {
114 erase(theObject, isUpdateViewer);
117 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
119 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
121 boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
123 Handle(AIS_Shape) aAISShape = Handle(AIS_Shape)::DownCast(
124 aAISObj->impl<Handle(AIS_InteractiveObject)>());
125 if (!aAISShape.IsNull()) {
126 aAISShape->Set(aShapePtr->impl<TopoDS_Shape>());
132 if (!aAISIO.IsNull()) {
133 Handle(AIS_InteractiveContext) aContext = AISContext();
134 if (aContext.IsNull())
136 aContext->Redisplay(aAISIO, isUpdateViewer);
137 //if (aContext->HasOpenedContext()) {
138 // aContext->Load(aAISIO, -1, true/*allow decomposition*/);
143 void XGUI_Displayer::activateInLocalContext(ObjectPtr theResult, const std::list<int>& theModes,
144 const bool isUpdateViewer)
146 Handle(AIS_InteractiveContext) aContext = AISContext();
147 if (aContext.IsNull())
149 // Open local context if there is no one
150 if (!aContext->HasOpenedContext()) {
151 aContext->ClearCurrents(false);
152 //aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
153 aContext->OpenLocalContext();
154 aContext->NotUseDisplayedObjects();
156 // display or redisplay presentation
157 Handle(AIS_InteractiveObject) anAIS;
158 if (isVisible(theResult)) {
159 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theResult];
161 anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
164 // Activate selection of objects from prs
165 if (!anAIS.IsNull()) {
166 aContext->ClearSelected(false); // ToCheck
167 //aContext->upClearSelected(false); // ToCheck
168 aContext->Load(anAIS, -1, true/*allow decomposition*/);
169 aContext->Deactivate(anAIS);
171 std::list<int>::const_iterator anIt = theModes.begin(), aLast = theModes.end();
172 for (; anIt != aLast; anIt++) {
173 aContext->Activate(anAIS, (*anIt));
181 void XGUI_Displayer::deactivate(ObjectPtr theObject)
183 if (isVisible(theObject)) {
184 Handle(AIS_InteractiveContext) aContext = AISContext();
185 if (aContext.IsNull())
188 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theObject];
189 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
190 aContext->Deactivate(anAIS);
194 void XGUI_Displayer::activate(ObjectPtr theObject)
196 if (isVisible(theObject)) {
197 Handle(AIS_InteractiveContext) aContext = AISContext();
198 if (aContext.IsNull())
201 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theObject];
202 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
203 aContext->Activate(anAIS);
207 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
209 Handle(AIS_InteractiveContext) aContext = AISContext();
210 if (aContext.IsNull())
212 if (!isVisible(theObject))
215 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap.at(theObject);
216 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
218 TColStd_ListOfInteger aModes;
219 aContext->ActivatedModes(anAIS, aModes);
220 return aModes.Extent() > 0;
223 void XGUI_Displayer::stopSelection(const QList<ObjectPtr>& theResults, const bool isStop,
224 const bool isUpdateViewer)
226 Handle(AIS_InteractiveContext) aContext = AISContext();
227 if (aContext.IsNull())
230 Handle(AIS_Shape) anAIS;
231 QList<ObjectPtr>::const_iterator anIt = theResults.begin(), aLast = theResults.end();
233 for (; anIt != aLast; anIt++) {
235 if (isVisible(aFeature))
236 anAIS = Handle(AIS_Shape)::DownCast(
237 myResult2AISObjectMap[aFeature]->impl<Handle(AIS_InteractiveObject)>());
242 QColor aColor(Qt::white);
244 Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
248 QColor aColor(Qt::red);
250 Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
259 void XGUI_Displayer::setSelected(const QList<ObjectPtr>& theResults, const bool isUpdateViewer)
261 Handle(AIS_InteractiveContext) aContext = AISContext();
262 // we need to unhighligth objects manually in the current local context
263 // in couple with the selection clear (TODO)
264 Handle(AIS_LocalContext) aLocalContext = aContext->LocalContext();
265 if (!aLocalContext.IsNull())
266 aLocalContext->UnhilightLastDetected(myWorkshop->viewer()->activeView());
268 aContext->ClearSelected();
269 foreach(ObjectPtr aResult, theResults)
271 if (myResult2AISObjectMap.find(aResult) == myResult2AISObjectMap.end())
274 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[aResult];
276 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
278 aContext->AddOrRemoveSelected(anAIS, false);
285 void XGUI_Displayer::eraseAll(const bool isUpdateViewer)
287 Handle(AIS_InteractiveContext) ic = AISContext();
291 ResultToAISMap::iterator aIt;
292 for (aIt = myResult2AISObjectMap.begin(); aIt != myResult2AISObjectMap.end(); aIt++) {
294 boost::shared_ptr<GeomAPI_AISObject> aAISObj = (*aIt).second;
295 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
297 ic->Remove(anIO, false);
299 myResult2AISObjectMap.clear();
304 void XGUI_Displayer::eraseDeletedResults(const bool isUpdateViewer)
306 Handle(AIS_InteractiveContext) aContext = AISContext();
307 if (aContext.IsNull())
310 ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(), aFLast =
311 myResult2AISObjectMap.end();
312 std::list<ObjectPtr> aRemoved;
313 for (; aFIt != aFLast; aFIt++) {
314 ObjectPtr aFeature = (*aFIt).first;
315 if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
316 boost::shared_ptr<GeomAPI_AISObject> anObj = (*aFIt).second;
319 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
320 if (!anAIS.IsNull()) {
321 aContext->Remove(anAIS, false);
322 aRemoved.push_back(aFeature);
326 std::list<ObjectPtr>::const_iterator anIt = aRemoved.begin(), aLast = aRemoved.end();
327 for (; anIt != aLast; anIt++) {
328 myResult2AISObjectMap.erase(myResult2AISObjectMap.find(*anIt));
335 void XGUI_Displayer::openLocalContext()
337 Handle(AIS_InteractiveContext) aContext = AISContext();
338 if (aContext.IsNull())
340 // Open local context if there is no one
341 if (!aContext->HasOpenedContext()) {
342 aContext->ClearCurrents(false);
343 //aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
344 aContext->OpenLocalContext();
345 aContext->NotUseDisplayedObjects();
349 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
351 AISContext()->ClearSelected(false);
352 closeAllContexts(true);
355 boost::shared_ptr<GeomAPI_AISObject> XGUI_Displayer::getAISObject(ObjectPtr theObject) const
357 boost::shared_ptr<GeomAPI_AISObject> anIO;
358 if (myResult2AISObjectMap.find(theObject) != myResult2AISObjectMap.end())
359 anIO = (myResult2AISObjectMap.find(theObject))->second;
363 ObjectPtr XGUI_Displayer::getObject(Handle(AIS_InteractiveObject) theIO) const
366 ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(), aFLast =
367 myResult2AISObjectMap.end();
368 for (; aFIt != aFLast && !aFeature; aFIt++) {
369 boost::shared_ptr<GeomAPI_AISObject> anObj = (*aFIt).second;
372 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
375 aFeature = (*aFIt).first;
380 void XGUI_Displayer::closeAllContexts(const bool isUpdateViewer)
382 Handle(AIS_InteractiveContext) ic = AISContext();
384 ic->CloseAllContexts(false);
390 void XGUI_Displayer::updateViewer()
392 Handle(AIS_InteractiveContext) ic = AISContext();
394 ic->UpdateCurrentViewer();
397 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
399 return myWorkshop->viewer()->AISContext();
402 void XGUI_Displayer::display(boost::shared_ptr<GeomAPI_AISObject> theAIS, bool isUpdate)
404 Handle(AIS_InteractiveContext) aContext = AISContext();
405 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
406 if (!anAISIO.IsNull())
407 aContext->Display(anAISIO, isUpdate);
410 void XGUI_Displayer::erase(boost::shared_ptr<GeomAPI_AISObject> theAIS, const bool isUpdate)
412 Handle(AIS_InteractiveContext) aContext = AISContext();
413 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
414 if (!anAISIO.IsNull()) {
415 aContext->Remove(anAISIO, isUpdate);
419 void XGUI_Displayer::activateObjectsOutOfContext(const std::list<int>& theModes,
420 Handle(SelectMgr_Filter) theFilter)
422 Handle(AIS_InteractiveContext) aContext = AISContext();
423 // Open local context if there is no one
424 if (!aContext->HasOpenedContext())
427 aContext->UseDisplayedObjects();
428 std::list<int>::const_iterator anIt = theModes.begin(), aLast = theModes.end();
429 for (; anIt != aLast; anIt++) {
430 aContext->ActivateStandardMode((TopAbs_ShapeEnum)(*anIt));
433 if (!theFilter.IsNull())
434 aContext->AddFilter(theFilter);
438 void XGUI_Displayer::deactivateObjectsOutOfContext()
440 Handle(AIS_InteractiveContext) aContext = AISContext();
441 // Open local context if there is no one
442 if (!aContext->HasOpenedContext())
445 aContext->RemoveFilters();
446 aContext->NotUseDisplayedObjects();
450 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool toUpdate)
452 if (theMode == NoMode)
455 Handle(AIS_InteractiveContext) aContext = AISContext();
456 if (aContext.IsNull())
459 boost::shared_ptr<GeomAPI_AISObject> aAISObj = getAISObject(theObject);
463 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
464 aContext->SetDisplayMode(aAISIO, theMode, toUpdate);
468 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
470 Handle(AIS_InteractiveContext) aContext = AISContext();
471 if (aContext.IsNull())
474 boost::shared_ptr<GeomAPI_AISObject> aAISObj = getAISObject(theObject);
478 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
479 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();