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.find(theObject) != myResult2AISObjectMap.end();
49 void XGUI_Displayer::display(ObjectPtr theObject, bool isUpdateViewer)
51 if (isVisible(theObject)) {
52 redisplay(theObject, isUpdateViewer);
56 GeomPresentablePtr aPrs = boost::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
57 bool isShading = false;
59 anAIS = aPrs->getAISObject(AISObjectPtr());
61 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
63 boost::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.erase(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 = boost::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.at(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 ResultToAISMap::iterator aIt;
276 for (aIt = myResult2AISObjectMap.begin(); aIt != myResult2AISObjectMap.end(); aIt++) {
278 AISObjectPtr aAISObj = (*aIt).second;
279 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
281 ic->Remove(anIO, false);
283 myResult2AISObjectMap.clear();
288 void XGUI_Displayer::eraseDeletedResults(const bool isUpdateViewer)
290 Handle(AIS_InteractiveContext) aContext = AISContext();
291 if (aContext.IsNull())
294 ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(), aFLast =
295 myResult2AISObjectMap.end();
296 std::list<ObjectPtr> aRemoved;
297 for (; aFIt != aFLast; aFIt++) {
298 ObjectPtr aFeature = (*aFIt).first;
299 if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
300 AISObjectPtr anObj = (*aFIt).second;
303 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
304 if (!anAIS.IsNull()) {
305 aContext->Remove(anAIS, false);
306 aRemoved.push_back(aFeature);
310 std::list<ObjectPtr>::const_iterator anIt = aRemoved.begin(), aLast = aRemoved.end();
311 for (; anIt != aLast; anIt++) {
312 myResult2AISObjectMap.erase(myResult2AISObjectMap.find(*anIt));
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 aContext->ClearCurrents(false);
327 //aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
328 aContext->OpenLocalContext();
329 aContext->NotUseDisplayedObjects();
331 myUseExternalObjects = false;
332 myActiveSelectionModes.clear();
336 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
338 AISContext()->ClearSelected(false);
339 closeAllContexts(true);
342 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
345 if (myResult2AISObjectMap.find(theObject) != myResult2AISObjectMap.end())
346 anIO = (myResult2AISObjectMap.find(theObject))->second;
350 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
352 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
353 return getObject(aRefAIS);
356 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
359 ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(), aFLast =
360 myResult2AISObjectMap.end();
361 for (; aFIt != aFLast && !aFeature; aFIt++) {
362 AISObjectPtr anObj = (*aFIt).second;
365 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
368 aFeature = (*aFIt).first;
373 void XGUI_Displayer::closeAllContexts(const bool isUpdateViewer)
375 Handle(AIS_InteractiveContext) ic = AISContext();
377 ic->CloseAllContexts(false);
380 myUseExternalObjects = false;
381 myActiveSelectionModes.clear();
385 void XGUI_Displayer::updateViewer()
387 Handle(AIS_InteractiveContext) ic = AISContext();
389 ic->UpdateCurrentViewer();
392 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
394 return myWorkshop->viewer()->AISContext();
397 void XGUI_Displayer::display(AISObjectPtr theAIS, bool isUpdate)
399 Handle(AIS_InteractiveContext) aContext = AISContext();
400 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
401 if (!anAISIO.IsNull()) {
402 aContext->Display(anAISIO, isUpdate);
403 if (aContext->HasOpenedContext()) {
404 if (myUseExternalObjects) {
405 if (myActiveSelectionModes.size() == 0)
406 aContext->Activate(anAISIO);
408 foreach(int aMode, myActiveSelectionModes) {
409 aContext->Activate(anAISIO, aMode);
417 void XGUI_Displayer::erase(AISObjectPtr theAIS, const bool isUpdate)
419 Handle(AIS_InteractiveContext) aContext = AISContext();
420 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
421 if (!anAISIO.IsNull()) {
422 aContext->Remove(anAISIO, isUpdate);
426 void XGUI_Displayer::activateObjectsOutOfContext(const QIntList& theModes)
428 Handle(AIS_InteractiveContext) aContext = AISContext();
429 // Open local context if there is no one
430 if (!aContext->HasOpenedContext())
433 aContext->UseDisplayedObjects();
434 myUseExternalObjects = true;
435 myActiveSelectionModes = theModes;
437 //Deactivate trihedron which can be activated in local selector
438 AIS_ListOfInteractive aPrsList;
439 aContext->DisplayedObjects(aPrsList, true);
441 Handle(AIS_Trihedron) aTrihedron;
442 AIS_ListIteratorOfListOfInteractive aLIt(aPrsList);
443 for(; aLIt.More(); aLIt.Next()){
444 aTrihedron = Handle(AIS_Trihedron)::DownCast(aLIt.Value());
445 if (!aTrihedron.IsNull()) {
446 aContext->Deactivate(aTrihedron);
451 ResultToAISMap::iterator aIt;
452 Handle(AIS_InteractiveObject) anAISIO;
453 for (aIt = myResult2AISObjectMap.begin(); aIt != myResult2AISObjectMap.end(); aIt++) {
454 anAISIO = (*aIt).second->impl<Handle(AIS_InteractiveObject)>();
455 aContext->Load(anAISIO, -1, true);
456 if (theModes.size() == 0)
457 aContext->Activate(anAISIO);
459 foreach(int aMode, theModes) {
460 aContext->Activate(anAISIO, aMode);
467 void XGUI_Displayer::deactivateObjectsOutOfContext()
469 Handle(AIS_InteractiveContext) aContext = AISContext();
470 // Open local context if there is no one
471 if (!aContext->HasOpenedContext())
474 aContext->NotUseDisplayedObjects();
478 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool toUpdate)
480 if (theMode == NoMode)
483 Handle(AIS_InteractiveContext) aContext = AISContext();
484 if (aContext.IsNull())
487 AISObjectPtr aAISObj = getAISObject(theObject);
491 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
492 aContext->SetDisplayMode(aAISIO, theMode, toUpdate);
495 void XGUI_Displayer::setSelectionModes(const QIntList& theModes)
497 Handle(AIS_InteractiveContext) aContext = AISContext();
498 if (aContext.IsNull())
500 if (!aContext->HasOpenedContext())
502 // Clear previous mode
503 const TColStd_ListOfInteger& aModes = aContext->ActivatedStandardModes();
504 if (!aModes.IsEmpty()) {
505 TColStd_ListOfInteger aMModes;
506 aMModes.Assign(aModes);
507 TColStd_ListIteratorOfListOfInteger it(aMModes);
508 for(; it.More(); it.Next()) {
509 aContext->DeactivateStandardMode((TopAbs_ShapeEnum)it.Value());
512 foreach(int aMode, theModes) {
513 aContext->ActivateStandardMode((TopAbs_ShapeEnum)aMode);
517 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
519 Handle(AIS_InteractiveContext) aContext = AISContext();
520 if (aContext.IsNull())
523 AISObjectPtr aAISObj = getAISObject(theObject);
527 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
528 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
531 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
533 Handle(AIS_InteractiveContext) aContext = AISContext();
534 if (aContext.IsNull())
536 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
537 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
538 for (; aIt.More(); aIt.Next()) {
539 if (theFilter.Access() == aIt.Value().Access())
542 aContext->AddFilter(theFilter);
545 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
547 Handle(AIS_InteractiveContext) aContext = AISContext();
548 if (aContext.IsNull())
550 aContext->RemoveFilter(theFilter);