1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: XGUI_Displayer.cpp
4 // Created: 20 Apr 2014
5 // Author: Natalia ERMOLAEVA
7 #include "XGUI_Displayer.h"
8 #include "XGUI_Workshop.h"
9 #include "XGUI_ViewerProxy.h"
11 #include <AppElements_Viewer.h>
13 #include <ModelAPI_Document.h>
14 #include <ModelAPI_Data.h>
15 #include <ModelAPI_Object.h>
16 #include <ModelAPI_Tools.h>
18 #include <ModuleBase_ResultPrs.h>
20 #include <GeomAPI_Shape.h>
21 #include <GeomAPI_IPresentable.h>
22 #include <GeomAPI_ICustomPrs.h>
24 #include <AIS_InteractiveContext.hxx>
25 #include <AIS_LocalContext.hxx>
26 #include <AIS_ListOfInteractive.hxx>
27 #include <AIS_ListIteratorOfListOfInteractive.hxx>
28 #include <AIS_DimensionSelectionMode.hxx>
29 #include <AIS_Shape.hxx>
30 #include <AIS_Dimension.hxx>
31 #include <TColStd_ListIteratorOfListOfInteger.hxx>
32 #include <SelectMgr_ListOfFilter.hxx>
33 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
35 #include <TColStd_MapOfTransient.hxx>
36 #include <TColStd_MapIteratorOfMapOfTransient.hxx>
40 const int MOUSE_SENSITIVITY_IN_PIXEL = 10; ///< defines the local context mouse selection sensitivity
42 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
43 : myWorkshop(theWorkshop)
47 XGUI_Displayer::~XGUI_Displayer()
51 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
53 return myResult2AISObjectMap.contains(theObject);
56 void XGUI_Displayer::display(ObjectPtr theObject, bool isUpdateViewer)
58 if (isVisible(theObject)) {
59 redisplay(theObject, isUpdateViewer);
63 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
64 bool isShading = false;
65 if (aPrs.get() != NULL) {
66 anAIS = aPrs->getAISObject(AISObjectPtr());
68 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
69 if (aResult.get() != NULL) {
70 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
71 if (aShapePtr.get() != NULL) {
72 anAIS = AISObjectPtr(new GeomAPI_AISObject());
73 anAIS->setImpl(new Handle(AIS_InteractiveObject)(new ModuleBase_ResultPrs(aResult)));
74 //anAIS->createShape(aShapePtr);
80 display(theObject, anAIS, isShading, isUpdateViewer);
84 void XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
85 bool isShading, bool isUpdateViewer)
87 Handle(AIS_InteractiveContext) aContext = AISContext();
88 if (aContext.IsNull())
91 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
92 if (!anAISIO.IsNull()) {
93 myResult2AISObjectMap[theObject] = theAIS;
95 closeLocalContexts(false);
96 aContext->Display(anAISIO, false);
97 aContext->SetDisplayMode(anAISIO, isShading? Shading : Wireframe, false);
98 // Customization of presentation
99 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
100 if (aFeature.get() != NULL) {
101 GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
102 if (aCustPrs.get() != NULL)
103 aCustPrs->customisePresentation(theAIS);
106 aContext->Load(anAISIO, -1, true);
108 //if (aContext->HasOpenedContext()) {
109 //if (myUseExternalObjects) {
110 //if (myActiveSelectionModes.size() == 0)
111 // aContext->Activate(anAISIO);
113 // foreach(int aMode, myActiveSelectionModes) {
114 // aContext->Activate(anAISIO, aMode);
124 void XGUI_Displayer::erase(ObjectPtr theObject, const bool isUpdateViewer)
126 if (!isVisible(theObject))
129 Handle(AIS_InteractiveContext) aContext = AISContext();
130 if (aContext.IsNull())
132 AISObjectPtr anObject = myResult2AISObjectMap[theObject];
134 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
135 if (!anAIS.IsNull()) {
136 aContext->Remove(anAIS, isUpdateViewer);
139 myResult2AISObjectMap.remove(theObject);
142 void XGUI_Displayer::redisplay(ObjectPtr theObject, bool isUpdateViewer)
144 if (!isVisible(theObject))
147 AISObjectPtr aAISObj = getAISObject(theObject);
148 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
150 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
152 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
154 erase(theObject, isUpdateViewer);
157 if (aAIS_Obj != aAISObj) {
158 myResult2AISObjectMap[theObject] = aAIS_Obj;
160 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
163 if (!aAISIO.IsNull()) {
164 Handle(AIS_InteractiveContext) aContext = AISContext();
165 if (aContext.IsNull())
167 aContext->Redisplay(aAISIO, isUpdateViewer);
171 void XGUI_Displayer::deactivate(ObjectPtr theObject)
173 if (isVisible(theObject)) {
174 Handle(AIS_InteractiveContext) aContext = AISContext();
175 if (aContext.IsNull())
178 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
179 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
180 aContext->Deactivate(anAIS);
184 void XGUI_Displayer::activate(ObjectPtr theFeature)
186 activate(theFeature, myActiveSelectionModes);
189 void XGUI_Displayer::activate(ObjectPtr theObject, const QIntList& theModes)
191 if (isVisible(theObject)) {
192 Handle(AIS_InteractiveContext) aContext = AISContext();
193 if (aContext.IsNull())
196 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
197 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
198 aContext->Deactivate(anAIS);
199 //if (aContext->HasOpenedContext()) {
200 // aContext->Load(anAIS, -1, true);
202 // In order to clear active modes list
203 if (theModes.size() > 0) {
204 foreach(int aMode, theModes) {
205 aContext->Activate(anAIS, aMode);
208 aContext->Activate(anAIS);
212 void XGUI_Displayer::activateObjects(const QIntList& theModes)
214 // In order to avoid doblications of selection modes
215 myActiveSelectionModes.clear();
216 foreach (int aMode, theModes) {
217 if (!myActiveSelectionModes.contains(aMode))
218 myActiveSelectionModes.append(aMode);
220 Handle(AIS_InteractiveContext) aContext = AISContext();
221 // Open local context if there is no one
222 if (!aContext->HasOpenedContext())
225 //aContext->UseDisplayedObjects();
226 //myUseExternalObjects = true;
228 AIS_ListOfInteractive aPrsList;
229 aContext->DisplayedObjects(aPrsList, true);
230 //Deactivate trihedron which can be activated in local selector
232 Handle(AIS_Trihedron) aTrihedron;
233 AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
234 Handle(AIS_InteractiveObject) anAISIO;
235 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
236 anAISIO = aLIt.Value();
237 aTrihedron = Handle(AIS_Trihedron)::DownCast(anAISIO);
238 if (!aTrihedron.IsNull()) {
239 aContext->Deactivate(aTrihedron);
241 //aContext->Load(anAISIO, -1, true);
242 // In order to clear active modes list
243 aContext->Deactivate(anAISIO);
244 if (myActiveSelectionModes.size() == 0)
245 aContext->Activate(anAISIO);
247 foreach(int aMode, myActiveSelectionModes) {
248 aContext->Activate(anAISIO, aMode);
256 void XGUI_Displayer::deactivateObjects()
258 myActiveSelectionModes.clear();
259 Handle(AIS_InteractiveContext) aContext = AISContext();
260 // Open local context if there is no one
261 if (!aContext->HasOpenedContext())
264 aContext->NotUseDisplayedObjects();
265 AIS_ListOfInteractive aPrsList;
266 aContext->DisplayedObjects(aPrsList);
268 AIS_ListIteratorOfListOfInteractive aLIt;
269 Handle(AIS_Trihedron) aTrihedron;
270 Handle(AIS_InteractiveObject) anAISIO;
271 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
272 anAISIO = aLIt.Value();
273 aTrihedron = Handle(AIS_Trihedron)::DownCast(anAISIO);
274 if (!aTrihedron.IsNull()) {
275 aContext->Deactivate(aTrihedron);
277 //aContext->Load(anAISIO, -1, true);
278 aContext->Deactivate(anAISIO);
279 //aContext->Activate(anAISIO);
284 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
286 Handle(AIS_InteractiveContext) aContext = AISContext();
287 if (aContext.IsNull())
289 if (!isVisible(theObject))
292 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
293 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
295 TColStd_ListOfInteger aModes;
296 aContext->ActivatedModes(anAIS, aModes);
297 return aModes.Extent() > 0;
300 void XGUI_Displayer::stopSelection(const QObjectPtrList& theResults, const bool isStop,
301 const bool isUpdateViewer)
303 Handle(AIS_InteractiveContext) aContext = AISContext();
304 if (aContext.IsNull())
307 Handle(AIS_Shape) anAIS;
308 QObjectPtrList::const_iterator anIt = theResults.begin(), aLast = theResults.end();
310 for (; anIt != aLast; anIt++) {
312 if (isVisible(aFeature))
313 anAIS = Handle(AIS_Shape)::DownCast(
314 myResult2AISObjectMap[aFeature]->impl<Handle(AIS_InteractiveObject)>());
319 QColor aColor(Qt::white);
321 Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
325 QColor aColor(Qt::red);
327 Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
336 void XGUI_Displayer::setSelected(const QObjectPtrList& theResults, const bool isUpdateViewer)
338 Handle(AIS_InteractiveContext) aContext = AISContext();
339 if (aContext.IsNull())
341 if (aContext->HasOpenedContext()) {
342 aContext->UnhilightSelected();
343 aContext->ClearSelected();
344 foreach(ObjectPtr aResult, theResults) {
345 if (isVisible(aResult)) {
346 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
347 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
349 aContext->SetSelected(anAIS, false);
353 aContext->UnhilightCurrents();
354 aContext->ClearCurrents();
355 foreach(ObjectPtr aResult, theResults) {
356 if (isVisible(aResult)) {
357 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
358 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
360 aContext->SetCurrentObject(anAIS, false);
369 void XGUI_Displayer::clearSelected()
371 Handle(AIS_InteractiveContext) aContext = AISContext();
373 aContext->UnhilightCurrents(false);
374 aContext->ClearSelected();
378 void XGUI_Displayer::eraseAll(const bool isUpdateViewer)
380 Handle(AIS_InteractiveContext) aContext = AISContext();
381 if (aContext.IsNull())
384 foreach (AISObjectPtr aAISObj, myResult2AISObjectMap) {
386 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
388 aContext->Remove(anIO, false);
390 myResult2AISObjectMap.clear();
395 //void XGUI_Displayer::eraseDeletedResults(const bool isUpdateViewer)
397 // Handle(AIS_InteractiveContext) aContext = AISContext();
398 // if (aContext.IsNull())
401 // QObjectPtrList aRemoved;
402 // foreach (ObjectPtr aFeature, myResult2AISObjectMap.keys()) {
403 // if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
404 // AISObjectPtr anObj = myResult2AISObjectMap[aFeature];
407 // Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
408 // if (!anAIS.IsNull()) {
409 // aContext->Remove(anAIS, false);
410 // aRemoved.append(aFeature);
414 // foreach(ObjectPtr aObj, aRemoved) {
415 // myResult2AISObjectMap.remove(aObj);
418 // if (isUpdateViewer)
422 void XGUI_Displayer::openLocalContext()
424 Handle(AIS_InteractiveContext) aContext = AISContext();
425 if (aContext.IsNull())
427 // Open local context if there is no one
428 if (!aContext->HasOpenedContext()) {
429 // Preserve selected objects
430 //AIS_ListOfInteractive aAisList;
431 //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
432 // aAisList.Append(aContext->Current());
434 // get the filters from the global context and append them to the local context
435 // a list of filters in the global context is not cleared and should be cleared here
436 SelectMgr_ListOfFilter aFilters;
437 aFilters.Assign(aContext->Filters());
438 // it is important to remove the filters in the global context, because there is a code
439 // in the closeLocalContex, which restore the global context filters
440 aContext->RemoveFilters();
442 aContext->ClearCurrents();
443 aContext->OpenLocalContext();
444 //aContext->NotUseDisplayedObjects();
446 //myUseExternalObjects = false;
447 myActiveSelectionModes.clear();
449 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
450 for (;aIt.More(); aIt.Next()) {
451 aContext->AddFilter(aIt.Value());
454 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
455 //for(; aIt2.More(); aIt2.Next()) {
456 // aContext->SetSelected(aIt2.Value(), false);
461 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
463 Handle(AIS_InteractiveContext) aContext = AISContext();
464 if ( (!aContext.IsNull()) && (aContext->HasOpenedContext()) ) {
465 // Preserve selected objects
466 //AIS_ListOfInteractive aAisList;
467 //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
468 // aAisList.Append(aContext->SelectedInteractive());
470 // get the filters from the local context and append them to the global context
471 // a list of filters in the local context is cleared
472 SelectMgr_ListOfFilter aFilters;
473 aFilters.Assign(aContext->Filters());
475 aContext->ClearSelected();
476 aContext->CloseAllContexts(false);
478 // Redisplay all object if they were displayed in localContext
479 Handle(AIS_InteractiveObject) aAISIO;
480 foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
481 aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
482 if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
483 aContext->Display(aAISIO, false);
484 aContext->SetDisplayMode(aAISIO, Shading, false);
488 // Append the filters from the local selection in the global selection context
489 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
490 for (;aIt.More(); aIt.Next()) {
491 Handle(SelectMgr_Filter) aFilter = aIt.Value();
492 aContext->AddFilter(aFilter);
497 //myUseExternalObjects = false;
498 myActiveSelectionModes.clear();
501 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
502 //for(; aIt2.More(); aIt2.Next()) {
503 // if (aContext->IsDisplayed(aIt2.Value()))
504 // aContext->SetCurrentObject(aIt2.Value(), false);
509 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
512 if (myResult2AISObjectMap.contains(theObject))
513 anIO = myResult2AISObjectMap[theObject];
517 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
519 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
520 return getObject(aRefAIS);
523 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
526 foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
527 AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
528 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
535 void XGUI_Displayer::updateViewer()
537 Handle(AIS_InteractiveContext) aContext = AISContext();
538 if (!aContext.IsNull())
539 aContext->UpdateCurrentViewer();
542 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
544 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
545 if ((!aContext.IsNull()) && (!aContext->HasOpenedContext())) {
546 aContext->OpenLocalContext();
551 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
553 Handle(AIS_InteractiveContext) aContext = AISContext();
554 if (myAndFilter.IsNull() && !aContext.IsNull()) {
555 myAndFilter = new SelectMgr_AndFilter();
556 aContext->AddFilter(myAndFilter);
561 void XGUI_Displayer::displayAIS(AISObjectPtr theAIS, bool isUpdate)
563 Handle(AIS_InteractiveContext) aContext = AISContext();
564 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
565 if (!anAISIO.IsNull()) {
566 aContext->Display(anAISIO, isUpdate);
567 if (aContext->HasOpenedContext()) {
568 //if (myUseExternalObjects) {
569 if (myActiveSelectionModes.size() == 0)
570 aContext->Activate(anAISIO);
572 foreach(int aMode, myActiveSelectionModes) {
573 aContext->Activate(anAISIO, aMode);
581 void XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool isUpdate)
583 Handle(AIS_InteractiveContext) aContext = AISContext();
584 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
585 if (!anAISIO.IsNull()) {
586 aContext->Remove(anAISIO, isUpdate);
591 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool toUpdate)
593 if (theMode == NoMode)
596 Handle(AIS_InteractiveContext) aContext = AISContext();
597 if (aContext.IsNull())
600 AISObjectPtr aAISObj = getAISObject(theObject);
604 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
605 aContext->SetDisplayMode(aAISIO, theMode, toUpdate);
608 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
610 Handle(AIS_InteractiveContext) aContext = AISContext();
611 if (aContext.IsNull())
614 AISObjectPtr aAISObj = getAISObject(theObject);
618 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
619 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
622 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
624 Handle(AIS_InteractiveContext) aContext = AISContext();
625 if (aContext.IsNull())
627 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
628 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
629 for (; aIt.More(); aIt.Next()) {
630 if (theFilter.Access() == aIt.Value().Access())
633 GetFilter()->Add(theFilter);
636 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
638 Handle(AIS_InteractiveContext) aContext = AISContext();
639 if (aContext.IsNull())
641 GetFilter()->Remove(theFilter);
644 void XGUI_Displayer::removeFilters()
646 Handle(AIS_InteractiveContext) aContext = AISContext();
647 if (aContext.IsNull())
649 GetFilter()->Clear();
652 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
654 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
655 foreach(ObjectPtr aObj, aDispList) {
656 if (!theList.contains(aObj))
659 foreach(ObjectPtr aObj, theList) {
660 if (!isVisible(aObj))
661 display(aObj, false);