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::deactivate(ObjectPtr theObject)
145 if (isVisible(theObject)) {
146 Handle(AIS_InteractiveContext) aContext = AISContext();
147 if (aContext.IsNull())
150 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theObject];
151 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
152 aContext->Deactivate(anAIS);
156 void XGUI_Displayer::activate(ObjectPtr theObject)
158 if (isVisible(theObject)) {
159 Handle(AIS_InteractiveContext) aContext = AISContext();
160 if (aContext.IsNull())
163 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theObject];
164 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
165 aContext->Activate(anAIS);
169 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
171 Handle(AIS_InteractiveContext) aContext = AISContext();
172 if (aContext.IsNull())
174 if (!isVisible(theObject))
177 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap.at(theObject);
178 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
180 TColStd_ListOfInteger aModes;
181 aContext->ActivatedModes(anAIS, aModes);
182 return aModes.Extent() > 0;
185 void XGUI_Displayer::stopSelection(const QList<ObjectPtr>& theResults, const bool isStop,
186 const bool isUpdateViewer)
188 Handle(AIS_InteractiveContext) aContext = AISContext();
189 if (aContext.IsNull())
192 Handle(AIS_Shape) anAIS;
193 QList<ObjectPtr>::const_iterator anIt = theResults.begin(), aLast = theResults.end();
195 for (; anIt != aLast; anIt++) {
197 if (isVisible(aFeature))
198 anAIS = Handle(AIS_Shape)::DownCast(
199 myResult2AISObjectMap[aFeature]->impl<Handle(AIS_InteractiveObject)>());
204 QColor aColor(Qt::white);
206 Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
210 QColor aColor(Qt::red);
212 Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
221 void XGUI_Displayer::setSelected(const QList<ObjectPtr>& theResults, const bool isUpdateViewer)
223 Handle(AIS_InteractiveContext) aContext = AISContext();
224 // we need to unhighligth objects manually in the current local context
225 // in couple with the selection clear (TODO)
226 Handle(AIS_LocalContext) aLocalContext = aContext->LocalContext();
227 if (!aLocalContext.IsNull())
228 aLocalContext->UnhilightLastDetected(myWorkshop->viewer()->activeView());
230 aContext->ClearSelected();
231 foreach(ObjectPtr aResult, theResults)
233 if (myResult2AISObjectMap.find(aResult) == myResult2AISObjectMap.end())
236 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[aResult];
238 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
240 aContext->AddOrRemoveSelected(anAIS, false);
247 void XGUI_Displayer::eraseAll(const bool isUpdateViewer)
249 Handle(AIS_InteractiveContext) ic = AISContext();
253 ResultToAISMap::iterator aIt;
254 for (aIt = myResult2AISObjectMap.begin(); aIt != myResult2AISObjectMap.end(); aIt++) {
256 boost::shared_ptr<GeomAPI_AISObject> aAISObj = (*aIt).second;
257 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
259 ic->Remove(anIO, false);
261 myResult2AISObjectMap.clear();
266 void XGUI_Displayer::eraseDeletedResults(const bool isUpdateViewer)
268 Handle(AIS_InteractiveContext) aContext = AISContext();
269 if (aContext.IsNull())
272 ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(), aFLast =
273 myResult2AISObjectMap.end();
274 std::list<ObjectPtr> aRemoved;
275 for (; aFIt != aFLast; aFIt++) {
276 ObjectPtr aFeature = (*aFIt).first;
277 if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
278 boost::shared_ptr<GeomAPI_AISObject> anObj = (*aFIt).second;
281 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
282 if (!anAIS.IsNull()) {
283 aContext->Remove(anAIS, false);
284 aRemoved.push_back(aFeature);
288 std::list<ObjectPtr>::const_iterator anIt = aRemoved.begin(), aLast = aRemoved.end();
289 for (; anIt != aLast; anIt++) {
290 myResult2AISObjectMap.erase(myResult2AISObjectMap.find(*anIt));
297 void XGUI_Displayer::openLocalContext()
299 Handle(AIS_InteractiveContext) aContext = AISContext();
300 if (aContext.IsNull())
302 // Open local context if there is no one
303 if (!aContext->HasOpenedContext()) {
304 aContext->ClearCurrents(false);
305 //aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
306 aContext->OpenLocalContext();
307 aContext->NotUseDisplayedObjects();
311 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
313 AISContext()->ClearSelected(false);
314 closeAllContexts(true);
317 boost::shared_ptr<GeomAPI_AISObject> XGUI_Displayer::getAISObject(ObjectPtr theObject) const
319 boost::shared_ptr<GeomAPI_AISObject> anIO;
320 if (myResult2AISObjectMap.find(theObject) != myResult2AISObjectMap.end())
321 anIO = (myResult2AISObjectMap.find(theObject))->second;
325 ObjectPtr XGUI_Displayer::getObject(Handle(AIS_InteractiveObject) theIO) const
328 ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(), aFLast =
329 myResult2AISObjectMap.end();
330 for (; aFIt != aFLast && !aFeature; aFIt++) {
331 boost::shared_ptr<GeomAPI_AISObject> anObj = (*aFIt).second;
334 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
337 aFeature = (*aFIt).first;
342 void XGUI_Displayer::closeAllContexts(const bool isUpdateViewer)
344 Handle(AIS_InteractiveContext) ic = AISContext();
346 ic->CloseAllContexts(false);
352 void XGUI_Displayer::updateViewer()
354 Handle(AIS_InteractiveContext) ic = AISContext();
356 ic->UpdateCurrentViewer();
359 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
361 return myWorkshop->viewer()->AISContext();
364 void XGUI_Displayer::display(boost::shared_ptr<GeomAPI_AISObject> theAIS, bool isUpdate)
366 Handle(AIS_InteractiveContext) aContext = AISContext();
367 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
368 if (!anAISIO.IsNull())
369 aContext->Display(anAISIO, isUpdate);
372 void XGUI_Displayer::erase(boost::shared_ptr<GeomAPI_AISObject> theAIS, const bool isUpdate)
374 Handle(AIS_InteractiveContext) aContext = AISContext();
375 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
376 if (!anAISIO.IsNull()) {
377 aContext->Remove(anAISIO, isUpdate);
381 void XGUI_Displayer::activateObjectsOutOfContext(const std::list<int>& theModes,
382 Handle(SelectMgr_Filter) theFilter)
384 Handle(AIS_InteractiveContext) aContext = AISContext();
385 // Open local context if there is no one
386 if (!aContext->HasOpenedContext())
389 aContext->UseDisplayedObjects();
390 std::list<int>::const_iterator anIt = theModes.begin(), aLast = theModes.end();
391 for (; anIt != aLast; anIt++) {
392 aContext->ActivateStandardMode((TopAbs_ShapeEnum)(*anIt));
395 if (!theFilter.IsNull())
396 aContext->AddFilter(theFilter);
400 void XGUI_Displayer::deactivateObjectsOutOfContext()
402 Handle(AIS_InteractiveContext) aContext = AISContext();
403 // Open local context if there is no one
404 if (!aContext->HasOpenedContext())
407 aContext->RemoveFilters();
408 aContext->NotUseDisplayedObjects();
412 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool toUpdate)
414 if (theMode == NoMode)
417 Handle(AIS_InteractiveContext) aContext = AISContext();
418 if (aContext.IsNull())
421 boost::shared_ptr<GeomAPI_AISObject> aAISObj = getAISObject(theObject);
425 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
426 aContext->SetDisplayMode(aAISIO, theMode, toUpdate);
430 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
432 Handle(AIS_InteractiveContext) aContext = AISContext();
433 if (aContext.IsNull())
436 boost::shared_ptr<GeomAPI_AISObject> aAISObj = getAISObject(theObject);
440 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
441 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();