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);
52 GeomPresentablePtr aPrs = boost::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
53 bool isShading = false;
55 anAIS = aPrs->getAISObject(AISObjectPtr());
57 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
59 boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
61 anAIS = AISObjectPtr(new GeomAPI_AISObject());
62 anAIS->createShape(aShapePtr);
68 display(theObject, anAIS, isShading, isUpdateViewer);
72 void XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr 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 AISObjectPtr 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 AISObjectPtr aAISObj = getAISObject(theObject);
112 GeomPresentablePtr aPrs = boost::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
114 AISObjectPtr 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 AISObjectPtr 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 AISObjectPtr 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 AISObjectPtr 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 AISObjectPtr 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->UnhilightCurrents(false);
263 aContext->ClearSelected();
267 void XGUI_Displayer::eraseAll(const bool isUpdateViewer)
269 Handle(AIS_InteractiveContext) ic = AISContext();
273 ResultToAISMap::iterator aIt;
274 for (aIt = myResult2AISObjectMap.begin(); aIt != myResult2AISObjectMap.end(); aIt++) {
276 AISObjectPtr aAISObj = (*aIt).second;
277 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
279 ic->Remove(anIO, false);
281 myResult2AISObjectMap.clear();
286 void XGUI_Displayer::eraseDeletedResults(const bool isUpdateViewer)
288 Handle(AIS_InteractiveContext) aContext = AISContext();
289 if (aContext.IsNull())
292 ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(), aFLast =
293 myResult2AISObjectMap.end();
294 std::list<ObjectPtr> aRemoved;
295 for (; aFIt != aFLast; aFIt++) {
296 ObjectPtr aFeature = (*aFIt).first;
297 if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
298 AISObjectPtr anObj = (*aFIt).second;
301 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
302 if (!anAIS.IsNull()) {
303 aContext->Remove(anAIS, false);
304 aRemoved.push_back(aFeature);
308 std::list<ObjectPtr>::const_iterator anIt = aRemoved.begin(), aLast = aRemoved.end();
309 for (; anIt != aLast; anIt++) {
310 myResult2AISObjectMap.erase(myResult2AISObjectMap.find(*anIt));
317 void XGUI_Displayer::openLocalContext()
319 Handle(AIS_InteractiveContext) aContext = AISContext();
320 if (aContext.IsNull())
322 // Open local context if there is no one
323 if (!aContext->HasOpenedContext()) {
324 aContext->ClearCurrents(false);
325 //aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
326 aContext->OpenLocalContext();
327 aContext->NotUseDisplayedObjects();
331 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
333 AISContext()->ClearSelected(false);
334 closeAllContexts(true);
337 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
340 if (myResult2AISObjectMap.find(theObject) != myResult2AISObjectMap.end())
341 anIO = (myResult2AISObjectMap.find(theObject))->second;
345 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
347 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
348 return getObject(aRefAIS);
351 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
354 ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(), aFLast =
355 myResult2AISObjectMap.end();
356 for (; aFIt != aFLast && !aFeature; aFIt++) {
357 AISObjectPtr anObj = (*aFIt).second;
360 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
363 aFeature = (*aFIt).first;
368 void XGUI_Displayer::closeAllContexts(const bool isUpdateViewer)
370 Handle(AIS_InteractiveContext) ic = AISContext();
372 ic->CloseAllContexts(false);
378 void XGUI_Displayer::updateViewer()
380 Handle(AIS_InteractiveContext) ic = AISContext();
382 ic->UpdateCurrentViewer();
385 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
387 return myWorkshop->viewer()->AISContext();
390 void XGUI_Displayer::display(AISObjectPtr theAIS, bool isUpdate)
392 Handle(AIS_InteractiveContext) aContext = AISContext();
393 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
394 if (!anAISIO.IsNull())
395 aContext->Display(anAISIO, isUpdate);
398 void XGUI_Displayer::erase(AISObjectPtr theAIS, const bool isUpdate)
400 Handle(AIS_InteractiveContext) aContext = AISContext();
401 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
402 if (!anAISIO.IsNull()) {
403 aContext->Remove(anAISIO, isUpdate);
407 void XGUI_Displayer::activateObjectsOutOfContext(const QIntList& theModes)
409 Handle(AIS_InteractiveContext) aContext = AISContext();
410 // Open local context if there is no one
411 if (!aContext->HasOpenedContext())
414 aContext->UseDisplayedObjects();
415 ResultToAISMap::iterator aIt;
416 Handle(AIS_InteractiveObject) anAISIO;
417 for (aIt = myResult2AISObjectMap.begin(); aIt != myResult2AISObjectMap.end(); aIt++) {
418 anAISIO = (*aIt).second->impl<Handle(AIS_InteractiveObject)>();
419 aContext->Load(anAISIO, -1, true);
420 if (theModes.size() == 0)
421 aContext->Activate(anAISIO);
423 foreach(int aMode, theModes) {
424 aContext->Activate(anAISIO, aMode);
431 void XGUI_Displayer::deactivateObjectsOutOfContext()
433 Handle(AIS_InteractiveContext) aContext = AISContext();
434 // Open local context if there is no one
435 if (!aContext->HasOpenedContext())
438 aContext->NotUseDisplayedObjects();
442 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool toUpdate)
444 if (theMode == NoMode)
447 Handle(AIS_InteractiveContext) aContext = AISContext();
448 if (aContext.IsNull())
451 AISObjectPtr aAISObj = getAISObject(theObject);
455 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
456 aContext->SetDisplayMode(aAISIO, theMode, toUpdate);
459 void XGUI_Displayer::setSelectionModes(const QIntList& theModes)
461 Handle(AIS_InteractiveContext) aContext = AISContext();
462 if (aContext.IsNull())
464 if (!aContext->HasOpenedContext())
466 // Clear previous mode
467 const TColStd_ListOfInteger& aModes = aContext->ActivatedStandardModes();
468 if (!aModes.IsEmpty()) {
469 TColStd_ListOfInteger aMModes;
470 aMModes.Assign(aModes);
471 TColStd_ListIteratorOfListOfInteger it(aMModes);
472 for(; it.More(); it.Next()) {
473 aContext->DeactivateStandardMode((TopAbs_ShapeEnum)it.Value());
476 foreach(int aMode, theModes) {
477 aContext->ActivateStandardMode((TopAbs_ShapeEnum)aMode);
481 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
483 Handle(AIS_InteractiveContext) aContext = AISContext();
484 if (aContext.IsNull())
487 AISObjectPtr aAISObj = getAISObject(theObject);
491 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
492 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
495 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
497 Handle(AIS_InteractiveContext) aContext = AISContext();
498 if (aContext.IsNull())
500 aContext->AddFilter(theFilter);
503 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
505 Handle(AIS_InteractiveContext) aContext = AISContext();
506 if (aContext.IsNull())
508 aContext->RemoveFilter(theFilter);