Salome HOME
updated copyright message
[modules/shaper.git] / src / CollectionPlugin / CollectionPlugin_Validators.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
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 "CollectionPlugin_Validators.h"
21 #include "CollectionPlugin_Group.h"
22 #include "CollectionPlugin_Field.h"
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_ResultGroup.h>
25 #include <Events_InfoMessage.h>
26
27
28 bool CollectionPlugin_FieldValidator::isValid(const FeaturePtr& theFeature,
29   const std::list<std::string>& /*theArguments*/,
30   Events_InfoMessage& theError) const
31 {
32   AttributeSelectionListPtr aSelList =
33     theFeature->selectionList(CollectionPlugin_Field::SELECTED_ID());
34   std::string aTypeStr = aSelList->selectionType();
35   if (aTypeStr == "part")
36     return true;
37   if (aSelList->isInitialized()) {
38     int aSize = aSelList->size();
39     bool aIsDefined = aSize > 0;
40     if (!aIsDefined)
41       theError = "Selection list is not initialized";
42     return aIsDefined;
43   }
44   theError = "Selection list is not initialized";
45   return false;
46 }
47
48 static bool isGroupTypeCorrect(const AttributeSelectionPtr& theSelection,
49                                std::string& theType,
50                                Events_InfoMessage& theError)
51 {
52   // applicable the groups only
53   ResultPtr aGroupResult = theSelection->context();
54   if (!aGroupResult.get() || aGroupResult->groupName() != ModelAPI_ResultGroup::group()) {
55     theError = "Error: Groups can be selected only.";
56     return false;
57   }
58   // groups of same type can be selected only
59   FeaturePtr aGroupFeature = ModelAPI_Feature::feature(aGroupResult->data()->owner());
60   std::string aGroupType =
61       aGroupFeature->selectionList(CollectionPlugin_Group::LIST_ID())->selectionType();
62   if (theType.empty())
63     theType = aGroupType;
64   else if (theType != aGroupType) {
65     theError = "Error: Groups should have same type";
66     return false;
67   }
68   return true;
69 }
70
71 bool CollectionPlugin_GroupOperationAttributeValidator::isValid(
72     const AttributePtr& theAttribute,
73     const std::list<std::string>& theArguments,
74     Events_InfoMessage& theError) const
75 {
76   AttributeSelectionListPtr aSelList =
77       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
78   if (!aSelList) {
79     theError = "Error: This validator can only work with selection list of attributes";
80     return false;
81   }
82
83   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
84   // check selected items
85   std::string aType;
86   for (int anIndex = 0; anIndex < aSelList->size(); ++anIndex) {
87     AttributeSelectionPtr aCurSelection = aSelList->value(anIndex);
88     if (!isGroupTypeCorrect(aCurSelection, aType, theError))
89       return false;
90   }
91   // check types of all selection lists are the same
92   for (std::list<std::string>::const_iterator aParIt = theArguments.begin();
93        aParIt != theArguments.end() && !aType.empty(); ++aParIt) {
94     AttributeSelectionListPtr aCurList = anOwner->selectionList(*aParIt);
95     if (aCurList->size() == 0)
96       continue;
97     AttributeSelectionPtr aCurSelection = aCurList->value(0);
98     if (!isGroupTypeCorrect(aCurSelection, aType, theError))
99       return false;
100   }
101   return true;
102 }
103
104 bool CollectionPlugin_GroupSelectionValidator::isValid(
105     const AttributePtr& theAttribute,
106     const std::list<std::string>& /*theArguments*/,
107     Events_InfoMessage& theError) const
108 {
109   AttributeSelectionListPtr aSelList =
110     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
111   if (!aSelList) {
112     theError = "Error: This validator can only work with selection list of attributes";
113     return false;
114   }
115
116   for (int anIndex = 0; anIndex < aSelList->size(); ++anIndex) {
117     AttributeSelectionPtr aCurSelection = aSelList->value(anIndex);
118     FeaturePtr aCurFeature = aCurSelection->contextFeature();
119     ResultPtr aGroupResult = aCurFeature.get() ? aCurFeature->lastResult()
120                                                : aCurSelection->context();
121     if (!aGroupResult.get() || aGroupResult->groupName() == ModelAPI_ResultGroup::group()) {
122       theError = "Error: Whole group mustn't be selected.";
123       return false;
124     }
125   }
126
127   return true;
128 }