Salome HOME
Documentation update
[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 && theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
49     AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
50     if (aColorAttr.get() && aColorAttr->size()) {
51       theColor.push_back(aColorAttr->value(0));
52       theColor.push_back(aColorAttr->value(1));
53       theColor.push_back(aColorAttr->value(2));
54     }
55   }
56 }
57
58 void XGUI_CustomPrs::getDefaultColor(ObjectPtr theObject, const bool isEmptyColorValid,
59                                      std::vector<int>& theColor)
60 {
61   theColor.clear();
62   // get default color from the preferences manager for the given result
63   if (theColor.empty()) {
64     std::string aSection, aName, aDefault;
65     theObject->colorConfigInfo(aSection, aName, aDefault);
66     if (!aSection.empty() && !aName.empty()) {
67       theColor = Config_PropManager::color(aSection, aName, aDefault);
68     }
69   }
70   if (!isEmptyColorValid && theColor.empty()) {
71     // all AIS objects, where the color is not set, are in black.
72     // The color should be defined in XML or set in the attribute
73     theColor = Config_PropManager::color("Visualization", "object_default_color", "#000000");
74     Events_InfoMessage("XGUI_CustomPrs", 
75       "A default color is not defined in the preferences for this kind of result").send();
76   }
77 }
78
79 double XGUI_CustomPrs::getDefaultDeflection(const ObjectPtr& theObject)
80 {
81   double aDeflection = -1;
82   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
83   if (aResult.get()) {
84     bool isConstruction = false;
85
86     std::string aResultGroup = aResult->groupName();
87     if (aResultGroup == ModelAPI_ResultConstruction::group())
88       isConstruction = true;
89     else if (aResultGroup == ModelAPI_ResultBody::group()) {
90       GeomShapePtr aGeomShape = aResult->shape();
91       if (aGeomShape.get()) {
92         // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
93         // correction of deviation for them should not influence to the application performance
94         GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
95         isConstruction = !anExp.more();
96       }
97     }
98     if (isConstruction)
99       aDeflection = Config_PropManager::real("Visualization", "construction_deflection",
100                                              ModelAPI_ResultConstruction::DEFAULT_DEFLECTION());
101     else
102       aDeflection = Config_PropManager::real("Visualization", "body_deflection",
103                                              ModelAPI_ResultBody::DEFAULT_DEFLECTION());
104   }
105   return aDeflection;
106 }
107
108 XGUI_CustomPrs::XGUI_CustomPrs(XGUI_Workshop* theWorkshop)
109 : myWorkshop(theWorkshop)
110 {
111 }
112
113 void XGUI_CustomPrs::getResultColor(const ResultPtr& theResult, std::vector<int>& theColor)
114 {
115   getColor(theResult, theColor);
116   if (theColor.empty())
117     getDefaultColor(theResult, false, theColor);
118 }
119
120 double XGUI_CustomPrs::getResultDeflection(const ResultPtr& theResult)
121 {
122   double aDeflection = getDeflection(theResult);
123   if (aDeflection < 0)
124     aDeflection = getDefaultDeflection(theResult);
125   return aDeflection;
126 }
127
128 bool XGUI_CustomPrs::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
129                                            std::shared_ptr<GeomAPI_ICustomPrs> theCustomPrs)
130 {
131   bool aCustomized = false;
132   if (theResult.get()) {
133     std::vector<int> aColor;
134     getResultColor(theResult, aColor);
135
136     SessionPtr aMgr = ModelAPI_Session::get();
137     if (aMgr->activeDocument() != theResult->document()) {
138       QColor aQColor(aColor[0], aColor[1], aColor[2]);
139       QColor aNewColor = QColor::fromHsvF(aQColor.hueF(), aQColor.saturationF()/3., aQColor.valueF());
140       aColor[0] = aNewColor.red();
141       aColor[1] = aNewColor.green();
142       aColor[2] = aNewColor.blue();
143     }
144     aCustomized = !aColor.empty() && thePrs->setColor(aColor[0], aColor[1], aColor[2]);
145
146     aCustomized = thePrs->setDeflection(getResultDeflection(theResult)) | aCustomized;
147   }
148   ModuleBase_IModule* aModule = myWorkshop->module();
149   aCustomized = aModule->customisePresentation(theResult, thePrs, theCustomPrs) || aCustomized;
150   return aCustomized;
151 }