Salome HOME
Update copyrights
[modules/shaper.git] / src / XGUI / XGUI_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 <XGUI_CustomPrs.h>
21 #include <XGUI_Workshop.h>
22 #include <XGUI_Displayer.h>
23
24 #include <ModuleBase_IModule.h>
25
26 #include <ModelAPI_AttributeIntArray.h>
27 #include <ModelAPI_AttributeDouble.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_ResultBody.h>
30 #include <ModelAPI_ResultConstruction.h>
31
32 #include <GeomAPI_ShapeExplorer.h>
33
34 #include <Config_PropManager.h>
35
36 #include <Events_InfoMessage.h>
37
38 #include <vector>
39 #include <QColor>
40
41 double getDeflection(const ResultPtr& theResult)
42 {
43   double aDeflection = -1;
44   // get deflection from the attribute of the result
45   if (theResult.get() != NULL &&
46       theResult->data()->attribute(ModelAPI_Result::DEFLECTION_ID()).get() != NULL) {
47     AttributeDoublePtr aDoubleAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
48     if (aDoubleAttr.get() && aDoubleAttr->isInitialized()) {
49       double aValue = aDoubleAttr->value();
50       if (aValue > 0) /// zero value should not be used as a deflection(previous studies)
51         aDeflection = aDoubleAttr->value();
52     }
53   }
54   return aDeflection;
55 }
56
57 void getColor(const ResultPtr& theResult, std::vector<int>& theColor)
58 {
59   theColor.clear();
60   // get color from the attribute of the result
61   if (theResult.get() != NULL &&
62       theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
63     AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
64     if (aColorAttr.get() && aColorAttr->size()) {
65       theColor.push_back(aColorAttr->value(0));
66       theColor.push_back(aColorAttr->value(1));
67       theColor.push_back(aColorAttr->value(2));
68     }
69   }
70 }
71
72 void XGUI_CustomPrs::getDefaultColor(ObjectPtr theObject, const bool isEmptyColorValid,
73                                      std::vector<int>& theColor)
74 {
75   theColor.clear();
76   // get default color from the preferences manager for the given result
77   if (theColor.empty()) {
78     std::string aSection, aName, aDefault;
79     theObject->colorConfigInfo(aSection, aName, aDefault);
80     if (!aSection.empty() && !aName.empty()) {
81       theColor = Config_PropManager::color(aSection, aName);
82     }
83   }
84   if (!isEmptyColorValid && theColor.empty()) {
85     // all AIS objects, where the color is not set, are in black.
86     // The color should be defined in XML or set in the attribute
87     theColor = Config_PropManager::color("Visualization", "object_default_color");
88     Events_InfoMessage("XGUI_CustomPrs",
89       "A default color is not defined in the preferences for this kind of result").send();
90   }
91 }
92
93 double XGUI_CustomPrs::getDefaultDeflection(const ObjectPtr& theObject)
94 {
95   double aDeflection = -1;
96   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
97   if (aResult.get()) {
98     bool isConstruction = false;
99
100     std::string aResultGroup = aResult->groupName();
101     if (aResultGroup == ModelAPI_ResultConstruction::group())
102       isConstruction = true;
103     else if (aResultGroup == ModelAPI_ResultBody::group()) {
104       GeomShapePtr aGeomShape = aResult->shape();
105       if (aGeomShape.get()) {
106         // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
107         // correction of deviation for them should not influence to the application performance
108         GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
109         isConstruction = !anExp.more();
110       }
111     }
112     if (isConstruction)
113       aDeflection = Config_PropManager::real("Visualization", "construction_deflection");
114     else
115       aDeflection = Config_PropManager::real("Visualization", "body_deflection");
116   }
117   return aDeflection;
118 }
119
120 double getTransparency(const ResultPtr& theResult)
121 {
122   double aDeflection = -1;
123   // get transparency from the attribute of the result
124   if (theResult.get() != NULL &&
125       theResult->data()->attribute(ModelAPI_Result::TRANSPARENCY_ID()).get() != NULL) {
126     AttributeDoublePtr aDoubleAttr = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
127     if (aDoubleAttr.get() && aDoubleAttr->isInitialized()) {
128       double aValue = aDoubleAttr->value();
129       if (aValue > 0) /// zero value should not be used as a transparency(previous studies)
130         aDeflection = aDoubleAttr->value();
131     }
132   }
133   return aDeflection;
134 }
135
136 double getDefaultTransparency(const ResultPtr& theResult)
137 {
138   return 0;
139 }
140
141 XGUI_CustomPrs::XGUI_CustomPrs(XGUI_Workshop* theWorkshop)
142 : myWorkshop(theWorkshop)
143 {
144 }
145
146 void XGUI_CustomPrs::getResultColor(const ResultPtr& theResult, std::vector<int>& theColor)
147 {
148   getColor(theResult, theColor);
149   if (theColor.empty())
150     getDefaultColor(theResult, false, theColor);
151 }
152
153 double XGUI_CustomPrs::getResultDeflection(const ResultPtr& theResult)
154 {
155   double aDeflection = getDeflection(theResult);
156   if (aDeflection < 0)
157     aDeflection = getDefaultDeflection(theResult);
158   return aDeflection;
159 }
160
161 double XGUI_CustomPrs::getResultTransparency(const ResultPtr& theResult)
162 {
163   double aTransparency = getTransparency(theResult);
164   if (aTransparency < 0)
165     aTransparency = getDefaultTransparency(theResult);
166   return aTransparency;
167 }
168
169 bool XGUI_CustomPrs::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
170                                            std::shared_ptr<GeomAPI_ICustomPrs> theCustomPrs)
171 {
172   bool aCustomized = false;
173   if (theResult.get()) {
174     std::vector<int> aColor;
175     getResultColor(theResult, aColor);
176
177     SessionPtr aMgr = ModelAPI_Session::get();
178     if (aMgr->activeDocument() != theResult->document()) {
179       QColor aQColor(aColor[0], aColor[1], aColor[2]);
180       QColor aNewColor =
181         QColor::fromHsvF(aQColor.hueF(), aQColor.saturationF()/3., aQColor.valueF());
182       aColor[0] = aNewColor.red();
183       aColor[1] = aNewColor.green();
184       aColor[2] = aNewColor.blue();
185     }
186     aCustomized = !aColor.empty() && thePrs->setColor(aColor[0], aColor[1], aColor[2]);
187
188     aCustomized = thePrs->setDeflection(getResultDeflection(theResult)) | aCustomized;
189
190     aCustomized = thePrs->setTransparency(getResultTransparency(theResult)) | aCustomized;
191   }
192   ModuleBase_IModule* aModule = myWorkshop->module();
193   aCustomized = aModule->customisePresentation(theResult, thePrs, theCustomPrs) || aCustomized;
194   return aCustomized;
195 }