Salome HOME
Issue #1664: In the Sketcher, add the function Split a segment: split of arc, move...
[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(theFlag, theUpdateViewer);
64     isModified = true;
65   }
66   return isModified;
67 }
68
69 bool PartSet_CustomPrs::deactivate(const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
70                                    const bool theUpdateViewer)
71 {
72   myIsActive[theFlag] = false;
73   erasePresentation(theFlag, theUpdateViewer);
74   return true;
75 }
76
77 bool PartSet_CustomPrs::displayPresentation(
78                                   const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
79                                   const bool theUpdateViewer)
80 {
81   bool isModified = false;
82
83   // update the AIS objects content
84   AISObjectPtr aPresentation = getPresentation(theFlag, true);
85   Handle(AIS_InteractiveObject) anAISIO = aPresentation->impl<Handle(AIS_InteractiveObject)>();
86   Handle(PartSet_OperationPrs) anOperationPrs = Handle(PartSet_OperationPrs)::DownCast(anAISIO);
87
88   // do nothing if the feature can not be displayed [is moved from presentation, to be checked]
89   if (!myFeature.get())
90     return isModified;
91
92   QMap<ObjectPtr, QList<GeomShapePtr> > aFeatureShapes;
93   switch (theFlag) {
94     case ModuleBase_IModule::CustomizeArguments:
95       PartSet_OperationPrs::getFeatureShapes(myFeature, myWorkshop, aFeatureShapes);
96       break;
97     case ModuleBase_IModule::CustomizeResults:
98       PartSet_OperationPrs::getResultShapes(myFeature, myWorkshop, aFeatureShapes);
99       break;
100     case ModuleBase_IModule::CustomizeHighlightedObjects:
101       PartSet_OperationPrs::getHighlightedShapes(myWorkshop, aFeatureShapes);
102       break;
103     default:
104       return isModified;
105   }
106   NCollection_DataMap<TopoDS_Shape, Handle_AIS_InteractiveObject>& aShapeMap =
107                                                                  anOperationPrs->shapesMap();
108   PartSet_OperationPrs::fillShapeList(aFeatureShapes, myWorkshop, aShapeMap);
109
110   myPresentationIsEmpty = false;
111   // redisplay AIS objects
112   bool aRedisplayed = false;
113   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
114   if (!aContext.IsNull() && !aContext->IsDisplayed(anOperationPrs)) {
115     // when the feature can not be visualized in the module, the operation preview should not
116     // be visualized also
117     if (anOperationPrs->hasShapes() && myWorkshop->module()->canDisplayObject(myFeature)) {
118       // set color here because it can be changed in preferences
119       Quantity_Color aShapeColor = getShapeColor(theFlag);
120       anOperationPrs->setShapeColor(aShapeColor);
121
122       PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
123       XGUI_Workshop* aWorkshop = workshop();
124       aRedisplayed = aWorkshop->displayer()->displayAIS(myPresentations[theFlag],
125                                          false/*load object in selection*/, false);
126       aContext->SetZLayer(anOperationPrs, aModule->getVisualLayerId());
127       isModified = true;
128     }
129   }
130   else {
131     // when the feature can not be visualized in the module, the operation preview should not
132     // be visualized also
133     if (!anOperationPrs->hasShapes() || !myWorkshop->module()->canDisplayObject(myFeature)) {
134       aRedisplayed = erasePresentation(theFlag, false);
135       isModified = true;
136     }
137     else {
138       anOperationPrs->Redisplay();
139       isModified = true;
140       aRedisplayed = true;
141     }
142   }
143   if (myPresentationIsEmpty) {
144     aRedisplayed = erasePresentation(theFlag, false);
145   }
146   if (aRedisplayed && theUpdateViewer)
147     workshop()->displayer()->updateViewer();
148
149   return isModified;
150 }
151
152 bool PartSet_CustomPrs::erasePresentation(const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
153                                           const bool theUpdateViewer)
154 {
155   bool isErased = false;
156   XGUI_Workshop* aWorkshop = workshop();
157   if (myPresentations.contains(theFlag))
158     isErased = aWorkshop->displayer()->eraseAIS(myPresentations[theFlag], theUpdateViewer);
159   return isErased;
160 }
161
162 void PartSet_CustomPrs::clearPresentation(const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag)
163 {
164   AISObjectPtr aPresentation = getPresentation(theFlag, false);
165   if (aPresentation.get()) {
166     Handle(AIS_InteractiveObject) anAISIO = aPresentation->impl<Handle(AIS_InteractiveObject)>();
167     Handle(PartSet_OperationPrs) anOperationPrs = Handle(PartSet_OperationPrs)::DownCast(anAISIO);
168
169     anOperationPrs->shapesMap().Clear();
170     if (!anOperationPrs.IsNull())
171       anOperationPrs.Nullify();
172     myPresentations.remove(theFlag);
173   }
174 }
175
176 AISObjectPtr PartSet_CustomPrs::getPresentation(
177                                        const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
178                                        const bool theToCreate)
179 {
180   Handle(PartSet_OperationPrs) aPresentation;
181
182   AISObjectPtr anOperationPrs;
183   if (myPresentations.contains(theFlag))
184     anOperationPrs = myPresentations[theFlag];
185   
186   if (!anOperationPrs.get() && theToCreate) {
187     initPresentation(theFlag);
188     anOperationPrs = myPresentations[theFlag];
189   }
190
191   return anOperationPrs;
192 }
193
194 bool PartSet_CustomPrs::redisplay(const ObjectPtr& theObject,
195                                   const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
196                                   const bool theUpdateViewer)
197 {
198 #ifdef DO_NOT_VISUALIZE_CUSTOM_PRESENTATION
199   return false;
200 #endif
201   bool aRedisplayed = false;
202   if (myIsActive[theFlag])
203     aRedisplayed = displayPresentation(theFlag, theUpdateViewer);
204   
205   return aRedisplayed;
206 }
207
208 void PartSet_CustomPrs::clearPrs()
209 {
210   clearPresentation(ModuleBase_IModule::CustomizeArguments);
211   clearPresentation(ModuleBase_IModule::CustomizeResults);
212   clearPresentation(ModuleBase_IModule::CustomizeHighlightedObjects);
213 }
214
215 void PartSet_CustomPrs::processEvent(const std::shared_ptr<Events_Message>& theMessage)
216 {
217   if (theMessage->eventID() == Events_Loop::eventByName(EVENT_EMPTY_OPERATION_PRESENTATION))
218     myPresentationIsEmpty = true; /// store state to analize it after display/erase is finished
219 }
220
221 void PartSet_CustomPrs::initPresentation(const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag)
222 {
223   AISObjectPtr anOperationPrs = AISObjectPtr(new GeomAPI_AISObject());
224   Handle(PartSet_OperationPrs) anAISPrs = new PartSet_OperationPrs(myWorkshop);
225   anOperationPrs->setImpl(new Handle(AIS_InteractiveObject)(anAISPrs));
226   if (theFlag == ModuleBase_IModule::CustomizeArguments ||
227       theFlag == ModuleBase_IModule::CustomizeResults) {
228     anOperationPrs->setPointMarker(5, 2.);
229     anOperationPrs->setWidth(1);
230   }
231   else if (theFlag == ModuleBase_IModule::CustomizeHighlightedObjects)
232     anAISPrs->useAISWidth();
233
234   if (anOperationPrs.get())
235     myPresentations[theFlag] = anOperationPrs;
236 }
237
238 Quantity_Color PartSet_CustomPrs::getShapeColor(
239                                   const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag)
240 {
241   Quantity_Color aColor;
242   switch(theFlag) {
243     case ModuleBase_IModule::CustomizeArguments:
244       aColor = ModuleBase_Tools::color("Visualization", "operation_parameter_color",
245                                        OPERATION_PARAMETER_COLOR());
246     break;
247     case ModuleBase_IModule::CustomizeResults:
248       aColor = ModuleBase_Tools::color("Visualization", "operation_result_color",
249                                        OPERATION_RESULT_COLOR());
250     break;
251     case ModuleBase_IModule::CustomizeHighlightedObjects:
252       aColor = ModuleBase_Tools::color("Visualization", "operation_highlight_color",
253                                        OPERATION_HIGHLIGHT_COLOR());
254     break;
255     default:
256     break;
257   }
258   return aColor;
259 }
260
261 XGUI_Workshop* PartSet_CustomPrs::workshop() const
262 {
263   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
264   return aConnector->workshop();
265 }