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