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