Salome HOME
Merge remote-tracking branch 'origin/Toolbars_Management'
[modules/shaper.git] / src / GeomValidators / GeomValidators_ValueOrder.cpp
1 // Copyright (C) 2017-20xx  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "GeomValidators_ValueOrder.h"
22
23 #include <Config_PropManager.h>
24 #include <Events_InfoMessage.h>
25
26 #include <ModelAPI_AttributeDouble.h>
27 #include <ModelAPI_AttributeInteger.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_Validator.h>
30
31 // LCOV_EXCL_START
32 static double attributeValue(AttributePtr theAttr)
33 {
34   AttributeIntegerPtr anIntAttr = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(theAttr);
35   if (anIntAttr)
36     return (double)anIntAttr->value();
37   AttributeDoublePtr aDoubleAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttr);
38   if (aDoubleAttr)
39     return aDoubleAttr->value();
40   return 0.0;
41 }
42
43 static bool isGreaterOrEqual(AttributePtr theFirstArg, AttributePtr theSecondArg)
44 {
45   if (theFirstArg && theFirstArg->isInitialized() &&
46       theSecondArg && theSecondArg->isInitialized())
47     return attributeValue(theFirstArg) >= attributeValue(theSecondArg);
48   return false;
49 }
50
51 static bool isLessOrEqual(AttributePtr theFirstArg, AttributePtr theSecondArg)
52 {
53   if (theFirstArg && theFirstArg->isInitialized() &&
54       theSecondArg && theSecondArg->isInitialized())
55     return attributeValue(theFirstArg) <= attributeValue(theSecondArg);
56   return false;
57 }
58
59 // Check the attributes are satisfy theCompare function
60 static bool isValidOrder(const AttributePtr& theAttribute,
61                          const std::list<std::string>& theArguments,
62                          Events_InfoMessage& theError,
63                          bool (*theCompare)(AttributePtr, AttributePtr))
64 {
65   std::string anAttrType = theAttribute->attributeType();
66   if (anAttrType != ModelAPI_AttributeDouble::typeId() &&
67       anAttrType != ModelAPI_AttributeInteger::typeId()) {
68     theError = "Unsupported attribute type (integer or double applicable)";
69     return false;
70   }
71
72   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
73   if (!anOwner) {
74     theError = "Attribute without owner";
75     return false;
76   }
77
78   for (std::list<std::string>::const_iterator anIt = theArguments.begin();
79        anIt != theArguments.end(); ++anIt) {
80     // check the argument links to the attribute of the current feature
81     AttributePtr aCurAttr = anOwner->attribute(*anIt);
82     if (!aCurAttr) {
83       theError = "Arguments should be names of attributes of current feature";
84       return false;
85     }
86
87     // compare values
88     if (!(*theCompare)(theAttribute, aCurAttr)) {
89       theError = "Attributes have incorrect order";
90       return false;
91     }
92   }
93
94   return true;
95 }
96 // LCOV_EXCL_STOP
97
98
99 /// Global instance for validators factory
100 GeomValidators_GreaterOrEqual MY_GEQ_INSTANCE;
101 GeomValidators_LessOrEqual MY_LEQ_INSTANCE;
102
103 GeomValidators_GreaterOrEqual::GeomValidators_GreaterOrEqual()
104 {
105   // this validator is registered in the factory on this library loading
106   SessionPtr aMgr = ModelAPI_Session::get();
107   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
108   aFactory->registerValidator("GeomValidators_GreaterOrEqual", this);
109 }
110
111 // LCOV_EXCL_START
112 bool GeomValidators_GreaterOrEqual::isValid(const AttributePtr& theAttribute,
113                                             const std::list<std::string>& theArguments,
114                                             Events_InfoMessage& theError) const
115 {
116   return isValidOrder(theAttribute, theArguments, theError, &isGreaterOrEqual);
117 }
118 // LCOV_EXCL_STOP
119
120
121 GeomValidators_LessOrEqual::GeomValidators_LessOrEqual()
122 {
123   // this validator is registered in the factory on this library loading
124   SessionPtr aMgr = ModelAPI_Session::get();
125   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
126   aFactory->registerValidator("GeomValidators_LessOrEqual", this);
127 }
128
129 // LCOV_EXCL_START
130 bool GeomValidators_LessOrEqual::isValid(const AttributePtr& theAttribute,
131                                          const std::list<std::string>& theArguments,
132                                          Events_InfoMessage& theError) const
133 {
134   return isValidOrder(theAttribute, theArguments, theError, &isLessOrEqual);
135 }
136 // LCOV_EXCL_STOP