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 aContext->Display(anAISIO, false);
92 aContext->SetDisplayMode(anAISIO, isShading? Shading : Wireframe, false);
94 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
95 if (aFeature.get() != NULL) {
96 GeomCustomPrsPtr aCustPrs = std::dynamic_pointer_cast<GeomAPI_ICustomPrs>(aFeature);
97 if (aCustPrs.get() != NULL)
98 aCustPrs->customisePresentation(theAIS);
100 if (aContext->HasOpenedContext()) {
101 if (myUseExternalObjects) {
102 if (myActiveSelectionModes.size() == 0)
103 aContext->Activate(anAISIO);
105 foreach(int aMode, myActiveSelectionModes) {
106 aContext->Activate(anAISIO, aMode);
116 void XGUI_Displayer::erase(ObjectPtr theObject, const bool isUpdateViewer)
118 if (!isVisible(theObject))
121 Handle(AIS_InteractiveContext) aContext = AISContext();
122 if (aContext.IsNull())
124 AISObjectPtr anObject = myResult2AISObjectMap[theObject];
126 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
127 if (!anAIS.IsNull()) {
128 aContext->Remove(anAIS, isUpdateViewer);
131 myResult2AISObjectMap.remove(theObject);
134 void XGUI_Displayer::redisplay(ObjectPtr theObject, bool isUpdateViewer)
136 if (!isVisible(theObject))
139 AISObjectPtr aAISObj = getAISObject(theObject);
140 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
142 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
144 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
146 erase(theObject, isUpdateViewer);
149 if (aAIS_Obj != aAISObj) {
150 myResult2AISObjectMap[theObject] = aAIS_Obj;
152 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
155 if (!aAISIO.IsNull()) {
156 Handle(AIS_InteractiveContext) aContext = AISContext();
157 if (aContext.IsNull())
159 aContext->Redisplay(aAISIO, isUpdateViewer);
163 void XGUI_Displayer::deactivate(ObjectPtr theObject)
165 if (isVisible(theObject)) {
166 Handle(AIS_InteractiveContext) aContext = AISContext();
167 if (aContext.IsNull())
170 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
171 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
172 aContext->Deactivate(anAIS);
176 void XGUI_Displayer::activate(ObjectPtr theFeature)
179 activate(theFeature, aModes);
182 void XGUI_Displayer::activate(ObjectPtr theObject, const QIntList& theModes)
184 if (isVisible(theObject)) {
185 Handle(AIS_InteractiveContext) aContext = AISContext();
186 if (aContext.IsNull())
189 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
190 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
191 if (aContext->HasOpenedContext()) {
192 aContext->Load(anAIS, -1, true);
194 if (theModes.size() > 0) {
195 foreach(int aMode, theModes) {
196 aContext->Activate(anAIS, aMode);
199 aContext->Activate(anAIS);
203 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
205 Handle(AIS_InteractiveContext) aContext = AISContext();
206 if (aContext.IsNull())
208 if (!isVisible(theObject))
211 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
212 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
214 TColStd_ListOfInteger aModes;
215 aContext->ActivatedModes(anAIS, aModes);
216 return aModes.Extent() > 0;
219 void XGUI_Displayer::stopSelection(const QObjectPtrList& theResults, const bool isStop,
220 const bool isUpdateViewer)
222 Handle(AIS_InteractiveContext) aContext = AISContext();
223 if (aContext.IsNull())
226 Handle(AIS_Shape) anAIS;
227 QObjectPtrList::const_iterator anIt = theResults.begin(), aLast = theResults.end();
229 for (; anIt != aLast; anIt++) {
231 if (isVisible(aFeature))
232 anAIS = Handle(AIS_Shape)::DownCast(
233 myResult2AISObjectMap[aFeature]->impl<Handle(AIS_InteractiveObject)>());
238 QColor aColor(Qt::white);
240 Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
244 QColor aColor(Qt::red);
246 Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
255 void XGUI_Displayer::setSelected(const QObjectPtrList& theResults, const bool isUpdateViewer)
257 Handle(AIS_InteractiveContext) aContext = AISContext();
258 if (aContext.IsNull())
260 if (aContext->HasOpenedContext()) {
261 aContext->UnhilightSelected();
262 aContext->ClearSelected();
263 foreach(ObjectPtr aResult, theResults) {
264 if (isVisible(aResult)) {
265 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
266 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
268 aContext->SetSelected(anAIS, false);
272 aContext->UnhilightCurrents();
273 aContext->ClearCurrents();
274 foreach(ObjectPtr aResult, theResults) {
275 if (isVisible(aResult)) {
276 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
277 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
279 aContext->SetCurrentObject(anAIS, false);
288 void XGUI_Displayer::clearSelected()
290 Handle(AIS_InteractiveContext) aContext = AISContext();
292 aContext->UnhilightCurrents(false);
293 aContext->ClearSelected();
297 void XGUI_Displayer::eraseAll(const bool isUpdateViewer)
299 Handle(AIS_InteractiveContext) aContext = AISContext();
300 if (aContext.IsNull())
303 foreach (AISObjectPtr aAISObj, myResult2AISObjectMap) {
305 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
307 aContext->Remove(anIO, false);
309 myResult2AISObjectMap.clear();
314 void XGUI_Displayer::eraseDeletedResults(const bool isUpdateViewer)
316 Handle(AIS_InteractiveContext) aContext = AISContext();
317 if (aContext.IsNull())
320 QObjectPtrList aRemoved;
321 foreach (ObjectPtr aFeature, myResult2AISObjectMap.keys()) {
322 if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
323 AISObjectPtr anObj = myResult2AISObjectMap[aFeature];
326 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
327 if (!anAIS.IsNull()) {
328 aContext->Remove(anAIS, false);
329 aRemoved.append(aFeature);
333 foreach(ObjectPtr aObj, aRemoved) {
334 myResult2AISObjectMap.remove(aObj);
341 void XGUI_Displayer::openLocalContext()
343 Handle(AIS_InteractiveContext) aContext = AISContext();
344 if (aContext.IsNull())
346 // Open local context if there is no one
347 if (!aContext->HasOpenedContext()) {
348 // Preserve selected objects
349 //AIS_ListOfInteractive aAisList;
350 //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
351 // aAisList.Append(aContext->Current());
353 // get the filters from the global context and append them to the local context
354 // a list of filters in the global context is not cleared and should be cleared here
355 SelectMgr_ListOfFilter aFilters;
356 aFilters.Assign(aContext->Filters());
357 // it is important to remove the filters in the global context, because there is a code
358 // in the closeLocalContex, which restore the global context filters
359 aContext->RemoveFilters();
361 aContext->ClearCurrents();
362 aContext->OpenLocalContext();
363 aContext->NotUseDisplayedObjects();
365 myUseExternalObjects = false;
366 myActiveSelectionModes.clear();
368 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
369 for (;aIt.More(); aIt.Next()) {
370 aContext->AddFilter(aIt.Value());
373 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
374 //for(; aIt2.More(); aIt2.Next()) {
375 // aContext->SetSelected(aIt2.Value(), false);
380 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
382 Handle(AIS_InteractiveContext) aContext = AISContext();
383 if ( (!aContext.IsNull()) && (aContext->HasOpenedContext()) ) {
384 // Preserve selected objects
385 //AIS_ListOfInteractive aAisList;
386 //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
387 // aAisList.Append(aContext->SelectedInteractive());
389 // get the filters from the local context and append them to the global context
390 // a list of filters in the local context is cleared
391 SelectMgr_ListOfFilter aFilters;
392 aFilters.Assign(aContext->Filters());
394 aContext->ClearSelected();
395 aContext->CloseAllContexts(false);
397 // Redisplay all object if they were displayed in localContext
398 Handle(AIS_InteractiveObject) aAISIO;
399 foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
400 aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
401 if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
402 aContext->Display(aAISIO, false);
403 aContext->SetDisplayMode(aAISIO, Shading, false);
407 // Append the filters from the local selection in the global selection context
408 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
409 for (;aIt.More(); aIt.Next()) {
410 Handle(SelectMgr_Filter) aFilter = aIt.Value();
411 aContext->AddFilter(aFilter);
416 myUseExternalObjects = false;
417 myActiveSelectionModes.clear();
420 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
421 //for(; aIt2.More(); aIt2.Next()) {
422 // if (aContext->IsDisplayed(aIt2.Value()))
423 // aContext->SetCurrentObject(aIt2.Value(), false);
428 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
431 if (myResult2AISObjectMap.contains(theObject))
432 anIO = myResult2AISObjectMap[theObject];
436 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
438 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
439 return getObject(aRefAIS);
442 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
445 foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
446 AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
447 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
454 void XGUI_Displayer::updateViewer()
456 Handle(AIS_InteractiveContext) aContext = AISContext();
457 if (!aContext.IsNull())
458 aContext->UpdateCurrentViewer();
461 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
463 return myWorkshop->viewer()->AISContext();
466 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
468 Handle(AIS_InteractiveContext) aContext = AISContext();
469 if (myAndFilter.IsNull() && !aContext.IsNull()) {
470 myAndFilter = new SelectMgr_AndFilter();
471 aContext->AddFilter(myAndFilter);
476 void XGUI_Displayer::displayAIS(AISObjectPtr theAIS, bool isUpdate)
478 Handle(AIS_InteractiveContext) aContext = AISContext();
479 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
480 if (!anAISIO.IsNull()) {
481 aContext->Display(anAISIO, isUpdate);
482 if (aContext->HasOpenedContext()) {
483 if (myUseExternalObjects) {
484 if (myActiveSelectionModes.size() == 0)
485 aContext->Activate(anAISIO);
487 foreach(int aMode, myActiveSelectionModes) {
488 aContext->Activate(anAISIO, aMode);
496 void XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool isUpdate)
498 Handle(AIS_InteractiveContext) aContext = AISContext();
499 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
500 if (!anAISIO.IsNull()) {
501 aContext->Remove(anAISIO, isUpdate);
505 void XGUI_Displayer::activateObjects(const QIntList& theModes)
507 Handle(AIS_InteractiveContext) aContext = AISContext();
508 // Open local context if there is no one
509 if (!aContext->HasOpenedContext())
512 aContext->UseDisplayedObjects();
513 myUseExternalObjects = true;
514 myActiveSelectionModes = theModes;
516 //Deactivate trihedron which can be activated in local selector
517 AIS_ListOfInteractive aPrsList;
518 aContext->DisplayedObjects(aPrsList, true);
520 Handle(AIS_Trihedron) aTrihedron;
521 AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
522 for(; aLIt.More(); aLIt.Next()){
523 aTrihedron = Handle(AIS_Trihedron)::DownCast(aLIt.Value());
524 if (!aTrihedron.IsNull()) {
525 aContext->Deactivate(aTrihedron);
530 //Activate all displayed objects with the module modes
531 //AIS_ListOfInteractive aPrsList;
532 //aContext->DisplayedObjects(aPrsList, true);
534 //AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
535 Handle(AIS_InteractiveObject) anAISIO;
536 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
537 anAISIO = aLIt.Value();
538 aTrihedron = Handle(AIS_Trihedron)::DownCast(anAISIO);
539 if (!aTrihedron.IsNull())
542 aContext->Load(anAISIO, -1, true);
543 if (theModes.size() == 0)
544 aContext->Activate(anAISIO);
546 foreach(int aMode, theModes) {
547 aContext->Activate(anAISIO, aMode);
554 void XGUI_Displayer::deactivateObjects()
556 Handle(AIS_InteractiveContext) aContext = AISContext();
557 // Open local context if there is no one
558 if (!aContext->HasOpenedContext())
561 aContext->NotUseDisplayedObjects();
565 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool toUpdate)
567 if (theMode == NoMode)
570 Handle(AIS_InteractiveContext) aContext = AISContext();
571 if (aContext.IsNull())
574 AISObjectPtr aAISObj = getAISObject(theObject);
578 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
579 aContext->SetDisplayMode(aAISIO, theMode, toUpdate);
582 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
584 Handle(AIS_InteractiveContext) aContext = AISContext();
585 if (aContext.IsNull())
588 AISObjectPtr aAISObj = getAISObject(theObject);
592 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
593 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
596 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
598 Handle(AIS_InteractiveContext) aContext = AISContext();
599 if (aContext.IsNull())
601 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
602 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
603 for (; aIt.More(); aIt.Next()) {
604 if (theFilter.Access() == aIt.Value().Access())
607 GetFilter()->Add(theFilter);
610 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
612 Handle(AIS_InteractiveContext) aContext = AISContext();
613 if (aContext.IsNull())
615 GetFilter()->Remove(theFilter);
618 void XGUI_Displayer::removeFilters()
620 Handle(AIS_InteractiveContext) aContext = AISContext();
621 if (aContext.IsNull())
623 GetFilter()->Clear();
626 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
628 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
629 foreach(ObjectPtr aObj, aDispList) {
630 if (!theList.contains(aObj))
633 foreach(ObjectPtr aObj, theList) {
634 if (!isVisible(aObj))
635 display(aObj, false);