]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_CustomPrs.cpp
Salome HOME
8c268fd1d2754375195ff041dc945ec1ea0031c9
[modules/shaper.git] / src / PartSet / PartSet_CustomPrs.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_CustomPrs.cpp
4 // Created:     30 Jun 2015
5 // Author:      Natalia ERMOLAEVA
6
7 #include <PartSet_CustomPrs.h>
8 #include <PartSet_Module.h>
9 #include "PartSet_OperationPrs.h"
10 #include "PartSet_OverconstraintListener.h"
11
12 #include <XGUI_ModuleConnector.h>
13 #include <XGUI_Workshop.h>
14 #include <XGUI_Displayer.h>
15
16 #include <ModuleBase_IWorkshop.h>
17 #include <ModuleBase_IViewer.h>
18 #include <ModuleBase_Tools.h>
19
20 #include <Config_PropManager.h>
21 #include <Events_Loop.h>
22 #include <ModelAPI_Events.h>
23
24 #include <AIS_InteractiveContext.hxx>
25 #include <AIS_InteractiveObject.hxx>
26 #include <Prs3d_PointAspect.hxx>
27
28 //#define DO_NOT_VISUALIZE_CUSTOM_PRESENTATION
29
30 PartSet_CustomPrs::PartSet_CustomPrs(ModuleBase_IWorkshop* theWorkshop)
31   : myWorkshop(theWorkshop), myFeature(FeaturePtr()), myPresentationIsEmpty(false)
32 {
33   Events_Loop* aLoop = Events_Loop::loop();
34   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_EMPTY_OPERATION_PRESENTATION));
35
36   initPresentation(ModuleBase_IModule::CustomizeArguments);
37   initPresentation(ModuleBase_IModule::CustomizeResults);
38   initPresentation(ModuleBase_IModule::CustomizeHighlightedObjects);
39
40   myIsActive[ModuleBase_IModule::CustomizeArguments] = false;
41   myIsActive[ModuleBase_IModule::CustomizeResults] = false;
42   myIsActive[ModuleBase_IModule::CustomizeHighlightedObjects] = false;
43 }
44
45 bool PartSet_CustomPrs::isActive(const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag)
46 {
47   return myIsActive[theFlag];
48 }
49
50 bool PartSet_CustomPrs::activate(const FeaturePtr& theFeature,
51                                  const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
52                                  const bool theUpdateViewer)
53 {
54 #ifdef DO_NOT_VISUALIZE_CUSTOM_PRESENTATION
55   return false;
56 #endif
57
58   myIsActive[theFlag] = true;
59   myFeature = theFeature;
60
61   bool isModified = false;
62   if (theFeature.get()) {
63     displayPresentation(ModuleBase_IModule::CustomizeArguments, theUpdateViewer);
64     displayPresentation(ModuleBase_IModule::CustomizeResults, theUpdateViewer);
65     displayPresentation(ModuleBase_IModule::CustomizeHighlightedObjects, theUpdateViewer);
66     isModified = true;
67   }
68   return isModified;
69 }
70
71 bool PartSet_CustomPrs::deactivate(const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
72                                    const bool theUpdateViewer)
73 {
74   myIsActive[theFlag] = false;
75   bool isModified = false;
76
77   erasePresentation(ModuleBase_IModule::CustomizeArguments, theUpdateViewer);
78   erasePresentation(ModuleBase_IModule::CustomizeResults, theUpdateViewer);
79   erasePresentation(ModuleBase_IModule::CustomizeHighlightedObjects, theUpdateViewer);
80   isModified = true;
81
82   return isModified;
83 }
84
85 bool PartSet_CustomPrs::displayPresentation(
86                                   const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
87                                   const bool theUpdateViewer)
88 {
89   bool isModified = false;
90
91   // update the AIS objects content
92   AISObjectPtr aPresentation = getPresentation(theFlag, true);
93   Handle(AIS_InteractiveObject) anAISIO = aPresentation->impl<Handle(AIS_InteractiveObject)>();
94   Handle(PartSet_OperationPrs) anOperationPrs = Handle(PartSet_OperationPrs)::DownCast(anAISIO);
95
96   // do nothing if the feature can not be displayed [is moved from presentation, to be checked]
97   if (!myFeature.get())
98     return isModified;
99
100   QMap<ObjectPtr, QList<GeomShapePtr> > aFeatureShapes;
101   switch (theFlag) {
102     case ModuleBase_IModule::CustomizeArguments:
103       PartSet_OperationPrs::getFeatureShapes(myFeature, myWorkshop, aFeatureShapes);
104       break;
105     case ModuleBase_IModule::CustomizeResults:
106       PartSet_OperationPrs::getResultShapes(myFeature, myWorkshop, aFeatureShapes);
107       break;
108     case ModuleBase_IModule::CustomizeHighlightedObjects:
109       PartSet_OperationPrs::getHighlightedShapes(myWorkshop, aFeatureShapes);
110       break;
111     default:
112       return isModified;
113   }
114   NCollection_DataMap<TopoDS_Shape, Handle_AIS_InteractiveObject>& aShapeMap =
115                                                                  anOperationPrs->shapesMap();
116   PartSet_OperationPrs::fillShapeList(aFeatureShapes, myWorkshop, aShapeMap);
117
118   myPresentationIsEmpty = false;
119   // redisplay AIS objects
120   bool aRedisplayed = false;
121   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
122   if (!aContext.IsNull() && !aContext->IsDisplayed(anOperationPrs)) {
123     // when the feature can not be visualized in the module, the operation preview should not
124     // be visualized also
125     if (anOperationPrs->hasShapes() && myWorkshop->module()->canDisplayObject(myFeature)) {
126       // set color here because it can be changed in preferences
127       Quantity_Color aShapeColor = getShapeColor(theFlag);
128       anOperationPrs->setShapeColor(aShapeColor);
129
130       PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
131       XGUI_Workshop* aWorkshop = workshop();
132       aRedisplayed = aWorkshop->displayer()->displayAIS(myPresentations[theFlag],
133                                          false/*load object in selection*/, false);
134       aContext->SetZLayer(anOperationPrs, aModule->getVisualLayerId());
135       isModified = true;
136     }
137   }
138   else {
139     // when the feature can not be visualized in the module, the operation preview should not
140     // be visualized also
141     if (!anOperationPrs->hasShapes() || !myWorkshop->module()->canDisplayObject(myFeature)) {
142       aRedisplayed = erasePresentation(theFlag, false);
143       isModified = true;
144     }
145     else {
146       anOperationPrs->Redisplay();
147       isModified = true;
148       aRedisplayed = true;
149     }
150   }
151   if (myPresentationIsEmpty) {
152     aRedisplayed = erasePresentation(theFlag, false);
153   }
154   if (aRedisplayed && theUpdateViewer)
155     workshop()->displayer()->updateViewer();
156
157   return isModified;
158 }
159
160 bool PartSet_CustomPrs::erasePresentation(const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
161                                           const bool theUpdateViewer)
162 {
163   bool isErased = false;
164   XGUI_Workshop* aWorkshop = workshop();
165   if (myPresentations.contains(theFlag))
166     isErased = aWorkshop->displayer()->eraseAIS(myPresentations[theFlag], theUpdateViewer);
167   return isErased;
168 }
169
170 void PartSet_CustomPrs::clearPresentation(const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag)
171 {
172   AISObjectPtr aPresentation = getPresentation(theFlag, false);
173   if (aPresentation.get()) {
174     Handle(AIS_InteractiveObject) anAISIO = aPresentation->impl<Handle(AIS_InteractiveObject)>();
175     Handle(PartSet_OperationPrs) anOperationPrs = Handle(PartSet_OperationPrs)::DownCast(anAISIO);
176
177     anOperationPrs->shapesMap().Clear();
178     if (!anOperationPrs.IsNull())
179       anOperationPrs.Nullify();
180     myPresentations.remove(theFlag);
181   }
182 }
183
184 AISObjectPtr PartSet_CustomPrs::getPresentation(
185                                        const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
186                                        const bool theToCreate)
187 {
188   Handle(PartSet_OperationPrs) aPresentation;
189
190   AISObjectPtr anOperationPrs;
191   if (myPresentations.contains(theFlag))
192     anOperationPrs = myPresentations[theFlag];
193   
194   if (!anOperationPrs.get() && theToCreate) {
195     initPresentation(theFlag);
196     anOperationPrs = myPresentations[theFlag];
197   }
198
199   return anOperationPrs;
200 }
201
202 bool PartSet_CustomPrs::redisplay(const ObjectPtr& theObject,
203                                   const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
204                                   const bool theUpdateViewer)
205 {
206 #ifdef DO_NOT_VISUALIZE_CUSTOM_PRESENTATION
207   return false;
208 #endif
209   bool aRedisplayed = false;
210   if (myIsActive[theFlag])
211     aRedisplayed = displayPresentation(theFlag, theUpdateViewer);
212   
213   return aRedisplayed;
214 }
215
216 void PartSet_CustomPrs::clearPrs()
217 {
218   clearPresentation(ModuleBase_IModule::CustomizeArguments);
219   clearPresentation(ModuleBase_IModule::CustomizeResults);
220   clearPresentation(ModuleBase_IModule::CustomizeHighlightedObjects);
221 }
222
223 void PartSet_CustomPrs::processEvent(const std::shared_ptr<Events_Message>& theMessage)
224 {
225   if (theMessage->eventID() == Events_Loop::eventByName(EVENT_EMPTY_OPERATION_PRESENTATION))
226     myPresentationIsEmpty = true; /// store state to analize it after display/erase is finished
227 }
228
229 void PartSet_CustomPrs::initPresentation(const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag)
230 {
231   AISObjectPtr anOperationPrs = AISObjectPtr(new GeomAPI_AISObject());
232   Handle(PartSet_OperationPrs) anAISPrs = new PartSet_OperationPrs(myWorkshop);
233   anOperationPrs->setImpl(new Handle(AIS_InteractiveObject)(anAISPrs));
234   if (theFlag == ModuleBase_IModule::CustomizeArguments ||
235       theFlag == ModuleBase_IModule::CustomizeResults) {
236     anOperationPrs->setPointMarker(5, 2.);
237     anOperationPrs->setWidth(1);
238   }
239   else if (theFlag == ModuleBase_IModule::CustomizeHighlightedObjects)
240     anAISPrs->useAISWidth();
241
242   if (anOperationPrs.get())
243     myPresentations[theFlag] = anOperationPrs;
244 }
245
246 Quantity_Color PartSet_CustomPrs::getShapeColor(
247                                   const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag)
248 {
249   Quantity_Color aColor;
250   switch(theFlag) {
251     case ModuleBase_IModule::CustomizeArguments:
252       aColor = ModuleBase_Tools::color("Visualization", "operation_parameter_color",
253                                        OPERATION_PARAMETER_COLOR());
254     break;
255     case ModuleBase_IModule::CustomizeResults:
256       aColor = ModuleBase_Tools::color("Visualization", "operation_result_color",
257                                        OPERATION_RESULT_COLOR());
258     break;
259     case ModuleBase_IModule::CustomizeHighlightedObjects:
260       aColor = ModuleBase_Tools::color("Visualization", "operation_highlight_color",
261                                        OPERATION_HIGHLIGHT_COLOR());
262     break;
263     default:
264     break;
265   }
266   return aColor;
267 }
268
269 XGUI_Workshop* PartSet_CustomPrs::workshop() const
270 {
271   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
272   return aConnector->workshop();
273 }