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>
31 const int MOUSE_SENSITIVITY_IN_PIXEL = 10; ///< defines the local context mouse selection sensitivity
33 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
35 myWorkshop = theWorkshop;
38 XGUI_Displayer::~XGUI_Displayer()
42 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
44 return myResult2AISObjectMap.find(theObject) != myResult2AISObjectMap.end();
47 void XGUI_Displayer::display(ObjectPtr theObject, bool isUpdateViewer)
49 if (isVisible(theObject)) {
50 redisplay(theObject, isUpdateViewer);
54 GeomPresentablePtr aPrs = boost::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
55 bool isShading = false;
57 anAIS = aPrs->getAISObject(AISObjectPtr());
59 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
61 boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
63 anAIS = AISObjectPtr(new GeomAPI_AISObject());
64 anAIS->setImpl(new Handle(AIS_InteractiveObject)(new ModuleBase_ResultPrs(aResult)));
65 //anAIS->createShape(aShapePtr);
71 display(theObject, anAIS, isShading, isUpdateViewer);
75 void XGUI_Displayer::display(ObjectPtr theObject, AISObjectPtr theAIS,
76 bool isShading, bool isUpdateViewer)
78 Handle(AIS_InteractiveContext) aContext = AISContext();
79 if (aContext.IsNull())
82 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
83 if (!anAISIO.IsNull()) {
84 myResult2AISObjectMap[theObject] = theAIS;
85 aContext->Display(anAISIO, false);
86 aContext->SetDisplayMode(anAISIO, isShading? Shading : Wireframe, isUpdateViewer);
90 void XGUI_Displayer::erase(ObjectPtr theObject, const bool isUpdateViewer)
92 if (!isVisible(theObject))
95 Handle(AIS_InteractiveContext) aContext = AISContext();
96 if (aContext.IsNull())
98 AISObjectPtr anObject = myResult2AISObjectMap[theObject];
100 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
101 if (!anAIS.IsNull()) {
102 aContext->Remove(anAIS, isUpdateViewer);
105 myResult2AISObjectMap.erase(theObject);
108 void XGUI_Displayer::redisplay(ObjectPtr theObject, bool isUpdateViewer)
110 if (!isVisible(theObject))
113 AISObjectPtr aAISObj = getAISObject(theObject);
114 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
116 GeomPresentablePtr aPrs = boost::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
118 AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
120 erase(theObject, isUpdateViewer);
123 if (aAIS_Obj != aAISObj) {
124 myResult2AISObjectMap[theObject] = aAIS_Obj;
126 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
129 if (!aAISIO.IsNull()) {
130 Handle(AIS_InteractiveContext) aContext = AISContext();
131 if (aContext.IsNull())
133 aContext->Redisplay(aAISIO, isUpdateViewer);
137 void XGUI_Displayer::deactivate(ObjectPtr theObject)
139 if (isVisible(theObject)) {
140 Handle(AIS_InteractiveContext) aContext = AISContext();
141 if (aContext.IsNull())
144 AISObjectPtr anObj = myResult2AISObjectMap[theObject];
145 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
146 aContext->Deactivate(anAIS);
150 void XGUI_Displayer::activate(ObjectPtr theObject, const QIntList& theModes)
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 if (aContext->HasOpenedContext()) {
160 aContext->Load(anAIS, -1, true);
162 if (theModes.size() > 0) {
163 foreach(int aMode, theModes) {
164 aContext->Activate(anAIS, aMode);
167 aContext->Activate(anAIS);
171 bool XGUI_Displayer::isActive(ObjectPtr theObject) const
173 Handle(AIS_InteractiveContext) aContext = AISContext();
174 if (aContext.IsNull())
176 if (!isVisible(theObject))
179 AISObjectPtr anObj = myResult2AISObjectMap.at(theObject);
180 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
182 TColStd_ListOfInteger aModes;
183 aContext->ActivatedModes(anAIS, aModes);
184 return aModes.Extent() > 0;
187 void XGUI_Displayer::stopSelection(const QList<ObjectPtr>& theResults, const bool isStop,
188 const bool isUpdateViewer)
190 Handle(AIS_InteractiveContext) aContext = AISContext();
191 if (aContext.IsNull())
194 Handle(AIS_Shape) anAIS;
195 QList<ObjectPtr>::const_iterator anIt = theResults.begin(), aLast = theResults.end();
197 for (; anIt != aLast; anIt++) {
199 if (isVisible(aFeature))
200 anAIS = Handle(AIS_Shape)::DownCast(
201 myResult2AISObjectMap[aFeature]->impl<Handle(AIS_InteractiveObject)>());
206 QColor aColor(Qt::white);
208 Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
212 QColor aColor(Qt::red);
214 Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
223 void XGUI_Displayer::setSelected(const QList<ObjectPtr>& theResults, const bool isUpdateViewer)
225 Handle(AIS_InteractiveContext) aContext = AISContext();
226 // we need to unhighligth objects manually in the current local context
227 // in couple with the selection clear (TODO)
228 Handle(AIS_LocalContext) aLocalContext = aContext->LocalContext();
229 if (!aLocalContext.IsNull())
230 aLocalContext->UnhilightLastDetected(myWorkshop->viewer()->activeView());
232 aContext->ClearSelected();
233 foreach(ObjectPtr aResult, theResults)
235 if (isVisible(aResult)) {
236 AISObjectPtr anObj = myResult2AISObjectMap[aResult];
237 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
239 aContext->SetSelected(anAIS, false);
247 void XGUI_Displayer::clearSelected()
249 Handle(AIS_InteractiveContext) aContext = AISContext();
251 aContext->UnhilightCurrents(false);
252 aContext->ClearSelected();
256 void XGUI_Displayer::eraseAll(const bool isUpdateViewer)
258 Handle(AIS_InteractiveContext) ic = AISContext();
262 ResultToAISMap::iterator aIt;
263 for (aIt = myResult2AISObjectMap.begin(); aIt != myResult2AISObjectMap.end(); aIt++) {
265 AISObjectPtr aAISObj = (*aIt).second;
266 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
268 ic->Remove(anIO, false);
270 myResult2AISObjectMap.clear();
275 void XGUI_Displayer::eraseDeletedResults(const bool isUpdateViewer)
277 Handle(AIS_InteractiveContext) aContext = AISContext();
278 if (aContext.IsNull())
281 ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(), aFLast =
282 myResult2AISObjectMap.end();
283 std::list<ObjectPtr> aRemoved;
284 for (; aFIt != aFLast; aFIt++) {
285 ObjectPtr aFeature = (*aFIt).first;
286 if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
287 AISObjectPtr anObj = (*aFIt).second;
290 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
291 if (!anAIS.IsNull()) {
292 aContext->Remove(anAIS, false);
293 aRemoved.push_back(aFeature);
297 std::list<ObjectPtr>::const_iterator anIt = aRemoved.begin(), aLast = aRemoved.end();
298 for (; anIt != aLast; anIt++) {
299 myResult2AISObjectMap.erase(myResult2AISObjectMap.find(*anIt));
306 void XGUI_Displayer::openLocalContext()
308 Handle(AIS_InteractiveContext) aContext = AISContext();
309 if (aContext.IsNull())
311 // Open local context if there is no one
312 if (!aContext->HasOpenedContext()) {
313 aContext->ClearCurrents(false);
314 //aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
315 aContext->OpenLocalContext();
316 aContext->NotUseDisplayedObjects();
320 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
322 AISContext()->ClearSelected(false);
323 closeAllContexts(true);
326 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
329 if (myResult2AISObjectMap.find(theObject) != myResult2AISObjectMap.end())
330 anIO = (myResult2AISObjectMap.find(theObject))->second;
334 ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
336 Handle(AIS_InteractiveObject) aRefAIS = theIO->impl<Handle(AIS_InteractiveObject)>();
337 return getObject(aRefAIS);
340 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
343 ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(), aFLast =
344 myResult2AISObjectMap.end();
345 for (; aFIt != aFLast && !aFeature; aFIt++) {
346 AISObjectPtr anObj = (*aFIt).second;
349 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
352 aFeature = (*aFIt).first;
357 void XGUI_Displayer::closeAllContexts(const bool isUpdateViewer)
359 Handle(AIS_InteractiveContext) ic = AISContext();
361 ic->CloseAllContexts(false);
367 void XGUI_Displayer::updateViewer()
369 Handle(AIS_InteractiveContext) ic = AISContext();
371 ic->UpdateCurrentViewer();
374 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
376 return myWorkshop->viewer()->AISContext();
379 void XGUI_Displayer::display(AISObjectPtr theAIS, bool isUpdate)
381 Handle(AIS_InteractiveContext) aContext = AISContext();
382 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
383 if (!anAISIO.IsNull())
384 aContext->Display(anAISIO, isUpdate);
387 void XGUI_Displayer::erase(AISObjectPtr theAIS, const bool isUpdate)
389 Handle(AIS_InteractiveContext) aContext = AISContext();
390 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
391 if (!anAISIO.IsNull()) {
392 aContext->Remove(anAISIO, isUpdate);
396 void XGUI_Displayer::activateObjectsOutOfContext(const QIntList& theModes)
398 Handle(AIS_InteractiveContext) aContext = AISContext();
399 // Open local context if there is no one
400 if (!aContext->HasOpenedContext())
403 aContext->UseDisplayedObjects();
404 ResultToAISMap::iterator aIt;
405 Handle(AIS_InteractiveObject) anAISIO;
406 for (aIt = myResult2AISObjectMap.begin(); aIt != myResult2AISObjectMap.end(); aIt++) {
407 anAISIO = (*aIt).second->impl<Handle(AIS_InteractiveObject)>();
408 aContext->Load(anAISIO, -1, true);
409 if (theModes.size() == 0)
410 aContext->Activate(anAISIO);
412 foreach(int aMode, theModes) {
413 aContext->Activate(anAISIO, aMode);
420 void XGUI_Displayer::deactivateObjectsOutOfContext()
422 Handle(AIS_InteractiveContext) aContext = AISContext();
423 // Open local context if there is no one
424 if (!aContext->HasOpenedContext())
427 aContext->NotUseDisplayedObjects();
431 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool toUpdate)
433 if (theMode == NoMode)
436 Handle(AIS_InteractiveContext) aContext = AISContext();
437 if (aContext.IsNull())
440 AISObjectPtr aAISObj = getAISObject(theObject);
444 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
445 aContext->SetDisplayMode(aAISIO, theMode, toUpdate);
448 void XGUI_Displayer::setSelectionModes(const QIntList& theModes)
450 Handle(AIS_InteractiveContext) aContext = AISContext();
451 if (aContext.IsNull())
453 if (!aContext->HasOpenedContext())
455 // Clear previous mode
456 const TColStd_ListOfInteger& aModes = aContext->ActivatedStandardModes();
457 if (!aModes.IsEmpty()) {
458 TColStd_ListOfInteger aMModes;
459 aMModes.Assign(aModes);
460 TColStd_ListIteratorOfListOfInteger it(aMModes);
461 for(; it.More(); it.Next()) {
462 aContext->DeactivateStandardMode((TopAbs_ShapeEnum)it.Value());
465 foreach(int aMode, theModes) {
466 aContext->ActivateStandardMode((TopAbs_ShapeEnum)aMode);
470 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
472 Handle(AIS_InteractiveContext) aContext = AISContext();
473 if (aContext.IsNull())
476 AISObjectPtr aAISObj = getAISObject(theObject);
480 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
481 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();
484 void XGUI_Displayer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
486 Handle(AIS_InteractiveContext) aContext = AISContext();
487 if (aContext.IsNull())
489 aContext->AddFilter(theFilter);
492 void XGUI_Displayer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
494 Handle(AIS_InteractiveContext) aContext = AISContext();
495 if (aContext.IsNull())
497 aContext->RemoveFilter(theFilter);