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