]> SALOME platform Git repositories - modules/shaper.git/blob - src/Config/Config_PropManager.cpp
Salome HOME
Preferences improvement
[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
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
14 Config_Properties Config_PropManager::myProps;
15
16
17
18 bool Config_PropManager::registerProp(const std::string& theSection,
19                                       const std::string& theName,
20                                       const std::string& theTitle,
21                                       Config_Prop::PropType theType,
22                                       const std::string& theValue)
23 {
24   Config_Prop* aProp = findProp(theSection, theName);
25   if (aProp) {
26     if (aProp->type() == Config_Prop::Disabled) {
27       aProp->setType(theType);
28       aProp->setTitle(theTitle);
29       return true;
30     }
31     return false;
32   }
33   aProp = new Config_Prop(theSection, theName, theTitle, theType, theValue);
34   myProps.push_back(aProp);
35   return true;
36 }
37
38 Config_Prop* Config_PropManager::findProp(const std::string& theSection,
39                                           const std::string& theName)
40 {
41   Config_Properties::const_iterator aIt;
42   for (aIt = myProps.cbegin(); aIt != myProps.cend(); ++aIt) {
43     Config_Prop* aProp = (*aIt);
44     if ((aProp->section() == theSection) && (aProp->name() == theName))
45       return aProp;
46   }
47   return NULL;
48 }
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
90 std::string Config_PropManager::string(const std::string& theSection, 
91                                       const std::string& theName,
92                                       const std::string& theDefault)
93 {
94   Config_Properties aProps = getProperties(theSection);
95   Config_Properties::const_iterator aIt;
96   for (aIt = aProps.cbegin(); aIt != aProps.cend(); aIt++) {
97     Config_Prop* aProp = (*aIt);
98     if (aProp->name() == theName)
99       return aProp->value();
100   }
101   return theDefault;
102 }
103
104
105 std::vector<int> Config_PropManager::color(const std::string& theSection, 
106                                            const std::string& theName,
107                                            const std::string& theDefault)
108 {
109   std::string aStr = string(theSection, theName, theDefault);
110   return stringToRGB(aStr);
111 }
112
113 int Config_PropManager::integer(const std::string& theSection, 
114                       const std::string& theName,
115                       const std::string& theDefault)
116 {
117   std::string aStr = string(theSection, theName, theDefault);
118   return stringToInteger(aStr);
119 }
120
121 double Config_PropManager::real(const std::string& theSection, 
122                       const std::string& theName,
123                       const std::string& theDefault)
124 {
125   std::string aStr = string(theSection, theName, theDefault);
126   return stringToDouble(aStr);
127 }
128
129
130
131 std::vector<int> stringToRGB(const std::string& theColor)
132 {
133   std::vector<int> aRes(3);
134
135   if (theColor[0] == '#') {
136     char aBuf[3];
137     char* aP;
138     aBuf[2] = '\0';
139
140     aBuf[0] = theColor[1];
141     aBuf[1] = theColor[2];
142     aRes[0] = strtol(aBuf, &aP, 16);
143
144     aBuf[0] = theColor[3];
145     aBuf[1] = theColor[4];
146     aRes[1] = strtol(aBuf, &aP, 16);
147
148     aBuf[0] = theColor[5];
149     aBuf[1] = theColor[6];
150     aRes[2] = strtol(aBuf, &aP, 16);
151   } else {
152     int aPos = theColor.find(",");
153     char aBuf[10];
154     // Get Red
155     std::size_t length = theColor.copy(aBuf, aPos, 0);
156     aBuf[length] = '\0';
157     aRes[0] = atoi(aBuf);
158
159     // Get Green
160     int aNPos = theColor.find(",", aPos + 1);
161     length = theColor.copy(aBuf, aNPos - aPos - 1, aPos + 1);
162     aBuf[length] = '\0';
163     aRes[1] = atoi(aBuf);
164
165     // Get Green
166     length = theColor.copy(aBuf, theColor.length() - aNPos - 1, aNPos + 1);
167     aBuf[length] = '\0';
168     aRes[2] = atoi(aBuf);
169   }
170   return aRes;
171 }
172
173 int stringToInteger(const std::string& theInt)
174 {
175   return atoi(theInt.c_str());
176 }
177
178 double stringToDouble(const std::string& theDouble)
179 {
180   char* p;
181   return strtod(theDouble.c_str(), &p);
182 }