1 // File: XGUI_Displayer.cpp
2 // Created: 20 Apr 2014
3 // Author: Natalia ERMOLAEVA
5 #include "XGUI_Displayer.h"
6 #include "XGUI_Viewer.h"
7 #include "XGUI_Workshop.h"
8 #include "XGUI_ViewerProxy.h"
10 #include <ModelAPI_Document.h>
11 #include <ModelAPI_Data.h>
12 #include <ModelAPI_Object.h>
13 #include <ModelAPI_Tools.h>
15 #include <ModuleBase_ResultPrs.h>
17 #include <GeomAPI_Shape.h>
18 #include <GeomAPI_IPresentable.h>
20 #include <AIS_InteractiveContext.hxx>
21 #include <AIS_LocalContext.hxx>
22 #include <AIS_ListOfInteractive.hxx>
23 #include <AIS_ListIteratorOfListOfInteractive.hxx>
24 #include <AIS_DimensionSelectionMode.hxx>
25 #include <AIS_Shape.hxx>
26 #include <AIS_Dimension.hxx>
27 #include <TColStd_ListIteratorOfListOfInteger.hxx>
28 #include <SelectMgr_ListOfFilter.hxx>
29 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
33 const int MOUSE_SENSITIVITY_IN_PIXEL = 10; ///< defines the local context mouse selection sensitivity
35 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
36 : myUseExternalObjects(false), myWorkshop(theWorkshop)
40 XGUI_Displayer::~XGUI_Displayer()
44 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
46 return myResult2AISObjectMap.contains(theObject);
49 void XGUI_Displayer::display(ObjectPtr theObject, bool isUpdateViewer)
51 if (isVisible(theObject)) {
52 redisplay(theObject, isUpdateViewer);
56 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
57 bool isShading = false;
59 anAIS = aPrs->getAISObject(AISObjectPtr());
61 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
63 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
65 anAIS = AISObjectPtr(new GeomAPI_AISObject());
66 anAIS->setImpl(new Handle(AIS_InteractiveObject)(new ModuleBase_ResultPrs(aResult)));
67 //anAIS->createShape(aShapePtr);
73 display(theObject, anAIS, isShading, isUpdateViewer);
77 void XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
78 bool isShading, bool isUpdateViewer)
80 Handle(AIS_InteractiveContext) aContext = AISContext();
81 if (aContext.IsNull())
84 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
85 if (!anAISIO.IsNull()) {
86 myResult2AISObjectMap[theObject] = theAIS;
87 aContext->Display(anAISIO, false);
88 aContext->SetDisplayMode(anAISIO, isShading? Shading : Wireframe, isUpdateViewer);
89 if (aContext->HasOpenedContext()) {
90 if (myUseExternalObjects) {
91 if (myActiveSelectionModes.size() == 0)
92 aContext->Activate(anAISIO);
94 foreach(int aMode, myActiveSelectionModes) {
95 aContext->Activate(anAISIO, aMode);
103 void XGUI_Displayer::erase(ObjectPtr theObject, const bool isUpdateViewer)
105 if (!isVisible(theObject))
108 Handle(AIS_InteractiveContext) aContext = AISContext();
109 if (aContext.IsNull())
111 AISObjectPtr anObject = myResult2AISObjectMap[theObject];
113 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
114 if (!anAIS.IsNull()) {
115 aContext->Remove(anAIS, isUpdateViewer);
118 myResult2AISObjectMap.remove(theObject);
121 void XGUI_Displayer::redisplay(ObjectPtr theObject, bool isUpdateViewer)
123 if (!isVisible(theObject))
126 AISObjectPtr aAISObj = getAISObject(theObject);
127 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
129 GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
131 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
133 erase(theObject, isUpdateViewer);
136 if (aAIS_Obj != aAISObj) {
137 myResult2AISObjectMap[theObject] = aAIS_Obj;
139 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
142 if (!aAISIO.IsNull()) {
143 Handle(AIS_InteractiveContext) aContext = AISContext();
144 if (aContext.IsNull())
146 aContext->Redisplay(aAISIO, isUpdateViewer);
150 void XGUI_Displayer::deactivate(ObjectPtr theObject)
152 if (isVisible(theObject)) {
153 Handle(AIS_InteractiveContext) aContext = AISContext();
154 if (aContext.IsNull())
157 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
158 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
159 aContext->Deactivate(anAIS);
163 void XGUI_Displayer::activate(ObjectPtr theFeature)
166 activate(theFeature, aModes);
169 void XGUI_Displayer::activate(ObjectPtr theObject, const QIntList& theModes)
171 if (isVisible(theObject)) {
172 Handle(AIS_InteractiveContext) aContext = AISContext();
173 if (aContext.IsNull())
176 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
177 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
178 if (aContext->HasOpenedContext()) {
179 aContext->Load(anAIS, -1, true);
181 if (theModes.size() > 0) {
182 foreach(int aMode, theModes) {
183 aContext->Activate(anAIS, aMode);
186 aContext->Activate(anAIS);
190 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
192 Handle(AIS_InteractiveContext) aContext = AISContext();
193 if (aContext.IsNull())
195 if (!isVisible(theObject))
198 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
199 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
201 TColStd_ListOfInteger aModes;
202 aContext->ActivatedModes(anAIS, aModes);
203 return aModes.Extent() > 0;
206 void XGUI_Displayer::stopSelection(const QObjectPtrList& theResults, const bool isStop,
207 const bool isUpdateViewer)
209 Handle(AIS_InteractiveContext) aContext = AISContext();
210 if (aContext.IsNull())
213 Handle(AIS_Shape) anAIS;
214 QObjectPtrList::const_iterator anIt = theResults.begin(), aLast = theResults.end();
216 for (; anIt != aLast; anIt++) {
218 if (isVisible(aFeature))
219 anAIS = Handle(AIS_Shape)::DownCast(
220 myResult2AISObjectMap[aFeature]->impl<Handle(AIS_InteractiveObject)>());
225 QColor aColor(Qt::white);
227 Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
231 QColor aColor(Qt::red);
233 Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
242 void XGUI_Displayer::setSelected(const QObjectPtrList& theResults, const bool isUpdateViewer)
244 Handle(AIS_InteractiveContext) aContext = AISContext();
245 // we need to unhighligth objects manually in the current local context
246 // in couple with the selection clear (TODO)
247 Handle(AIS_LocalContext) aLocalContext = aContext->LocalContext();
248 if (!aLocalContext.IsNull())
249 aLocalContext->UnhilightLastDetected(myWorkshop->viewer()->activeView());
251 aContext->ClearSelected();
252 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);
266 void XGUI_Displayer::clearSelected()
268 Handle(AIS_InteractiveContext) aContext = AISContext();
270 aContext->UnhilightCurrents(false);
271 aContext->ClearSelected();
275 void XGUI_Displayer::eraseAll(const bool isUpdateViewer)
277 Handle(AIS_InteractiveContext) ic = AISContext();
281 foreach (AISObjectPtr aAISObj, myResult2AISObjectMap) {
283 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
285 ic->Remove(anIO, false);
287 myResult2AISObjectMap.clear();
292 void XGUI_Displayer::eraseDeletedResults(const bool isUpdateViewer)
294 Handle(AIS_InteractiveContext) aContext = AISContext();
295 if (aContext.IsNull())
298 QObjectPtrList aRemoved;
299 foreach (ObjectPtr aFeature, myResult2AISObjectMap.keys()) {
300 if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
301 AISObjectPtr anObj = myResult2AISObjectMap[aFeature];
304 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
305 if (!anAIS.IsNull()) {
306 aContext->Remove(anAIS, false);
307 aRemoved.append(aFeature);
311 foreach(ObjectPtr aObj, aRemoved) {
312 myResult2AISObjectMap.remove(aObj);
319 void XGUI_Displayer::openLocalContext()
321 Handle(AIS_InteractiveContext) aContext = AISContext();
322 if (aContext.IsNull())
324 // Open local context if there is no one
325 if (!aContext->HasOpenedContext()) {
326 // Preserve selected objects
327 //AIS_ListOfInteractive aAisList;
328 //for (aContext->InitCurrent(); aContext->MoreCurrent(); aContext->NextCurrent())
329 // aAisList.Append(aContext->Current());
331 // get the filters from the global context and append them to the local context
332 // a list of filters in the global context is not cleared and should be cleared here
333 SelectMgr_ListOfFilter aFilters;
334 aFilters.Assign(aContext->Filters());
335 // it is important to remove the filters in the global context, because there is a code
336 // in the closeLocalContex, which restore the global context filters
337 aContext->RemoveFilters();
339 //aContext->ClearCurrents();
340 aContext->OpenLocalContext();
341 aContext->NotUseDisplayedObjects();
343 myUseExternalObjects = false;
344 myActiveSelectionModes.clear();
346 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
347 for (;aIt.More(); aIt.Next()) {
348 aContext->AddFilter(aIt.Value());
349 //GetFilter()->Add(aIt.Value());
352 //AIS_ListIteratorOfListOfInteractive aIt(aAisList);
353 //for(; aIt.More(); aIt.Next()) {
354 // if (aContext->IsDisplayed(aIt.Value()))
355 // aContext->SetSelected(aIt.Value(), false);
360 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
362 Handle(AIS_InteractiveContext) ic = AISContext();
363 if ( (!ic.IsNull()) && (ic->HasOpenedContext()) ) {
364 // Preserve selected objects
365 //AIS_ListOfInteractive aAisList;
366 //for (ic->InitSelected(); ic->MoreSelected(); ic->NextSelected())
367 // aAisList.Append(ic->SelectedInteractive());
369 // get the filters from the local context and append them to the global context
370 // a list of filters in the local context is cleared
371 SelectMgr_ListOfFilter aFilters;
372 aFilters.Assign(ic->Filters());
374 //ic->ClearSelected();
375 ic->CloseAllContexts(false);
377 // Redisplay all object if they were displayed in localContext
378 Handle(AIS_InteractiveObject) aAISIO;
379 foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
380 aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
381 if (ic->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
382 ic->Display(aAISIO, false);
383 ic->SetDisplayMode(aAISIO, Shading, false);
387 // Append the filters from the local selection in the global selection context
388 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
389 for (;aIt.More(); aIt.Next()) {
390 Handle(SelectMgr_Filter) aFilter = aIt.Value();
391 ic->AddFilter(aFilter);
392 //GetFilter()->Add(aIt.Value());
397 myUseExternalObjects = false;
398 myActiveSelectionModes.clear();
401 //AIS_ListIteratorOfListOfInteractive aIt(aAisList);
402 //for(; aIt.More(); aIt.Next()) {
403 // if (ic->IsDisplayed(aIt.Value()))
404 // ic->SetCurrentObject(aIt.Value(), false);
409 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
412 if (myResult2AISObjectMap.contains(theObject))
413 anIO = myResult2AISObjectMap[theObject];
417 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
419 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
420 return getObject(aRefAIS);
423 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
426 foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
427 AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
428 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
435 void XGUI_Displayer::updateViewer()
437 Handle(AIS_InteractiveContext) ic = AISContext();
439 ic->UpdateCurrentViewer();
442 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
444 return myWorkshop->viewer()->AISContext();
447 Handle(SelectMgr_AndFilter) XGUI_Displayer::GetFilter()
449 Handle(AIS_InteractiveContext) aContext = AISContext();
450 if (myAndFilter.IsNull() && !aContext.IsNull()) {
451 myAndFilter = new SelectMgr_AndFilter();
452 aContext->AddFilter(myAndFilter);
457 void XGUI_Displayer::displayAIS(AISObjectPtr theAIS, bool isUpdate)
459 Handle(AIS_InteractiveContext) aContext = AISContext();
460 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
461 if (!anAISIO.IsNull()) {
462 aContext->Display(anAISIO, isUpdate);
463 if (aContext->HasOpenedContext()) {
464 if (myUseExternalObjects) {
465 if (myActiveSelectionModes.size() == 0)
466 aContext->Activate(anAISIO);
468 foreach(int aMode, myActiveSelectionModes) {
469 aContext->Activate(anAISIO, aMode);
477 void XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool isUpdate)
479 Handle(AIS_InteractiveContext) aContext = AISContext();
480 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
481 if (!anAISIO.IsNull()) {
482 aContext->Remove(anAISIO, isUpdate);
486 void XGUI_Displayer::activateObjects(const QIntList& theModes)
488 Handle(AIS_InteractiveContext) aContext = AISContext();
489 // Open local context if there is no one
490 if (!aContext->HasOpenedContext())
493 aContext->UseDisplayedObjects();
494 myUseExternalObjects = true;
495 myActiveSelectionModes = theModes;
497 //Deactivate trihedron which can be activated in local selector
498 AIS_ListOfInteractive aPrsList;
499 aContext->DisplayedObjects(aPrsList, true);
501 Handle(AIS_Trihedron) aTrihedron;
502 AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
503 for(; aLIt.More(); aLIt.Next()){
504 aTrihedron = Handle(AIS_Trihedron)::DownCast(aLIt.Value());
505 if (!aTrihedron.IsNull()) {
506 aContext->Deactivate(aTrihedron);
511 //Activate all displayed objects with the module modes
512 //AIS_ListOfInteractive aPrsList;
513 //aContext->DisplayedObjects(aPrsList, true);
515 //AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
516 Handle(AIS_InteractiveObject) anAISIO;
517 for(aLIt.Initialize(aPrsList); aLIt.More(); aLIt.Next()){
518 anAISIO = aLIt.Value();
519 aTrihedron = Handle(AIS_Trihedron)::DownCast(anAISIO);
520 if (!aTrihedron.IsNull())
523 aContext->Load(anAISIO, -1, true);
524 if (theModes.size() == 0)
525 aContext->Activate(anAISIO);
527 foreach(int aMode, theModes) {
528 aContext->Activate(anAISIO, aMode);
535 void XGUI_Displayer::deactivateObjects()
537 Handle(AIS_InteractiveContext) aContext = AISContext();
538 // Open local context if there is no one
539 if (!aContext->HasOpenedContext())
542 aContext->NotUseDisplayedObjects();
546 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool toUpdate)
548 if (theMode == NoMode)
551 Handle(AIS_InteractiveContext) aContext = AISContext();
552 if (aContext.IsNull())
555 AISObjectPtr aAISObj = getAISObject(theObject);
559 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
560 aContext->SetDisplayMode(aAISIO, theMode, toUpdate);
563 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
565 Handle(AIS_InteractiveContext) aContext = AISContext();
566 if (aContext.IsNull())
569 AISObjectPtr aAISObj = getAISObject(theObject);
573 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
574 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
577 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
579 Handle(AIS_InteractiveContext) aContext = AISContext();
580 if (aContext.IsNull())
582 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
583 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
584 for (; aIt.More(); aIt.Next()) {
585 if (theFilter.Access() == aIt.Value().Access())
588 //aContext->AddFilter(theFilter);
589 GetFilter()->Add(theFilter);
592 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
594 Handle(AIS_InteractiveContext) aContext = AISContext();
595 if (aContext.IsNull())
597 //aContext->RemoveFilter(theFilter);
598 GetFilter()->Remove(theFilter);
601 void XGUI_Displayer::removeFilters()
603 Handle(AIS_InteractiveContext) aContext = AISContext();
604 if (aContext.IsNull())
606 //aContext->RemoveFilters();
607 GetFilter()->Clear();