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>
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 = ModelAPI_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, boost::shared_ptr<GeomAPI_AISObject> theAIS,
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);
81 void XGUI_Displayer::erase(ObjectPtr theObject, const bool isUpdateViewer)
83 if (!isVisible(theObject))
86 Handle(AIS_InteractiveContext) aContext = AISContext();
87 boost::shared_ptr<GeomAPI_AISObject> anObject = myResult2AISObjectMap[theObject];
89 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
90 if (!anAIS.IsNull()) {
91 aContext->Remove(anAIS, isUpdateViewer);
94 myResult2AISObjectMap.erase(theObject);
97 /*bool XGUI_Displayer::redisplay(ObjectPtr theObject,
98 boost::shared_ptr<GeomAPI_AISObject> theAIS,
99 const bool isUpdateViewer)
101 bool isCreated = false;
102 Handle(AIS_InteractiveObject) anAIS =
103 theAIS ? theAIS->impl<Handle(AIS_InteractiveObject)>() : Handle(AIS_InteractiveObject)();
104 Handle(AIS_InteractiveContext) aContext = AISContext();
105 // Open local context if there is no one
106 if (!aContext->HasOpenedContext()) {
107 aContext->ClearCurrents(false);
108 aContext->OpenLocalContext(false /use displayed objects/, true /allow shape decomposition/);
109 // set mouse sensitivity
110 //aContext->SetSensitivityMode(StdSelect_SM_WINDOW);
111 //aContext->SetPixelTolerance(MOUSE_SENSITIVITY_IN_PIXEL);
113 // display or redisplay presentation
114 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theObject];
115 if (isVisible(theObject) && anObj && !anObj->empty()) {
116 aContext->Redisplay(anAIS, isUpdateViewer);
117 //aContext->RecomputeSelectionOnly(anAIS);
120 myResult2AISObjectMap[theObject] = theAIS;
121 aContext->Display(anAIS, isUpdateViewer);
127 void XGUI_Displayer::redisplay(ObjectPtr theObject, bool isUpdateViewer)
129 if (!isVisible(theObject))
132 Handle(AIS_InteractiveObject) aAISIO;
133 boost::shared_ptr<GeomAPI_AISObject> aAISObj = getAISObject(theObject);
134 GeomPresentablePtr aPrs = boost::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
136 boost::shared_ptr<GeomAPI_AISObject> aAIS_Obj = aPrs->getAISObject(aAISObj);
137 if (aAISObj && !aAIS_Obj) {
138 erase(theObject, isUpdateViewer);
141 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
143 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
145 boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
147 Handle(AIS_Shape) aAISShape = Handle(AIS_Shape)::DownCast(
148 aAISObj->impl<Handle(AIS_InteractiveObject)>());
149 if (!aAISShape.IsNull()) {
150 aAISShape->Set(aShapePtr->impl<TopoDS_Shape>());
156 if (!aAISIO.IsNull()) {
157 Handle(AIS_InteractiveContext) aContext = AISContext();
158 aContext->Redisplay(aAISIO, isUpdateViewer);
159 //if (aContext->HasOpenedContext()) {
160 // aContext->Load(aAISIO, -1, true/*allow decomposition*/);
165 void XGUI_Displayer::activateInLocalContext(ObjectPtr theResult, const std::list<int>& theModes,
166 const bool isUpdateViewer)
168 Handle(AIS_InteractiveContext) aContext = AISContext();
169 // Open local context if there is no one
170 if (!aContext->HasOpenedContext()) {
171 aContext->ClearCurrents(false);
172 aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
174 // display or redisplay presentation
175 Handle(AIS_InteractiveObject) anAIS;
176 if (isVisible(theResult)) {
177 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theResult];
179 anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
182 // Activate selection of objects from prs
183 if (!anAIS.IsNull()) {
184 aContext->ClearSelected(false); // ToCheck
185 //aContext->upClearSelected(false); // ToCheck
186 aContext->Load(anAIS, -1, true/*allow decomposition*/);
187 aContext->Deactivate(anAIS);
189 std::list<int>::const_iterator anIt = theModes.begin(), aLast = theModes.end();
190 for (; anIt != aLast; anIt++) {
191 aContext->Activate(anAIS, (*anIt));
199 void XGUI_Displayer::deactivate(ObjectPtr theObject)
201 if (isVisible(theObject)) {
202 Handle(AIS_InteractiveContext) aContext = AISContext();
204 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theObject];
205 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
206 aContext->Deactivate(anAIS);
210 void XGUI_Displayer::activate(ObjectPtr theObject)
212 if (isVisible(theObject)) {
213 Handle(AIS_InteractiveContext) aContext = AISContext();
215 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theObject];
216 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
217 aContext->Activate(anAIS);
221 void XGUI_Displayer::stopSelection(const QList<ObjectPtr>& theResults, const bool isStop,
222 const bool isUpdateViewer)
224 Handle(AIS_InteractiveContext) aContext = AISContext();
226 Handle(AIS_Shape) anAIS;
227 QList<ObjectPtr>::const_iterator anIt = theResults.begin(), aLast = theResults.end();
229 for (; anIt != aLast; anIt++) {
231 if (isVisible(aFeature))
232 anAIS = Handle(AIS_Shape)::DownCast(
233 myResult2AISObjectMap[aFeature]->impl<Handle(AIS_InteractiveObject)>());
238 QColor aColor(Qt::white);
240 Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
244 QColor aColor(Qt::red);
246 Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
255 void XGUI_Displayer::setSelected(const QList<ObjectPtr>& theResults, const bool isUpdateViewer)
257 Handle(AIS_InteractiveContext) aContext = AISContext();
258 // we need to unhighligth objects manually in the current local context
259 // in couple with the selection clear (TODO)
260 Handle(AIS_LocalContext) aLocalContext = aContext->LocalContext();
261 if (!aLocalContext.IsNull())
262 aLocalContext->UnhilightLastDetected(myWorkshop->viewer()->activeView());
264 aContext->ClearSelected();
265 foreach(ObjectPtr aResult, theResults)
267 if (myResult2AISObjectMap.find(aResult) == myResult2AISObjectMap.end())
270 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[aResult];
272 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
274 aContext->AddOrRemoveSelected(anAIS, false);
281 /*void XGUI_Displayer::EraseAll(const bool isUpdateViewer)
283 Handle(AIS_InteractiveContext) ic = AISContext();
285 AIS_ListOfInteractive aList;
286 ic->DisplayedObjects(aList);
287 AIS_ListIteratorOfListOfInteractive anIter(aList);
288 for (; anIter.More(); anIter.Next()) {
289 if ((anIter.Value()->DynamicType() == STANDARD_TYPE(AIS_Trihedron)))
293 Handle(AIS_InteractiveObject) anIO = anIter.Value();
294 ic->Erase(anIO, false);
296 myResult2AISObjectMap.clear();
301 void XGUI_Displayer::eraseDeletedResults(const bool isUpdateViewer)
303 Handle(AIS_InteractiveContext) aContext = AISContext();
305 ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(), aFLast =
306 myResult2AISObjectMap.end();
307 std::list<ObjectPtr> aRemoved;
308 for (; aFIt != aFLast; aFIt++) {
309 ObjectPtr aFeature = (*aFIt).first;
310 if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
311 boost::shared_ptr<GeomAPI_AISObject> anObj = (*aFIt).second;
314 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
315 if (!anAIS.IsNull()) {
316 aContext->Remove(anAIS, false);
317 aRemoved.push_back(aFeature);
321 std::list<ObjectPtr>::const_iterator anIt = aRemoved.begin(), aLast = aRemoved.end();
322 for (; anIt != aLast; anIt++) {
323 myResult2AISObjectMap.erase(myResult2AISObjectMap.find(*anIt));
330 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
332 closeAllContexts(true);
335 boost::shared_ptr<GeomAPI_AISObject> XGUI_Displayer::getAISObject(ObjectPtr theObject) const
337 boost::shared_ptr<GeomAPI_AISObject> anIO;
338 if (myResult2AISObjectMap.find(theObject) != myResult2AISObjectMap.end())
339 anIO = (myResult2AISObjectMap.find(theObject))->second;
343 ObjectPtr XGUI_Displayer::getObject(Handle(AIS_InteractiveObject) theIO) const
346 ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(), aFLast =
347 myResult2AISObjectMap.end();
348 for (; aFIt != aFLast && !aFeature; aFIt++) {
349 boost::shared_ptr<GeomAPI_AISObject> anObj = (*aFIt).second;
352 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
355 aFeature = (*aFIt).first;
360 void XGUI_Displayer::closeAllContexts(const bool isUpdateViewer)
362 Handle(AIS_InteractiveContext) ic = AISContext();
364 ic->CloseAllContexts(false);
370 void XGUI_Displayer::updateViewer()
372 Handle(AIS_InteractiveContext) ic = AISContext();
374 ic->UpdateCurrentViewer();
377 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
379 return myWorkshop->viewer()->AISContext();
382 void XGUI_Displayer::display(boost::shared_ptr<GeomAPI_AISObject> theAIS, bool isUpdate)
384 Handle(AIS_InteractiveContext) aContext = AISContext();
385 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
386 if (!anAISIO.IsNull())
387 aContext->Display(anAISIO, isUpdate);
390 void XGUI_Displayer::erase(boost::shared_ptr<GeomAPI_AISObject> theAIS, const bool isUpdate)
392 Handle(AIS_InteractiveContext) aContext = AISContext();
393 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
394 if (!anAISIO.IsNull()) {
395 aContext->Remove(anAISIO, isUpdate);