#include <string>
-bool ExchangePlugin_ImportFormatValidator::isValid(const FeaturePtr& theFeature,
- const std::list<std::string>& theArguments,
- const ObjectPtr& theObject) const
+bool ExchangePlugin_ImportFormatValidator::isValid(
+ const AttributePtr& theAttribute, const std::list<std::string>& theArguments) const
{
PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
class ExchangePlugin_ImportFormatValidator : public ModelAPI_AttributeValidator
{
public:
- virtual bool isValid(const FeaturePtr& theFeature,
- const std::list<std::string>& theArguments,
- const ObjectPtr& theObject) const;
+ virtual bool isValid(
+ const AttributePtr& theAttribute, const std::list<std::string>& theArguments) const;
};
std::list<boost::shared_ptr<ModelAPI_Attribute> > Model_Data::attributes(const std::string& theType)
{
std::list<boost::shared_ptr<ModelAPI_Attribute> > aResult;
- std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter = myAttrs.begin();
+ std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
+ myAttrs.begin();
for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
aResult.push_back(anAttrsIter->second);
return aResult;
}
+std::list<std::string> Model_Data::attributesIDs(const std::string& theType)
+{
+ std::list<std::string> aResult;
+ std::map<std::string, boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrsIter =
+ myAttrs.begin();
+ for (; anAttrsIter != myAttrs.end(); anAttrsIter++) {
+ if (theType.empty() || anAttrsIter->second->attributeType() == theType) {
+ aResult.push_back(anAttrsIter->first);
+ }
+ }
+ return aResult;
+}
+
void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
{
theAttr->setInitialized();
/// Returns the generic attribute by identifier
/// \param theID identifier of the attribute
MODEL_EXPORT virtual boost::shared_ptr<ModelAPI_Attribute> attribute(const std::string& theID);
- /// Returns all attributes ofthe feature of the given type
+ /// Returns all attributes of the feature of the given type
/// or all attributes if "theType" is empty
MODEL_EXPORT virtual std::list<boost::shared_ptr<ModelAPI_Attribute> >
attributes(const std::string& theType);
+ /// Returns all attributes ids of the feature of the given type
+ /// or all attributes if "theType" is empty
+ MODEL_EXPORT virtual std::list<std::string> attributesIDs(const std::string& theType);
/// Identifier by the id (not fast, iteration by map)
/// \param theAttr attribute already created in this data
#include <list>
#include <boost/shared_ptr.hpp>
-bool Model_FeatureValidator::isValid(const boost::shared_ptr<ModelAPI_Feature>& theFeature) const
+bool Model_FeatureValidator::isValid(const boost::shared_ptr<ModelAPI_Feature>& theFeature,
+ const std::list<std::string>& theArguments) const
{
boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
if (!aData->isValid())
public:
/// Returns true if feature and/or attributes are valid
/// \param theFeature the validated feature
- MODEL_EXPORT virtual bool isValid(const boost::shared_ptr<ModelAPI_Feature>& theFeature) const;
+ MODEL_EXPORT virtual bool isValid(const boost::shared_ptr<ModelAPI_Feature>& theFeature,
+ const std::list<std::string>& theArguments) const;
};
#endif
#include <Model_Validator.h>
#include <Model_FeatureValidator.h>
#include <ModelAPI_Feature.h>
+#include <ModelAPI_Attribute.h>
+#include <ModelAPI_Data.h>
+#include <ModelAPI_AttributeValidator.h>
#include <Events_Error.h>
+const static std::string DefaultId = "Model_FeatureValidator";
+
void Model_ValidatorsFactory::registerValidator(const std::string& theID,
- ModelAPI_Validator* theValidator)
+ ModelAPI_Validator* theValidator)
{
if (myIDs.find(theID) != myIDs.end()) {
Events_Error::send(std::string("Validator ") + theID + " is already registered");
}
void Model_ValidatorsFactory::assignValidator(const std::string& theID,
- const std::string& theFeatureID)
+ const std::string& theFeatureID)
{
if (myFeatures.find(theFeatureID) == myFeatures.end()) {
myFeatures[theFeatureID] = AttrValidators();
}
void Model_ValidatorsFactory::assignValidator(const std::string& theID,
- const std::string& theFeatureID,
- const std::list<std::string>& theArguments)
+ const std::string& theFeatureID,
+ const std::list<std::string>& theArguments)
{
if (myFeatures.find(theFeatureID) == myFeatures.end()) {
myFeatures[theFeatureID] = AttrValidators();
}
void Model_ValidatorsFactory::assignValidator(const std::string& theID,
- const std::string& theFeatureID,
- const std::string& theAttrID,
- const std::list<std::string>& theArguments)
+ const std::string& theFeatureID,
+ const std::string& theAttrID,
+ const std::list<std::string>& theArguments)
{
// create feature-structures if not exist
std::map<std::string, std::map<std::string, AttrValidators> >::iterator aFeature = myAttrs.find(
- theFeatureID);
+ theFeatureID);
if (aFeature == myAttrs.end()) {
myAttrs[theFeatureID] = std::map<std::string, AttrValidators>();
aFeature = myAttrs.find(theFeatureID);
}
void Model_ValidatorsFactory::validators(const std::string& theFeatureID,
- std::list<ModelAPI_Validator*>& theResult,
- std::list<std::list<std::string> >& theArguments) const
+ std::list<ModelAPI_Validator*>& theResult,
+ std::list<std::list<std::string> >& theArguments) const
{
std::map<std::string, AttrValidators>::const_iterator aFeature = myFeatures.find(theFeatureID);
if (aFeature != myFeatures.cend()) {
}
void Model_ValidatorsFactory::validators(const std::string& theFeatureID,
- const std::string& theAttrID,
- std::list<ModelAPI_Validator*>& theValidators,
- std::list<std::list<std::string> >& theArguments) const
+ const std::string& theAttrID,
+ std::list<ModelAPI_Validator*>& theValidators,
+ std::list<std::list<std::string> >& theArguments) const
{
- std::map<std::string, std::map<std::string, AttrValidators> >::const_iterator aFeature = myAttrs
- .find(theFeatureID);
+ std::map<std::string, std::map<std::string, AttrValidators> >::const_iterator aFeature =
+ myAttrs.find(theFeatureID);
if (aFeature != myAttrs.cend()) {
std::map<std::string, AttrValidators>::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<std::string, ModelAPI_Validator*>::const_iterator aFound = myIDs.find(
- aValIter->first);
+ aValIter->first);
if (aFound == myIDs.end()) {
Events_Error::send(std::string("Validator ") + aValIter->first + " was not registered");
} else {
}
Model_ValidatorsFactory::Model_ValidatorsFactory()
- : ModelAPI_ValidatorsFactory()
+ : ModelAPI_ValidatorsFactory()
{
registerValidator("Model_FeatureValidator", new Model_FeatureValidator);
}
void Model_ValidatorsFactory::addDefaultValidators(std::list<ModelAPI_Validator*>& theValidators) const
{
- std::string anId = "Model_FeatureValidator";
- std::map<std::string, ModelAPI_Validator*>::const_iterator it = myIDs.find(anId);
+ std::map<std::string, ModelAPI_Validator*>::const_iterator it = myIDs.find(DefaultId);
if(it == myIDs.end())
return;
theValidators.push_back(it->second);
}
+
+bool Model_ValidatorsFactory::validate(const boost::shared_ptr<ModelAPI_Feature>& theFeature) const
+{
+ // check feature validators first
+ std::map<std::string, AttrValidators>::const_iterator aFeature =
+ myFeatures.find(theFeature->getKind());
+ if (aFeature != myFeatures.end()) {
+ AttrValidators::const_iterator aValidator = aFeature->second.begin();
+ for(; aValidator != aFeature->second.end(); aValidator++) {
+ std::map<std::string, ModelAPI_Validator*>::const_iterator aValFind =
+ myIDs.find(aValidator->first);
+ if (aValFind == myIDs.end()) {
+ Events_Error::send(std::string("Validator ") + aValidator->first + " was not registered");
+ continue;
+ }
+ const ModelAPI_FeatureValidator* aFValidator =
+ dynamic_cast<const ModelAPI_FeatureValidator*>(aValFind->second);
+ if (aFValidator) {
+ if (!aFValidator->isValid(theFeature, aValidator->second))
+ return false;
+ }
+ }
+ }
+ // check default validator
+ std::map<std::string, ModelAPI_Validator*>::const_iterator aDefaultVal = myIDs.find(DefaultId);
+ if(aDefaultVal != myIDs.end()) {
+ static const std::list<std::string> anEmptyArgList;
+ const ModelAPI_FeatureValidator* aFValidator =
+ dynamic_cast<const ModelAPI_FeatureValidator*>(aDefaultVal->second);
+ if (aFValidator) {
+ if (!aFValidator->isValid(theFeature, anEmptyArgList))
+ return false;
+ }
+ }
+ // check all attributes for validity
+ boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
+ if (!aData->isValid())
+ return false;
+ static const std::string kAllTypes = "";
+ std::map<std::string, std::map<std::string, AttrValidators> >::const_iterator aFeatureIter =
+ myAttrs.find(theFeature->getKind());
+ if (aFeatureIter != myAttrs.cend()) {
+ std::list<std::string> aLtAttributes = aData->attributesIDs(kAllTypes);
+ std::list<std::string>::iterator anAttrIter = aLtAttributes.begin();
+ for (; anAttrIter != aLtAttributes.end(); anAttrIter++) {
+ std::map<std::string, AttrValidators>::const_iterator anAttr =
+ aFeatureIter->second.find(*anAttrIter);
+ if (anAttr != aFeatureIter->second.end()) {
+ AttrValidators::const_iterator aValIter = anAttr->second.cbegin();
+ for (; aValIter != anAttr->second.cend(); aValIter++) {
+ std::map<std::string, ModelAPI_Validator*>::const_iterator aFound = myIDs.find(
+ aValIter->first);
+ if (aFound == myIDs.end()) {
+ Events_Error::send(std::string("Validator ") + aValIter->first + " was not registered");
+ } else {
+ const ModelAPI_AttributeValidator* anAttrValidator =
+ dynamic_cast<const ModelAPI_AttributeValidator*>(aFound->second);
+ if (anAttrValidator) {
+ if (!anAttrValidator->isValid(theFeature->data()->attribute(*anAttrIter),
+ aValIter->second)) {
+ return false;
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ return true;
+}
std::list<std::list<std::string> >& theArguments) const;
/// Returns registered validator by its Id
- virtual const ModelAPI_Validator* validator(const std::string& theID) const;
+ MODEL_EXPORT virtual const ModelAPI_Validator* validator(const std::string& theID) const;
- /// Returns the result of "validate" method for attribute of validator.
- /// If validator is not exists, returns true: everything is valid by default.
- //MODEL_EXPORT virtual bool validate(
- // const boost::shared_ptr<ModelAPI_Feature>& theFeature, const std::string& theAttrID) const;
+ /// Returns true if feature and all its attributes are valid.
+ MODEL_EXPORT virtual bool validate(const boost::shared_ptr<ModelAPI_Feature>& theFeature) const;
protected:
void addDefaultValidators(std::list<ModelAPI_Validator*>& theValidators) const;
ModelAPI_ResultConstruction.h
ModelAPI_ResultPart.h
ModelAPI_ResultParameters.h
- ModelAPI_ResultValidator.h
- ModelAPI_AttributeValidator.h
- ModelAPI_Tools.h
+ ModelAPI_ResultValidator.h
+ ModelAPI_AttributeValidator.h
+ ModelAPI_Tools.h
+ ModelAPI_RefAttrValidator.h
)
SET(PROJECT_SOURCES
ModelAPI_Feature.cpp
ModelAPI_PluginManager.cpp
- ModelAPI_Tools.cpp
+ ModelAPI_Tools.cpp
)
SET(PROJECT_LIBRARIES
}
/// Returns the owner of this attribute
- MODELAPI_EXPORT const boost::shared_ptr<ModelAPI_Object>& owner()
+ MODELAPI_EXPORT const boost::shared_ptr<ModelAPI_Object>& owner() const
{
return myObject;
}
// File: ModelAPI_AttributeValidator.h
-// Created: 5 Aug 2014
-// Author: Vitaly SMETANNIKOV
+// Created: 4 Sep 2014
+// Author: Mikhail PONIKAROV
#ifndef ModelAPI_AttributeValidator_H
#define ModelAPI_AttributeValidator_H
-#include <ModelAPI_Feature.h>
-#include <ModelAPI_Object.h>
+#include <ModelAPI_Attribute.h>
#include <ModelAPI_Validator.h>
+/**
+ * Generic validator for any attribute of a feature.
+ */
class ModelAPI_AttributeValidator : public ModelAPI_Validator
{
- public:
- virtual bool isValid(const FeaturePtr& theFeature, const std::list<std::string>& theArguments,
- const ObjectPtr& theObject) const = 0;
+public:
+ //! returns true if attribute is valid
+ //! \param theAttribute the checked attribute
+ //! \param theArguments arguments of the attribute
+ virtual bool isValid(
+ const AttributePtr& theAttribute, const std::list<std::string>& theArguments) const = 0;
};
#endif
/// Returns the generic attribute by identifier
/// \param theID identifier of the attribute
virtual boost::shared_ptr<ModelAPI_Attribute> attribute(const std::string& theID) = 0;
- /// Returns all attributes ofthe feature of the given type
+ /// Returns all attributes of the feature of the given type
/// or all attributes if "theType" is empty
virtual std::list<boost::shared_ptr<ModelAPI_Attribute> >
attributes(const std::string& theType) = 0;
+ /// Returns all attributes ids of the feature of the given type
+ /// or all attributes if "theType" is empty
+ virtual std::list<std::string> attributesIDs(const std::string& theType) = 0;
/// Identifier by the id (not fast, iteration by map)
/// \param theAttr attribute already created in this data
virtual const std::string& id(const boost::shared_ptr<ModelAPI_Attribute>& theAttr) = 0;
/// \param theFeature the validated feature
/// \param theAttr the validated attribute ID, empty string of feature is validated
/// \param theArguments list of string, feature attribute names: dependent attributes
- virtual bool isValid(const boost::shared_ptr<ModelAPI_Feature>& theFeature) const = 0;
+ virtual bool isValid(const boost::shared_ptr<ModelAPI_Feature>& theFeature,
+ const std::list<std::string>& theArguments) const = 0;
};
#endif
--- /dev/null
+// File: ModelAPI_RefAttrValidator.h
+// Created: 5 Aug 2014
+// Author: Vitaly SMETANNIKOV
+
+#ifndef ModelAPI_RefAttrValidator_H
+#define ModelAPI_RefAttrValidator_H
+
+#include <ModelAPI_Feature.h>
+#include <ModelAPI_Object.h>
+#include <ModelAPI_AttributeValidator.h>
+
+/*
+ * Used for filtering out the object that can be used for reference attribute value
+ */
+class ModelAPI_RefAttrValidator : public ModelAPI_AttributeValidator
+{
+public:
+ //! Returns true if object is good for the feature attribute
+ virtual bool isValid(const FeaturePtr& theFeature, const std::list<std::string>& theArguments,
+ const ObjectPtr& theObject) const = 0;
+};
+
+#endif
/// Returns registered validator by its Id
virtual const ModelAPI_Validator* validator(const std::string& theID) const = 0;
- /// Returns the result of "validate" method for attribute of validator.
- /// If validator is not exists, returns true: everything is valid by default.
- //virtual bool validate(
- // const boost::shared_ptr<ModelAPI_Feature>& theFeature, const std::string& theAttrID) const = 0;
+ /// Returns true if feature and all its attributes are valid.
+ virtual bool validate(const boost::shared_ptr<ModelAPI_Feature>& theFeature) const = 0;
protected:
/// Get instance from PluginManager
#include <ModelAPI_AttributeRefAttr.h>
#include <ModelAPI_Validator.h>
#include <ModelAPI_ResultValidator.h>
-#include <ModelAPI_AttributeValidator.h>
+#include <ModelAPI_RefAttrValidator.h>
#include <QWidget>
#include <QLineEdit>
aValidator = aValidators.begin();
std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
for (; aValidator != aValidators.end(); aValidator++, aArgs++) {
- const ModelAPI_AttributeValidator* aAttrValidator =
- dynamic_cast<const ModelAPI_AttributeValidator*>(*aValidator);
+ const ModelAPI_RefAttrValidator* aAttrValidator =
+ dynamic_cast<const ModelAPI_RefAttrValidator*>(*aValidator);
if (aAttrValidator) {
if (!aAttrValidator->isValid(myFeature, *aArgs, theObject)) {
return false;
boost::shared_ptr<GeomAPI_Shape> aShape = ModelAPI_Tools::shape(aResult);
return aShape && aShape->isEdge() && GeomAPI_Curve(aShape).isCircle();
}
-
return false;
}
+bool SketchPlugin_DistanceAttrValidator::isValid(
+ const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
+{
+ boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
+ boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
+ if (anAttr) {
+ const ObjectPtr& anObj = theAttribute->owner();
+ const FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
+ return isValid(aFeature, theArguments, anAttr->object());
+ }
+ return true; // it may be not reference attribute, in this case, it is OK
+}
#define SketchPlugin_Validators_H
#include "SketchPlugin.h"
-//#include <ModuleBase_FeatureValidator.h>
-#include <ModelAPI_AttributeValidator.h>
+#include <ModelAPI_RefAttrValidator.h>
class SketchPlugin_DistanceAttrValidator : public ModelAPI_AttributeValidator
{
public:
+ //! returns true if attribute is valid
+ //! \param theAttribute the checked attribute
+ //! \param theArguments arguments of the attribute
+ virtual bool isValid(
+ const AttributePtr& theAttribute, const std::list<std::string>& theArguments) const;
+ //! Returns true if object is good for the feature attribute
virtual bool isValid(const FeaturePtr& theFeature, const std::list<std::string>& theArguments,
const ObjectPtr& theObject) const;
//Get validators for the Id
PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
- std::list<ModelAPI_Validator*> aValidators;
- std::list<std::list<std::string> > anArguments;
- aFactory->validators(anOperationId.toStdString(), aValidators, anArguments);
- //
- std::list<ModelAPI_Validator*>::iterator it = aValidators.begin();
- bool isValid = true;
- for (; it != aValidators.end(); it++) {
- const ModelAPI_FeatureValidator* aFeatureValidator =
- dynamic_cast<const ModelAPI_FeatureValidator*>(*it);
- if (!aFeatureValidator)
- continue;
- if (!aFeatureValidator->isValid(aFeature)) {
- isValid = false;
- break;
- }
- }
+
+ bool isValid = aFactory->validate(aFeature);
emit operationValidated(isValid);
}