Salome HOME
bos #19068: Unable to quit Sketch because of conflicting constraint
[modules/shaper.git] / src / CollectionPlugin / CollectionPlugin_Validators.cpp
index d644c13f64817c4bc9f0da5fa700e3ddaea72fe1..527b61c8d49a1e4914dd71eff8e8205d24671897 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2019  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2020  CEA/DEN, EDF R&D
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 //
 
 #include "CollectionPlugin_Validators.h"
+#include "CollectionPlugin_Group.h"
 #include "CollectionPlugin_Field.h"
 #include <ModelAPI_AttributeSelectionList.h>
+#include <ModelAPI_ResultGroup.h>
 #include <Events_InfoMessage.h>
 
 
@@ -42,3 +44,59 @@ bool CollectionPlugin_FieldValidator::isValid(const FeaturePtr& theFeature,
   theError = "Selection list is not initialized";
   return false;
 }
+
+static bool isGroupTypeCorrect(const AttributeSelectionPtr& theSelection,
+                               std::string& theType,
+                               Events_InfoMessage& theError)
+{
+  // applicable the groups only
+  ResultPtr aGroupResult = theSelection->context();
+  if (!aGroupResult.get() || aGroupResult->groupName() != ModelAPI_ResultGroup::group()) {
+    theError = "Error: Groups can be selected only.";
+    return false;
+  }
+  // groups of same type can be selected only
+  FeaturePtr aGroupFeature = ModelAPI_Feature::feature(aGroupResult->data()->owner());
+  std::string aGroupType =
+      aGroupFeature->selectionList(CollectionPlugin_Group::LIST_ID())->selectionType();
+  if (theType.empty())
+    theType = aGroupType;
+  else if (theType != aGroupType) {
+    theError = "Error: Groups should have same type";
+    return false;
+  }
+  return true;
+}
+
+bool CollectionPlugin_GroupOperationAttributeValidator::isValid(
+    const AttributePtr& theAttribute,
+    const std::list<std::string>& theArguments,
+    Events_InfoMessage& theError) const
+{
+  AttributeSelectionListPtr aSelList =
+      std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
+  if (!aSelList) {
+    theError = "Error: This validator can only work with selection list of attributes";
+    return false;
+  }
+
+  FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
+  // check selected items
+  std::string aType;
+  for (int anIndex = 0; anIndex < aSelList->size(); ++anIndex) {
+    AttributeSelectionPtr aCurSelection = aSelList->value(anIndex);
+    if (!isGroupTypeCorrect(aCurSelection, aType, theError))
+      return false;
+  }
+  // check types of all selection lists are the same
+  for (std::list<std::string>::const_iterator aParIt = theArguments.begin();
+       aParIt != theArguments.end() && !aType.empty(); ++aParIt) {
+    AttributeSelectionListPtr aCurList = anOwner->selectionList(*aParIt);
+    if (aCurList->size() == 0)
+      continue;
+    AttributeSelectionPtr aCurSelection = aCurList->value(0);
+    if (!isGroupTypeCorrect(aCurSelection, aType, theError))
+      return false;
+  }
+  return true;
+}