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