]> SALOME platform Git repositories - modules/shaper.git/blobdiff - src/PartSet/PartSet_OverconstraintListener.cpp
Salome HOME
Issue #2029 Change the color of the Sketch when fully constrained
[modules/shaper.git] / src / PartSet / PartSet_OverconstraintListener.cpp
index 5e6aefc74cb164f4282d7fc447846c12ed79db79..8dd5af2eb002627505ae916c9f95725784f4390f 100755 (executable)
@@ -4,11 +4,16 @@
 // Created:     20 August 2015
 // Author:      Vitaly SMETANNIKOV
 
+#include <ModelAPI_Tools.h>
+
 #include "PartSet_OverconstraintListener.h"
+#include <PartSet_Module.h>
+#include <PartSet_SketcherMgr.h>
 
 #include "XGUI_ModuleConnector.h"
 #include "XGUI_Workshop.h"
 #include "XGUI_Displayer.h"
+#include "XGUI_CustomPrs.h"
 
 #include "SketcherPrs_SymbolPrs.h"
 #include "SketchPlugin_SketchEntity.h"
 //#define DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
 
 PartSet_OverconstraintListener::PartSet_OverconstraintListener(ModuleBase_IWorkshop* theWorkshop)
-: myWorkshop(theWorkshop)
+: myWorkshop(theWorkshop), myIsFullyConstrained(false), myIsNeedUpdateCustomColor(false)
 {
   Events_Loop* aLoop = Events_Loop::loop();
   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_FAILED));
   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_REPAIRED));
-}
 
-bool PartSet_OverconstraintListener::isConflictingObject(const ObjectPtr& theObject)
-{
-  return myConflictingObjects.find(theObject) != myConflictingObjects.end();
+  aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SKETCH_UNDER_CONSTRAINED));
+  aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED));
 }
 
-void PartSet_OverconstraintListener::getConflictingColor(std::vector<int>& theColor)
+void PartSet_OverconstraintListener::getCustomColor(const ObjectPtr& theObject,
+                                                    std::vector<int>& theColor)
 {
-  Quantity_Color aColor = ModuleBase_Tools::color("Visualization", "sketch_overconstraint_color",
-                                                  SKETCH_OVERCONSTRAINT_COLOR);
-
-  theColor.push_back(aColor.Red()*255.);
-  theColor.push_back(aColor.Green()*255.);
-  theColor.push_back(aColor.Blue()*255.);
+  if (myConflictingObjects.find(theObject) != myConflictingObjects.end()) {
+    Quantity_Color aColor = ModuleBase_Tools::color("Visualization",
+                                                    "sketch_overconstraint_color");
+    theColor.push_back(aColor.Red()*255.);
+    theColor.push_back(aColor.Green()*255.);
+    theColor.push_back(aColor.Blue()*255.);
+  }
+  if (myIsFullyConstrained) {
+    FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
+    if (aFeature.get()) {
+      PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
+      CompositeFeaturePtr aCompositeFeature = aModule->sketchMgr()->activeSketch();
+      // the given object is sub feature of the current sketch(created or edited)
+      if (ModelAPI_Tools::compositeOwner(aFeature) == aCompositeFeature) {
+        Quantity_Color aColor = ModuleBase_Tools::color("Visualization",
+                                                        "sketch_fully_constrained_color");
+        theColor.push_back(aColor.Red()*0.);
+        theColor.push_back(aColor.Green()*255.);
+        theColor.push_back(aColor.Blue()*0.);
+      }
+    }
+  }
 }
 
 void PartSet_OverconstraintListener::processEvent(
                                                  const std::shared_ptr<Events_Message>& theMessage)
 {
-
 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
   bool isRepaired = theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED);
-  qDebug(QString("PartSet_OverconstraintListener::processEvent:\n %1").arg(isRepaired ? "REPAIRED" : "FAILED").toStdString().c_str());
+  int aCount = 0;
+
+  std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
+                  std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
+  QString anInfoStr;
+  if (anErrorMsg.get()) {
+    const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
+    aCount = aConflictingObjects.size();
+    anInfoStr = getObjectsInfo(aConflictingObjects);
+  }
+
+  QString aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
+
+  QString aMsg("PartSet_OverconstraintListener::processEvent: %1,\nobjects "
+               "count = %2:%3\ncurrent objects count = %4:%5");
+  qDebug(aMsg.arg(isRepaired ? "REPAIRED" : "FAILED")
+             .arg(aCount).arg(anInfoStr).arg(myConflictingObjects.size())
+             .arg(aCurrentInfoStr).toStdString().c_str());
 #endif
 
-  if (theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_FAILED) ||
-             theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED)) {
+  Events_ID anEventID = theMessage->eventID();
+  if (anEventID == Events_Loop::eventByName(EVENT_SOLVER_FAILED) ||
+      anEventID == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED)) {
     std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
                    std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
     bool anUpdated = false;
     if (anErrorMsg.get()) {
       const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
-      anUpdated = updateConflictingObjects(aConflictingObjects);
+      if (anEventID == Events_Loop::eventByName(EVENT_SOLVER_FAILED))
+        anUpdated = appendConflictingObjects(aConflictingObjects);
+      else
+        anUpdated = repairConflictingObjects(aConflictingObjects);
+    }
+  }
+  else if (anEventID == Events_Loop::eventByName(EVENT_SKETCH_UNDER_CONSTRAINED) ||
+           anEventID == Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED)) {
+    bool aPrevFullyConstrained = myIsFullyConstrained;
+    myIsFullyConstrained = anEventID == Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED);
+
+    if (aPrevFullyConstrained != myIsFullyConstrained) {
+      myIsNeedUpdateCustomColor = true;
+      std::set<ObjectPtr> aModifiedObjects;
+      PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
+      CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
+      for (int i = 0; i < aSketch->numberOfSubs(); i++) {
+        FeaturePtr aFeature = aSketch->subFeature(i);
+        aModifiedObjects.insert(aFeature); // is necessary to redisplay presentations
+        std::list<ResultPtr> aResults = aFeature->results();
+        for (std::list<ResultPtr>::const_iterator aIt = aResults.begin();
+             aIt != aResults.end(); ++aIt) {
+          aModifiedObjects.insert(*aIt);
+        }
+      }
+      redisplayObjects(aModifiedObjects);
+      myIsNeedUpdateCustomColor = false;
     }
-    else
-      anUpdated = updateConflictingObjects(std::set<ObjectPtr>());
   }
-}
-
-bool PartSet_OverconstraintListener::updateConflictingObjects(
-                                                  const std::set<ObjectPtr>& theConflictingObjects)
-{
-  std::set<ObjectPtr>::const_iterator anIt, aLast;
 
 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
-  anIt = theConflictingObjects.begin();
-  aLast = theConflictingObjects.end();
-
-  QStringList anInfo;
-  for (; anIt != aLast; ++anIt) {
-    anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
-  }
-  QString anInfoStr = anInfo.join(";\n");
-  qDebug(QString("PartSet_OverconstraintListener::updateConflictingObjects: %1: \n%2").arg(theConflictingObjects.size())
-                                                                                    .arg(anInfoStr).toStdString().c_str());
+  aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
+  qDebug(QString("RESULT: current objects count = %1:%2\n")
+                .arg(myConflictingObjects.size()).arg(aCurrentInfoStr).toStdString().c_str());
 #endif
+}
 
-  bool isUpdated = false;
+bool PartSet_OverconstraintListener::appendConflictingObjects(
+                                               const std::set<ObjectPtr>& theConflictingObjects)
+{
   std::set<ObjectPtr> aModifiedObjects;
-  // erase error state of absent current objects in the parameter list
-  for (anIt = myConflictingObjects.begin(), aLast = myConflictingObjects.end() ; anIt != aLast; anIt++) {
+
+  // set error state for new objects and append them in the internal map of objects
+  std::set<ObjectPtr>::const_iterator
+    anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
+  for (; anIt != aLast; anIt++) {
     ObjectPtr anObject = *anIt;
-    if (theConflictingObjects.find(anObject) == theConflictingObjects.end()) { // it is not found
-      setConflictingObject(anObject, false);
+    if (myConflictingObjects.find(anObject) == myConflictingObjects.end()) { // it is not found
       aModifiedObjects.insert(anObject);
+      myConflictingObjects.insert(anObject);
     }
   }
+  bool isUpdated = !aModifiedObjects.empty();
+  if (isUpdated)
+    redisplayObjects(aModifiedObjects);
 
-  // erase absent objects from the internal container
-  for (anIt = aModifiedObjects.begin(), aLast = aModifiedObjects.end(); anIt != aLast; anIt++) {
-    myConflictingObjects.erase(*anIt);
-  }
+  return isUpdated;
+}
 
-  // set error state for new objects and append them in the internal map of objects
-  for (anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end() ; anIt != aLast; anIt++) {
+bool PartSet_OverconstraintListener::repairConflictingObjects(
+                                              const std::set<ObjectPtr>& theConflictingObjects)
+{
+  std::set<ObjectPtr> aModifiedObjects;
+  // erase error state of absent current objects in the parameter list
+  std::set<ObjectPtr>::const_iterator anIt, aLast;
+  for (anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
+       anIt != aLast; anIt++) {
     ObjectPtr anObject = *anIt;
-    if (myConflictingObjects.find(anObject) == myConflictingObjects.end()) { // it is not found
-      setConflictingObject(anObject, true);
+    if (theConflictingObjects.find(anObject) != theConflictingObjects.end()) { // it is found
+      myConflictingObjects.erase(anObject);
+
       aModifiedObjects.insert(anObject);
-      myConflictingObjects.insert(anObject);
     }
   }
-  isUpdated = !aModifiedObjects.empty();
+  bool isUpdated = !aModifiedObjects.empty();
   if (isUpdated)
     redisplayObjects(aModifiedObjects);
 
@@ -122,57 +183,16 @@ bool PartSet_OverconstraintListener::updateConflictingObjects(
 void PartSet_OverconstraintListener::redisplayObjects(
                                               const std::set<ObjectPtr>& theObjects)
 {
-/*static Events_Loop* aLoop = Events_Loop::loop();
-  static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_UPDATED);
+  static Events_Loop* aLoop = Events_Loop::loop();
 
+  static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
 
   std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
-  for (; anIt != aLast; anIt++) {
+  for (; anIt != aLast; anIt++)
     aECreator->sendUpdated(*anIt, EVENT_DISP);
 
-    //#ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
-    //  QString anInfoStr = ModuleBase_Tools::objectInfo(*anIt);
-    //  qDebug(QString("PartSet_OverconstraintListener::SEND UPDATED: %1").arg(anInfoStr).toStdString().c_str());
-    //#endif
-  }
-  aLoop->flush(EVENT_DISP);*/
-
-  XGUI_Displayer* aDisplayer = workshop()->displayer();
-  bool aHidden;
-  std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
-  for (; anIt != aLast; anIt++) {
-    ObjectPtr anObject = *anIt;
-    aHidden = !anObject->data() || !anObject->data()->isValid() || 
-               anObject->isDisabled() || (!anObject->isDisplayed());
-    if (!aHidden)
-      aDisplayer->redisplay(anObject, false);
-  }
-  aDisplayer->updateViewer();
-}
-
-void PartSet_OverconstraintListener::setConflictingObject(const ObjectPtr& theObject,
-                                                          const bool theConflicting)
-{
-  AISObjectPtr anAISObject;
-  GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
-
-  if (aPrs.get() != NULL) {
-    XGUI_Workshop* aWorkshop = workshop();
-    XGUI_Displayer* aDisplayer = aWorkshop->displayer();
-
-    anAISObject = aPrs->getAISObject(aDisplayer->getAISObject(theObject));
-    if (anAISObject.get()) {
-      Handle(AIS_InteractiveObject) anAISIO = anAISObject->impl<Handle(AIS_InteractiveObject)>();
-      if (!anAISIO.IsNull()) {
-        if (!Handle(SketcherPrs_SymbolPrs)::DownCast(anAISIO).IsNull()) {
-          Handle(SketcherPrs_SymbolPrs) aPrs = Handle(SketcherPrs_SymbolPrs)::DownCast(anAISIO);
-          if (!aPrs.IsNull())
-            aPrs->SetConflictingConstraint(theConflicting);
-        }
-      }
-    }
-  }
+  aLoop->flush(EVENT_DISP);
 }
 
 XGUI_Workshop* PartSet_OverconstraintListener::workshop() const
@@ -181,3 +201,15 @@ XGUI_Workshop* PartSet_OverconstraintListener::workshop() const
   return aConnector->workshop();
 }
 
+#ifdef _DEBUG
+QString PartSet_OverconstraintListener::getObjectsInfo(const std::set<ObjectPtr>& theObjects)
+{
+  std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(),
+                                      aLast = theObjects.end();
+  QStringList anInfo;
+  for (; anIt != aLast; ++anIt)
+    anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
+
+  return anInfo.join(";\n");
+}
+#endif