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