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>
37 const int MOUSE_SENSITIVITY_IN_PIXEL = 10; ///< defines the local context mouse selection sensitivity
39 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
40 : myUseExternalObjects(false), myWorkshop(theWorkshop)
44 XGUI_Displayer::~XGUI_Displayer()
48 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
50 return myResult2AISObjectMap.contains(theObject);
53 void XGUI_Displayer::display(ObjectPtr theObject, bool isUpdateViewer)
55 if (isVisible(theObject)) {
56 redisplay(theObject, isUpdateViewer);
60 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
61 bool isShading = false;
62 if (aPrs.get() != NULL) {
63 anAIS = aPrs->getAISObject(AISObjectPtr());
65 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
66 if (aResult.get() != NULL) {
67 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
68 if (aShapePtr.get() != NULL) {
69 anAIS = AISObjectPtr(new GeomAPI_AISObject());
70 anAIS->setImpl(new Handle(AIS_InteractiveObject)(new ModuleBase_ResultPrs(aResult)));
71 //anAIS->createShape(aShapePtr);
77 display(theObject, anAIS, isShading, isUpdateViewer);
81 void XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
82 bool isShading, bool isUpdateViewer)
84 Handle(AIS_InteractiveContext) aContext = AISContext();
85 if (aContext.IsNull())
88 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
89 if (!anAISIO.IsNull()) {
90 myResult2AISObjectMap[theObject] = theAIS;
91 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
92 if (aFeature.get() != NULL) {
93 GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
94 if (aCustPrs.get() != NULL)
95 aCustPrs->customisePresentation(theAIS);
97 aContext->Display(anAISIO, false);
98 aContext->SetDisplayMode(anAISIO, isShading? Shading : Wireframe, isUpdateViewer);
99 if (aContext->HasOpenedContext()) {
100 if (myUseExternalObjects) {
101 if (myActiveSelectionModes.size() == 0)
102 aContext->Activate(anAISIO);
104 foreach(int aMode, myActiveSelectionModes) {
105 aContext->Activate(anAISIO, aMode);
113 void XGUI_Displayer::erase(ObjectPtr theObject, const bool isUpdateViewer)
115 if (!isVisible(theObject))
118 Handle(AIS_InteractiveContext) aContext = AISContext();
119 if (aContext.IsNull())
121 AISObjectPtr anObject = myResult2AISObjectMap[theObject];
123 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
124 if (!anAIS.IsNull()) {
125 aContext->Remove(anAIS, isUpdateViewer);
128 myResult2AISObjectMap.remove(theObject);
131 void XGUI_Displayer::redisplay(ObjectPtr theObject, bool isUpdateViewer)
133 if (!isVisible(theObject))
136 AISObjectPtr aAISObj = getAISObject(theObject);
137 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
139 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
141 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
143 erase(theObject, isUpdateViewer);
146 if (aAIS_Obj != aAISObj) {
147 myResult2AISObjectMap[theObject] = aAIS_Obj;
149 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
152 if (!aAISIO.IsNull()) {
153 Handle(AIS_InteractiveContext) aContext = AISContext();
154 if (aContext.IsNull())
156 aContext->Redisplay(aAISIO, isUpdateViewer);
160 void XGUI_Displayer::deactivate(ObjectPtr theObject)
162 if (isVisible(theObject)) {
163 Handle(AIS_InteractiveContext) aContext = AISContext();
164 if (aContext.IsNull())
167 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
168 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
169 aContext->Deactivate(anAIS);
173 void XGUI_Displayer::activate(ObjectPtr theFeature)
176 activate(theFeature, aModes);
179 void XGUI_Displayer::activate(ObjectPtr theObject, const QIntList& theModes)
181 if (isVisible(theObject)) {
182 Handle(AIS_InteractiveContext) aContext = AISContext();
183 if (aContext.IsNull())
186 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
187 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
188 if (aContext->HasOpenedContext()) {
189 aContext->Load(anAIS, -1, true);
191 if (theModes.size() > 0) {
192 foreach(int aMode, theModes) {
193 aContext->Activate(anAIS, aMode);
196 aContext->Activate(anAIS);
200 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
202 Handle(AIS_InteractiveContext) aContext = AISContext();
203 if (aContext.IsNull())
205 if (!isVisible(theObject))
208 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
209 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
211 TColStd_ListOfInteger aModes;
212 aContext->ActivatedModes(anAIS, aModes);
213 return aModes.Extent() > 0;
216 void XGUI_Displayer::stopSelection(const QObjectPtrList& theResults, const bool isStop,
217 const bool isUpdateViewer)
219 Handle(AIS_InteractiveContext) aContext = AISContext();
220 if (aContext.IsNull())
223 Handle(AIS_Shape) anAIS;
224 QObjectPtrList::const_iterator anIt = theResults.begin(), aLast = theResults.end();
226 for (; anIt != aLast; anIt++) {
228 if (isVisible(aFeature))
229 anAIS = Handle(AIS_Shape)::DownCast(
230 myResult2AISObjectMap[aFeature]->impl<Handle(AIS_InteractiveObject)>());
235 QColor aColor(Qt::white);
237 Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
241 QColor aColor(Qt::red);
243 Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
252 void XGUI_Displayer::setSelected(const QObjectPtrList& theResults, const bool isUpdateViewer)
254 Handle(AIS_InteractiveContext) aContext = AISContext();
255 if (aContext.IsNull())
257 if (aContext->HasOpenedContext()) {
258 aContext->UnhilightSelected();
259 aContext->ClearSelected();
260 foreach(ObjectPtr aResult, theResults) {
261 if (isVisible(aResult)) {
262 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
263 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
265 aContext->SetSelected(anAIS, false);
269 aContext->UnhilightCurrents();
270 aContext->ClearCurrents();
271 foreach(ObjectPtr aResult, theResults) {
272 if (isVisible(aResult)) {
273 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
274 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
276 aContext->SetCurrentObject(anAIS, false);
285 void XGUI_Displayer::clearSelected()
287 Handle(AIS_InteractiveContext) aContext = AISContext();
289 aContext->UnhilightCurrents(false);
290 aContext->ClearSelected();
294 void XGUI_Displayer::eraseAll(const bool isUpdateViewer)
296 Handle(AIS_InteractiveContext) aContext = AISContext();
297 if (aContext.IsNull())
300 foreach (AISObjectPtr aAISObj, myResult2AISObjectMap) {
302 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
304 aContext->Remove(anIO, false);
306 myResult2AISObjectMap.clear();
311 void XGUI_Displayer::eraseDeletedResults(const bool isUpdateViewer)
313 Handle(AIS_InteractiveContext) aContext = AISContext();
314 if (aContext.IsNull())
317 QObjectPtrList aRemoved;
318 foreach (ObjectPtr aFeature, myResult2AISObjectMap.keys()) {
319 if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
320 AISObjectPtr anObj = myResult2AISObjectMap[aFeature];
323 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
324 if (!anAIS.IsNull()) {
325 aContext->Remove(anAIS, false);
326 aRemoved.append(aFeature);
330 foreach(ObjectPtr aObj, aRemoved) {
331 myResult2AISObjectMap.remove(aObj);
338 void XGUI_Displayer::openLocalContext()
340 Handle(AIS_InteractiveContext) aContext = AISContext();
341 if (aContext.IsNull())
343 // Open local context if there is no one
344 if (!aContext->HasOpenedContext()) {
345 // Preserve selected objects
346 //AIS_ListOfInteractive aAisList;
347 //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
348 // aAisList.Append(aContext->Current());
350 // get the filters from the global context and append them to the local context
351 // a list of filters in the global context is not cleared and should be cleared here
352 SelectMgr_ListOfFilter aFilters;
353 aFilters.Assign(aContext->Filters());
354 // it is important to remove the filters in the global context, because there is a code
355 // in the closeLocalContex, which restore the global context filters
356 aContext->RemoveFilters();
358 aContext->ClearCurrents();
359 aContext->OpenLocalContext();
360 aContext->NotUseDisplayedObjects();
362 myUseExternalObjects = false;
363 myActiveSelectionModes.clear();
365 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
366 for (;aIt.More(); aIt.Next()) {
367 aContext->AddFilter(aIt.Value());
370 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
371 //for(; aIt2.More(); aIt2.Next()) {
372 // aContext->SetSelected(aIt2.Value(), false);
377 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
379 Handle(AIS_InteractiveContext) aContext = AISContext();
380 if ( (!aContext.IsNull()) && (aContext->HasOpenedContext()) ) {
381 // Preserve selected objects
382 //AIS_ListOfInteractive aAisList;
383 //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
384 // aAisList.Append(aContext->SelectedInteractive());
386 // get the filters from the local context and append them to the global context
387 // a list of filters in the local context is cleared
388 SelectMgr_ListOfFilter aFilters;
389 aFilters.Assign(aContext->Filters());
391 aContext->ClearSelected();
392 aContext->CloseAllContexts(false);
394 // Redisplay all object if they were displayed in localContext
395 Handle(AIS_InteractiveObject) aAISIO;
396 foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
397 aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
398 if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
399 aContext->Display(aAISIO, false);
400 aContext->SetDisplayMode(aAISIO, Shading, false);
404 // Append the filters from the local selection in the global selection context
405 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
406 for (;aIt.More(); aIt.Next()) {
407 Handle(SelectMgr_Filter) aFilter = aIt.Value();
408 aContext->AddFilter(aFilter);
413 myUseExternalObjects = false;
414 myActiveSelectionModes.clear();
417 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
418 //for(; aIt2.More(); aIt2.Next()) {
419 // if (aContext->IsDisplayed(aIt2.Value()))
420 // aContext->SetCurrentObject(aIt2.Value(), false);
425 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
428 if (myResult2AISObjectMap.contains(theObject))
429 anIO = myResult2AISObjectMap[theObject];
433 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
435 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
436 return getObject(aRefAIS);
439 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
442 foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
443 AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
444 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
451 void XGUI_Displayer::updateViewer()
453 Handle(AIS_InteractiveContext) aContext = AISContext();
454 if (!aContext.IsNull())
455 aContext->UpdateCurrentViewer();
458 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
460 return myWorkshop->viewer()->AISContext();
463 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
465 Handle(AIS_InteractiveContext) aContext = AISContext();
466 if (myAndFilter.IsNull() && !aContext.IsNull()) {
467 myAndFilter = new SelectMgr_AndFilter();
468 aContext->AddFilter(myAndFilter);
473 void XGUI_Displayer::displayAIS(AISObjectPtr theAIS, bool isUpdate)
475 Handle(AIS_InteractiveContext) aContext = AISContext();
476 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
477 if (!anAISIO.IsNull()) {
478 aContext->Display(anAISIO, isUpdate);
479 if (aContext->HasOpenedContext()) {
480 if (myUseExternalObjects) {
481 if (myActiveSelectionModes.size() == 0)
482 aContext->Activate(anAISIO);
484 foreach(int aMode, myActiveSelectionModes) {
485 aContext->Activate(anAISIO, aMode);
493 void XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool isUpdate)
495 Handle(AIS_InteractiveContext) aContext = AISContext();
496 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
497 if (!anAISIO.IsNull()) {
498 aContext->Remove(anAISIO, isUpdate);
502 void XGUI_Displayer::activateObjects(const QIntList& theModes)
504 Handle(AIS_InteractiveContext) aContext = AISContext();
505 // Open local context if there is no one
506 if (!aContext->HasOpenedContext())
509 aContext->UseDisplayedObjects();
510 myUseExternalObjects = true;
511 myActiveSelectionModes = theModes;
513 //Deactivate trihedron which can be activated in local selector
514 AIS_ListOfInteractive aPrsList;
515 aContext->DisplayedObjects(aPrsList, true);
517 Handle(AIS_Trihedron) aTrihedron;
518 AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
519 for(; aLIt.More(); aLIt.Next()){
520 aTrihedron = Handle(AIS_Trihedron)::DownCast(aLIt.Value());
521 if (!aTrihedron.IsNull()) {
522 aContext->Deactivate(aTrihedron);
527 //Activate all displayed objects with the module modes
528 //AIS_ListOfInteractive aPrsList;
529 //aContext->DisplayedObjects(aPrsList, true);
531 //AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
532 Handle(AIS_InteractiveObject) anAISIO;
533 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
534 anAISIO = aLIt.Value();
535 aTrihedron = Handle(AIS_Trihedron)::DownCast(anAISIO);
536 if (!aTrihedron.IsNull())
539 aContext->Load(anAISIO, -1, true);
540 if (theModes.size() == 0)
541 aContext->Activate(anAISIO);
543 foreach(int aMode, theModes) {
544 aContext->Activate(anAISIO, aMode);
551 void XGUI_Displayer::deactivateObjects()
553 Handle(AIS_InteractiveContext) aContext = AISContext();
554 // Open local context if there is no one
555 if (!aContext->HasOpenedContext())
558 aContext->NotUseDisplayedObjects();
562 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool toUpdate)
564 if (theMode == NoMode)
567 Handle(AIS_InteractiveContext) aContext = AISContext();
568 if (aContext.IsNull())
571 AISObjectPtr aAISObj = getAISObject(theObject);
575 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
576 aContext->SetDisplayMode(aAISIO, theMode, toUpdate);
579 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
581 Handle(AIS_InteractiveContext) aContext = AISContext();
582 if (aContext.IsNull())
585 AISObjectPtr aAISObj = getAISObject(theObject);
589 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
590 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
593 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
595 Handle(AIS_InteractiveContext) aContext = AISContext();
596 if (aContext.IsNull())
598 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
599 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
600 for (; aIt.More(); aIt.Next()) {
601 if (theFilter.Access() == aIt.Value().Access())
604 GetFilter()->Add(theFilter);
607 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
609 Handle(AIS_InteractiveContext) aContext = AISContext();
610 if (aContext.IsNull())
612 GetFilter()->Remove(theFilter);
615 void XGUI_Displayer::removeFilters()
617 Handle(AIS_InteractiveContext) aContext = AISContext();
618 if (aContext.IsNull())
620 GetFilter()->Clear();
623 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
625 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
626 foreach(ObjectPtr aObj, aDispList) {
627 if (!theList.contains(aObj))
630 foreach(ObjectPtr aObj, theList) {
631 if (!isVisible(aObj))
632 display(aObj, false);