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 <GeomAPI_Shape.h>
16 #include <GeomAPI_IPresentable.h>
18 #include <AIS_InteractiveContext.hxx>
19 #include <AIS_LocalContext.hxx>
20 #include <AIS_ListOfInteractive.hxx>
21 #include <AIS_ListIteratorOfListOfInteractive.hxx>
22 #include <AIS_DimensionSelectionMode.hxx>
23 #include <AIS_Shape.hxx>
27 const int MOUSE_SENSITIVITY_IN_PIXEL = 10; ///< defines the local context mouse selection sensitivity
29 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
31 myWorkshop = theWorkshop;
34 XGUI_Displayer::~XGUI_Displayer()
38 bool XGUI_Displayer::isVisible(ObjectPtr theObject) const
40 return myResult2AISObjectMap.find(theObject) != myResult2AISObjectMap.end();
43 void XGUI_Displayer::display(ObjectPtr theObject, bool isUpdateViewer)
45 if (isVisible(theObject)) {
46 redisplay(theObject, isUpdateViewer);
48 boost::shared_ptr<GeomAPI_AISObject> anAIS;
50 GeomPresentablePtr aPrs = boost::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
51 bool isShading = false;
53 anAIS = aPrs->getAISObject(boost::shared_ptr<GeomAPI_AISObject>());
55 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
57 boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
59 anAIS = boost::shared_ptr<GeomAPI_AISObject>(new GeomAPI_AISObject());
60 anAIS->createShape(aShapePtr);
66 display(theObject, anAIS, isShading, isUpdateViewer);
70 void XGUI_Displayer::display(ObjectPtr theObject, boost::shared_ptr<GeomAPI_AISObject> theAIS,
71 bool isShading, bool isUpdateViewer)
73 Handle(AIS_InteractiveContext) aContext = AISContext();
74 if (aContext.IsNull())
77 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
78 if (!anAISIO.IsNull()) {
79 myResult2AISObjectMap[theObject] = theAIS;
80 aContext->Display(anAISIO, false);
81 aContext->SetDisplayMode(anAISIO, isShading? Shading : Wireframe, isUpdateViewer);
85 void XGUI_Displayer::erase(ObjectPtr theObject, const bool isUpdateViewer)
87 if (!isVisible(theObject))
90 Handle(AIS_InteractiveContext) aContext = AISContext();
91 if (aContext.IsNull())
93 boost::shared_ptr<GeomAPI_AISObject> anObject = myResult2AISObjectMap[theObject];
95 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
96 if (!anAIS.IsNull()) {
97 aContext->Remove(anAIS, isUpdateViewer);
100 myResult2AISObjectMap.erase(theObject);
103 void XGUI_Displayer::redisplay(ObjectPtr theObject, bool isUpdateViewer)
105 if (!isVisible(theObject))
108 Handle(AIS_InteractiveObject) aAISIO;
109 boost::shared_ptr<GeomAPI_AISObject> aAISObj = getAISObject(theObject);
110 GeomPresentablePtr aPrs = boost::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
112 boost::shared_ptr<GeomAPI_AISObject> aAIS_Obj = aPrs->getAISObject(aAISObj);
113 if (aAISObj && !aAIS_Obj) {
114 erase(theObject, isUpdateViewer);
117 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
119 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
121 boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
123 Handle(AIS_Shape) aAISShape = Handle(AIS_Shape)::DownCast(
124 aAISObj->impl<Handle(AIS_InteractiveObject)>());
125 if (!aAISShape.IsNull()) {
126 aAISShape->Set(aShapePtr->impl<TopoDS_Shape>());
132 if (!aAISIO.IsNull()) {
133 Handle(AIS_InteractiveContext) aContext = AISContext();
134 if (aContext.IsNull())
136 aContext->Redisplay(aAISIO, isUpdateViewer);
137 //if (aContext->HasOpenedContext()) {
138 // aContext->Load(aAISIO, -1, true/*allow decomposition*/);
143 void XGUI_Displayer::activateInLocalContext(ObjectPtr theResult, const std::list<int>& theModes,
144 const bool isUpdateViewer)
146 Handle(AIS_InteractiveContext) aContext = AISContext();
147 if (aContext.IsNull())
149 // Open local context if there is no one
150 if (!aContext->HasOpenedContext()) {
151 aContext->ClearCurrents(false);
152 //aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
153 aContext->OpenLocalContext();
154 aContext->NotUseDisplayedObjects();
156 // display or redisplay presentation
157 Handle(AIS_InteractiveObject) anAIS;
158 if (isVisible(theResult)) {
159 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theResult];
161 anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
164 // Activate selection of objects from prs
165 if (!anAIS.IsNull()) {
166 aContext->ClearSelected(false); // ToCheck
167 //aContext->upClearSelected(false); // ToCheck
168 aContext->Load(anAIS, -1, true/*allow decomposition*/);
169 aContext->Deactivate(anAIS);
171 std::list<int>::const_iterator anIt = theModes.begin(), aLast = theModes.end();
172 for (; anIt != aLast; anIt++) {
173 aContext->Activate(anAIS, (*anIt));
181 void XGUI_Displayer::deactivate(ObjectPtr theObject)
183 if (isVisible(theObject)) {
184 Handle(AIS_InteractiveContext) aContext = AISContext();
185 if (aContext.IsNull())
188 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theObject];
189 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
190 aContext->Deactivate(anAIS);
194 void XGUI_Displayer::activate(ObjectPtr theObject)
196 if (isVisible(theObject)) {
197 Handle(AIS_InteractiveContext) aContext = AISContext();
198 if (aContext.IsNull())
201 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theObject];
202 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
203 aContext->Activate(anAIS);
207 void XGUI_Displayer::stopSelection(const QList<ObjectPtr>& theResults, const bool isStop,
208 const bool isUpdateViewer)
210 Handle(AIS_InteractiveContext) aContext = AISContext();
211 if (aContext.IsNull())
214 Handle(AIS_Shape) anAIS;
215 QList<ObjectPtr>::const_iterator anIt = theResults.begin(), aLast = theResults.end();
217 for (; anIt != aLast; anIt++) {
219 if (isVisible(aFeature))
220 anAIS = Handle(AIS_Shape)::DownCast(
221 myResult2AISObjectMap[aFeature]->impl<Handle(AIS_InteractiveObject)>());
226 QColor aColor(Qt::white);
228 Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
232 QColor aColor(Qt::red);
234 Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
243 void XGUI_Displayer::setSelected(const QList<ObjectPtr>& theResults, const bool isUpdateViewer)
245 Handle(AIS_InteractiveContext) aContext = AISContext();
246 // we need to unhighligth objects manually in the current local context
247 // in couple with the selection clear (TODO)
248 Handle(AIS_LocalContext) aLocalContext = aContext->LocalContext();
249 if (!aLocalContext.IsNull())
250 aLocalContext->UnhilightLastDetected(myWorkshop->viewer()->activeView());
252 aContext->ClearSelected();
253 foreach(ObjectPtr aResult, theResults)
255 if (myResult2AISObjectMap.find(aResult) == myResult2AISObjectMap.end())
258 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[aResult];
260 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
262 aContext->AddOrRemoveSelected(anAIS, false);
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 boost::shared_ptr<GeomAPI_AISObject> 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 boost::shared_ptr<GeomAPI_AISObject> 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();
333 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
335 AISContext()->ClearSelected(false);
336 closeAllContexts(true);
339 boost::shared_ptr<GeomAPI_AISObject> XGUI_Displayer::getAISObject(ObjectPtr theObject) const
341 boost::shared_ptr<GeomAPI_AISObject> anIO;
342 if (myResult2AISObjectMap.find(theObject) != myResult2AISObjectMap.end())
343 anIO = (myResult2AISObjectMap.find(theObject))->second;
347 ObjectPtr XGUI_Displayer::getObject(Handle(AIS_InteractiveObject) theIO) const
350 ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(), aFLast =
351 myResult2AISObjectMap.end();
352 for (; aFIt != aFLast && !aFeature; aFIt++) {
353 boost::shared_ptr<GeomAPI_AISObject> anObj = (*aFIt).second;
356 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
359 aFeature = (*aFIt).first;
364 void XGUI_Displayer::closeAllContexts(const bool isUpdateViewer)
366 Handle(AIS_InteractiveContext) ic = AISContext();
368 ic->CloseAllContexts(false);
374 void XGUI_Displayer::updateViewer()
376 Handle(AIS_InteractiveContext) ic = AISContext();
378 ic->UpdateCurrentViewer();
381 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
383 return myWorkshop->viewer()->AISContext();
386 void XGUI_Displayer::display(boost::shared_ptr<GeomAPI_AISObject> theAIS, bool isUpdate)
388 Handle(AIS_InteractiveContext) aContext = AISContext();
389 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
390 if (!anAISIO.IsNull())
391 aContext->Display(anAISIO, isUpdate);
394 void XGUI_Displayer::erase(boost::shared_ptr<GeomAPI_AISObject> theAIS, const bool isUpdate)
396 Handle(AIS_InteractiveContext) aContext = AISContext();
397 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
398 if (!anAISIO.IsNull()) {
399 aContext->Remove(anAISIO, isUpdate);
403 void XGUI_Displayer::activateObjectsOutOfContext(const std::list<int>& theModes,
404 Handle(SelectMgr_Filter) theFilter)
406 Handle(AIS_InteractiveContext) aContext = AISContext();
407 // Open local context if there is no one
408 if (!aContext->HasOpenedContext())
411 aContext->UseDisplayedObjects();
412 std::list<int>::const_iterator anIt = theModes.begin(), aLast = theModes.end();
413 for (; anIt != aLast; anIt++) {
414 aContext->ActivateStandardMode((TopAbs_ShapeEnum)(*anIt));
417 if (!theFilter.IsNull())
418 aContext->AddFilter(theFilter);
422 void XGUI_Displayer::deactivateObjectsOutOfContext()
424 Handle(AIS_InteractiveContext) aContext = AISContext();
425 // Open local context if there is no one
426 if (!aContext->HasOpenedContext())
429 aContext->RemoveFilters();
430 aContext->NotUseDisplayedObjects();
434 void XGUI_Displayer::setDisplayMode(ObjectPtr theObject, DisplayMode theMode, bool toUpdate)
436 if (theMode == NoMode)
439 Handle(AIS_InteractiveContext) aContext = AISContext();
440 if (aContext.IsNull())
443 boost::shared_ptr<GeomAPI_AISObject> aAISObj = getAISObject(theObject);
447 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
448 aContext->SetDisplayMode(aAISIO, theMode, toUpdate);
452 XGUI_Displayer::DisplayMode XGUI_Displayer::displayMode(ObjectPtr theObject) const
454 Handle(AIS_InteractiveContext) aContext = AISContext();
455 if (aContext.IsNull())
458 boost::shared_ptr<GeomAPI_AISObject> aAISObj = getAISObject(theObject);
462 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
463 return (XGUI_Displayer::DisplayMode) aAISIO->DisplayMode();