Salome HOME
#968 parameter creation problem
[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
11 #include <XGUI_ModuleConnector.h>
12 #include <XGUI_Workshop.h>
13 #include <XGUI_Displayer.h>
14
15 #include <ModuleBase_IWorkshop.h>
16 #include <ModuleBase_IViewer.h>
17
18 #include <GeomValidators_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 OPERATION_PARAMETER_COLOR "255, 255, 0"
27
28 PartSet_CustomPrs::PartSet_CustomPrs(ModuleBase_IWorkshop* theWorkshop)
29   : myWorkshop(theWorkshop), myIsActive(false)
30 {
31   initPrs();
32 }
33
34 bool PartSet_CustomPrs::isActive()
35 {
36   return myIsActive;
37   /*Handle(PartSet_OperationPrs) anOperationPrs = getPresentation();
38   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
39
40   return !aContext.IsNull() && aContext->IsDisplayed(anOperationPrs);*/
41 }
42
43 bool PartSet_CustomPrs::activate(const FeaturePtr& theFeature, const bool theUpdateViewer)
44 {
45   bool isModified = false;
46   Handle(PartSet_OperationPrs) anOperationPrs = getPresentation();
47
48   if (anOperationPrs->canActivate(theFeature)) {
49     myIsActive = true;
50     anOperationPrs->setFeature(theFeature);
51     if (theFeature.get()) {
52       displayPresentation(theUpdateViewer);
53       isModified = true;
54     }
55   }
56   return isModified;
57 }
58
59 bool PartSet_CustomPrs::deactivate(const bool theUpdateViewer)
60 {
61   myIsActive = false;
62   bool isModified = false;
63
64   Handle(PartSet_OperationPrs) anOperationPrs = getPresentation();
65   anOperationPrs->setFeature(FeaturePtr());
66
67   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
68   if (!aContext.IsNull() && aContext->IsDisplayed(anOperationPrs)) {
69     erasePresentation(theUpdateViewer);
70     isModified = true;
71   }
72
73   return isModified;
74 }
75
76
77 void PartSet_CustomPrs::displayPresentation(const bool theUpdateViewer)
78 {
79   Handle(PartSet_OperationPrs) anOperationPrs = getPresentation();
80
81   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
82   if (!aContext.IsNull() && !aContext->IsDisplayed(anOperationPrs)) {
83     if (anOperationPrs->hasShapes()) {
84       PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
85       XGUI_Workshop* aWorkshop = workshop();
86       aWorkshop->displayer()->displayAIS(myOperationPrs, false/*load object in selection*/, theUpdateViewer);
87       aContext->SetZLayer(anOperationPrs, aModule->getVisualLayerId());
88     }
89   }
90   else {
91     if (!anOperationPrs->hasShapes())
92       erasePresentation(theUpdateViewer);
93     else {
94       anOperationPrs->Redisplay();
95       if (theUpdateViewer)
96         workshop()->displayer()->updateViewer();
97     }
98   }
99 }
100
101 void PartSet_CustomPrs::erasePresentation(const bool theUpdateViewer)
102 {
103   XGUI_Workshop* aWorkshop = workshop();
104   aWorkshop->displayer()->eraseAIS(myOperationPrs, theUpdateViewer);
105 }
106
107 Handle(PartSet_OperationPrs) PartSet_CustomPrs::getPresentation()
108 {
109   if (!myOperationPrs.get())
110     initPrs();
111   Handle(AIS_InteractiveObject) anAISIO = myOperationPrs->impl<Handle(AIS_InteractiveObject)>();
112   return Handle(PartSet_OperationPrs)::DownCast(anAISIO);
113 }
114
115 bool PartSet_CustomPrs::redisplay(const ObjectPtr& theObject, const bool theUpdateViewer)
116 {
117   bool isModified = false;
118   // the presentation should be recomputed if the previous AIS depend on the result
119   // [it should be hiddend] or the new AIS depend on it [it should be visualized]
120   Handle(PartSet_OperationPrs) anOperationPrs = getPresentation();
121   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
122   if (!aContext.IsNull()) {
123     if (aContext->IsDisplayed(anOperationPrs)) {
124       // if there are performance poblems, to improve them, the necessity of redisplay can be checked
125       //bool aChanged = anOperationPrs->dependOn(theObject);
126       anOperationPrs->updateShapes();
127       //aChanged = aChanged || anOperationPrs->dependOn(theObject);
128       //if (aChanged)
129       anOperationPrs->Redisplay();
130       isModified = true;
131       if (theUpdateViewer)
132         workshop()->displayer()->updateViewer();
133     }
134     else {
135       anOperationPrs->updateShapes();
136       displayPresentation(theUpdateViewer);
137     }
138   }
139   return isModified;
140 }
141
142 void PartSet_CustomPrs::clearPrs()
143 {
144   Handle(PartSet_OperationPrs) anOperationPrs = getPresentation();
145   if (!anOperationPrs.IsNull())
146     anOperationPrs.Nullify();
147
148   myOperationPrs.reset();
149 }
150
151 void PartSet_CustomPrs::initPrs()
152 {
153   myOperationPrs = AISObjectPtr(new GeomAPI_AISObject());
154   myOperationPrs->setImpl(new Handle(AIS_InteractiveObject)(new PartSet_OperationPrs(myWorkshop)));
155
156   std::vector<int> aColor = Config_PropManager::color("Visualization", "operation_parameter_color",
157                                                       OPERATION_PARAMETER_COLOR);
158   myOperationPrs->setColor(aColor[0], aColor[1], aColor[2]);
159
160   myOperationPrs->setPointMarker(5, 2.);
161   myOperationPrs->setWidth(1);
162 }
163
164 XGUI_Workshop* PartSet_CustomPrs::workshop() const
165 {
166   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
167   return aConnector->workshop();
168 }