Salome HOME
updated copyright message
[modules/shaper.git] / src / Config / Config_PropManager.cpp
index c296cc864772f6b0c66a95cc8bd7b8d07f47898d..480c1cdaea5f27ec6753c62d8fb30092719da45d 100644 (file)
@@ -1,37 +1,84 @@
-// File:        Config_PropManager.cpp
-// Created:     13 Aug 2014
-// Author:      Vitaly SMETANNIKOV
+// Copyright (C) 2014-2023  CEA, EDF
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
 
 #include "Config_PropManager.h"
 
+bool Config_PropManager::autoColorStatus = false;
+
 std::vector<int> stringToRGB(const std::string& theColor);
 int stringToInteger(const std::string& theInt);
-double stringToDouble(const std::string& theDouble);
+bool stringToBoolean(const std::string& theInt);
+
+Config_Properties& Config_PropManager::props() {
+  static Config_Properties* confProps = new Config_Properties();
+  return *confProps;
+}
+
+bool Config_PropManager::getAutoColorStatus()
+{
+    return Config_PropManager::autoColorStatus;
+}
 
-Config_Properties Config_PropManager::myProps;
+void Config_PropManager::setAutoColorStatus(const bool theValue)
+{
+    Config_PropManager::autoColorStatus = theValue;
+}
 
-bool Config_PropManager::registerProp(const std::string& theSection, const std::string& theName,
-                                      const std::string& theTitle, Config_Prop::PropType theType,
-                                      const std::string& theValue)
+Config_Prop* Config_PropManager::registerProp(const std::string& theSection,
+                                              const std::string& theName,
+                                              const std::string& theTitle,
+                                              Config_Prop::PropType theType,
+                                              const std::string& theDefaultValue,
+                                              const std::string& theMin,
+                                              const std::string& theMax)
 {
   Config_Prop* aProp = findProp(theSection, theName);
+
   if (aProp) {
+    if (aProp->value() == "") {
+      aProp->setValue(theDefaultValue);
+    }
+    if (aProp->defaultValue() == "") {
+      aProp->setDefaultValue(theDefaultValue);
+    }
     if (aProp->type() == Config_Prop::Disabled) {
       aProp->setType(theType);
       aProp->setTitle(theTitle);
-      return true;
     }
-    return false;
+    if (theMin != "")
+      aProp->setMin(theMin);
+    if (theMax != "")
+      aProp->setMax(theMax);
+  }
+  else {
+    aProp =
+      new Config_Prop(theSection, theName, theTitle, theType, theDefaultValue, theMin, theMax);
+    props().push_back(aProp);
   }
-  aProp = new Config_Prop(theSection, theName, theTitle, theType, theValue);
-  myProps.push_back(aProp);
-  return true;
+  return aProp;
 }
 
 Config_Prop* Config_PropManager::findProp(const std::string& theSection, const std::string& theName)
 {
   Config_Properties::const_iterator aIt;
-  for (aIt = myProps.cbegin(); aIt != myProps.cend(); ++aIt) {
+  Config_Properties aProps = props();
+  for (aIt = aProps.cbegin(); aIt != aProps.cend(); ++aIt) {
     Config_Prop* aProp = (*aIt);
     if ((aProp->section() == theSection) && (aProp->name() == theName))
       return aProp;
@@ -43,7 +90,8 @@ Config_Properties Config_PropManager::getProperties()
 {
   Config_Properties aRes;
   Config_Properties::const_iterator aIt;
-  for (aIt = myProps.cbegin(); aIt != myProps.cend(); aIt++) {
+  Config_Properties aProps = props();
+  for (aIt = aProps.cbegin(); aIt != aProps.cend(); aIt++) {
     Config_Prop* aProp = (*aIt);
     if (aProp->type() != Config_Prop::Disabled)
       aRes.push_back(aProp);
@@ -56,7 +104,8 @@ std::list<std::string> Config_PropManager::getSections()
   // Return only non disabled sections
   std::list<std::string> aSections;
   Config_Properties::const_iterator aIt;
-  for (aIt = myProps.cbegin(); aIt != myProps.cend(); aIt++) {
+  Config_Properties aProps = props();
+  for (aIt = aProps.cbegin(); aIt != aProps.cend(); aIt++) {
     const Config_Prop* aProp = (*aIt);
     if (aProp->type() != Config_Prop::Disabled)
       aSections.push_back(aProp->section());
@@ -69,7 +118,8 @@ Config_Properties Config_PropManager::getProperties(const std::string& theSectio
 {
   Config_Properties aRes;
   Config_Properties::iterator aIt;
-  for (aIt = myProps.begin(); aIt != myProps.end(); aIt++) {
+  Config_Properties aProps = props();
+  for (aIt = aProps.begin(); aIt != aProps.end(); aIt++) {
     Config_Prop* aProp = (*aIt);
     if ((aProp->section() == theSection) && (aProp->type() != Config_Prop::Disabled))
       aRes.push_back(aProp);
@@ -77,8 +127,7 @@ Config_Properties Config_PropManager::getProperties(const std::string& theSectio
   return aRes;
 }
 
-std::string Config_PropManager::string(const std::string& theSection, const std::string& theName,
-                                       const std::string& theDefault)
+std::string Config_PropManager::string(const std::string& theSection, const std::string& theName)
 {
   Config_Properties aProps = getProperties(theSection);
   Config_Properties::const_iterator aIt;
@@ -87,31 +136,36 @@ std::string Config_PropManager::string(const std::string& theSection, const std:
     if (aProp->name() == theName)
       return aProp->value();
   }
-  return theDefault;
+  std::string aMsg = "Property " + theSection + ":" + theName + " is not registered";
+  throw aMsg.c_str();
 }
 
 std::vector<int> Config_PropManager::color(const std::string& theSection,
-                                           const std::string& theName,
-                                           const std::string& theDefault)
+                                           const std::string& theName)
 {
-  std::string aStr = string(theSection, theName, theDefault);
+  std::string aStr = string(theSection, theName);
   return stringToRGB(aStr);
 }
 
-int Config_PropManager::integer(const std::string& theSection, const std::string& theName,
-                                const std::string& theDefault)
+int Config_PropManager::integer(const std::string& theSection, const std::string& theName)
 {
-  std::string aStr = string(theSection, theName, theDefault);
+  std::string aStr = string(theSection, theName);
   return stringToInteger(aStr);
 }
 
-double Config_PropManager::real(const std::string& theSection, const std::string& theName,
-                                const std::string& theDefault)
+double Config_PropManager::real(const std::string& theSection, const std::string& theName)
 {
-  std::string aStr = string(theSection, theName, theDefault);
+  std::string aStr = string(theSection, theName);
   return stringToDouble(aStr);
 }
 
+bool Config_PropManager::boolean(const std::string& theSection,
+                                 const std::string& theName)
+{
+  std::string aStr = string(theSection, theName);
+  return stringToBoolean(aStr);
+}
+
 std::vector<int> stringToRGB(const std::string& theColor)
 {
   std::vector<int> aRes(3);
@@ -133,7 +187,7 @@ std::vector<int> stringToRGB(const std::string& theColor)
     aBuf[1] = theColor[6];
     aRes[2] = strtol(aBuf, &aP, 16);
   } else {
-    int aPos = theColor.find(",");
+    int aPos = (int)theColor.find(",");
     char aBuf[10];
     // Get Red
     std::size_t length = theColor.copy(aBuf, aPos, 0);
@@ -141,7 +195,7 @@ std::vector<int> stringToRGB(const std::string& theColor)
     aRes[0] = atoi(aBuf);
 
     // Get Green
-    int aNPos = theColor.find(",", aPos + 1);
+    int aNPos = (int)theColor.find(",", aPos + 1);
     length = theColor.copy(aBuf, aNPos - aPos - 1, aPos + 1);
     aBuf[length] = '\0';
     aRes[1] = atoi(aBuf);
@@ -159,8 +213,26 @@ int stringToInteger(const std::string& theInt)
   return atoi(theInt.c_str());
 }
 
-double stringToDouble(const std::string& theDouble)
+double Config_PropManager::stringToDouble(const std::string& theDouble)
 {
+  std::string aStr = theDouble;
+
+  // change locale and convert "," to "." if exists
+  std::string aCurLocale = setlocale(LC_NUMERIC, 0);
+  setlocale(LC_NUMERIC, "C");
+  size_t dotpos = aStr.find(',');
+  if (dotpos != std::string::npos)
+    aStr.replace(dotpos, 1, ".");
+
   char* p;
-  return strtod(theDouble.c_str(), &p);
+  double aValue = strtod(aStr.c_str(), &p);
+
+  // restore locale
+  setlocale(LC_NUMERIC, aCurLocale.c_str());
+  return aValue;
+}
+
+bool stringToBoolean(const std::string& theBoolean)
+{
+  return theBoolean == "true";
 }