]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_CustomPrs.cpp
Salome HOME
Remove default values from properties requests
[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);
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");
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     else
102       aDeflection = Config_PropManager::real("Visualization", "body_deflection");
103   }
104   return aDeflection;
105 }
106
107 XGUI_CustomPrs::XGUI_CustomPrs(XGUI_Workshop* theWorkshop)
108 : myWorkshop(theWorkshop)
109 {
110 }
111
112 void XGUI_CustomPrs::getResultColor(const ResultPtr& theResult, std::vector<int>& theColor)
113 {
114   getColor(theResult, theColor);
115   if (theColor.empty())
116     getDefaultColor(theResult, false, theColor);
117 }
118
119 double XGUI_CustomPrs::getResultDeflection(const ResultPtr& theResult)
120 {
121   double aDeflection = getDeflection(theResult);
122   if (aDeflection < 0)
123     aDeflection = getDefaultDeflection(theResult);
124   return aDeflection;
125 }
126
127 bool XGUI_CustomPrs::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
128                                            std::shared_ptr<GeomAPI_ICustomPrs> theCustomPrs)
129 {
130   bool aCustomized = false;
131   if (theResult.get()) {
132     std::vector<int> aColor;
133     getResultColor(theResult, aColor);
134
135     SessionPtr aMgr = ModelAPI_Session::get();
136     if (aMgr->activeDocument() != theResult->document()) {
137       QColor aQColor(aColor[0], aColor[1], aColor[2]);
138       QColor aNewColor =
139         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 }