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 "XGUI_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(FeaturePtr theFeature)
40 FeaturePtr aFeature = XGUI_Tools::realFeature(theFeature);
41 return myFeature2AISObjectMap.find(aFeature) != myFeature2AISObjectMap.end();
44 bool XGUI_Displayer::display(FeaturePtr theFeature, bool isUpdateViewer)
46 FeaturePtr aFeature = XGUI_Tools::realFeature(theFeature);
47 boost::shared_ptr<GeomAPI_Shape> aShapePtr = aFeature->data()->shape();
50 boost::shared_ptr<GeomAPI_AISObject> anAIS(new GeomAPI_AISObject());
51 anAIS->createShape(aShapePtr);
52 return display(aFeature, anAIS, isUpdateViewer);
57 bool XGUI_Displayer::display(FeaturePtr theFeature,
58 boost::shared_ptr<GeomAPI_AISObject> theAIS, bool isUpdateViewer)
60 Handle(AIS_InteractiveContext) aContext = AISContext();
62 Handle(AIS_InteractiveObject) anAISIO = theAIS->impl<Handle(AIS_InteractiveObject)>();
63 if (!anAISIO.IsNull()) {
64 myFeature2AISObjectMap[theFeature] = theAIS;
65 aContext->Display(anAISIO, isUpdateViewer);
72 std::list<XGUI_ViewerPrs> XGUI_Displayer::getSelected(const int theShapeTypeToSkip)
74 std::set<FeaturePtr > aPrsFeatures;
75 std::list<XGUI_ViewerPrs> aPresentations;
77 Handle(AIS_InteractiveContext) aContext = AISContext();
78 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
79 Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
80 TopoDS_Shape aShape = aContext->SelectedShape();
82 if (theShapeTypeToSkip >= 0 && !aShape.IsNull() && aShape.ShapeType() == theShapeTypeToSkip)
85 FeaturePtr aFeature = getFeature(anIO);
86 if (aPrsFeatures.find(aFeature) != aPrsFeatures.end())
88 Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
89 aPresentations.push_back(XGUI_ViewerPrs(aFeature, aShape, anOwner));
90 aPrsFeatures.insert(aFeature);
92 return aPresentations;
95 QFeatureList XGUI_Displayer::selectedFeatures() const
97 QFeatureList aSelectedList;
99 Handle(AIS_InteractiveContext) aContext = AISContext();
100 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
101 Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
102 FeaturePtr aFeature = getFeature(anIO);
104 aSelectedList.append(aFeature);
106 return aSelectedList;
110 std::list<XGUI_ViewerPrs> XGUI_Displayer::getHighlighted(const int theShapeTypeToSkip)
112 std::set<FeaturePtr > aPrsFeatures;
113 std::list<XGUI_ViewerPrs> aPresentations;
115 Handle(AIS_InteractiveContext) aContext = AISContext();
116 for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
117 Handle(AIS_InteractiveObject) anIO = aContext->DetectedInteractive();
118 TopoDS_Shape aShape = aContext->DetectedShape();
119 if (theShapeTypeToSkip >= 0 && !aShape.IsNull() && aShape.ShapeType() == theShapeTypeToSkip)
122 FeaturePtr aFeature = getFeature(anIO);
123 if (aPrsFeatures.find(aFeature) != aPrsFeatures.end())
125 aPresentations.push_back(XGUI_ViewerPrs(aFeature, aShape, NULL));
126 aPrsFeatures.insert(aFeature);
129 return aPresentations;
132 void XGUI_Displayer::erase(FeaturePtr theFeature,
133 const bool isUpdateViewer)
135 FeaturePtr aFeature = XGUI_Tools::realFeature(theFeature);
137 if (myFeature2AISObjectMap.find(aFeature) == myFeature2AISObjectMap.end())
140 Handle(AIS_InteractiveContext) aContext = AISContext();
141 boost::shared_ptr<GeomAPI_AISObject> anObject = myFeature2AISObjectMap[aFeature];
143 Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
144 if (!anAIS.IsNull()) {
145 aContext->Erase(anAIS, isUpdateViewer);
149 myFeature2AISObjectMap.erase(aFeature);
153 bool XGUI_Displayer::redisplay(FeaturePtr theFeature,
154 boost::shared_ptr<GeomAPI_AISObject> theAIS,
155 const bool isUpdateViewer)
157 bool isCreated = false;
158 Handle(AIS_InteractiveObject) anAIS =
159 theAIS ? theAIS->impl<Handle(AIS_InteractiveObject)>() : Handle(AIS_InteractiveObject)();
160 Handle(AIS_InteractiveContext) aContext = AISContext();
161 // Open local context if there is no one
162 if (!aContext->HasOpenedContext()) {
163 aContext->ClearCurrents(false);
164 aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
165 // set mouse sensitivity
166 //aContext->SetSensitivityMode(StdSelect_SM_WINDOW);
167 //aContext->SetPixelTolerance(MOUSE_SENSITIVITY_IN_PIXEL);
169 // display or redisplay presentation
170 boost::shared_ptr<GeomAPI_AISObject> anObj = myFeature2AISObjectMap[theFeature];
171 if (isVisible(theFeature) && anObj && !anObj->empty()) {
172 aContext->RecomputeSelectionOnly(anAIS);
175 myFeature2AISObjectMap[theFeature] = theAIS;
176 aContext->Display(anAIS, false);
185 bool XGUI_Displayer::redisplay(FeaturePtr theFeature, bool isUpdateViewer)
187 FeaturePtr aFeature = XGUI_Tools::realFeature(theFeature);
188 if (!isVisible(aFeature))
191 boost::shared_ptr<GeomAPI_Shape> aShapePtr = aFeature->data()->shape();
193 boost::shared_ptr<GeomAPI_AISObject> aAISObj = getAISObject(aFeature);
194 Handle(AIS_Shape) aAISShape = Handle(AIS_Shape)::DownCast(aAISObj->impl<Handle(AIS_InteractiveObject)>());
195 if (!aAISShape.IsNull()) {
196 aAISShape->Set(aShapePtr->impl<TopoDS_Shape>());
197 AISContext()->Redisplay(aAISShape);
204 void XGUI_Displayer::activateInLocalContext(FeaturePtr theFeature,
205 const std::list<int>& theModes, const bool isUpdateViewer)
207 Handle(AIS_InteractiveContext) aContext = AISContext();
208 // Open local context if there is no one
209 if (!aContext->HasOpenedContext()) {
210 aContext->ClearCurrents(false);
211 aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
213 // display or redisplay presentation
214 Handle(AIS_InteractiveObject) anAIS;
215 if (isVisible(theFeature))
217 boost::shared_ptr<GeomAPI_AISObject> anObj = myFeature2AISObjectMap[theFeature];
219 anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
222 // Activate selection of objects from prs
223 if (!anAIS.IsNull()) {
224 aContext->Load(anAIS, -1, true/*allow decomposition*/);
225 aContext->Deactivate(anAIS);
227 std::list<int>::const_iterator anIt = theModes.begin(), aLast = theModes.end();
228 for (; anIt != aLast; anIt++)
230 aContext->Activate(anAIS, (*anIt));
238 void XGUI_Displayer::stopSelection(const QFeatureList& theFeatures, const bool isStop,
239 const bool isUpdateViewer)
241 Handle(AIS_InteractiveContext) aContext = AISContext();
243 Handle(AIS_Shape) anAIS;
244 QFeatureList::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
246 for (; anIt != aLast; anIt++) {
248 if (isVisible(aFeature))
249 anAIS = Handle(AIS_Shape)::DownCast(myFeature2AISObjectMap[aFeature]->impl<Handle(AIS_InteractiveObject)>());
254 QColor aColor(Qt::white);
255 anAIS->SetColor(Quantity_Color(aColor.red()/255., aColor.green()/255., aColor.blue()/255., Quantity_TOC_RGB));
259 QColor aColor(Qt::red);
260 anAIS->SetColor(Quantity_Color(aColor.red()/255., aColor.green()/255., aColor.blue()/255., Quantity_TOC_RGB));
268 void XGUI_Displayer::setSelected(const QFeatureList& theFeatures, const bool isUpdateViewer)
270 Handle(AIS_InteractiveContext) aContext = AISContext();
271 // we need to unhighligth objects manually in the current local context
272 // in couple with the selection clear (TODO)
273 Handle(AIS_LocalContext) aLocalContext = aContext->LocalContext();
274 if (!aLocalContext.IsNull())
275 aLocalContext->UnhilightLastDetected(myWorkshop->viewer()->activeView());
277 aContext->ClearSelected();
278 foreach(FeaturePtr aFeature, theFeatures) {
279 FeaturePtr aRFeature = XGUI_Tools::realFeature(aFeature);
280 if (myFeature2AISObjectMap.find(aRFeature) == myFeature2AISObjectMap.end())
283 boost::shared_ptr<GeomAPI_AISObject> anObj = myFeature2AISObjectMap[aRFeature];
286 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
288 aContext->AddOrRemoveSelected(anAIS, false);
296 /*void XGUI_Displayer::EraseAll(const bool isUpdateViewer)
298 Handle(AIS_InteractiveContext) ic = AISContext();
300 AIS_ListOfInteractive aList;
301 ic->DisplayedObjects(aList);
302 AIS_ListIteratorOfListOfInteractive anIter(aList);
303 for (; anIter.More(); anIter.Next()) {
304 if ((anIter.Value()->DynamicType() == STANDARD_TYPE(AIS_Trihedron)))
308 Handle(AIS_InteractiveObject) anIO = anIter.Value();
309 ic->Erase(anIO, false);
311 myFeature2AISObjectMap.clear();
316 void XGUI_Displayer::eraseDeletedFeatures(const bool isUpdateViewer)
318 Handle(AIS_InteractiveContext) aContext = AISContext();
320 FeatureToAISMap::const_iterator aFIt = myFeature2AISObjectMap.begin(),
321 aFLast = myFeature2AISObjectMap.end();
322 std::list<FeaturePtr> aRemoved;
323 for (; aFIt != aFLast; aFIt++)
325 FeaturePtr aFeature = (*aFIt).first;
326 if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
327 boost::shared_ptr<GeomAPI_AISObject> anObj = (*aFIt).second;
328 if (!anObj) continue;
329 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
330 if (!anAIS.IsNull()) {
331 aContext->Erase(anAIS, false);
332 aRemoved.push_back(aFeature);
336 std::list<FeaturePtr>::const_iterator anIt = aRemoved.begin(),
337 aLast = aRemoved.end();
338 for (; anIt != aLast; anIt++) {
339 myFeature2AISObjectMap.erase(myFeature2AISObjectMap.find(*anIt));
346 void XGUI_Displayer::closeLocalContexts(const bool isUpdateViewer)
348 closeAllContexts(true);
351 boost::shared_ptr<GeomAPI_AISObject> XGUI_Displayer::getAISObject(
352 FeaturePtr theFeature) const
354 boost::shared_ptr<GeomAPI_AISObject> anIO;
355 if (myFeature2AISObjectMap.find(theFeature) != myFeature2AISObjectMap.end())
356 anIO = (myFeature2AISObjectMap.find(theFeature))->second;
360 FeaturePtr XGUI_Displayer::getFeature(Handle(AIS_InteractiveObject) theIO) const
363 FeatureToAISMap::const_iterator aFIt = myFeature2AISObjectMap.begin(),
364 aFLast = myFeature2AISObjectMap.end();
365 for (; aFIt != aFLast && !aFeature; aFIt++) {
366 boost::shared_ptr<GeomAPI_AISObject> anObj = (*aFIt).second;
367 if (!anObj) continue;
368 Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
371 aFeature = (*aFIt).first;
376 void XGUI_Displayer::closeAllContexts(const bool isUpdateViewer)
378 Handle(AIS_InteractiveContext) ic = AISContext();
380 ic->CloseAllContexts(false);
386 void XGUI_Displayer::updateViewer()
388 Handle(AIS_InteractiveContext) ic = AISContext();
390 ic->UpdateCurrentViewer();
393 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
395 return myWorkshop->viewer()->AISContext();