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)
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);
52 anAIS = aPrs->getAISObject(boost::shared_ptr<GeomAPI_AISObject>());
54 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
56 boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
58 anAIS = boost::shared_ptr<GeomAPI_AISObject>(new GeomAPI_AISObject());
59 anAIS->createShape(aShapePtr);
64 display(theObject, anAIS, isUpdateViewer);
68 void XGUI_Displayer::display(ObjectPtr theObject, boost::shared_ptr<GeomAPI_AISObject> theAIS,
71 Handle(AIS_InteractiveContext) aContext = AISContext();
73 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
74 if (!anAISIO.IsNull()) {
75 myResult2AISObjectMap[theObject] = theAIS;
76 aContext->Display(anAISIO, isUpdateViewer);
80 void XGUI_Displayer::erase(ObjectPtr theObject, const bool isUpdateViewer)
82 if (!isVisible(theObject))
85 Handle(AIS_InteractiveContext) aContext = AISContext();
86 boost::shared_ptr<GeomAPI_AISObject> anObject = myResult2AISObjectMap[theObject];
88 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
89 if (!anAIS.IsNull()) {
90 aContext->Remove(anAIS, isUpdateViewer);
93 myResult2AISObjectMap.erase(theObject);
96 /*bool XGUI_Displayer::redisplay(ObjectPtr theObject,
97 boost::shared_ptr<GeomAPI_AISObject> theAIS,
98 const bool isUpdateViewer)
100 bool isCreated = false;
101 Handle(AIS_InteractiveObject) anAIS =
102 theAIS ? theAIS->impl<Handle(AIS_InteractiveObject)>() : Handle(AIS_InteractiveObject)();
103 Handle(AIS_InteractiveContext) aContext = AISContext();
104 // Open local context if there is no one
105 if (!aContext->HasOpenedContext()) {
106 aContext->ClearCurrents(false);
107 aContext->OpenLocalContext(false /use displayed objects/, true /allow shape decomposition/);
108 // set mouse sensitivity
109 //aContext->SetSensitivityMode(StdSelect_SM_WINDOW);
110 //aContext->SetPixelTolerance(MOUSE_SENSITIVITY_IN_PIXEL);
112 // display or redisplay presentation
113 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theObject];
114 if (isVisible(theObject) && anObj && !anObj->empty()) {
115 aContext->Redisplay(anAIS, isUpdateViewer);
116 //aContext->RecomputeSelectionOnly(anAIS);
119 myResult2AISObjectMap[theObject] = theAIS;
120 aContext->Display(anAIS, isUpdateViewer);
126 void XGUI_Displayer::redisplay(ObjectPtr theObject, bool isUpdateViewer)
128 if (!isVisible(theObject))
131 Handle(AIS_InteractiveObject) aAISIO;
132 boost::shared_ptr<GeomAPI_AISObject> aAISObj = getAISObject(theObject);
133 GeomPresentablePtr aPrs = boost::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
135 boost::shared_ptr<GeomAPI_AISObject> aAIS_Obj = aPrs->getAISObject(aAISObj);
136 if (aAISObj && !aAIS_Obj) {
137 erase(theObject, isUpdateViewer);
140 aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
142 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
144 boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
146 Handle(AIS_Shape) aAISShape = Handle(AIS_Shape)::DownCast(
147 aAISObj->impl<Handle(AIS_InteractiveObject)>());
148 if (!aAISShape.IsNull()) {
149 aAISShape->Set(aShapePtr->impl<TopoDS_Shape>());
155 if (!aAISIO.IsNull()) {
156 Handle(AIS_InteractiveContext) aContext = AISContext();
157 aContext->Redisplay(aAISIO, isUpdateViewer);
158 //if (aContext->HasOpenedContext()) {
159 // aContext->Load(aAISIO, -1, true/*allow decomposition*/);
164 void XGUI_Displayer::activateInLocalContext(ObjectPtr theResult, const std::list<int>& theModes,
165 const bool isUpdateViewer)
167 Handle(AIS_InteractiveContext) aContext = AISContext();
168 // Open local context if there is no one
169 if (!aContext->HasOpenedContext()) {
170 aContext->ClearCurrents(false);
171 //aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
172 aContext->OpenLocalContext();
173 aContext->NotUseDisplayedObjects();
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)
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::activate(ObjectPtr theObject)
213 if (isVisible(theObject)) {
214 Handle(AIS_InteractiveContext) aContext = AISContext();
216 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theObject];
217 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
218 aContext->Activate(anAIS);
222 void XGUI_Displayer::stopSelection(const QList<ObjectPtr>& theResults, const bool isStop,
223 const bool isUpdateViewer)
225 Handle(AIS_InteractiveContext) aContext = AISContext();
227 Handle(AIS_Shape) anAIS;
228 QList<ObjectPtr>::const_iterator anIt = theResults.begin(), aLast = theResults.end();
230 for (; anIt != aLast; anIt++) {
232 if (isVisible(aFeature))
233 anAIS = Handle(AIS_Shape)::DownCast(
234 myResult2AISObjectMap[aFeature]->impl<Handle(AIS_InteractiveObject)>());
239 QColor aColor(Qt::white);
241 Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
245 QColor aColor(Qt::red);
247 Quantity_Color(aColor.red() / 255., aColor.green() / 255., aColor.blue() / 255.,
256 void XGUI_Displayer::setSelected(const QList<ObjectPtr>& theResults, const bool isUpdateViewer)
258 Handle(AIS_InteractiveContext) aContext = AISContext();
259 // we need to unhighligth objects manually in the current local context
260 // in couple with the selection clear (TODO)
261 Handle(AIS_LocalContext) aLocalContext = aContext->LocalContext();
262 if (!aLocalContext.IsNull())
263 aLocalContext->UnhilightLastDetected(myWorkshop->viewer()->activeView());
265 aContext->ClearSelected();
266 foreach(ObjectPtr aResult, theResults)
268 if (myResult2AISObjectMap.find(aResult) == myResult2AISObjectMap.end())
271 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[aResult];
273 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
275 aContext->AddOrRemoveSelected(anAIS, false);
282 void XGUI_Displayer::eraseAll(const bool isUpdateViewer)
284 Handle(AIS_InteractiveContext) ic = AISContext();
286 ResultToAISMap::iterator aIt;
287 for (aIt = myResult2AISObjectMap.begin(); aIt != myResult2AISObjectMap.end(); aIt++) {
289 boost::shared_ptr<GeomAPI_AISObject> aAISObj = (*aIt).second;
290 Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
292 ic->Remove(anIO, false);
294 myResult2AISObjectMap.clear();
299 void XGUI_Displayer::eraseDeletedResults(const bool isUpdateViewer)
301 Handle(AIS_InteractiveContext) aContext = AISContext();
303 ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(), aFLast =
304 myResult2AISObjectMap.end();
305 std::list<ObjectPtr> aRemoved;
306 for (; aFIt != aFLast; aFIt++) {
307 ObjectPtr aFeature = (*aFIt).first;
308 if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
309 boost::shared_ptr<GeomAPI_AISObject> anObj = (*aFIt).second;
312 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
313 if (!anAIS.IsNull()) {
314 aContext->Remove(anAIS, false);
315 aRemoved.push_back(aFeature);
319 std::list<ObjectPtr>::const_iterator anIt = aRemoved.begin(), aLast = aRemoved.end();
320 for (; anIt != aLast; anIt++) {
321 myResult2AISObjectMap.erase(myResult2AISObjectMap.find(*anIt));
328 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
330 AISContext()->ClearSelected(false);
331 closeAllContexts(true);
334 boost::shared_ptr<GeomAPI_AISObject> XGUI_Displayer::getAISObject(ObjectPtr theObject) const
336 boost::shared_ptr<GeomAPI_AISObject> anIO;
337 if (myResult2AISObjectMap.find(theObject) != myResult2AISObjectMap.end())
338 anIO = (myResult2AISObjectMap.find(theObject))->second;
342 ObjectPtr XGUI_Displayer::getObject(Handle(AIS_InteractiveObject) theIO) const
345 ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(), aFLast =
346 myResult2AISObjectMap.end();
347 for (; aFIt != aFLast && !aFeature; aFIt++) {
348 boost::shared_ptr<GeomAPI_AISObject> anObj = (*aFIt).second;
351 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
354 aFeature = (*aFIt).first;
359 void XGUI_Displayer::closeAllContexts(const bool isUpdateViewer)
361 Handle(AIS_InteractiveContext) ic = AISContext();
363 ic->CloseAllContexts(false);
369 void XGUI_Displayer::updateViewer()
371 Handle(AIS_InteractiveContext) ic = AISContext();
373 ic->UpdateCurrentViewer();
376 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
378 return myWorkshop->viewer()->AISContext();
381 void XGUI_Displayer::display(boost::shared_ptr<GeomAPI_AISObject> theAIS, bool isUpdate)
383 Handle(AIS_InteractiveContext) aContext = AISContext();
384 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
385 if (!anAISIO.IsNull())
386 aContext->Display(anAISIO, isUpdate);
389 void XGUI_Displayer::erase(boost::shared_ptr<GeomAPI_AISObject> theAIS, const bool isUpdate)
391 Handle(AIS_InteractiveContext) aContext = AISContext();
392 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
393 if (!anAISIO.IsNull()) {
394 aContext->Remove(anAISIO, isUpdate);
398 void XGUI_Displayer::activateObjectsOutOfContext(const std::list<int>& theModes,
399 Handle(SelectMgr_Filter) theFilter)
401 Handle(AIS_InteractiveContext) aContext = AISContext();
402 // Open local context if there is no one
403 if (!aContext->HasOpenedContext())
406 aContext->UseDisplayedObjects();
407 std::list<int>::const_iterator anIt = theModes.begin(), aLast = theModes.end();
408 for (; anIt != aLast; anIt++) {
409 aContext->ActivateStandardMode((TopAbs_ShapeEnum)(*anIt));
412 if (!theFilter.IsNull())
413 aContext->AddFilter(theFilter);
417 void XGUI_Displayer::deactivateObjectsOutOfContext()
419 Handle(AIS_InteractiveContext) aContext = AISContext();
420 // Open local context if there is no one
421 if (!aContext->HasOpenedContext())
424 aContext->RemoveFilters();
425 aContext->NotUseDisplayedObjects();