From: mpv Date: Mon, 4 Aug 2014 10:28:45 +0000 (+0400) Subject: Set of validators accessible for one feature or attribute X-Git-Tag: V_0.4.4~119^2 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=381c91777991209024958c5a92b2302ff63b3e21;p=modules%2Fshaper.git Set of validators accessible for one feature or attribute --- diff --git a/src/Model/Model_Validator.cpp b/src/Model/Model_Validator.cpp index 8ca1bf09f..77af27b99 100644 --- a/src/Model/Model_Validator.cpp +++ b/src/Model/Model_Validator.cpp @@ -6,103 +6,88 @@ #include #include -using namespace std; - void Model_ValidatorsFactory::registerValidator( - const string& theID, ModelAPI_Validator* theValidator) + const std::string& theID, ModelAPI_Validator* theValidator) { if (myIDs.find(theID) != myIDs.end()) { - Events_Error::send(string("Validator ") + theID + " is already registered"); + Events_Error::send(std::string("Validator ") + theID + " is already registered"); } else { myIDs[theID] = theValidator; } } -void Model_ValidatorsFactory::assignValidator(const string& theID, const string& theFeatureID) +void Model_ValidatorsFactory::assignValidator( + const std::string& theID, const std::string& theFeatureID) { - bool isError = false; - map::iterator aVal = myIDs.find(theID); - if (aVal == myIDs.end()) { - Events_Error::send( - string("Validator ") + theID + " for feature " + theFeatureID + " was not registered"); - isError = true; - } - if (myFeatures.find(theFeatureID) != myFeatures.end()) { - // TODO: it is called many times now because of not-optimized XML reader - //Events_Error::send( - // string("Validator for feature ") + theFeatureID + " is already registered"); - isError = true; + if (myFeatures.find(theFeatureID) == myFeatures.end()) { + myFeatures[theFeatureID] = std::set(); } - if (!isError) - myFeatures[theFeatureID] = aVal->second; + myFeatures[theFeatureID].insert(theID); } -void Model_ValidatorsFactory::assignValidator(const string& theID, - const string& theFeatureID, const string& theAttrID, const list& theArguments) +void Model_ValidatorsFactory::assignValidator(const std::string& theID, + const std::string& theFeatureID, const std::string& theAttrID, + const std::list& theArguments) { - bool isError = false; - map::iterator aVal = myIDs.find(theID); - if (aVal == myIDs.end()) { - Events_Error::send( - string("Validator ") + theID + " for feature " + theFeatureID + " was not registered"); - isError = true; - } // create feature-structures if not exist - map > > >::iterator - aFeature = myAttrs.find(theFeatureID); + std::map >::iterator aFeature = + myAttrs.find(theFeatureID); if (aFeature == myAttrs.end()) { - myAttrs[theFeatureID] = map > >(); + myAttrs[theFeatureID] = std::map(); aFeature = myAttrs.find(theFeatureID); } // add attr-structure if not exist, or generate error if already exist - map > >::iterator - anAttr = aFeature->second.find(theAttrID); + std::map::iterator anAttr = aFeature->second.find(theAttrID); if (anAttr == aFeature->second.end()) { - if (!isError) { - aFeature->second[theAttrID] = - pair >(aVal->second, theArguments); - } - } else { - // TODO: it is called many times now because of not-optimized XML reader - //Events_Error::send( - // string("Validator ") + theID + " for feature " + theFeatureID + - // "attribute " + theAttrID + " is already registered"); - isError = true; + aFeature->second[theAttrID] = AttrValidators(); } + aFeature->second[theAttrID].insert( + std::pair >(theID, theArguments)); } -const ModelAPI_Validator* Model_ValidatorsFactory::validator(const string& theFeatureID) const +void Model_ValidatorsFactory::validators( + const std::string& theFeatureID, std::list& theResult ) const { - map::const_iterator aFeature = myFeatures.find(theFeatureID); - if (aFeature != myFeatures.cend()) - return aFeature->second; - return NULL; // not found + std::map >::const_iterator aFeature = + myFeatures.find(theFeatureID); + if (aFeature != myFeatures.cend()) { + std::set::const_iterator aValIter = aFeature->second.cbegin(); + for(; aValIter != aFeature->second.cend(); aValIter++) { + std::map::const_iterator aFound = myIDs.find(*aValIter); + if (aFound == myIDs.end()) { + Events_Error::send(std::string("Validator ") + *aValIter + " was not registered"); + } else { + theResult.push_back(aFound->second); + } + } + } } -const ModelAPI_Validator* Model_ValidatorsFactory::validator( - const std::string& theFeatureID, const std::string& theAttrID) const +void Model_ValidatorsFactory::validators(const std::string& theFeatureID, + const std::string& theAttrID, std::list& theValidators, + std::list >& theArguments) const { - map > > >::const_iterator - aFeature = myAttrs.find(theFeatureID); - if (aFeature == myAttrs.cend()) return NULL; // feature is not found - map > >::const_iterator - anAttr = aFeature->second.find(theAttrID); - if (anAttr == aFeature->second.cend()) return NULL; // attribute is not found - return anAttr->second.first; + std::map >::const_iterator aFeature = + myAttrs.find(theFeatureID); + if (aFeature != myAttrs.cend()) { + std::map::const_iterator anAttr = + aFeature->second.find(theAttrID); + if (anAttr != aFeature->second.end()) { + AttrValidators::const_iterator aValIter = anAttr->second.cbegin(); + for(; aValIter != anAttr->second.cend(); aValIter++) { + std::map::const_iterator aFound = + myIDs.find(aValIter->first); + if (aFound == myIDs.end()) { + Events_Error::send(std::string("Validator ") + aValIter->first + " was not registered"); + } else { + theValidators.push_back(aFound->second); + theArguments.push_back(aValIter->second); + } + } + } + } } -/*bool Model_ValidatorsFactory::validate( - const boost::shared_ptr& theFeature, const string& theAttrID ) const -{ - map > > >::const_iterator - aFeature = myAttrs.find(theFeature->getKind()); - if (aFeature == myAttrs.cend()) return true; // feature is not found - map > >::const_iterator - anAttr = aFeature->second.find(theAttrID); - if (anAttr == aFeature->second.cend()) return true; // attribute is not found - return anAttr->second.first->validate(theFeature, theAttrID, anAttr->second.second); -}*/ - Model_ValidatorsFactory::Model_ValidatorsFactory() : ModelAPI_ValidatorsFactory() { } diff --git a/src/Model/Model_Validator.h b/src/Model/Model_Validator.h index 8655b91a9..f1cfe24fc 100644 --- a/src/Model/Model_Validator.h +++ b/src/Model/Model_Validator.h @@ -8,6 +8,7 @@ #include #include #include +#include /**\class Model_ValidatorsFactory * \ingroup DataModel @@ -23,10 +24,14 @@ */ class Model_ValidatorsFactory: public ModelAPI_ValidatorsFactory { +private: std::map myIDs; ///< map from ID to registered validator - std::map myFeatures; ///< validators by feature ID - std::map > > > myAttrs; ///< validators and arguments by feature and attribute IDs + /// validators IDs by feature ID + std::map > myFeatures; + /// set of pairs: validators IDs, list of arguments + typedef std::set > > AttrValidators; + /// validators IDs and arguments by feature and attribute IDs + std::map > myAttrs; public: /// Registers the instance of the validator by the ID MODEL_EXPORT virtual void registerValidator( @@ -42,10 +47,13 @@ public: const std::list& theArguments); /// Provides a validator for the feature, returns NULL if no validator - MODEL_EXPORT virtual const ModelAPI_Validator* validator(const std::string& theFeatureID) const; + MODEL_EXPORT virtual void validators(const std::string& theFeatureID, + std::list& theResult) const; /// Provides a validator for the attribute, returns NULL if no validator - MODEL_EXPORT virtual const ModelAPI_Validator* validator( - const std::string& theFeatureID, const std::string& theAttrID) const; + MODEL_EXPORT virtual void validators( + const std::string& theFeatureID, const std::string& theAttrID, + std::list& theValidators, + std::list >& theArguments) const; /// Returns the result of "validate" method for attribute of validator. /// If validator is not exists, returns true: everything is valid by default. diff --git a/src/ModelAPI/ModelAPI_Validator.h b/src/ModelAPI/ModelAPI_Validator.h index 7bc08324a..4f17e634c 100644 --- a/src/ModelAPI/ModelAPI_Validator.h +++ b/src/ModelAPI/ModelAPI_Validator.h @@ -65,11 +65,13 @@ public: const std::list& theArguments) = 0; /// Provides a validator for the feature, returns NULL if no validator - virtual const ModelAPI_Validator* validator(const std::string& theFeatureID) const = 0; - + virtual void validators(const std::string& theFeatureID, + std::list& theResult) const = 0; /// Provides a validator for the attribute, returns NULL if no validator - virtual const ModelAPI_Validator* validator( - const std::string& theFeatureID, const std::string& theAttrID) const = 0; + virtual void validators( + const std::string& theFeatureID, const std::string& theAttrID, + std::list& theValidators, + std::list >& theArguments) const = 0; /// Returns the result of "validate" method for attribute of validator. /// If validator is not exists, returns true: everything is valid by default. diff --git a/src/ModuleBase/ModuleBase_WidgetFeature.cpp b/src/ModuleBase/ModuleBase_WidgetFeature.cpp index b08058a60..0246514d1 100644 --- a/src/ModuleBase/ModuleBase_WidgetFeature.cpp +++ b/src/ModuleBase/ModuleBase_WidgetFeature.cpp @@ -76,13 +76,18 @@ bool ModuleBase_WidgetFeature::setObject(const ObjectPtr& theObject) { PluginManagerPtr aMgr = ModelAPI_PluginManager::get(); ModelAPI_ValidatorsFactory* aFactory = aMgr->validators(); - const ModelAPI_Validator* aValidator = aFactory->validator(parentID(), attributeID()); - if (aValidator) { - const ModuleBase_ResultValidator* aResValidator = - dynamic_cast(aValidator); - if (aResValidator) { - if (!aResValidator->isValid(theObject)) - return false; + std::list aValidators; + std::list > anArguments; + aFactory->validators(parentID(), attributeID(), aValidators, anArguments); + std::list::iterator aValidator = aValidators.begin(); + for(; aValidator != aValidators.end(); aValidator++) { + if (*aValidator) { + const ModuleBase_ResultValidator* aResValidator = + dynamic_cast(*aValidator); + if (aResValidator) { + if (!aResValidator->isValid(theObject)) + return false; + } } } diff --git a/src/XGUI/XGUI_Workshop.cpp b/src/XGUI/XGUI_Workshop.cpp index a8d23dd52..df6d0493b 100644 --- a/src/XGUI/XGUI_Workshop.cpp +++ b/src/XGUI/XGUI_Workshop.cpp @@ -1022,12 +1022,16 @@ void XGUI_Workshop::updateCommandsOnViewSelection() QList aActions = getModuleCommands(); foreach(QAction* aAction, aActions) { QString aId = aAction->data().toString(); - const ModelAPI_Validator* aValidator = aFactory->validator(aId.toStdString()); - if (aValidator) { - const ModuleBase_SelectionValidator* aSelValidator = - dynamic_cast(aValidator); - if (aSelValidator) { - aAction->setEnabled(aSelValidator->isValid(aSelection)); + std::list aValidators; + aFactory->validators(aId.toStdString(), aValidators); + std::list::iterator aValidator = aValidators.begin(); + for(; aValidator != aValidators.end(); aValidator++) { + if (*aValidator) { + const ModuleBase_SelectionValidator* aSelValidator = + dynamic_cast(*aValidator); + if (aSelValidator) { + aAction->setEnabled(aSelValidator->isValid(aSelection)); + } } } }