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