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 theObject, const QIntList& theModes)
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 if (aContext->HasOpenedContext()) {
173 aContext->Load(anAIS, -1, true);
175 if (theModes.size() > 0) {
176 foreach(int aMode, theModes) {
177 aContext->Activate(anAIS, aMode);
180 aContext->Activate(anAIS);
184 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
186 Handle(AIS_InteractiveContext) aContext = AISContext();
187 if (aContext.IsNull())
189 if (!isVisible(theObject))
192 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
193 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
195 TColStd_ListOfInteger aModes;
196 aContext->ActivatedModes(anAIS, aModes);
197 return aModes.Extent() > 0;
200 void XGUI_Displayer::stopSelection(const QList<ObjectPtr>& theResults, const bool isStop,
201 const bool isUpdateViewer)
203 Handle(AIS_InteractiveContext) aContext = AISContext();
204 if (aContext.IsNull())
207 Handle(AIS_Shape) anAIS;
208 QList<ObjectPtr>::const_iterator anIt = theResults.begin(), aLast = theResults.end();
210 for (; anIt != aLast; anIt++) {
212 if (isVisible(aFeature))
213 anAIS = Handle(AIS_Shape)::DownCast(
214 myResult2AISObjectMap[aFeature]->impl<Handle(AIS_InteractiveObject)>());
219 QColor aColor(Qt::white);
221 Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
225 QColor aColor(Qt::red);
227 Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
236 void XGUI_Displayer::setSelected(const QList<ObjectPtr>& theResults, const bool isUpdateViewer)
238 Handle(AIS_InteractiveContext) aContext = AISContext();
239 // we need to unhighligth objects manually in the current local context
240 // in couple with the selection clear (TODO)
241 Handle(AIS_LocalContext) aLocalContext = aContext->LocalContext();
242 if (!aLocalContext.IsNull())
243 aLocalContext->UnhilightLastDetected(myWorkshop->viewer()->activeView());
245 aContext->ClearSelected();
246 foreach(ObjectPtr aResult, theResults)
248 if (isVisible(aResult)) {
249 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
250 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
252 aContext->SetSelected(anAIS, false);
260 void XGUI_Displayer::clearSelected()
262 Handle(AIS_InteractiveContext) aContext = AISContext();
264 aContext->UnhilightCurrents(false);
265 aContext->ClearSelected();
269 void XGUI_Displayer::eraseAll(const bool isUpdateViewer)
271 Handle(AIS_InteractiveContext) ic = AISContext();
275 foreach (AISObjectPtr aAISObj, myResult2AISObjectMap) {
277 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
279 ic->Remove(anIO, false);
281 myResult2AISObjectMap.clear();
286 void XGUI_Displayer::eraseDeletedResults(const bool isUpdateViewer)
288 Handle(AIS_InteractiveContext) aContext = AISContext();
289 if (aContext.IsNull())
292 QList<ObjectPtr> aRemoved;
293 foreach (ObjectPtr aFeature, myResult2AISObjectMap.keys()) {
294 if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
295 AISObjectPtr anObj = myResult2AISObjectMap[aFeature];
298 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
299 if (!anAIS.IsNull()) {
300 aContext->Remove(anAIS, false);
301 aRemoved.append(aFeature);
305 foreach(ObjectPtr aObj, aRemoved) {
306 myResult2AISObjectMap.remove(aObj);
313 void XGUI_Displayer::openLocalContext()
315 Handle(AIS_InteractiveContext) aContext = AISContext();
316 if (aContext.IsNull())
318 // Open local context if there is no one
319 if (!aContext->HasOpenedContext()) {
320 aContext->ClearCurrents(false);
321 //aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
322 aContext->OpenLocalContext();
323 aContext->NotUseDisplayedObjects();
325 myUseExternalObjects = false;
326 myActiveSelectionModes.clear();
330 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
332 Handle(AIS_InteractiveContext) ic = AISContext();
333 if ( (!ic.IsNull()) && (ic->HasOpenedContext()) ) {
334 ic->ClearSelected(false);
335 ic->CloseAllContexts(false);
337 // Redisplay all object if they were displayed in localContext
338 Handle(AIS_InteractiveObject) aAISIO;
339 foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
340 aAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
341 if (ic->DisplayStatus(aAISIO) != AIS_DS_Displayed) {
342 ic->Display(aAISIO, false);
343 ic->SetDisplayMode(aAISIO, Shading, false);
348 myUseExternalObjects = false;
349 myActiveSelectionModes.clear();
353 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
356 if (myResult2AISObjectMap.contains(theObject))
357 anIO = myResult2AISObjectMap[theObject];
361 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
363 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
364 return getObject(aRefAIS);
367 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
370 foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
371 AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
372 Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
379 void XGUI_Displayer::updateViewer()
381 Handle(AIS_InteractiveContext) ic = AISContext();
383 ic->UpdateCurrentViewer();
386 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
388 return myWorkshop->viewer()->AISContext();
391 void XGUI_Displayer::displayAIS(AISObjectPtr theAIS, bool isUpdate)
393 Handle(AIS_InteractiveContext) aContext = AISContext();
394 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
395 if (!anAISIO.IsNull()) {
396 aContext->Display(anAISIO, isUpdate);
397 if (aContext->HasOpenedContext()) {
398 if (myUseExternalObjects) {
399 if (myActiveSelectionModes.size() == 0)
400 aContext->Activate(anAISIO);
402 foreach(int aMode, myActiveSelectionModes) {
403 aContext->Activate(anAISIO, aMode);
411 void XGUI_Displayer::eraseAIS(AISObjectPtr theAIS, const bool isUpdate)
413 Handle(AIS_InteractiveContext) aContext = AISContext();
414 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
415 if (!anAISIO.IsNull()) {
416 aContext->Remove(anAISIO, isUpdate);
420 void XGUI_Displayer::activateObjectsOutOfContext(const QIntList& theModes)
422 Handle(AIS_InteractiveContext) aContext = AISContext();
423 // Open local context if there is no one
424 if (!aContext->HasOpenedContext())
427 aContext->UseDisplayedObjects();
428 myUseExternalObjects = true;
429 myActiveSelectionModes = theModes;
431 //Deactivate trihedron which can be activated in local selector
432 AIS_ListOfInteractive aPrsList;
433 aContext->DisplayedObjects(aPrsList, true);
435 Handle(AIS_Trihedron) aTrihedron;
436 AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
437 for(; aLIt.More(); aLIt.Next()){
438 aTrihedron = Handle(AIS_Trihedron)::DownCast(aLIt.Value());
439 if (!aTrihedron.IsNull()) {
440 aContext->Deactivate(aTrihedron);
445 Handle(AIS_InteractiveObject) anAISIO;
446 foreach (AISObjectPtr aAIS, myResult2AISObjectMap) {
447 anAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
448 aContext->Load(anAISIO, -1, true);
449 if (theModes.size() == 0)
450 aContext->Activate(anAISIO);
452 foreach(int aMode, theModes) {
453 aContext->Activate(anAISIO, aMode);
460 void XGUI_Displayer::deactivateObjectsOutOfContext()
462 Handle(AIS_InteractiveContext) aContext = AISContext();
463 // Open local context if there is no one
464 if (!aContext->HasOpenedContext())
467 aContext->NotUseDisplayedObjects();
471 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool toUpdate)
473 if (theMode == NoMode)
476 Handle(AIS_InteractiveContext) aContext = AISContext();
477 if (aContext.IsNull())
480 AISObjectPtr aAISObj = getAISObject(theObject);
484 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
485 aContext->SetDisplayMode(aAISIO, theMode, toUpdate);
488 void XGUI_Displayer::setSelectionModes(const QIntList& theModes)
490 Handle(AIS_InteractiveContext) aContext = AISContext();
491 if (aContext.IsNull())
493 if (!aContext->HasOpenedContext())
495 // Clear previous mode
496 const TColStd_ListOfInteger& aModes = aContext->ActivatedStandardModes();
497 if (!aModes.IsEmpty()) {
498 TColStd_ListOfInteger aMModes;
499 aMModes.Assign(aModes);
500 TColStd_ListIteratorOfListOfInteger it(aMModes);
501 for(; it.More(); it.Next()) {
502 aContext->DeactivateStandardMode((TopAbs_ShapeEnum)it.Value());
505 foreach(int aMode, theModes) {
506 aContext->ActivateStandardMode((TopAbs_ShapeEnum)aMode);
510 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
512 Handle(AIS_InteractiveContext) aContext = AISContext();
513 if (aContext.IsNull())
516 AISObjectPtr aAISObj = getAISObject(theObject);
520 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
521 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
524 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
526 Handle(AIS_InteractiveContext) aContext = AISContext();
527 if (aContext.IsNull())
529 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
530 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
531 for (; aIt.More(); aIt.Next()) {
532 if (theFilter.Access() == aIt.Value().Access())
535 aContext->AddFilter(theFilter);
538 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
540 Handle(AIS_InteractiveContext) aContext = AISContext();
541 if (aContext.IsNull())
543 aContext->RemoveFilter(theFilter);