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"
9 #include "ModuleBase_Tools.h"
11 #include <ModelAPI_Document.h>
12 #include <ModelAPI_Data.h>
13 #include <ModelAPI_Object.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>
24 #include <AIS_Shape.hxx>
28 const int MOUSE_SENSITIVITY_IN_PIXEL = 10; ///< defines the local context mouse selection sensitivity
30 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
32 myWorkshop = theWorkshop;
35 XGUI_Displayer::~XGUI_Displayer()
39 bool XGUI_Displayer::isVisible(ObjectPtr theObject)
41 return myResult2AISObjectMap.find(theObject) != myResult2AISObjectMap.end();
44 void XGUI_Displayer::display(ObjectPtr theObject, bool isUpdateViewer)
46 if (isVisible(theObject)) {
47 redisplay(theObject, isUpdateViewer);
49 boost::shared_ptr<GeomAPI_AISObject> anAIS;
51 GeomPresentablePtr aPrs = boost::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
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 = ModuleBase_Tools::shape(aResult);
59 anAIS = boost::shared_ptr<GeomAPI_AISObject>(new GeomAPI_AISObject());
60 anAIS->createShape(aShapePtr);
65 display(theObject, anAIS, isUpdateViewer);
69 void XGUI_Displayer::display(ObjectPtr theObject,
70 boost::shared_ptr<GeomAPI_AISObject> theAIS, bool isUpdateViewer)
72 Handle(AIS_InteractiveContext) aContext = AISContext();
74 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
75 if (!anAISIO.IsNull()) {
76 myResult2AISObjectMap[theObject] = theAIS;
77 aContext->Display(anAISIO, isUpdateViewer);
83 void XGUI_Displayer::erase(ObjectPtr theObject, const bool isUpdateViewer)
85 if (!isVisible(theObject)) return;
87 Handle(AIS_InteractiveContext) aContext = AISContext();
88 boost::shared_ptr<GeomAPI_AISObject> anObject = myResult2AISObjectMap[theObject];
90 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
91 if (!anAIS.IsNull()) {
92 aContext->Remove(anAIS, isUpdateViewer);
95 myResult2AISObjectMap.erase(theObject);
99 /*bool XGUI_Displayer::redisplay(ObjectPtr theObject,
100 boost::shared_ptr<GeomAPI_AISObject> theAIS,
101 const bool isUpdateViewer)
103 bool isCreated = false;
104 Handle(AIS_InteractiveObject) anAIS =
105 theAIS ? theAIS->impl<Handle(AIS_InteractiveObject)>() : Handle(AIS_InteractiveObject)();
106 Handle(AIS_InteractiveContext) aContext = AISContext();
107 // Open local context if there is no one
108 if (!aContext->HasOpenedContext()) {
109 aContext->ClearCurrents(false);
110 aContext->OpenLocalContext(false /use displayed objects/, true /allow shape decomposition/);
111 // set mouse sensitivity
112 //aContext->SetSensitivityMode(StdSelect_SM_WINDOW);
113 //aContext->SetPixelTolerance(MOUSE_SENSITIVITY_IN_PIXEL);
115 // display or redisplay presentation
116 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theObject];
117 if (isVisible(theObject) && anObj && !anObj->empty()) {
118 aContext->Redisplay(anAIS, isUpdateViewer);
119 //aContext->RecomputeSelectionOnly(anAIS);
122 myResult2AISObjectMap[theObject] = theAIS;
123 aContext->Display(anAIS, isUpdateViewer);
129 void XGUI_Displayer::redisplay(ObjectPtr theObject, bool isUpdateViewer)
131 if (!isVisible(theObject))
134 Handle(AIS_InteractiveObject) aAISIO;
135 boost::shared_ptr<GeomAPI_AISObject> aAISObj = getAISObject(theObject);
136 GeomPresentablePtr aPrs = boost::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
138 boost::shared_ptr<GeomAPI_AISObject> aAIS_Obj = aPrs->getAISObject(aAISObj);
139 if (aAISObj && !aAIS_Obj) {
140 erase(theObject, isUpdateViewer);
143 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
145 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
147 boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModuleBase_Tools::shape(aResult);
149 Handle(AIS_Shape) aAISShape = Handle(AIS_Shape)::DownCast(aAISObj->impl<Handle(AIS_InteractiveObject)>());
150 if (!aAISShape.IsNull()) {
151 aAISShape->Set(aShapePtr->impl<TopoDS_Shape>());
157 if (!aAISIO.IsNull()) {
158 Handle(AIS_InteractiveContext) aContext = AISContext();
159 aContext->Redisplay(aAISIO, isUpdateViewer);
160 //if (aContext->HasOpenedContext()) {
161 // aContext->Load(aAISIO, -1, true/*allow decomposition*/);
166 void XGUI_Displayer::activateInLocalContext(ObjectPtr theResult,
167 const std::list<int>& theModes, const bool isUpdateViewer)
169 Handle(AIS_InteractiveContext) aContext = AISContext();
170 // Open local context if there is no one
171 if (!aContext->HasOpenedContext()) {
172 aContext->ClearCurrents(false);
173 aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
175 // display or redisplay presentation
176 Handle(AIS_InteractiveObject) anAIS;
177 if (isVisible(theResult)) {
178 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theResult];
180 anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
183 // Activate selection of objects from prs
184 if (!anAIS.IsNull()) {
185 aContext->ClearSelected(false); // ToCheck
186 //aContext->upClearSelected(false); // ToCheck
187 aContext->Load(anAIS, -1, true/*allow decomposition*/);
188 aContext->Deactivate(anAIS);
190 std::list<int>::const_iterator anIt = theModes.begin(), aLast = theModes.end();
191 for (; anIt != aLast; anIt++) {
192 aContext->Activate(anAIS, (*anIt));
200 void XGUI_Displayer::deactivate(ObjectPtr theObject, bool toUpdate)
202 if (isVisible(theObject)) {
203 Handle(AIS_InteractiveContext) aContext = AISContext();
205 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theObject];
206 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
207 aContext->Deactivate(anAIS);
211 void XGUI_Displayer::stopSelection(const QList<ObjectPtr>& theResults, const bool isStop,
212 const bool isUpdateViewer)
214 Handle(AIS_InteractiveContext) aContext = AISContext();
216 Handle(AIS_Shape) anAIS;
217 QList<ObjectPtr>::const_iterator anIt = theResults.begin(), aLast = theResults.end();
219 for (; anIt != aLast; anIt++) {
221 if (isVisible(aFeature))
222 anAIS = Handle(AIS_Shape)::DownCast(myResult2AISObjectMap[aFeature]->impl<Handle(AIS_InteractiveObject)>());
227 QColor aColor(Qt::white);
228 anAIS->SetColor(Quantity_Color(aColor.red()/255., aColor.green()/255., aColor.blue()/255., Quantity_TOC_RGB));
232 QColor aColor(Qt::red);
233 anAIS->SetColor(Quantity_Color(aColor.red()/255., aColor.green()/255., aColor.blue()/255., Quantity_TOC_RGB));
241 void XGUI_Displayer::setSelected(const QList<ObjectPtr>& theResults, const bool isUpdateViewer)
243 Handle(AIS_InteractiveContext) aContext = AISContext();
244 // we need to unhighligth objects manually in the current local context
245 // in couple with the selection clear (TODO)
246 Handle(AIS_LocalContext) aLocalContext = aContext->LocalContext();
247 if (!aLocalContext.IsNull())
248 aLocalContext->UnhilightLastDetected(myWorkshop->viewer()->activeView());
250 aContext->ClearSelected();
251 foreach(ObjectPtr aResult, theResults) {
252 if (myResult2AISObjectMap.find(aResult) == myResult2AISObjectMap.end())
255 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[aResult];
257 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
259 aContext->AddOrRemoveSelected(anAIS, false);
267 /*void XGUI_Displayer::EraseAll(const bool isUpdateViewer)
269 Handle(AIS_InteractiveContext) ic = AISContext();
271 AIS_ListOfInteractive aList;
272 ic->DisplayedObjects(aList);
273 AIS_ListIteratorOfListOfInteractive anIter(aList);
274 for (; anIter.More(); anIter.Next()) {
275 if ((anIter.Value()->DynamicType() == STANDARD_TYPE(AIS_Trihedron)))
279 Handle(AIS_InteractiveObject) anIO = anIter.Value();
280 ic->Erase(anIO, false);
282 myResult2AISObjectMap.clear();
287 void XGUI_Displayer::eraseDeletedResults(const bool isUpdateViewer)
289 Handle(AIS_InteractiveContext) aContext = AISContext();
291 ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(),
292 aFLast = myResult2AISObjectMap.end();
293 std::list<ObjectPtr> aRemoved;
294 for (; aFIt != aFLast; aFIt++)
296 ObjectPtr aFeature = (*aFIt).first;
297 if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
298 boost::shared_ptr<GeomAPI_AISObject> anObj = (*aFIt).second;
299 if (!anObj) continue;
300 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
301 if (!anAIS.IsNull()) {
302 aContext->Remove(anAIS, false);
303 aRemoved.push_back(aFeature);
307 std::list<ObjectPtr>::const_iterator anIt = aRemoved.begin(),
308 aLast = aRemoved.end();
309 for (; anIt != aLast; anIt++) {
310 myResult2AISObjectMap.erase(myResult2AISObjectMap.find(*anIt));
317 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
319 closeAllContexts(true);
322 boost::shared_ptr<GeomAPI_AISObject> XGUI_Displayer::getAISObject(ObjectPtr theObject) const
324 boost::shared_ptr<GeomAPI_AISObject> anIO;
325 if (myResult2AISObjectMap.find(theObject) != myResult2AISObjectMap.end())
326 anIO = (myResult2AISObjectMap.find(theObject))->second;
330 ObjectPtr XGUI_Displayer::getObject(Handle(AIS_InteractiveObject) theIO) const
333 ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(),
334 aFLast = myResult2AISObjectMap.end();
335 for (; aFIt != aFLast && !aFeature; aFIt++) {
336 boost::shared_ptr<GeomAPI_AISObject> anObj = (*aFIt).second;
337 if (!anObj) continue;
338 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
341 aFeature = (*aFIt).first;
346 void XGUI_Displayer::closeAllContexts(const bool isUpdateViewer)
348 Handle(AIS_InteractiveContext) ic = AISContext();
350 ic->CloseAllContexts(false);
356 void XGUI_Displayer::updateViewer()
358 Handle(AIS_InteractiveContext) ic = AISContext();
360 ic->UpdateCurrentViewer();
363 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
365 return myWorkshop->viewer()->AISContext();
369 void XGUI_Displayer::display(boost::shared_ptr<GeomAPI_AISObject> theAIS, bool isUpdate)
371 Handle(AIS_InteractiveContext) aContext = AISContext();
372 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
373 if (!anAISIO.IsNull())
374 aContext->Display(anAISIO, isUpdate);
377 void XGUI_Displayer::erase(boost::shared_ptr<GeomAPI_AISObject> theAIS, const bool isUpdate)
379 Handle(AIS_InteractiveContext) aContext = AISContext();
380 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
381 if (!anAISIO.IsNull()) {
382 aContext->Remove(anAISIO, isUpdate);