Salome HOME
6cdc7cd915d01d49e3ec28f4d3842003316a27a6
[modules/shaper.git] / src / Config / Config_PropManager.cpp
1 // Copyright (C) 2014-2023  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "Config_PropManager.h"
21
22 bool Config_PropManager::autoColorStatus = false;
23
24 std::vector<int> stringToRGB(const std::string& theColor);
25 int stringToInteger(const std::string& theInt);
26 bool stringToBoolean(const std::string& theInt);
27
28 Config_Properties& Config_PropManager::props() {
29   static Config_Properties* confProps = new Config_Properties();
30   return *confProps;
31 }
32
33 bool Config_PropManager::getAutoColorStatus()
34 {
35     return Config_PropManager::autoColorStatus;
36 }
37
38 void Config_PropManager::setAutoColorStatus(const bool theValue)
39 {
40     Config_PropManager::autoColorStatus = theValue;
41 }
42
43 Config_Prop* Config_PropManager::registerProp(const std::string& theSection,
44                                               const std::string& theName,
45                                               const std::string& theTitle,
46                                               Config_Prop::PropType theType,
47                                               const std::string& theDefaultValue,
48                                               const std::string& theMin,
49                                               const std::string& theMax)
50 {
51   Config_Prop* aProp = findProp(theSection, theName);
52
53   if (aProp) {
54     if (aProp->value() == "") {
55       aProp->setValue(theDefaultValue);
56     }
57     if (aProp->defaultValue() == "") {
58       aProp->setDefaultValue(theDefaultValue);
59     }
60     if (aProp->type() == Config_Prop::Disabled) {
61       aProp->setType(theType);
62       aProp->setTitle(theTitle);
63     }
64     if (theMin != "")
65       aProp->setMin(theMin);
66     if (theMax != "")
67       aProp->setMax(theMax);
68   }
69   else {
70     aProp =
71       new Config_Prop(theSection, theName, theTitle, theType, theDefaultValue, theMin, theMax);
72     props().push_back(aProp);
73   }
74   return aProp;
75 }
76
77 Config_Prop* Config_PropManager::findProp(const std::string& theSection, const std::string& theName)
78 {
79   Config_Properties::const_iterator aIt;
80   Config_Properties aProps = props();
81   for (aIt = aProps.cbegin(); aIt != aProps.cend(); ++aIt) {
82     Config_Prop* aProp = (*aIt);
83     if ((aProp->section() == theSection) && (aProp->name() == theName))
84       return aProp;
85   }
86   return NULL;
87 }
88
89 Config_Properties Config_PropManager::getProperties()
90 {
91   Config_Properties aRes;
92   Config_Properties::const_iterator aIt;
93   Config_Properties aProps = props();
94   for (aIt = aProps.cbegin(); aIt != aProps.cend(); aIt++) {
95     Config_Prop* aProp = (*aIt);
96     if (aProp->type() != Config_Prop::Disabled)
97       aRes.push_back(aProp);
98   }
99   return aRes;
100 }
101
102 std::list<std::string> Config_PropManager::getSections()
103 {
104   // Return only non disabled sections
105   std::list<std::string> aSections;
106   Config_Properties::const_iterator aIt;
107   Config_Properties aProps = props();
108   for (aIt = aProps.cbegin(); aIt != aProps.cend(); aIt++) {
109     const Config_Prop* aProp = (*aIt);
110     if (aProp->type() != Config_Prop::Disabled)
111       aSections.push_back(aProp->section());
112   }
113   aSections.unique();
114   return aSections;
115 }
116
117 Config_Properties Config_PropManager::getProperties(const std::string& theSection)
118 {
119   Config_Properties aRes;
120   Config_Properties::iterator aIt;
121   Config_Properties aProps = props();
122   for (aIt = aProps.begin(); aIt != aProps.end(); aIt++) {
123     Config_Prop* aProp = (*aIt);
124     if ((aProp->section() == theSection) && (aProp->type() != Config_Prop::Disabled))
125       aRes.push_back(aProp);
126   }
127   return aRes;
128 }
129
130 std::string Config_PropManager::string(const std::string& theSection, const std::string& theName)
131 {
132   Config_Properties aProps = getProperties(theSection);
133   Config_Properties::const_iterator aIt;
134   for (aIt = aProps.cbegin(); aIt != aProps.cend(); aIt++) {
135     Config_Prop* aProp = (*aIt);
136     if (aProp->name() == theName)
137       return aProp->value();
138   }
139   std::string aMsg = "Property " + theSection + ":" + theName + " is not registered";
140   throw aMsg.c_str();
141 }
142
143 std::vector<int> Config_PropManager::color(const std::string& theSection,
144                                            const std::string& theName)
145 {
146   std::string aStr = string(theSection, theName);
147   return stringToRGB(aStr);
148 }
149
150 int Config_PropManager::integer(const std::string& theSection, const std::string& theName)
151 {
152   std::string aStr = string(theSection, theName);
153   return stringToInteger(aStr);
154 }
155
156 double Config_PropManager::real(const std::string& theSection, const std::string& theName)
157 {
158   std::string aStr = string(theSection, theName);
159   return stringToDouble(aStr);
160 }
161
162 bool Config_PropManager::boolean(const std::string& theSection,
163                                  const std::string& theName)
164 {
165   std::string aStr = string(theSection, theName);
166   return stringToBoolean(aStr);
167 }
168
169 std::vector<int> stringToRGB(const std::string& theColor)
170 {
171   std::vector<int> aRes(3);
172
173   if (theColor[0] == '#') {
174     char aBuf[3];
175     char* aP;
176     aBuf[2] = '\0';
177
178     aBuf[0] = theColor[1];
179     aBuf[1] = theColor[2];
180     aRes[0] = strtol(aBuf, &aP, 16);
181
182     aBuf[0] = theColor[3];
183     aBuf[1] = theColor[4];
184     aRes[1] = strtol(aBuf, &aP, 16);
185
186     aBuf[0] = theColor[5];
187     aBuf[1] = theColor[6];
188     aRes[2] = strtol(aBuf, &aP, 16);
189   } else {
190     int aPos = (int)theColor.find(",");
191     char aBuf[10];
192     // Get Red
193     std::size_t length = theColor.copy(aBuf, aPos, 0);
194     aBuf[length] = '\0';
195     aRes[0] = atoi(aBuf);
196
197     // Get Green
198     int aNPos = (int)theColor.find(",", aPos + 1);
199     length = theColor.copy(aBuf, aNPos - aPos - 1, aPos + 1);
200     aBuf[length] = '\0';
201     aRes[1] = atoi(aBuf);
202
203     // Get Green
204     length = theColor.copy(aBuf, theColor.length() - aNPos - 1, aNPos + 1);
205     aBuf[length] = '\0';
206     aRes[2] = atoi(aBuf);
207   }
208   return aRes;
209 }
210
211 int stringToInteger(const std::string& theInt)
212 {
213   return atoi(theInt.c_str());
214 }
215
216 double Config_PropManager::stringToDouble(const std::string& theDouble)
217 {
218   std::string aStr = theDouble;
219
220   // change locale and convert "," to "." if exists
221   std::string aCurLocale = setlocale(LC_NUMERIC, 0);
222   setlocale(LC_NUMERIC, "C");
223   size_t dotpos = aStr.find(',');
224   if (dotpos != std::string::npos)
225     aStr.replace(dotpos, 1, ".");
226
227   char* p;
228   double aValue = strtod(aStr.c_str(), &p);
229
230   // restore locale
231   setlocale(LC_NUMERIC, aCurLocale.c_str());
232   return aValue;
233 }
234
235 bool stringToBoolean(const std::string& theBoolean)
236 {
237   return theBoolean == "true";
238 }