Salome HOME
Merge branch 'master' of newgeom:newgeom
[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 std::map<std::string, Config_Properties> Config_PropManager::myPropMap;
9
10
11
12 bool Config_PropManager::registerProp(const std::string& theOwnerId, 
13                                       const std::string& theSection,
14                                       const std::string& theName,
15                                       const std::string& theTitle,
16                                       Config_Prop::PropType theType,
17                                       const std::string& theValue)
18 {
19   Config_Prop* aProp = new Config_Prop(theSection, theName, theTitle, theType, theValue);
20   return registerProp(theOwnerId, aProp);
21 }
22
23 bool Config_PropManager::registerProp(const std::string& theOwnerId, 
24                                       Config_Prop* theProp)
25 {
26   if (myPropMap.find(theOwnerId) == myPropMap.end()) {
27     Config_Properties aPropList;
28     aPropList.push_back(theProp);
29     myPropMap[theOwnerId] = aPropList;
30   } else {
31     myPropMap[theOwnerId].push_back(theProp);
32   }
33   return true;
34 }
35
36 std::list<std::string> Config_PropManager::getOwners()
37 {
38   std::list<std::string> aKeys;
39   std::map<std::string, Config_Properties>::const_iterator aIt;
40   for (aIt = myPropMap.cbegin(); aIt != myPropMap.cend(); ++aIt) 
41     aKeys.push_back((*aIt).first);
42   return aKeys;
43 }
44
45 Config_Properties Config_PropManager::getProperties(const std::string& theOwnerId)
46 {
47   return myPropMap[theOwnerId];
48 }
49
50
51 std::list<std::string> Config_PropManager::getSections(const std::string& theOwnerId)
52 {
53   std::list<std::string> aSections;
54   Config_Properties aProps = getProperties(theOwnerId);
55   Config_Properties::const_iterator aIt;
56   for (aIt = aProps.cbegin(); aIt != aProps.cend(); aIt++) {
57     const Config_Prop* aProp = (*aIt);
58     aSections.push_back(aProp->section());
59   }
60   aSections.unique();
61   return aSections;
62 }
63
64 Config_Properties Config_PropManager::getProperties(const std::string& theOwnerId, 
65                                                     const std::string& theSection)
66 {
67   Config_Properties aProps = getProperties(theOwnerId);
68   Config_Properties aRes;
69   Config_Properties::iterator aIt;
70   for (aIt = aProps.begin(); aIt != aProps.end(); aIt++) {
71     Config_Prop* aProp = (*aIt);
72     if (aProp->section() == theSection)
73       aRes.push_back(aProp);
74   }
75   return aRes;
76 }
77
78
79 std::string Config_PropManager::value(const std::string& theOwnerId, 
80                                       const std::string& theSection, 
81                                       const std::string& theName,
82                                       const std::string& theDefault)
83 {
84   Config_Properties aProps = getProperties(theOwnerId, theSection);
85   Config_Properties::const_iterator aIt;
86   for (aIt = aProps.cbegin(); aIt != aProps.cend(); aIt++) {
87     Config_Prop* aProp = (*aIt);
88     if (aProp->name() == theName)
89       return aProp->value();
90   }
91   return theDefault;
92 }
93
94
95 std::vector<int> stringToRGB(const std::string& theColor)
96 {
97   std::vector<int> aRes(3);
98
99   int aPos = theColor.find(",");
100   char aBuf[10];
101   // Get Red
102   std::size_t length = theColor.copy(aBuf, aPos, 0);
103   aBuf[length] = '\0';
104   int aC = atoi(aBuf);
105   aRes[0] = aC;
106
107   // Get Green
108   int aNPos = theColor.find(",", aPos + 1);
109   length = theColor.copy(aBuf, aNPos - aPos - 1, aPos + 1);
110   aBuf[length] = '\0';
111   aC = atoi(aBuf);
112   aRes[1] = aC;
113
114   // Get Green
115   length = theColor.copy(aBuf, theColor.length() - aNPos - 1, aNPos + 1);
116   aBuf[length] = '\0';
117   aC = atoi(aBuf);
118   aRes[2] = aC;
119
120   return aRes;
121 }