Salome HOME
#1182 point is attched to Sketch Constraint line
[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
9 #include <ModelAPI_AttributeIntArray.h>
10 #include <ModelAPI_Session.h>
11 #include <Config_PropManager.h>
12
13 #include <Events_Error.h>
14
15 #include <vector>
16 #include <QColor>
17
18
19 void getColor(ResultPtr theResult, std::vector<int>& theColor)
20 {
21   theColor.clear();
22   // get color from the attribute of the result
23   if (theResult.get() != NULL && theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
24     AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
25     if (aColorAttr.get() && aColorAttr->size()) {
26       theColor.push_back(aColorAttr->value(0));
27       theColor.push_back(aColorAttr->value(1));
28       theColor.push_back(aColorAttr->value(2));
29     }
30   }
31 }
32
33 void getDefaultColor(ResultPtr theResult, std::vector<int>& theColor)
34 {
35   theColor.clear();
36   // get default color from the preferences manager for the given result
37   if (theColor.empty()) {
38     std::string aSection, aName, aDefault;
39     theResult->colorConfigInfo(aSection, aName, aDefault);
40     if (!aSection.empty() && !aName.empty()) {
41       theColor = Config_PropManager::color(aSection, aName, aDefault);
42     }
43   }
44   if (theColor.empty()) {
45     // all AIS objects, where the color is not set, are in black.
46     // The color should be defined in XML or set in the attribute
47     theColor = Config_PropManager::color("Visualization", "object_default_color", "#000000");
48     Events_Error::send("A default color is not defined in the preferences for this kind of result");
49   }
50 }
51
52 void XGUI_CustomPrs::getResultColor(ResultPtr theResult, std::vector<int>& theColor)
53 {
54   getColor(theResult, theColor);
55   if (theColor.empty())
56     getDefaultColor(theResult, theColor);
57 }
58
59 bool XGUI_CustomPrs::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
60                                            std::shared_ptr<GeomAPI_ICustomPrs> theCustomPrs)
61 {
62   std::vector<int> aColor;
63
64   getResultColor(theResult, aColor);
65
66   SessionPtr aMgr = ModelAPI_Session::get();
67   if (aMgr->activeDocument() != theResult->document()) {
68     QColor aQColor(aColor[0], aColor[1], aColor[2]);
69     QColor aNewColor = QColor::fromHsvF(aQColor.hueF(), aQColor.saturationF()/3., aQColor.valueF());
70     aColor[0] = aNewColor.red();
71     aColor[1] = aNewColor.green();
72     aColor[2] = aNewColor.blue();
73   }
74   return !aColor.empty() && thePrs->setColor(aColor[0], aColor[1], aColor[2]);
75 }