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