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