Salome HOME
Issue #1079: Make Apply and Cancel buttons independent on enable/disable of complex...
[modules/shaper.git] / src / GeomValidators / GeomValidators_Different.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomValidators_Different.cpp
4 // Created:     08 July 2015
5 // Author:      Sergey POKHODENKO
6
7 #include <GeomValidators_Different.h>
8
9 #include <GeomDataAPI_Point2D.h>
10 #include <GeomAPI_Pnt2d.h>
11
12 #include <algorithm>
13 #include <map>
14 #include <list>
15
16 const double tolerance = 1e-7;
17
18 //=================================================================================================
19 /* Help
20 To extend GeomValidators_Different validator with new attribute types:
21 1. Modify function isEqualAttributes
22 2. Create new implementation of isEqual() for the new type
23 */
24 //=================================================================================================
25
26 bool isEqual(const AttributePoint2DPtr& theLeft, const AttributePoint2DPtr& theRight)
27 {
28   return theLeft->pnt()->distance(theRight->pnt()) < tolerance;
29 }
30
31 bool isEqualAttributes(const AttributePtr& theLeft, const AttributePtr& theRight)
32 {
33   if (theLeft->attributeType() == GeomDataAPI_Point2D::typeId() && 
34       theRight->attributeType() == GeomDataAPI_Point2D::typeId())
35     return isEqual(std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theLeft),
36                    std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theRight));
37   return false;
38 }
39
40 /** \class IsEqual
41  *  \ingroup Validators
42  *  \brief Auxiliary class used in std::find_if
43  */
44 class IsEqual {
45   AttributePtr myAttribute;
46 public:
47   /// Constructor
48   IsEqual(const AttributePtr& theAttribute) : myAttribute(theAttribute) {}
49   /// \return true in case if AttributePtr is equal with myAttribute
50   bool operator()(const AttributePtr& theAttribute) {
51     return isEqualAttributes(myAttribute, theAttribute);
52   }
53 };
54
55 bool GeomValidators_Different::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
56                                        const std::list<std::string>& theArguments,
57                                        std::string& theError) const
58 {
59   std::map<std::string, std::list<AttributePtr> > anAttributesMap;
60   // For all attributes referred by theArguments 
61   // sort it using attributeType() and store into anAttributesMap 
62   std::list<std::string>::const_iterator anArgumentIt = theArguments.begin();
63   for (; anArgumentIt != theArguments.end(); ++anArgumentIt) {
64     AttributePtr anAttribute = theFeature->attribute(*anArgumentIt);
65     anAttributesMap[anAttribute->attributeType()].push_back(anAttribute);
66   }
67
68   // Search differences inside each attribute list
69   std::map<std::string, std::list<AttributePtr> >::const_iterator anAttributesMapIt = anAttributesMap.begin();
70   for (; anAttributesMapIt != anAttributesMap.end(); ++anAttributesMapIt) {
71     const std::list<AttributePtr>& anAttributes = anAttributesMapIt->second;
72     // for the list of attributes check that all elements are unique
73     std::list<AttributePtr>::const_iterator anAttributeIt = anAttributes.begin();
74     if (anAttributeIt != anAttributes.end()) {
75       std::list<AttributePtr>::const_iterator aNextIt = anAttributeIt; ++aNextIt;
76       while (aNextIt != anAttributes.end()) {
77         // if equal attribute is found then all attributes are not different
78         std::list<AttributePtr>::const_iterator aFindIt =
79             std::find_if(aNextIt, anAttributes.end(), IsEqual(*anAttributeIt));
80         if (aFindIt != anAttributes.end()) {
81           theError = "Attributes " + (*anAttributeIt)->id() + " and " + (*aFindIt)->id() + " are equal." ;
82           return false;
83         }
84         ++anAttributeIt;
85         ++aNextIt; 
86       }
87     }
88   }
89
90   return true;
91 }
92
93 bool GeomValidators_Different::isNotObligatory(std::string theFeature, std::string theAttribute)
94 {
95   return true;
96 }