Salome HOME
Issue #1660: Ability to change the deflection coefficient
[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 <Config_PropManager.h>
17
18 #include <Events_InfoMessage.h>
19
20 #include <vector>
21 #include <QColor>
22
23 double getDeflection(const ResultPtr& theResult)
24 {
25   double aDeflection = -1;
26   // get color from the attribute of the result
27   if (theResult.get() != NULL &&
28       theResult->data()->attribute(ModelAPI_Result::DEFLECTION_ID()).get() != NULL) {
29     AttributeDoublePtr aDoubleAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
30     if (aDoubleAttr.get() && aDoubleAttr->isInitialized())
31       aDeflection = aDoubleAttr->value();
32   }
33   return aDeflection;
34 }
35
36 void getColor(const ResultPtr& theResult, std::vector<int>& theColor)
37 {
38   theColor.clear();
39   // get color from the attribute of the result
40   if (theResult.get() != NULL && theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
41     AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
42     if (aColorAttr.get() && aColorAttr->size()) {
43       theColor.push_back(aColorAttr->value(0));
44       theColor.push_back(aColorAttr->value(1));
45       theColor.push_back(aColorAttr->value(2));
46     }
47   }
48 }
49
50 void XGUI_CustomPrs::getDefaultColor(ObjectPtr theObject, const bool isEmptyColorValid,
51                                      std::vector<int>& theColor)
52 {
53   theColor.clear();
54   // get default color from the preferences manager for the given result
55   if (theColor.empty()) {
56     std::string aSection, aName, aDefault;
57     theObject->colorConfigInfo(aSection, aName, aDefault);
58     if (!aSection.empty() && !aName.empty()) {
59       theColor = Config_PropManager::color(aSection, aName, aDefault);
60     }
61   }
62   if (!isEmptyColorValid && theColor.empty()) {
63     // all AIS objects, where the color is not set, are in black.
64     // The color should be defined in XML or set in the attribute
65     theColor = Config_PropManager::color("Visualization", "object_default_color", "#000000");
66     Events_InfoMessage("XGUI_CustomPrs", 
67       "A default color is not defined in the preferences for this kind of result").send();
68   }
69 }
70
71 double XGUI_CustomPrs::getDefaultDeflection(const ObjectPtr& theObject)
72 {
73   return Config_PropManager::real("Visualization", "result_deflection", "0.001");
74 }
75
76 XGUI_CustomPrs::XGUI_CustomPrs(XGUI_Workshop* theWorkshop)
77 : myWorkshop(theWorkshop)
78 {
79 }
80
81 void XGUI_CustomPrs::getResultColor(const ResultPtr& theResult, std::vector<int>& theColor)
82 {
83   getColor(theResult, theColor);
84   if (theColor.empty())
85     getDefaultColor(theResult, false, theColor);
86 }
87
88 double XGUI_CustomPrs::getResultDeflection(const ResultPtr& theResult)
89 {
90   double aDeflection = getDeflection(theResult);
91   if (aDeflection < 0)
92     aDeflection = getDefaultDeflection(theResult);
93   return aDeflection;
94 }
95
96 bool XGUI_CustomPrs::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
97                                            std::shared_ptr<GeomAPI_ICustomPrs> theCustomPrs)
98 {
99   bool aCustomized = false;
100   if (theResult.get()) {
101     std::vector<int> aColor;
102     getResultColor(theResult, aColor);
103
104     SessionPtr aMgr = ModelAPI_Session::get();
105     if (aMgr->activeDocument() != theResult->document()) {
106       QColor aQColor(aColor[0], aColor[1], aColor[2]);
107       QColor aNewColor = QColor::fromHsvF(aQColor.hueF(), aQColor.saturationF()/3., aQColor.valueF());
108       aColor[0] = aNewColor.red();
109       aColor[1] = aNewColor.green();
110       aColor[2] = aNewColor.blue();
111     }
112     aCustomized = !aColor.empty() && thePrs->setColor(aColor[0], aColor[1], aColor[2]);
113
114     aCustomized = thePrs->setDeflection(getResultDeflection(theResult)) | aCustomized;
115   }
116   ModuleBase_IModule* aModule = myWorkshop->module();
117   aCustomized = aModule->customisePresentation(theResult, thePrs, theCustomPrs) || aCustomized;
118   return aCustomized;
119 }