Salome HOME
Selection priority in Sketch, clear selection when sketch goes from entity to neutral...
[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_Session.h>
15 #include <Config_PropManager.h>
16
17 #include <Events_InfoMessage.h>
18
19 #include <vector>
20 #include <QColor>
21
22
23 void getColor(ResultPtr theResult, std::vector<int>& theColor)
24 {
25   theColor.clear();
26   // get color from the attribute of the result
27   if (theResult.get() != NULL && theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
28     AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
29     if (aColorAttr.get() && aColorAttr->size()) {
30       theColor.push_back(aColorAttr->value(0));
31       theColor.push_back(aColorAttr->value(1));
32       theColor.push_back(aColorAttr->value(2));
33     }
34   }
35 }
36
37 void XGUI_CustomPrs::getDefaultColor(ObjectPtr theObject, const bool isEmptyColorValid,
38                                      std::vector<int>& theColor)
39 {
40   theColor.clear();
41   // get default color from the preferences manager for the given result
42   if (theColor.empty()) {
43     std::string aSection, aName, aDefault;
44     theObject->colorConfigInfo(aSection, aName, aDefault);
45     if (!aSection.empty() && !aName.empty()) {
46       theColor = Config_PropManager::color(aSection, aName, aDefault);
47     }
48   }
49   if (!isEmptyColorValid && theColor.empty()) {
50     // all AIS objects, where the color is not set, are in black.
51     // The color should be defined in XML or set in the attribute
52     theColor = Config_PropManager::color("Visualization", "object_default_color", "#000000");
53     Events_InfoMessage("XGUI_CustomPrs", 
54       "A default color is not defined in the preferences for this kind of result").send();
55   }
56 }
57
58 XGUI_CustomPrs::XGUI_CustomPrs(XGUI_Workshop* theWorkshop)
59 : myWorkshop(theWorkshop)
60 {
61 }
62
63 void XGUI_CustomPrs::getResultColor(ResultPtr theResult, std::vector<int>& theColor)
64 {
65   getColor(theResult, theColor);
66   if (theColor.empty())
67     getDefaultColor(theResult, false, theColor);
68 }
69
70 bool XGUI_CustomPrs::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
71                                            std::shared_ptr<GeomAPI_ICustomPrs> theCustomPrs)
72 {
73   bool aCustomized = false;
74   if (theResult.get()) {
75     std::vector<int> aColor;
76     getResultColor(theResult, aColor);
77
78     SessionPtr aMgr = ModelAPI_Session::get();
79     if (aMgr->activeDocument() != theResult->document()) {
80       QColor aQColor(aColor[0], aColor[1], aColor[2]);
81       QColor aNewColor = QColor::fromHsvF(aQColor.hueF(), aQColor.saturationF()/3., aQColor.valueF());
82       aColor[0] = aNewColor.red();
83       aColor[1] = aNewColor.green();
84       aColor[2] = aNewColor.blue();
85     }
86     aCustomized = !aColor.empty() && thePrs->setColor(aColor[0], aColor[1], aColor[2]);
87   }
88   ModuleBase_IModule* aModule = myWorkshop->module();
89   aCustomized = aModule->customisePresentation(theResult, thePrs, theCustomPrs) || aCustomized;
90   return aCustomized;
91 }