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>
24 #include <AIS_Dimension.hxx>
25 #include <TColStd_ListIteratorOfListOfInteger.hxx>
29 const int MOUSE_SENSITIVITY_IN_PIXEL = 10; ///< defines the local context mouse selection sensitivity
31 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
33 myWorkshop = theWorkshop;
36 XGUI_Displayer::~XGUI_Displayer()
40 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
42 return myResult2AISObjectMap.find(theObject) != myResult2AISObjectMap.end();
45 void XGUI_Displayer::display(ObjectPtr theObject, bool isUpdateViewer)
47 if (isVisible(theObject)) {
48 redisplay(theObject, isUpdateViewer);
50 boost::shared_ptr<GeomAPI_AISObject> anAIS;
52 GeomPresentablePtr aPrs = boost::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
53 bool isShading = false;
55 anAIS = aPrs->getAISObject(boost::shared_ptr<GeomAPI_AISObject>());
57 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
59 boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
61 anAIS = boost::shared_ptr<GeomAPI_AISObject>(new GeomAPI_AISObject());
62 anAIS->createShape(aShapePtr);
68 display(theObject, anAIS, isShading, isUpdateViewer);
72 void XGUI_Displayer::display(ObjectPtr theObject, boost::shared_ptr<GeomAPI_AISObject> theAIS,
73 bool isShading, bool isUpdateViewer)
75 Handle(AIS_InteractiveContext) aContext = AISContext();
76 if (aContext.IsNull())
79 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
80 if (!anAISIO.IsNull()) {
81 myResult2AISObjectMap[theObject] = theAIS;
82 aContext->Display(anAISIO, false);
83 aContext->SetDisplayMode(anAISIO, isShading? Shading : Wireframe, isUpdateViewer);
87 void XGUI_Displayer::erase(ObjectPtr theObject, const bool isUpdateViewer)
89 if (!isVisible(theObject))
92 Handle(AIS_InteractiveContext) aContext = AISContext();
93 if (aContext.IsNull())
95 boost::shared_ptr<GeomAPI_AISObject> anObject = myResult2AISObjectMap[theObject];
97 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
98 if (!anAIS.IsNull()) {
99 aContext->Remove(anAIS, isUpdateViewer);
102 myResult2AISObjectMap.erase(theObject);
105 void XGUI_Displayer::redisplay(ObjectPtr theObject, bool isUpdateViewer)
107 if (!isVisible(theObject))
110 Handle(AIS_InteractiveObject) aAISIO;
111 boost::shared_ptr<GeomAPI_AISObject> aAISObj = getAISObject(theObject);
112 GeomPresentablePtr aPrs = boost::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
114 boost::shared_ptr<GeomAPI_AISObject> aAIS_Obj = aPrs->getAISObject(aAISObj);
115 if (aAISObj && !aAIS_Obj) {
116 erase(theObject, isUpdateViewer);
119 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
121 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
123 boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
125 Handle(AIS_Shape) aAISShape = Handle(AIS_Shape)::DownCast(
126 aAISObj->impl<Handle(AIS_InteractiveObject)>());
127 if (!aAISShape.IsNull()) {
128 aAISShape->Set(aShapePtr->impl<TopoDS_Shape>());
134 if (!aAISIO.IsNull()) {
135 Handle(AIS_InteractiveContext) aContext = AISContext();
136 if (aContext.IsNull())
138 aContext->Redisplay(aAISIO, isUpdateViewer);
139 //if (aContext->HasOpenedContext()) {
140 // aContext->Load(aAISIO, -1, true/*allow decomposition*/);
145 void XGUI_Displayer::deactivate(ObjectPtr theObject)
147 if (isVisible(theObject)) {
148 Handle(AIS_InteractiveContext) aContext = AISContext();
149 if (aContext.IsNull())
152 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theObject];
153 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
154 aContext->Deactivate(anAIS);
158 void XGUI_Displayer::activate(ObjectPtr theObject, const QIntList& theModes)
160 if (isVisible(theObject)) {
161 Handle(AIS_InteractiveContext) aContext = AISContext();
162 if (aContext.IsNull())
165 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theObject];
166 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
167 if (aContext->HasOpenedContext()) {
168 aContext->Load(anAIS, -1, true);
170 if (theModes.size() > 0) {
171 foreach(int aMode, theModes) {
172 aContext->Activate(anAIS, aMode);
175 aContext->Activate(anAIS);
179 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
181 Handle(AIS_InteractiveContext) aContext = AISContext();
182 if (aContext.IsNull())
184 if (!isVisible(theObject))
187 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap.at(theObject);
188 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
190 TColStd_ListOfInteger aModes;
191 aContext->ActivatedModes(anAIS, aModes);
192 return aModes.Extent() > 0;
195 void XGUI_Displayer::stopSelection(const QList<ObjectPtr>& theResults, const bool isStop,
196 const bool isUpdateViewer)
198 Handle(AIS_InteractiveContext) aContext = AISContext();
199 if (aContext.IsNull())
202 Handle(AIS_Shape) anAIS;
203 QList<ObjectPtr>::const_iterator anIt = theResults.begin(), aLast = theResults.end();
205 for (; anIt != aLast; anIt++) {
207 if (isVisible(aFeature))
208 anAIS = Handle(AIS_Shape)::DownCast(
209 myResult2AISObjectMap[aFeature]->impl<Handle(AIS_InteractiveObject)>());
214 QColor aColor(Qt::white);
216 Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
220 QColor aColor(Qt::red);
222 Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
231 void XGUI_Displayer::setSelected(const QList<ObjectPtr>& theResults, const bool isUpdateViewer)
233 Handle(AIS_InteractiveContext) aContext = AISContext();
234 // we need to unhighligth objects manually in the current local context
235 // in couple with the selection clear (TODO)
236 Handle(AIS_LocalContext) aLocalContext = aContext->LocalContext();
237 if (!aLocalContext.IsNull())
238 aLocalContext->UnhilightLastDetected(myWorkshop->viewer()->activeView());
240 aContext->ClearSelected();
241 foreach(ObjectPtr aResult, theResults)
243 if (myResult2AISObjectMap.find(aResult) == myResult2AISObjectMap.end())
246 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[aResult];
248 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
250 aContext->AddOrRemoveSelected(anAIS, false);
258 void XGUI_Displayer::clearSelected()
260 Handle(AIS_InteractiveContext) aContext = AISContext();
262 aContext->ClearSelected();
265 void XGUI_Displayer::eraseAll(const bool isUpdateViewer)
267 Handle(AIS_InteractiveContext) ic = AISContext();
271 ResultToAISMap::iterator aIt;
272 for (aIt = myResult2AISObjectMap.begin(); aIt != myResult2AISObjectMap.end(); aIt++) {
274 boost::shared_ptr<GeomAPI_AISObject> aAISObj = (*aIt).second;
275 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
277 ic->Remove(anIO, false);
279 myResult2AISObjectMap.clear();
284 void XGUI_Displayer::eraseDeletedResults(const bool isUpdateViewer)
286 Handle(AIS_InteractiveContext) aContext = AISContext();
287 if (aContext.IsNull())
290 ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(), aFLast =
291 myResult2AISObjectMap.end();
292 std::list<ObjectPtr> aRemoved;
293 for (; aFIt != aFLast; aFIt++) {
294 ObjectPtr aFeature = (*aFIt).first;
295 if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
296 boost::shared_ptr<GeomAPI_AISObject> anObj = (*aFIt).second;
299 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
300 if (!anAIS.IsNull()) {
301 aContext->Remove(anAIS, false);
302 aRemoved.push_back(aFeature);
306 std::list<ObjectPtr>::const_iterator anIt = aRemoved.begin(), aLast = aRemoved.end();
307 for (; anIt != aLast; anIt++) {
308 myResult2AISObjectMap.erase(myResult2AISObjectMap.find(*anIt));
315 void XGUI_Displayer::openLocalContext()
317 Handle(AIS_InteractiveContext) aContext = AISContext();
318 if (aContext.IsNull())
320 // Open local context if there is no one
321 if (!aContext->HasOpenedContext()) {
322 aContext->ClearCurrents(false);
323 //aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
324 aContext->OpenLocalContext();
325 aContext->NotUseDisplayedObjects();
329 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
331 AISContext()->ClearSelected(false);
332 closeAllContexts(true);
335 boost::shared_ptr<GeomAPI_AISObject> XGUI_Displayer::getAISObject(ObjectPtr theObject) const
337 boost::shared_ptr<GeomAPI_AISObject> anIO;
338 if (myResult2AISObjectMap.find(theObject) != myResult2AISObjectMap.end())
339 anIO = (myResult2AISObjectMap.find(theObject))->second;
343 ObjectPtr XGUI_Displayer::getObject(Handle(AIS_InteractiveObject) theIO) const
346 ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(), aFLast =
347 myResult2AISObjectMap.end();
348 for (; aFIt != aFLast && !aFeature; aFIt++) {
349 boost::shared_ptr<GeomAPI_AISObject> anObj = (*aFIt).second;
352 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
355 aFeature = (*aFIt).first;
360 void XGUI_Displayer::closeAllContexts(const bool isUpdateViewer)
362 Handle(AIS_InteractiveContext) ic = AISContext();
364 ic->CloseAllContexts(false);
370 void XGUI_Displayer::updateViewer()
372 Handle(AIS_InteractiveContext) ic = AISContext();
374 ic->UpdateCurrentViewer();
377 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
379 return myWorkshop->viewer()->AISContext();
382 void XGUI_Displayer::display(boost::shared_ptr<GeomAPI_AISObject> theAIS, bool isUpdate)
384 Handle(AIS_InteractiveContext) aContext = AISContext();
385 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
386 if (!anAISIO.IsNull())
387 aContext->Display(anAISIO, isUpdate);
390 void XGUI_Displayer::erase(boost::shared_ptr<GeomAPI_AISObject> theAIS, const bool isUpdate)
392 Handle(AIS_InteractiveContext) aContext = AISContext();
393 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
394 if (!anAISIO.IsNull()) {
395 aContext->Remove(anAISIO, isUpdate);
399 void XGUI_Displayer::activateObjectsOutOfContext()
401 Handle(AIS_InteractiveContext) aContext = AISContext();
402 // Open local context if there is no one
403 if (!aContext->HasOpenedContext())
406 aContext->UseDisplayedObjects();
407 ResultToAISMap::iterator aIt;
408 Handle(AIS_InteractiveObject) anAISIO;
409 for (aIt = myResult2AISObjectMap.begin(); aIt != myResult2AISObjectMap.end(); aIt++) {
410 anAISIO = (*aIt).second->impl<Handle(AIS_InteractiveObject)>();
411 aContext->Load(anAISIO, -1, true);
416 void XGUI_Displayer::deactivateObjectsOutOfContext()
418 Handle(AIS_InteractiveContext) aContext = AISContext();
419 // Open local context if there is no one
420 if (!aContext->HasOpenedContext())
423 aContext->NotUseDisplayedObjects();
427 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool toUpdate)
429 if (theMode == NoMode)
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 aContext->SetDisplayMode(aAISIO, theMode, toUpdate);
444 void XGUI_Displayer::setSelectionModes(const QIntList& theModes)
446 Handle(AIS_InteractiveContext) aContext = AISContext();
447 if (aContext.IsNull())
449 if (!aContext->HasOpenedContext())
451 // Clear previous mode
452 const TColStd_ListOfInteger& aModes = aContext->ActivatedStandardModes();
453 if (!aModes.IsEmpty()) {
454 TColStd_ListOfInteger aMModes;
455 aMModes.Assign(aModes);
456 TColStd_ListIteratorOfListOfInteger it(aMModes);
457 for(; it.More(); it.Next()) {
458 aContext->DeactivateStandardMode((TopAbs_ShapeEnum)it.Value());
461 foreach(int aMode, theModes) {
462 aContext->ActivateStandardMode((TopAbs_ShapeEnum)aMode);
466 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
468 Handle(AIS_InteractiveContext) aContext = AISContext();
469 if (aContext.IsNull())
472 boost::shared_ptr<GeomAPI_AISObject> aAISObj = getAISObject(theObject);
476 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
477 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
480 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
482 Handle(AIS_InteractiveContext) aContext = AISContext();
483 if (aContext.IsNull())
485 aContext->AddFilter(theFilter);
488 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
490 Handle(AIS_InteractiveContext) aContext = AISContext();
491 if (aContext.IsNull())
493 aContext->RemoveFilter(theFilter);