Salome HOME
Concealed objects now also ersaed in the viewer
[modules/shaper.git] / src / Config / Config_PropManager.cpp
1 // File:        Config_PropManager.cpp
2 // Created:     13 Aug 2014
3 // Author:      Vitaly SMETANNIKOV
4
5 #include "Config_PropManager.h"
6
7 std::vector<int> stringToRGB(const std::string& theColor);
8 int stringToInteger(const std::string& theInt);
9 double stringToDouble(const std::string& theDouble);
10
11 Config_Properties Config_PropManager::myProps;
12
13 Config_Prop* Config_PropManager::registerProp(const std::string& theSection, const std::string& theName,
14                                               const std::string& theTitle, Config_Prop::PropType theType,
15                                               const std::string& theDefaultValue)
16 {
17   Config_Prop* aProp = findProp(theSection, theName);
18
19   if (aProp) {
20     if (aProp->value() == "") {
21       aProp->setValue(theDefaultValue);
22     }
23     if (aProp->defaultValue() == "") {
24       aProp->setDefaultValue(theDefaultValue);
25     }
26     if (aProp->type() == Config_Prop::Disabled) {
27       aProp->setType(theType);
28       aProp->setTitle(theTitle);
29     }
30   }
31   else {
32     aProp = new Config_Prop(theSection, theName, theTitle, theType, theDefaultValue);
33     myProps.push_back(aProp);
34   }
35   return aProp;
36 }
37
38 Config_Prop* Config_PropManager::findProp(const std::string& theSection, const std::string& theName)
39 {
40   Config_Properties::const_iterator aIt;
41   for (aIt = myProps.cbegin(); aIt != myProps.cend(); ++aIt) {
42     Config_Prop* aProp = (*aIt);
43     if ((aProp->section() == theSection) && (aProp->name() == theName))
44       return aProp;
45   }
46   return NULL;
47 }
48
49 Config_Properties Config_PropManager::getProperties()
50 {
51   Config_Properties aRes;
52   Config_Properties::const_iterator aIt;
53   for (aIt = myProps.cbegin(); aIt != myProps.cend(); aIt++) {
54     Config_Prop* aProp = (*aIt);
55     if (aProp->type() != Config_Prop::Disabled)
56       aRes.push_back(aProp);
57   }
58   return aRes;
59 }
60
61 std::list<std::string> Config_PropManager::getSections()
62 {
63   // Return only non disabled sections
64   std::list<std::string> aSections;
65   Config_Properties::const_iterator aIt;
66   for (aIt = myProps.cbegin(); aIt != myProps.cend(); aIt++) {
67     const Config_Prop* aProp = (*aIt);
68     if (aProp->type() != Config_Prop::Disabled)
69       aSections.push_back(aProp->section());
70   }
71   aSections.unique();
72   return aSections;
73 }
74
75 Config_Properties Config_PropManager::getProperties(const std::string& theSection)
76 {
77   Config_Properties aRes;
78   Config_Properties::iterator aIt;
79   for (aIt = myProps.begin(); aIt != myProps.end(); aIt++) {
80     Config_Prop* aProp = (*aIt);
81     if ((aProp->section() == theSection) && (aProp->type() != Config_Prop::Disabled))
82       aRes.push_back(aProp);
83   }
84   return aRes;
85 }
86
87 std::string Config_PropManager::string(const std::string& theSection, const std::string& theName,
88                                        const std::string& theDefault)
89 {
90   Config_Properties aProps = getProperties(theSection);
91   Config_Properties::const_iterator aIt;
92   for (aIt = aProps.cbegin(); aIt != aProps.cend(); aIt++) {
93     Config_Prop* aProp = (*aIt);
94     if (aProp->name() == theName)
95       return aProp->value();
96   }
97   return theDefault;
98 }
99
100 std::vector<int> Config_PropManager::color(const std::string& theSection,
101                                            const std::string& theName,
102                                            const std::string& theDefault)
103 {
104   std::string aStr = string(theSection, theName, theDefault);
105   return stringToRGB(aStr);
106 }
107
108 int Config_PropManager::integer(const std::string& theSection, const std::string& theName,
109                                 const std::string& theDefault)
110 {
111   std::string aStr = string(theSection, theName, theDefault);
112   return stringToInteger(aStr);
113 }
114
115 double Config_PropManager::real(const std::string& theSection, const std::string& theName,
116                                 const std::string& theDefault)
117 {
118   std::string aStr = string(theSection, theName, theDefault);
119   return stringToDouble(aStr);
120 }
121
122 std::vector<int> stringToRGB(const std::string& theColor)
123 {
124   std::vector<int> aRes(3);
125
126   if (theColor[0] == '#') {
127     char aBuf[3];
128     char* aP;
129     aBuf[2] = '\0';
130
131     aBuf[0] = theColor[1];
132     aBuf[1] = theColor[2];
133     aRes[0] = strtol(aBuf, &aP, 16);
134
135     aBuf[0] = theColor[3];
136     aBuf[1] = theColor[4];
137     aRes[1] = strtol(aBuf, &aP, 16);
138
139     aBuf[0] = theColor[5];
140     aBuf[1] = theColor[6];
141     aRes[2] = strtol(aBuf, &aP, 16);
142   } else {
143     int aPos = theColor.find(",");
144     char aBuf[10];
145     // Get Red
146     std::size_t length = theColor.copy(aBuf, aPos, 0);
147     aBuf[length] = '\0';
148     aRes[0] = atoi(aBuf);
149
150     // Get Green
151     int aNPos = theColor.find(",", aPos + 1);
152     length = theColor.copy(aBuf, aNPos - aPos - 1, aPos + 1);
153     aBuf[length] = '\0';
154     aRes[1] = atoi(aBuf);
155
156     // Get Green
157     length = theColor.copy(aBuf, theColor.length() - aNPos - 1, aNPos + 1);
158     aBuf[length] = '\0';
159     aRes[2] = atoi(aBuf);
160   }
161   return aRes;
162 }
163
164 int stringToInteger(const std::string& theInt)
165 {
166   return atoi(theInt.c_str());
167 }
168
169 double stringToDouble(const std::string& theDouble)
170 {
171   char* p;
172   return strtod(theDouble.c_str(), &p);
173 }