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 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
53 boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModuleBase_Tools::shape(aResult);
55 anAIS = boost::shared_ptr<GeomAPI_AISObject>(new GeomAPI_AISObject());
56 anAIS->createShape(aShapePtr);
59 GeomPresentablePtr aPrs = boost::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
61 anAIS = aPrs->getAISObject(boost::shared_ptr<GeomAPI_AISObject>());
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->Erase(anAIS, isUpdateViewer);
96 myResult2AISObjectMap.erase(theObject);
100 /*bool XGUI_Displayer::redisplay(ObjectPtr theObject,
101 boost::shared_ptr<GeomAPI_AISObject> theAIS,
102 const bool isUpdateViewer)
104 bool isCreated = false;
105 Handle(AIS_InteractiveObject) anAIS =
106 theAIS ? theAIS->impl<Handle(AIS_InteractiveObject)>() : Handle(AIS_InteractiveObject)();
107 Handle(AIS_InteractiveContext) aContext = AISContext();
108 // Open local context if there is no one
109 if (!aContext->HasOpenedContext()) {
110 aContext->ClearCurrents(false);
111 aContext->OpenLocalContext(false /use displayed objects/, true /allow shape decomposition/);
112 // set mouse sensitivity
113 //aContext->SetSensitivityMode(StdSelect_SM_WINDOW);
114 //aContext->SetPixelTolerance(MOUSE_SENSITIVITY_IN_PIXEL);
116 // display or redisplay presentation
117 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theObject];
118 if (isVisible(theObject) && anObj && !anObj->empty()) {
119 aContext->Redisplay(anAIS, isUpdateViewer);
120 //aContext->RecomputeSelectionOnly(anAIS);
123 myResult2AISObjectMap[theObject] = theAIS;
124 aContext->Display(anAIS, isUpdateViewer);
130 void XGUI_Displayer::redisplay(ObjectPtr theObject, bool isUpdateViewer)
132 if (!isVisible(theObject))
135 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
137 boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModuleBase_Tools::shape(aResult);
139 boost::shared_ptr<GeomAPI_AISObject> aAISObj = getAISObject(theObject);
140 Handle(AIS_Shape) aAISShape = Handle(AIS_Shape)::DownCast(aAISObj->impl<Handle(AIS_InteractiveObject)>());
141 if (!aAISShape.IsNull()) {
142 aAISShape->Set(aShapePtr->impl<TopoDS_Shape>());
143 AISContext()->Redisplay(aAISShape, isUpdateViewer);
147 GeomPresentablePtr aPrs = boost::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
149 boost::shared_ptr<GeomAPI_AISObject> aAISObj = aPrs->getAISObject(getAISObject(theObject));
150 Handle(AIS_InteractiveObject) aAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
151 AISContext()->Redisplay(aAISIO, isUpdateViewer);
156 void XGUI_Displayer::activateInLocalContext(ResultPtr theResult,
157 const std::list<int>& theModes, const bool isUpdateViewer)
159 Handle(AIS_InteractiveContext) aContext = AISContext();
160 // Open local context if there is no one
161 if (!aContext->HasOpenedContext()) {
162 aContext->ClearCurrents(false);
163 aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
165 // display or redisplay presentation
166 Handle(AIS_InteractiveObject) anAIS;
167 if (isVisible(theResult))
169 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theResult];
171 anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
174 // Activate selection of objects from prs
175 if (!anAIS.IsNull()) {
176 aContext->Load(anAIS, -1, true/*allow decomposition*/);
177 aContext->Deactivate(anAIS);
179 std::list<int>::const_iterator anIt = theModes.begin(), aLast = theModes.end();
180 for (; anIt != aLast; anIt++)
182 aContext->Activate(anAIS, (*anIt));
190 void XGUI_Displayer::stopSelection(const QResultList& theResults, const bool isStop,
191 const bool isUpdateViewer)
193 Handle(AIS_InteractiveContext) aContext = AISContext();
195 Handle(AIS_Shape) anAIS;
196 QResultList::const_iterator anIt = theResults.begin(), aLast = theResults.end();
198 for (; anIt != aLast; anIt++) {
200 if (isVisible(aFeature))
201 anAIS = Handle(AIS_Shape)::DownCast(myResult2AISObjectMap[aFeature]->impl<Handle(AIS_InteractiveObject)>());
206 QColor aColor(Qt::white);
207 anAIS->SetColor(Quantity_Color(aColor.red()/255., aColor.green()/255., aColor.blue()/255., Quantity_TOC_RGB));
211 QColor aColor(Qt::red);
212 anAIS->SetColor(Quantity_Color(aColor.red()/255., aColor.green()/255., aColor.blue()/255., Quantity_TOC_RGB));
220 void XGUI_Displayer::setSelected(const QResultList& theResults, const bool isUpdateViewer)
222 Handle(AIS_InteractiveContext) aContext = AISContext();
223 // we need to unhighligth objects manually in the current local context
224 // in couple with the selection clear (TODO)
225 Handle(AIS_LocalContext) aLocalContext = aContext->LocalContext();
226 if (!aLocalContext.IsNull())
227 aLocalContext->UnhilightLastDetected(myWorkshop->viewer()->activeView());
229 aContext->ClearSelected();
230 foreach(ResultPtr aResult, theResults) {
231 if (myResult2AISObjectMap.find(aResult) == myResult2AISObjectMap.end())
234 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[aResult];
237 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
239 aContext->AddOrRemoveSelected(anAIS, false);
247 /*void XGUI_Displayer::EraseAll(const bool isUpdateViewer)
249 Handle(AIS_InteractiveContext) ic = AISContext();
251 AIS_ListOfInteractive aList;
252 ic->DisplayedObjects(aList);
253 AIS_ListIteratorOfListOfInteractive anIter(aList);
254 for (; anIter.More(); anIter.Next()) {
255 if ((anIter.Value()->DynamicType() == STANDARD_TYPE(AIS_Trihedron)))
259 Handle(AIS_InteractiveObject) anIO = anIter.Value();
260 ic->Erase(anIO, false);
262 myResult2AISObjectMap.clear();
267 void XGUI_Displayer::eraseDeletedResults(const bool isUpdateViewer)
269 Handle(AIS_InteractiveContext) aContext = AISContext();
271 ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(),
272 aFLast = myResult2AISObjectMap.end();
273 std::list<ObjectPtr> aRemoved;
274 for (; aFIt != aFLast; aFIt++)
276 ObjectPtr aFeature = (*aFIt).first;
277 if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
278 boost::shared_ptr<GeomAPI_AISObject> anObj = (*aFIt).second;
279 if (!anObj) continue;
280 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
281 if (!anAIS.IsNull()) {
282 aContext->Erase(anAIS, false);
283 aRemoved.push_back(aFeature);
287 std::list<ObjectPtr>::const_iterator anIt = aRemoved.begin(),
288 aLast = aRemoved.end();
289 for (; anIt != aLast; anIt++) {
290 myResult2AISObjectMap.erase(myResult2AISObjectMap.find(*anIt));
297 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
299 closeAllContexts(true);
302 boost::shared_ptr<GeomAPI_AISObject> XGUI_Displayer::getAISObject(ObjectPtr theObject) const
304 boost::shared_ptr<GeomAPI_AISObject> anIO;
305 if (myResult2AISObjectMap.find(theObject) != myResult2AISObjectMap.end())
306 anIO = (myResult2AISObjectMap.find(theObject))->second;
310 ObjectPtr XGUI_Displayer::getObject(Handle(AIS_InteractiveObject) theIO) const
313 ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(),
314 aFLast = myResult2AISObjectMap.end();
315 for (; aFIt != aFLast && !aFeature; aFIt++) {
316 boost::shared_ptr<GeomAPI_AISObject> anObj = (*aFIt).second;
317 if (!anObj) continue;
318 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
321 aFeature = (*aFIt).first;
326 void XGUI_Displayer::closeAllContexts(const bool isUpdateViewer)
328 Handle(AIS_InteractiveContext) ic = AISContext();
330 ic->CloseAllContexts(false);
336 void XGUI_Displayer::updateViewer()
338 Handle(AIS_InteractiveContext) ic = AISContext();
340 ic->UpdateCurrentViewer();
343 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
345 return myWorkshop->viewer()->AISContext();