X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FConfig%2FConfig_PropManager.cpp;h=ad5ceca9b779bb2a0b0c3865c6625219646e1b86;hb=853e0eec6016120b641efa6adf427a239cf203cf;hp=b8593206d57275f14ae0e12c5e15b53c8f5500d1;hpb=e8f6a194e38ff95708bf0984081d9f4e7d9ab115;p=modules%2Fshaper.git diff --git a/src/Config/Config_PropManager.cpp b/src/Config/Config_PropManager.cpp index b8593206d..ad5ceca9b 100644 --- a/src/Config/Config_PropManager.cpp +++ b/src/Config/Config_PropManager.cpp @@ -1,87 +1,96 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D + // File: Config_PropManager.cpp // Created: 13 Aug 2014 // Author: Vitaly SMETANNIKOV #include "Config_PropManager.h" +std::vector stringToRGB(const std::string& theColor); +int stringToInteger(const std::string& theInt); +double stringToDouble(const std::string& theDouble); +bool stringToBoolean(const std::string& theInt); -std::map Config_PropManager::myPropMap; - - - -bool Config_PropManager::registerProp(const std::string& theOwnerId, - const std::string& theSection, - const std::string& theName, - const std::string& theTitle, - Config_Prop::PropType theType, - const std::string& theValue) -{ - Config_Prop* aProp = new Config_Prop(theSection, theName, theTitle, theType, theValue); - return registerProp(theOwnerId, aProp); -} +Config_Properties Config_PropManager::myProps; -bool Config_PropManager::registerProp(const std::string& theOwnerId, - Config_Prop* theProp) +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) { - if (myPropMap.find(theOwnerId) == myPropMap.end()) { - Config_Properties aPropList; - aPropList.push_back(theProp); - myPropMap[theOwnerId] = aPropList; - } else { - myPropMap[theOwnerId].push_back(theProp); + 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); + } + } + else { + aProp = new Config_Prop(theSection, theName, theTitle, theType, theDefaultValue); + myProps.push_back(aProp); } - return true; + return aProp; } -std::list Config_PropManager::getOwners() +Config_Prop* Config_PropManager::findProp(const std::string& theSection, const std::string& theName) { - std::list aKeys; - std::map::const_iterator aIt; - for (aIt = myPropMap.cbegin(); aIt != myPropMap.cend(); ++aIt) - aKeys.push_back((*aIt).first); - return aKeys; + Config_Properties::const_iterator aIt; + for (aIt = myProps.cbegin(); aIt != myProps.cend(); ++aIt) { + Config_Prop* aProp = (*aIt); + if ((aProp->section() == theSection) && (aProp->name() == theName)) + return aProp; + } + return NULL; } -Config_Properties Config_PropManager::getProperties(const std::string& theOwnerId) +Config_Properties Config_PropManager::getProperties() { - return myPropMap[theOwnerId]; + Config_Properties aRes; + Config_Properties::const_iterator aIt; + for (aIt = myProps.cbegin(); aIt != myProps.cend(); aIt++) { + Config_Prop* aProp = (*aIt); + if (aProp->type() != Config_Prop::Disabled) + aRes.push_back(aProp); + } + return aRes; } - -std::list Config_PropManager::getSections(const std::string& theOwnerId) +std::list Config_PropManager::getSections() { + // Return only non disabled sections std::list aSections; - Config_Properties aProps = getProperties(theOwnerId); Config_Properties::const_iterator aIt; - for (aIt = aProps.cbegin(); aIt != aProps.cend(); aIt++) { + for (aIt = myProps.cbegin(); aIt != myProps.cend(); aIt++) { const Config_Prop* aProp = (*aIt); - aSections.push_back(aProp->section()); + if (aProp->type() != Config_Prop::Disabled) + aSections.push_back(aProp->section()); } aSections.unique(); return aSections; } -Config_Properties Config_PropManager::getProperties(const std::string& theOwnerId, - const std::string& theSection) +Config_Properties Config_PropManager::getProperties(const std::string& theSection) { - Config_Properties aProps = getProperties(theOwnerId); Config_Properties aRes; Config_Properties::iterator aIt; - for (aIt = aProps.begin(); aIt != aProps.end(); aIt++) { + for (aIt = myProps.begin(); aIt != myProps.end(); aIt++) { Config_Prop* aProp = (*aIt); - if (aProp->section() == theSection) + if ((aProp->section() == theSection) && (aProp->type() != Config_Prop::Disabled)) aRes.push_back(aProp); } return aRes; } - -std::string Config_PropManager::value(const std::string& theOwnerId, - 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, + const std::string& theDefault) { - Config_Properties aProps = getProperties(theOwnerId, theSection); + Config_Properties aProps = getProperties(theSection); Config_Properties::const_iterator aIt; for (aIt = aProps.cbegin(); aIt != aProps.cend(); aIt++) { Config_Prop* aProp = (*aIt); @@ -91,31 +100,90 @@ std::string Config_PropManager::value(const std::string& theOwnerId, return theDefault; } +std::vector Config_PropManager::color(const std::string& theSection, + const std::string& theName, + const std::string& theDefault) +{ + std::string aStr = string(theSection, theName, theDefault); + return stringToRGB(aStr); +} + +int Config_PropManager::integer(const std::string& theSection, const std::string& theName, + const std::string& theDefault) +{ + std::string aStr = string(theSection, theName, theDefault); + return stringToInteger(aStr); +} + +double Config_PropManager::real(const std::string& theSection, const std::string& theName, + const std::string& theDefault) +{ + std::string aStr = string(theSection, theName, theDefault); + return stringToDouble(aStr); +} + +bool Config_PropManager::boolean(const std::string& theSection, + const std::string& theName, + const std::string& theDefault) +{ + std::string aStr = string(theSection, theName, theDefault); + return stringToBoolean(aStr); +} std::vector stringToRGB(const std::string& theColor) { std::vector aRes(3); - int aPos = theColor.find(","); - char aBuf[10]; - // Get Red - std::size_t length = theColor.copy(aBuf, aPos, 0); - aBuf[length] = '\0'; - int aC = atoi(aBuf); - aRes[0] = aC; - - // Get Green - int aNPos = theColor.find(",", aPos + 1); - length = theColor.copy(aBuf, aNPos - aPos - 1, aPos + 1); - aBuf[length] = '\0'; - aC = atoi(aBuf); - aRes[1] = aC; - - // Get Green - length = theColor.copy(aBuf, theColor.length() - aNPos - 1, aNPos + 1); - aBuf[length] = '\0'; - aC = atoi(aBuf); - aRes[2] = aC; + if (theColor[0] == '#') { + char aBuf[3]; + char* aP; + aBuf[2] = '\0'; + + aBuf[0] = theColor[1]; + aBuf[1] = theColor[2]; + aRes[0] = strtol(aBuf, &aP, 16); + aBuf[0] = theColor[3]; + aBuf[1] = theColor[4]; + aRes[1] = strtol(aBuf, &aP, 16); + + aBuf[0] = theColor[5]; + aBuf[1] = theColor[6]; + aRes[2] = strtol(aBuf, &aP, 16); + } else { + int aPos = theColor.find(","); + char aBuf[10]; + // Get Red + std::size_t length = theColor.copy(aBuf, aPos, 0); + aBuf[length] = '\0'; + aRes[0] = atoi(aBuf); + + // Get Green + int aNPos = theColor.find(",", aPos + 1); + length = theColor.copy(aBuf, aNPos - aPos - 1, aPos + 1); + aBuf[length] = '\0'; + aRes[1] = atoi(aBuf); + + // Get Green + length = theColor.copy(aBuf, theColor.length() - aNPos - 1, aNPos + 1); + aBuf[length] = '\0'; + aRes[2] = atoi(aBuf); + } return aRes; -} \ No newline at end of file +} + +int stringToInteger(const std::string& theInt) +{ + return atoi(theInt.c_str()); +} + +double stringToDouble(const std::string& theDouble) +{ + char* p; + return strtod(theDouble.c_str(), &p); +} + +bool stringToBoolean(const std::string& theBoolean) +{ + return theBoolean == "true"; +}