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>
17 #include <AIS_InteractiveContext.hxx>
18 #include <AIS_LocalContext.hxx>
19 #include <AIS_ListOfInteractive.hxx>
20 #include <AIS_ListIteratorOfListOfInteractive.hxx>
21 #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(ResultPtr theResult)
40 return myResult2AISObjectMap.find(theResult) != myResult2AISObjectMap.end();
43 bool XGUI_Displayer::display(ResultPtr theResult, bool isUpdateViewer)
45 boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModuleBase_Tools::shape(theResult);
48 boost::shared_ptr<GeomAPI_AISObject> anAIS(new GeomAPI_AISObject());
49 anAIS->createShape(aShapePtr);
50 return display(theResult, anAIS, isUpdateViewer);
55 bool XGUI_Displayer::display(ResultPtr theResult,
56 boost::shared_ptr<GeomAPI_AISObject> theAIS, bool isUpdateViewer)
58 Handle(AIS_InteractiveContext) aContext = AISContext();
60 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
61 if (!anAISIO.IsNull()) {
62 myResult2AISObjectMap[theResult] = theAIS;
63 aContext->Display(anAISIO, isUpdateViewer);
71 void XGUI_Displayer::erase(ResultPtr theResult,
72 const bool isUpdateViewer)
74 if (myResult2AISObjectMap.find(theResult) == myResult2AISObjectMap.end())
77 Handle(AIS_InteractiveContext) aContext = AISContext();
78 boost::shared_ptr<GeomAPI_AISObject> anObject = myResult2AISObjectMap[theResult];
80 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
81 if (!anAIS.IsNull()) {
82 aContext->Erase(anAIS, isUpdateViewer);
86 myResult2AISObjectMap.erase(theResult);
90 bool XGUI_Displayer::redisplay(ResultPtr theFeature,
91 boost::shared_ptr<GeomAPI_AISObject> theAIS,
92 const bool isUpdateViewer)
94 bool isCreated = false;
95 Handle(AIS_InteractiveObject) anAIS =
96 theAIS ? theAIS->impl<Handle(AIS_InteractiveObject)>() : Handle(AIS_InteractiveObject)();
97 Handle(AIS_InteractiveContext) aContext = AISContext();
98 // Open local context if there is no one
99 if (!aContext->HasOpenedContext()) {
100 aContext->ClearCurrents(false);
101 aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
102 // set mouse sensitivity
103 //aContext->SetSensitivityMode(StdSelect_SM_WINDOW);
104 //aContext->SetPixelTolerance(MOUSE_SENSITIVITY_IN_PIXEL);
106 // display or redisplay presentation
107 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theFeature];
108 if (isVisible(theFeature) && anObj && !anObj->empty()) {
109 aContext->RecomputeSelectionOnly(anAIS);
112 myResult2AISObjectMap[theFeature] = theAIS;
113 aContext->Display(anAIS, false);
122 bool XGUI_Displayer::redisplay(ResultPtr theResult, bool isUpdateViewer)
124 if (!isVisible(theResult))
127 boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModuleBase_Tools::shape(theResult);
129 boost::shared_ptr<GeomAPI_AISObject> aAISObj = getAISObject(theResult);
130 Handle(AIS_Shape) aAISShape = Handle(AIS_Shape)::DownCast(aAISObj->impl<Handle(AIS_InteractiveObject)>());
131 if (!aAISShape.IsNull()) {
132 aAISShape->Set(aShapePtr->impl<TopoDS_Shape>());
133 AISContext()->Redisplay(aAISShape);
140 void XGUI_Displayer::activateInLocalContext(ResultPtr theResult,
141 const std::list<int>& theModes, const bool isUpdateViewer)
143 Handle(AIS_InteractiveContext) aContext = AISContext();
144 // Open local context if there is no one
145 if (!aContext->HasOpenedContext()) {
146 aContext->ClearCurrents(false);
147 aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
149 // display or redisplay presentation
150 Handle(AIS_InteractiveObject) anAIS;
151 if (isVisible(theResult))
153 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[theResult];
155 anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
158 // Activate selection of objects from prs
159 if (!anAIS.IsNull()) {
160 aContext->Load(anAIS, -1, true/*allow decomposition*/);
161 aContext->Deactivate(anAIS);
163 std::list<int>::const_iterator anIt = theModes.begin(), aLast = theModes.end();
164 for (; anIt != aLast; anIt++)
166 aContext->Activate(anAIS, (*anIt));
174 void XGUI_Displayer::stopSelection(const QResultList& theResults, const bool isStop,
175 const bool isUpdateViewer)
177 Handle(AIS_InteractiveContext) aContext = AISContext();
179 Handle(AIS_Shape) anAIS;
180 QResultList::const_iterator anIt = theResults.begin(), aLast = theResults.end();
182 for (; anIt != aLast; anIt++) {
184 if (isVisible(aFeature))
185 anAIS = Handle(AIS_Shape)::DownCast(myResult2AISObjectMap[aFeature]->impl<Handle(AIS_InteractiveObject)>());
190 QColor aColor(Qt::white);
191 anAIS->SetColor(Quantity_Color(aColor.red()/255., aColor.green()/255., aColor.blue()/255., Quantity_TOC_RGB));
195 QColor aColor(Qt::red);
196 anAIS->SetColor(Quantity_Color(aColor.red()/255., aColor.green()/255., aColor.blue()/255., Quantity_TOC_RGB));
204 void XGUI_Displayer::setSelected(const QResultList& theResults, const bool isUpdateViewer)
206 Handle(AIS_InteractiveContext) aContext = AISContext();
207 // we need to unhighligth objects manually in the current local context
208 // in couple with the selection clear (TODO)
209 Handle(AIS_LocalContext) aLocalContext = aContext->LocalContext();
210 if (!aLocalContext.IsNull())
211 aLocalContext->UnhilightLastDetected(myWorkshop->viewer()->activeView());
213 aContext->ClearSelected();
214 foreach(ResultPtr aResult, theResults) {
215 if (myResult2AISObjectMap.find(aResult) == myResult2AISObjectMap.end())
218 boost::shared_ptr<GeomAPI_AISObject> anObj = myResult2AISObjectMap[aResult];
221 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
223 aContext->AddOrRemoveSelected(anAIS, false);
231 /*void XGUI_Displayer::EraseAll(const bool isUpdateViewer)
233 Handle(AIS_InteractiveContext) ic = AISContext();
235 AIS_ListOfInteractive aList;
236 ic->DisplayedObjects(aList);
237 AIS_ListIteratorOfListOfInteractive anIter(aList);
238 for (; anIter.More(); anIter.Next()) {
239 if ((anIter.Value()->DynamicType() == STANDARD_TYPE(AIS_Trihedron)))
243 Handle(AIS_InteractiveObject) anIO = anIter.Value();
244 ic->Erase(anIO, false);
246 myResult2AISObjectMap.clear();
251 void XGUI_Displayer::eraseDeletedResults(const bool isUpdateViewer)
253 Handle(AIS_InteractiveContext) aContext = AISContext();
255 ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(),
256 aFLast = myResult2AISObjectMap.end();
257 std::list<ResultPtr> aRemoved;
258 for (; aFIt != aFLast; aFIt++)
260 ResultPtr aFeature = (*aFIt).first;
261 if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
262 boost::shared_ptr<GeomAPI_AISObject> anObj = (*aFIt).second;
263 if (!anObj) continue;
264 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
265 if (!anAIS.IsNull()) {
266 aContext->Erase(anAIS, false);
267 aRemoved.push_back(aFeature);
271 std::list<ResultPtr>::const_iterator anIt = aRemoved.begin(),
272 aLast = aRemoved.end();
273 for (; anIt != aLast; anIt++) {
274 myResult2AISObjectMap.erase(myResult2AISObjectMap.find(*anIt));
281 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
283 closeAllContexts(true);
286 boost::shared_ptr<GeomAPI_AISObject> XGUI_Displayer::getAISObject(
287 ResultPtr theFeature) const
289 boost::shared_ptr<GeomAPI_AISObject> anIO;
290 if (myResult2AISObjectMap.find(theFeature) != myResult2AISObjectMap.end())
291 anIO = (myResult2AISObjectMap.find(theFeature))->second;
295 ResultPtr XGUI_Displayer::getResult(Handle(AIS_InteractiveObject) theIO) const
298 ResultToAISMap::const_iterator aFIt = myResult2AISObjectMap.begin(),
299 aFLast = myResult2AISObjectMap.end();
300 for (; aFIt != aFLast && !aFeature; aFIt++) {
301 boost::shared_ptr<GeomAPI_AISObject> anObj = (*aFIt).second;
302 if (!anObj) continue;
303 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
306 aFeature = (*aFIt).first;
311 void XGUI_Displayer::closeAllContexts(const bool isUpdateViewer)
313 Handle(AIS_InteractiveContext) ic = AISContext();
315 ic->CloseAllContexts(false);
321 void XGUI_Displayer::updateViewer()
323 Handle(AIS_InteractiveContext) ic = AISContext();
325 ic->UpdateCurrentViewer();
328 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
330 return myWorkshop->viewer()->AISContext();