Salome HOME
2.17. Improved management of overconstraint situation: dimension constraints are...
[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_Error.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 getDefaultColor(ObjectPtr theObject, std::vector<int>& theColor, const bool isEmptyColorValid)
38 {
39   theColor.clear();
40   // get default color from the preferences manager for the given result
41   if (theColor.empty()) {
42     std::string aSection, aName, aDefault;
43     theObject->colorConfigInfo(aSection, aName, aDefault);
44     if (!aSection.empty() && !aName.empty()) {
45       theColor = Config_PropManager::color(aSection, aName, aDefault);
46     }
47   }
48   if (!isEmptyColorValid && theColor.empty()) {
49     // all AIS objects, where the color is not set, are in black.
50     // The color should be defined in XML or set in the attribute
51     theColor = Config_PropManager::color("Visualization", "object_default_color", "#000000");
52     Events_Error::send("A default color is not defined in the preferences for this kind of result");
53   }
54 }
55
56 XGUI_CustomPrs::XGUI_CustomPrs(XGUI_Workshop* theWorkshop)
57 : myWorkshop(theWorkshop)
58 {
59 }
60
61 void XGUI_CustomPrs::getResultColor(ResultPtr theResult, std::vector<int>& theColor)
62 {
63   getColor(theResult, theColor);
64   if (theColor.empty())
65     getDefaultColor(theResult, theColor, false);
66 }
67
68 bool XGUI_CustomPrs::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
69                                            std::shared_ptr<GeomAPI_ICustomPrs> theCustomPrs)
70 {
71   bool aCustomized = false;
72   if (theResult.get()) {
73     std::vector<int> aColor;
74     getResultColor(theResult, aColor);
75
76     SessionPtr aMgr = ModelAPI_Session::get();
77     if (aMgr->activeDocument() != theResult->document()) {
78       QColor aQColor(aColor[0], aColor[1], aColor[2]);
79       QColor aNewColor = QColor::fromHsvF(aQColor.hueF(), aQColor.saturationF()/3., aQColor.valueF());
80       aColor[0] = aNewColor.red();
81       aColor[1] = aNewColor.green();
82       aColor[2] = aNewColor.blue();
83     }
84     aCustomized = !aColor.empty() && thePrs->setColor(aColor[0], aColor[1], aColor[2]);
85   }
86   else {
87     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
88     ObjectPtr anObject = aDisplayer->getObject(thePrs);
89     if (anObject.get()) {
90      std::vector<int> aColor;
91      ModuleBase_IModule* aModule = myWorkshop->module();
92      aModule->getColor(anObject, aColor);
93      if (aColor.empty())
94        getDefaultColor(anObject, aColor, true);
95      if (!aColor.empty())
96        thePrs->setColor(aColor[0], aColor[1], aColor[2]);
97     }
98   }
99   return aCustomized;
100 }