Salome HOME
Get rid of compilation warnings. Part II. MSVC warnings.
[modules/shaper.git] / src / ParametersPlugin / ParametersPlugin_Validators.cpp
1 // Copyright (C) 2014-2020  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include <ParametersPlugin_Validators.h>
21
22 #include <ParametersPlugin_Parameter.h>
23
24 #include <Events_InfoMessage.h>
25
26 #include <ModelAPI_AttributeString.h>
27 #include <ModelAPI_Feature.h>
28 #include <ModelAPI_ResultParameter.h>
29 #include <ModelAPI_Tools.h>
30 #include <ModelAPI_Expression.h>
31
32 ParametersPlugin_VariableValidator::ParametersPlugin_VariableValidator()
33 {
34 }
35
36 ParametersPlugin_VariableValidator::~ParametersPlugin_VariableValidator()
37 {
38 }
39
40 bool ParametersPlugin_VariableValidator::isValid(const AttributePtr& theAttribute,
41                                                  const std::list<std::string>& /*theArguments*/,
42                                                  Events_InfoMessage& theError) const
43 {
44   AttributeStringPtr aStrAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
45   if (!aStrAttr->isInitialized()) {
46     theError = "Attribute \"%1\" is not initialized.";
47     theError.arg(aStrAttr->id());
48     return false;
49   }
50   bool isEmptyExpr = aStrAttr->value().empty();
51   if (isEmptyExpr) {
52     theError = "Attribute \"%1\" value is empty.";
53     theError.arg(aStrAttr->id());
54     return false;
55   }
56   if (!ModelAPI_Expression::isVariable(aStrAttr->value())) {
57     theError = "Incorrect variable name.";
58     return false;
59   }
60   if (!isUnique(theAttribute, aStrAttr->value())) {
61     theError = "Variable name is not unique.";
62     return false;
63   }
64   return true;
65 }
66
67 bool ParametersPlugin_VariableValidator::isUnique(const AttributePtr& theAttribute,
68                                                   const std::string& theString) const
69 {
70   DocumentPtr aDocument = theAttribute->owner()->document();
71   for (int anIndex = 0, aSize = aDocument->size(ModelAPI_ResultParameter::group());
72        anIndex < aSize; ++anIndex) {
73     ObjectPtr aParamObj = aDocument->object(ModelAPI_ResultParameter::group(), anIndex);
74     if (aParamObj->data()->name() != theString)
75       continue;
76     ResultParameterPtr aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
77     if (!aParam.get())
78       continue;
79     FeaturePtr aFeature = ModelAPI_Feature::feature(aParam);
80     if (aFeature == theAttribute->owner())
81       continue;
82     return false;
83   }
84   return true;
85 }
86
87 ParametersPlugin_ExpressionValidator::ParametersPlugin_ExpressionValidator()
88 {
89
90 }
91
92 ParametersPlugin_ExpressionValidator::~ParametersPlugin_ExpressionValidator()
93 {
94
95 }
96
97 bool ParametersPlugin_ExpressionValidator::isValid(const AttributePtr& theAttribute,
98                                                    const std::list<std::string>& /*theArguments*/,
99                                                    Events_InfoMessage& theError) const
100 {
101   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
102
103   AttributeStringPtr aStrAttr =
104       std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
105   if (!aStrAttr->isInitialized()) {
106     theError = "Attribute \"%1\" is not initialized.";
107     theError.arg(aStrAttr->id());
108     return false;
109   }
110   bool isEmptyExpr = aStrAttr->value().empty();
111   if (isEmptyExpr) {
112     theError = "Expression is empty.";
113     return false;
114   }
115
116   theError = aFeature->string(ParametersPlugin_Parameter::EXPRESSION_ERROR_ID())->value();
117   return theError.empty();
118 }