Salome HOME
bf4a8b8d69510de6c687584a9309113299e1d96d
[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
22 #include <AIS_InteractiveContext.hxx>
23 #include <AIS_InteractiveObject.hxx>
24 #include <Prs3d_PointAspect.hxx>
25
26 //#define DO_NOT_VISUALIZE_CUSTOM_PRESENTATION
27
28 PartSet_CustomPrs::PartSet_CustomPrs(ModuleBase_IWorkshop* theWorkshop)
29   : myWorkshop(theWorkshop), myFeature(FeaturePtr())
30 {
31   initPresentation(ModuleBase_IModule::CustomizeArguments);
32   initPresentation(ModuleBase_IModule::CustomizeResults);
33   initPresentation(ModuleBase_IModule::CustomizeHighlightedObjects);
34
35   myIsActive[ModuleBase_IModule::CustomizeArguments] = false;
36   myIsActive[ModuleBase_IModule::CustomizeResults] = false;
37   myIsActive[ModuleBase_IModule::CustomizeHighlightedObjects] = false;
38 }
39
40 bool PartSet_CustomPrs::isActive(const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag)
41 {
42   return myIsActive[theFlag];
43 }
44
45 bool PartSet_CustomPrs::activate(const FeaturePtr& theFeature,
46                                  const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
47                                  const bool theUpdateViewer)
48 {
49 #ifdef DO_NOT_VISUALIZE_CUSTOM_PRESENTATION
50   return false;
51 #endif
52
53   myIsActive[theFlag] = true;
54   myFeature = theFeature;
55
56   bool isModified = false;
57   if (theFeature.get()) {
58     displayPresentation(ModuleBase_IModule::CustomizeArguments, theUpdateViewer);
59     displayPresentation(ModuleBase_IModule::CustomizeResults, theUpdateViewer);
60     displayPresentation(ModuleBase_IModule::CustomizeHighlightedObjects, theUpdateViewer);
61     isModified = true;
62   }
63   return isModified;
64 }
65
66 bool PartSet_CustomPrs::deactivate(const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
67                                    const bool theUpdateViewer)
68 {
69   myIsActive[theFlag] = false;
70   bool isModified = false;
71
72   erasePresentation(ModuleBase_IModule::CustomizeArguments, theUpdateViewer);
73   erasePresentation(ModuleBase_IModule::CustomizeResults, theUpdateViewer);
74   erasePresentation(ModuleBase_IModule::CustomizeHighlightedObjects, theUpdateViewer);
75   isModified = true;
76
77   return isModified;
78 }
79
80 bool PartSet_CustomPrs::displayPresentation(
81                                   const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
82                                   const bool theUpdateViewer)
83 {
84   bool isModified = false;
85
86   // update the AIS objects content
87   AISObjectPtr aPresentation = getPresentation(theFlag, true);
88   Handle(AIS_InteractiveObject) anAISIO = aPresentation->impl<Handle(AIS_InteractiveObject)>();
89   Handle(PartSet_OperationPrs) anOperationPrs = Handle(PartSet_OperationPrs)::DownCast(anAISIO);
90
91   // do nothing if the feature can not be displayed [is moved from presentation, to be checked]
92   if (!myFeature.get())
93     return isModified;
94
95   switch (theFlag) {
96     case ModuleBase_IModule::CustomizeArguments:
97       PartSet_OperationPrs::getFeatureShapes(myFeature, myWorkshop, anOperationPrs->featureShapes());
98       break;
99     case ModuleBase_IModule::CustomizeResults:
100       PartSet_OperationPrs::getResultShapes(myFeature, myWorkshop, anOperationPrs->featureShapes());
101       break;
102     case ModuleBase_IModule::CustomizeHighlightedObjects:
103       PartSet_OperationPrs::getHighlightedShapes(myWorkshop, anOperationPrs->featureShapes());
104       break;
105     default:
106       return isModified;
107   }
108
109   // redisplay AIS objects
110   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
111   if (!aContext.IsNull() && !aContext->IsDisplayed(anOperationPrs)) {
112     // when the feature can not be visualized in the module, the operation preview should not
113     // be visualized also
114     if (anOperationPrs->hasShapes() && myWorkshop->module()->canDisplayObject(myFeature)) {
115       // set color here because it can be changed in preferences
116       Quantity_Color aShapeColor = getShapeColor(theFlag);
117       anOperationPrs->setShapeColor(aShapeColor);
118
119       PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
120       XGUI_Workshop* aWorkshop = workshop();
121       aWorkshop->displayer()->displayAIS(myPresentations[theFlag], false/*load object in selection*/,
122                                          theUpdateViewer);
123       aContext->SetZLayer(anOperationPrs, aModule->getVisualLayerId());
124       isModified = true;
125     }
126   }
127   else {
128     // when the feature can not be visualized in the module, the operation preview should not
129     // be visualized also
130     if (!anOperationPrs->hasShapes() || !myWorkshop->module()->canDisplayObject(myFeature)) {
131       erasePresentation(theFlag, theUpdateViewer);
132       isModified = true;
133     }
134     else {
135       anOperationPrs->Redisplay();
136       isModified = true;
137       if (theUpdateViewer)
138         workshop()->displayer()->updateViewer();
139     }
140   }
141   return isModified;
142 }
143
144 void PartSet_CustomPrs::erasePresentation(const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
145                                           const bool theUpdateViewer)
146 {
147   XGUI_Workshop* aWorkshop = workshop();
148   if (myPresentations.contains(theFlag))
149     aWorkshop->displayer()->eraseAIS(myPresentations[theFlag], theUpdateViewer);
150 }
151
152 void PartSet_CustomPrs::clearPresentation(const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag)
153 {
154   AISObjectPtr aPresentation = getPresentation(theFlag, false);
155   if (aPresentation.get()) {
156     Handle(AIS_InteractiveObject) anAISIO = aPresentation->impl<Handle(AIS_InteractiveObject)>();
157     Handle(PartSet_OperationPrs) anOperationPrs = Handle(PartSet_OperationPrs)::DownCast(anAISIO);
158
159     anOperationPrs->featureShapes().clear();
160     if (!anOperationPrs.IsNull())
161       anOperationPrs.Nullify();
162     myPresentations.remove(theFlag);
163   }
164 }
165
166 AISObjectPtr PartSet_CustomPrs::getPresentation(
167                                        const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
168                                        const bool theToCreate)
169 {
170   Handle(PartSet_OperationPrs) aPresentation;
171
172   AISObjectPtr anOperationPrs;
173   if (myPresentations.contains(theFlag))
174     anOperationPrs = myPresentations[theFlag];
175   
176   if (!anOperationPrs.get() && theToCreate) {
177     initPresentation(theFlag);
178     anOperationPrs = myPresentations[theFlag];
179   }
180
181   return anOperationPrs;
182 }
183
184 bool PartSet_CustomPrs::redisplay(const ObjectPtr& theObject,
185                                   const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
186                                   const bool theUpdateViewer)
187 {
188 #ifdef DO_NOT_VISUALIZE_CUSTOM_PRESENTATION
189   return false;
190 #endif
191   bool aRedisplayed = false;
192   if (myIsActive[theFlag])
193     aRedisplayed = displayPresentation(theFlag, theUpdateViewer);
194   
195   return aRedisplayed;
196 }
197
198 void PartSet_CustomPrs::clearPrs()
199 {
200   clearPresentation(ModuleBase_IModule::CustomizeArguments);
201   clearPresentation(ModuleBase_IModule::CustomizeResults);
202   clearPresentation(ModuleBase_IModule::CustomizeHighlightedObjects);
203 }
204
205 void PartSet_CustomPrs::initPresentation(const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag)
206 {
207   AISObjectPtr anOperationPrs = AISObjectPtr(new GeomAPI_AISObject());
208   Handle(PartSet_OperationPrs) anAISPrs = new PartSet_OperationPrs(myWorkshop);
209   anOperationPrs->setImpl(new Handle(AIS_InteractiveObject)(anAISPrs));
210   if (theFlag == ModuleBase_IModule::CustomizeArguments ||
211       theFlag == ModuleBase_IModule::CustomizeResults) {
212     anOperationPrs->setPointMarker(5, 2.);
213     anOperationPrs->setWidth(1);
214   }
215   else if (theFlag == ModuleBase_IModule::CustomizeHighlightedObjects)
216     anAISPrs->useAISWidth();
217
218   if (anOperationPrs.get())
219     myPresentations[theFlag] = anOperationPrs;
220 }
221
222 Quantity_Color PartSet_CustomPrs::getShapeColor(
223                                   const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag)
224 {
225   Quantity_Color aColor;
226   switch(theFlag) {
227     case ModuleBase_IModule::CustomizeArguments:
228       aColor = ModuleBase_Tools::color("Visualization", "operation_parameter_color",
229                                        OPERATION_PARAMETER_COLOR());
230     break;
231     case ModuleBase_IModule::CustomizeResults:
232       aColor = ModuleBase_Tools::color("Visualization", "operation_result_color",
233                                        OPERATION_RESULT_COLOR());
234     break;
235     case ModuleBase_IModule::CustomizeHighlightedObjects:
236       aColor = ModuleBase_Tools::color("Visualization", "operation_highlight_color",
237                                        OPERATION_HIGHLIGHT_COLOR());
238     break;
239     default:
240     break;
241   }
242   return aColor;
243 }
244
245 XGUI_Workshop* PartSet_CustomPrs::workshop() const
246 {
247   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
248   return aConnector->workshop();
249 }