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>
23 #include <AIS_InteractiveContext.hxx>
24 #include <AIS_LocalContext.hxx>
25 #include <AIS_ListOfInteractive.hxx>
26 #include <AIS_ListIteratorOfListOfInteractive.hxx>
27 #include <AIS_DimensionSelectionMode.hxx>
28 #include <AIS_Shape.hxx>
29 #include <AIS_Dimension.hxx>
30 #include <TColStd_ListIteratorOfListOfInteger.hxx>
31 #include <SelectMgr_ListOfFilter.hxx>
32 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
36 const int MOUSE_SENSITIVITY_IN_PIXEL = 10; ///< defines the local context mouse selection sensitivity
38 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
39 : myUseExternalObjects(false), myWorkshop(theWorkshop)
43 XGUI_Displayer::~XGUI_Displayer()
47 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
49 return myResult2AISObjectMap.contains(theObject);
52 void XGUI_Displayer::display(ObjectPtr theObject, bool isUpdateViewer)
54 if (isVisible(theObject)) {
55 redisplay(theObject, isUpdateViewer);
59 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
60 bool isShading = false;
62 anAIS = aPrs->getAISObject(AISObjectPtr());
64 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
66 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
68 anAIS = AISObjectPtr(new GeomAPI_AISObject());
69 anAIS->setImpl(new Handle(AIS_InteractiveObject)(new ModuleBase_ResultPrs(aResult)));
70 //anAIS->createShape(aShapePtr);
76 display(theObject, anAIS, isShading, isUpdateViewer);
80 void XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
81 bool isShading, bool isUpdateViewer)
83 Handle(AIS_InteractiveContext) aContext = AISContext();
84 if (aContext.IsNull())
87 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
88 if (!anAISIO.IsNull()) {
89 myResult2AISObjectMap[theObject] = theAIS;
90 aContext->Display(anAISIO, false);
91 aContext->SetDisplayMode(anAISIO, isShading? Shading : Wireframe, isUpdateViewer);
92 if (aContext->HasOpenedContext()) {
93 if (myUseExternalObjects) {
94 if (myActiveSelectionModes.size() == 0)
95 aContext->Activate(anAISIO);
97 foreach(int aMode, myActiveSelectionModes) {
98 aContext->Activate(anAISIO, aMode);
106 void XGUI_Displayer::erase(ObjectPtr theObject, const bool isUpdateViewer)
108 if (!isVisible(theObject))
111 Handle(AIS_InteractiveContext) aContext = AISContext();
112 if (aContext.IsNull())
114 AISObjectPtr anObject = myResult2AISObjectMap[theObject];
116 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
117 if (!anAIS.IsNull()) {
118 aContext->Remove(anAIS, isUpdateViewer);
121 myResult2AISObjectMap.remove(theObject);
124 void XGUI_Displayer::redisplay(ObjectPtr theObject, bool isUpdateViewer)
126 if (!isVisible(theObject))
129 AISObjectPtr aAISObj = getAISObject(theObject);
130 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
132 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
134 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
136 erase(theObject, isUpdateViewer);
139 if (aAIS_Obj != aAISObj) {
140 myResult2AISObjectMap[theObject] = aAIS_Obj;
142 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
145 if (!aAISIO.IsNull()) {
146 Handle(AIS_InteractiveContext) aContext = AISContext();
147 if (aContext.IsNull())
149 aContext->Redisplay(aAISIO, isUpdateViewer);
153 void XGUI_Displayer::deactivate(ObjectPtr theObject)
155 if (isVisible(theObject)) {
156 Handle(AIS_InteractiveContext) aContext = AISContext();
157 if (aContext.IsNull())
160 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
161 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
162 aContext->Deactivate(anAIS);
166 void XGUI_Displayer::activate(ObjectPtr theFeature)
169 activate(theFeature, aModes);
172 void XGUI_Displayer::activate(ObjectPtr theObject, const QIntList& theModes)
174 if (isVisible(theObject)) {
175 Handle(AIS_InteractiveContext) aContext = AISContext();
176 if (aContext.IsNull())
179 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
180 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
181 if (aContext->HasOpenedContext()) {
182 aContext->Load(anAIS, -1, true);
184 if (theModes.size() > 0) {
185 foreach(int aMode, theModes) {
186 aContext->Activate(anAIS, aMode);
189 aContext->Activate(anAIS);
193 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
195 Handle(AIS_InteractiveContext) aContext = AISContext();
196 if (aContext.IsNull())
198 if (!isVisible(theObject))
201 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
202 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
204 TColStd_ListOfInteger aModes;
205 aContext->ActivatedModes(anAIS, aModes);
206 return aModes.Extent() > 0;
209 void XGUI_Displayer::stopSelection(const QObjectPtrList& theResults, const bool isStop,
210 const bool isUpdateViewer)
212 Handle(AIS_InteractiveContext) aContext = AISContext();
213 if (aContext.IsNull())
216 Handle(AIS_Shape) anAIS;
217 QObjectPtrList::const_iterator anIt = theResults.begin(), aLast = theResults.end();
219 for (; anIt != aLast; anIt++) {
221 if (isVisible(aFeature))
222 anAIS = Handle(AIS_Shape)::DownCast(
223 myResult2AISObjectMap[aFeature]->impl<Handle(AIS_InteractiveObject)>());
228 QColor aColor(Qt::white);
230 Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
234 QColor aColor(Qt::red);
236 Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
245 void XGUI_Displayer::setSelected(const QObjectPtrList& theResults, const bool isUpdateViewer)
247 Handle(AIS_InteractiveContext) aContext = AISContext();
248 if (aContext.IsNull())
250 if (aContext->HasOpenedContext()) {
251 aContext->UnhilightSelected();
252 aContext->ClearSelected();
253 foreach(ObjectPtr aResult, theResults) {
254 if (isVisible(aResult)) {
255 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
256 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
258 aContext->SetSelected(anAIS, false);
262 aContext->UnhilightCurrents();
263 aContext->ClearCurrents();
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->SetCurrentObject(anAIS, false);
278 void XGUI_Displayer::clearSelected()
280 Handle(AIS_InteractiveContext) aContext = AISContext();
282 aContext->UnhilightCurrents(false);
283 aContext->ClearSelected();
287 void XGUI_Displayer::eraseAll(const bool isUpdateViewer)
289 Handle(AIS_InteractiveContext) aContext = AISContext();
290 if (aContext.IsNull())
293 foreach (AISObjectPtr aAISObj, myResult2AISObjectMap) {
295 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
297 aContext->Remove(anIO, false);
299 myResult2AISObjectMap.clear();
304 void XGUI_Displayer::eraseDeletedResults(const bool isUpdateViewer)
306 Handle(AIS_InteractiveContext) aContext = AISContext();
307 if (aContext.IsNull())
310 QObjectPtrList aRemoved;
311 foreach (ObjectPtr aFeature, myResult2AISObjectMap.keys()) {
312 if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
313 AISObjectPtr anObj = myResult2AISObjectMap[aFeature];
316 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
317 if (!anAIS.IsNull()) {
318 aContext->Remove(anAIS, false);
319 aRemoved.append(aFeature);
323 foreach(ObjectPtr aObj, aRemoved) {
324 myResult2AISObjectMap.remove(aObj);
331 void XGUI_Displayer::openLocalContext()
333 Handle(AIS_InteractiveContext) aContext = AISContext();
334 if (aContext.IsNull())
336 // Open local context if there is no one
337 if (!aContext->HasOpenedContext()) {
338 // Preserve selected objects
339 //AIS_ListOfInteractive aAisList;
340 //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
341 // aAisList.Append(aContext->Current());
343 // get the filters from the global context and append them to the local context
344 // a list of filters in the global context is not cleared and should be cleared here
345 SelectMgr_ListOfFilter aFilters;
346 aFilters.Assign(aContext->Filters());
347 // it is important to remove the filters in the global context, because there is a code
348 // in the closeLocalContex, which restore the global context filters
349 aContext->RemoveFilters();
351 aContext->ClearCurrents();
352 aContext->OpenLocalContext();
353 aContext->NotUseDisplayedObjects();
355 myUseExternalObjects = false;
356 myActiveSelectionModes.clear();
358 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
359 for (;aIt.More(); aIt.Next()) {
360 aContext->AddFilter(aIt.Value());
363 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
364 //for(; aIt2.More(); aIt2.Next()) {
365 // aContext->SetSelected(aIt2.Value(), false);
370 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
372 Handle(AIS_InteractiveContext) aContext = AISContext();
373 if ( (!aContext.IsNull()) && (aContext->HasOpenedContext()) ) {
374 // Preserve selected objects
375 //AIS_ListOfInteractive aAisList;
376 //for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
377 // aAisList.Append(aContext->SelectedInteractive());
379 // get the filters from the local context and append them to the global context
380 // a list of filters in the local context is cleared
381 SelectMgr_ListOfFilter aFilters;
382 aFilters.Assign(aContext->Filters());
384 aContext->ClearSelected();
385 aContext->CloseAllContexts(false);
387 // Redisplay all object if they were displayed in localContext
388 Handle(AIS_InteractiveObject) aAISIO;
389 foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
390 aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
391 if (aContext->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
392 aContext->Display(aAISIO, false);
393 aContext->SetDisplayMode(aAISIO, Shading, false);
397 // Append the filters from the local selection in the global selection context
398 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
399 for (;aIt.More(); aIt.Next()) {
400 Handle(SelectMgr_Filter) aFilter = aIt.Value();
401 aContext->AddFilter(aFilter);
406 myUseExternalObjects = false;
407 myActiveSelectionModes.clear();
410 //AIS_ListIteratorOfListOfInteractive aIt2(aAisList);
411 //for(; aIt2.More(); aIt2.Next()) {
412 // if (aContext->IsDisplayed(aIt2.Value()))
413 // aContext->SetCurrentObject(aIt2.Value(), false);
418 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
421 if (myResult2AISObjectMap.contains(theObject))
422 anIO = myResult2AISObjectMap[theObject];
426 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
428 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
429 return getObject(aRefAIS);
432 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
435 foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
436 AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
437 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
444 void XGUI_Displayer::updateViewer()
446 Handle(AIS_InteractiveContext) aContext = AISContext();
447 if (!aContext.IsNull())
448 aContext->UpdateCurrentViewer();
451 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
453 return myWorkshop->viewer()->AISContext();
456 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
458 Handle(AIS_InteractiveContext) aContext = AISContext();
459 if (myAndFilter.IsNull() && !aContext.IsNull()) {
460 myAndFilter = new SelectMgr_AndFilter();
461 aContext->AddFilter(myAndFilter);
466 void XGUI_Displayer::displayAIS(AISObjectPtr theAIS, bool isUpdate)
468 Handle(AIS_InteractiveContext) aContext = AISContext();
469 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
470 if (!anAISIO.IsNull()) {
471 aContext->Display(anAISIO, isUpdate);
472 if (aContext->HasOpenedContext()) {
473 if (myUseExternalObjects) {
474 if (myActiveSelectionModes.size() == 0)
475 aContext->Activate(anAISIO);
477 foreach(int aMode, myActiveSelectionModes) {
478 aContext->Activate(anAISIO, aMode);
486 void XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool isUpdate)
488 Handle(AIS_InteractiveContext) aContext = AISContext();
489 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
490 if (!anAISIO.IsNull()) {
491 aContext->Remove(anAISIO, isUpdate);
495 void XGUI_Displayer::activateObjects(const QIntList& theModes)
497 Handle(AIS_InteractiveContext) aContext = AISContext();
498 // Open local context if there is no one
499 if (!aContext->HasOpenedContext())
502 aContext->UseDisplayedObjects();
503 myUseExternalObjects = true;
504 myActiveSelectionModes = theModes;
506 //Deactivate trihedron which can be activated in local selector
507 AIS_ListOfInteractive aPrsList;
508 aContext->DisplayedObjects(aPrsList, true);
510 Handle(AIS_Trihedron) aTrihedron;
511 AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
512 for(; aLIt.More(); aLIt.Next()){
513 aTrihedron = Handle(AIS_Trihedron)::DownCast(aLIt.Value());
514 if (!aTrihedron.IsNull()) {
515 aContext->Deactivate(aTrihedron);
520 //Activate all displayed objects with the module modes
521 //AIS_ListOfInteractive aPrsList;
522 //aContext->DisplayedObjects(aPrsList, true);
524 //AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
525 Handle(AIS_InteractiveObject) anAISIO;
526 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
527 anAISIO = aLIt.Value();
528 aTrihedron = Handle(AIS_Trihedron)::DownCast(anAISIO);
529 if (!aTrihedron.IsNull())
532 aContext->Load(anAISIO, -1, true);
533 if (theModes.size() == 0)
534 aContext->Activate(anAISIO);
536 foreach(int aMode, theModes) {
537 aContext->Activate(anAISIO, aMode);
544 void XGUI_Displayer::deactivateObjects()
546 Handle(AIS_InteractiveContext) aContext = AISContext();
547 // Open local context if there is no one
548 if (!aContext->HasOpenedContext())
551 aContext->NotUseDisplayedObjects();
555 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool toUpdate)
557 if (theMode == NoMode)
560 Handle(AIS_InteractiveContext) aContext = AISContext();
561 if (aContext.IsNull())
564 AISObjectPtr aAISObj = getAISObject(theObject);
568 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
569 aContext->SetDisplayMode(aAISIO, theMode, toUpdate);
572 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
574 Handle(AIS_InteractiveContext) aContext = AISContext();
575 if (aContext.IsNull())
578 AISObjectPtr aAISObj = getAISObject(theObject);
582 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
583 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
586 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
588 Handle(AIS_InteractiveContext) aContext = AISContext();
589 if (aContext.IsNull())
591 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
592 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
593 for (; aIt.More(); aIt.Next()) {
594 if (theFilter.Access() == aIt.Value().Access())
597 GetFilter()->Add(theFilter);
600 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
602 Handle(AIS_InteractiveContext) aContext = AISContext();
603 if (aContext.IsNull())
605 GetFilter()->Remove(theFilter);
608 void XGUI_Displayer::removeFilters()
610 Handle(AIS_InteractiveContext) aContext = AISContext();
611 if (aContext.IsNull())
613 GetFilter()->Clear();
616 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
618 QObjectPtrList aDispList = myResult2AISObjectMap.keys();
619 foreach(ObjectPtr aObj, aDispList) {
620 if (!theList.contains(aObj))
623 foreach(ObjectPtr aObj, theList) {
624 if (!isVisible(aObj))
625 display(aObj, false);