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 aContext->Deactivate(anAIS);
195 if (theModes.size() > 0) {
196 foreach(int aMode, theModes) {
197 aContext->Activate(anAIS, aMode);
200 aContext->Activate(anAIS);
204 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
206 Handle(AIS_InteractiveContext) aContext = AISContext();
207 if (aContext.IsNull())
209 if (!isVisible(theObject))
212 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
213 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
215 TColStd_ListOfInteger aModes;
216 aContext->ActivatedModes(anAIS, aModes);
217 return aModes.Extent() > 0;
220 void XGUI_Displayer::stopSelection(const QObjectPtrList& theResults, const bool isStop,
221 const bool isUpdateViewer)
223 Handle(AIS_InteractiveContext) aContext = AISContext();
224 if (aContext.IsNull())
227 Handle(AIS_Shape) anAIS;
228 QObjectPtrList::const_iterator anIt = theResults.begin(), aLast = theResults.end();
230 for (; anIt != aLast; anIt++) {
232 if (isVisible(aFeature))
233 anAIS = Handle(AIS_Shape)::DownCast(
234 myResult2AISObjectMap[aFeature]->impl<Handle(AIS_InteractiveObject)>());
239 QColor aColor(Qt::white);
241 Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
245 QColor aColor(Qt::red);
247 Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
256 void XGUI_Displayer::setSelected(const QObjectPtrList& theResults, const bool isUpdateViewer)
258 Handle(AIS_InteractiveContext) aContext = AISContext();
259 if (aContext.IsNull())
261 if (aContext->HasOpenedContext()) {
262 aContext->UnhilightSelected();
263 aContext->ClearSelected();
264 foreach(ObjectPtr aResult, theResults) {
265 if (isVisible(aResult)) {
266 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
267 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
269 aContext->SetSelected(anAIS, false);
273 aContext->UnhilightCurrents();
274 aContext->ClearCurrents();
275 foreach(ObjectPtr aResult, theResults) {
276 if (isVisible(aResult)) {
277 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
278 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
280 aContext->SetCurrentObject(anAIS, false);
289 void XGUI_Displayer::clearSelected()
291 Handle(AIS_InteractiveContext) aContext = AISContext();
293 aContext->UnhilightCurrents(false);
294 aContext->ClearSelected();
298 void XGUI_Displayer::eraseAll(const bool isUpdateViewer)
300 Handle(AIS_InteractiveContext) aContext = AISContext();
301 if (aContext.IsNull())
304 foreach (AISObjectPtr aAISObj, myResult2AISObjectMap) {
306 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
308 aContext->Remove(anIO, false);
310 myResult2AISObjectMap.clear();
315 void XGUI_Displayer::eraseDeletedResults(const bool isUpdateViewer)
317 Handle(AIS_InteractiveContext) aContext = AISContext();
318 if (aContext.IsNull())
321 QObjectPtrList aRemoved;
322 foreach (ObjectPtr aFeature, myResult2AISObjectMap.keys()) {
323 if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
324 AISObjectPtr anObj = myResult2AISObjectMap[aFeature];
327 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
328 if (!anAIS.IsNull()) {
329 aContext->Remove(anAIS, false);
330 aRemoved.append(aFeature);
334 foreach(ObjectPtr aObj, aRemoved) {
335 myResult2AISObjectMap.remove(aObj);
342 void XGUI_Displayer::openLocalContext()
344 Handle(AIS_InteractiveContext) aContext = AISContext();
345 if (aContext.IsNull())
347 // Open local context if there is no one
348 if (!aContext->HasOpenedContext()) {
349 // Preserve selected objects
350 //AIS_ListOfInteractive aAisList;
351 //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
352 // aAisList.Append(aContext->Current());
354 // get the filters from the global context and append them to the local context
355 // a list of filters in the global context is not cleared and should be cleared here
356 SelectMgr_ListOfFilter aFilters;
357 aFilters.Assign(aContext->Filters());
358 // it is important to remove the filters in the global context, because there is a code
359 // in the closeLocalContex, which restore the global context filters
360 aContext->RemoveFilters();
362 //aContext->ClearCurrents();
363 aContext->OpenLocalContext();
365 qDebug("### Open Local context");
367 aContext->NotUseDisplayedObjects();
369 myUseExternalObjects = false;
370 myActiveSelectionModes.clear();
372 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
373 for (;aIt.More(); aIt.Next()) {
374 aContext->AddFilter(aIt.Value());
377 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
378 //for(; aIt2.More(); aIt2.Next()) {
379 // aContext->SetSelected(aIt2.Value(), false);
384 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
386 Handle(AIS_InteractiveContext) aContext = AISContext();
387 if ( (!aContext.IsNull()) && (aContext->HasOpenedContext()) ) {
388 // Preserve selected objects
389 //AIS_ListOfInteractive aAisList;
390 //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
391 // aAisList.Append(aContext->SelectedInteractive());
393 // get the filters from the local context and append them to the global context
394 // a list of filters in the local context is cleared
395 SelectMgr_ListOfFilter aFilters;
396 aFilters.Assign(aContext->Filters());
398 //aContext->ClearSelected();
399 aContext->CloseAllContexts(false);
401 qDebug("### Close Local context");
403 // Redisplay all object if they were displayed in localContext
404 Handle(AIS_InteractiveObject) aAISIO;
405 foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
406 aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
407 if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
408 aContext->Display(aAISIO, false);
409 aContext->SetDisplayMode(aAISIO, Shading, false);
413 // Append the filters from the local selection in the global selection context
414 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
415 for (;aIt.More(); aIt.Next()) {
416 Handle(SelectMgr_Filter) aFilter = aIt.Value();
417 aContext->AddFilter(aFilter);
422 myUseExternalObjects = false;
423 myActiveSelectionModes.clear();
426 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
427 //for(; aIt2.More(); aIt2.Next()) {
428 // if (aContext->IsDisplayed(aIt2.Value()))
429 // aContext->SetCurrentObject(aIt2.Value(), false);
434 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
437 if (myResult2AISObjectMap.contains(theObject))
438 anIO = myResult2AISObjectMap[theObject];
442 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
444 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
445 return getObject(aRefAIS);
448 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
451 foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
452 AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
453 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
460 void XGUI_Displayer::updateViewer()
462 Handle(AIS_InteractiveContext) aContext = AISContext();
463 if (!aContext.IsNull())
464 aContext->UpdateCurrentViewer();
467 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
469 return myWorkshop->viewer()->AISContext();
472 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
474 Handle(AIS_InteractiveContext) aContext = AISContext();
475 if (myAndFilter.IsNull() && !aContext.IsNull()) {
476 myAndFilter = new SelectMgr_AndFilter();
477 aContext->AddFilter(myAndFilter);
482 void XGUI_Displayer::displayAIS(AISObjectPtr theAIS, bool isUpdate)
484 Handle(AIS_InteractiveContext) aContext = AISContext();
485 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
486 if (!anAISIO.IsNull()) {
487 aContext->Display(anAISIO, isUpdate);
488 if (aContext->HasOpenedContext()) {
489 if (myUseExternalObjects) {
490 aContext->Deactivate(anAISIO);
491 if (myActiveSelectionModes.size() == 0)
492 aContext->Activate(anAISIO);
494 foreach(int aMode, myActiveSelectionModes) {
495 aContext->Activate(anAISIO, aMode);
503 void XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool isUpdate)
505 Handle(AIS_InteractiveContext) aContext = AISContext();
506 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
507 if (!anAISIO.IsNull()) {
508 aContext->Remove(anAISIO, isUpdate);
512 void XGUI_Displayer::activateObjects(const QIntList& theModes)
514 Handle(AIS_InteractiveContext) aContext = AISContext();
515 // Open local context if there is no one
516 if (!aContext->HasOpenedContext())
519 aContext->UseDisplayedObjects();
520 myUseExternalObjects = true;
521 myActiveSelectionModes = theModes;
523 //Deactivate trihedron which can be activated in local selector
524 AIS_ListOfInteractive aPrsList;
525 aContext->DisplayedObjects(aPrsList, true);
527 AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
528 Handle(AIS_InteractiveObject) anAISIO;
529 Handle(AIS_Trihedron) aTrihedron;
530 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
531 anAISIO = aLIt.Value();
532 aTrihedron = Handle(AIS_Trihedron)::DownCast(anAISIO);
533 aContext->Deactivate(anAISIO);
534 if (aTrihedron.IsNull()) {
535 aContext->Load(anAISIO, -1, true);
536 if (theModes.size() == 0)
537 aContext->Activate(anAISIO);
539 foreach(int aMode, theModes) {
540 aContext->Activate(anAISIO, aMode);
548 void XGUI_Displayer::deactivateObjects()
550 Handle(AIS_InteractiveContext) aContext = AISContext();
551 // Open local context if there is no one
552 if (!aContext->HasOpenedContext())
555 aContext->NotUseDisplayedObjects();
559 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool toUpdate)
561 if (theMode == NoMode)
564 Handle(AIS_InteractiveContext) aContext = AISContext();
565 if (aContext.IsNull())
568 AISObjectPtr aAISObj = getAISObject(theObject);
572 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
573 aContext->SetDisplayMode(aAISIO, theMode, toUpdate);
576 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
578 Handle(AIS_InteractiveContext) aContext = AISContext();
579 if (aContext.IsNull())
582 AISObjectPtr aAISObj = getAISObject(theObject);
586 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
587 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
590 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
592 Handle(AIS_InteractiveContext) aContext = AISContext();
593 if (aContext.IsNull())
595 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
596 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
597 for (; aIt.More(); aIt.Next()) {
598 if (theFilter.Access() == aIt.Value().Access())
601 GetFilter()->Add(theFilter);
604 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
606 Handle(AIS_InteractiveContext) aContext = AISContext();
607 if (aContext.IsNull())
609 GetFilter()->Remove(theFilter);
612 void XGUI_Displayer::removeFilters()
614 Handle(AIS_InteractiveContext) aContext = AISContext();
615 if (aContext.IsNull())
617 GetFilter()->Clear();
620 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
622 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
623 foreach(ObjectPtr aObj, aDispList) {
624 if (!theList.contains(aObj))
627 foreach(ObjectPtr aObj, theList) {
628 if (!isVisible(aObj))
629 display(aObj, false);