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