Salome HOME
2.17. Improved management of overconstraint situation: overconstraint color is in...
[modules/shaper.git] / src / PartSet / PartSet_OverconstraintListener.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        SketcherPrs_Angle.cpp
4 // Created:     20 August 2015
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "PartSet_OverconstraintListener.h"
8
9 #include "XGUI_ModuleConnector.h"
10 #include "XGUI_Workshop.h"
11 #include "XGUI_Displayer.h"
12
13 #include "SketcherPrs_SymbolPrs.h"
14 #include "SketchPlugin_SketchEntity.h"
15
16 #include "Events_Loop.h"
17
18 #include <GeomAPI_IPresentable.h>
19 #include <ModelAPI_Events.h>
20 #include <ModuleBase_Tools.h>
21
22 #include <QString>
23
24 #define DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
25
26 PartSet_OverconstraintListener::PartSet_OverconstraintListener(ModuleBase_IWorkshop* theWorkshop)
27 : myWorkshop(theWorkshop)
28 {
29   Events_Loop* aLoop = Events_Loop::loop();
30   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_FAILED));
31   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_REPAIRED));
32 }
33
34 bool PartSet_OverconstraintListener::isConflictingObject(const ObjectPtr& theObject)
35 {
36   return myConflictingObjects.find(theObject) != myConflictingObjects.end();
37 }
38
39 void PartSet_OverconstraintListener::getConflictingColor(std::vector<int>& theColor)
40 {
41   Quantity_Color aColor = ModuleBase_Tools::color("Visualization", "sketch_overconstraint_color",
42                                                   SKETCH_OVERCONSTRAINT_COLOR);
43
44   theColor.push_back(aColor.Red()*255.);
45   theColor.push_back(aColor.Green()*255.);
46   theColor.push_back(aColor.Blue()*255.);
47 }
48
49 void PartSet_OverconstraintListener::processEvent(
50                                                  const std::shared_ptr<Events_Message>& theMessage)
51 {
52
53 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
54
55   /*
56   anIt = theConflictingObjects.begin();
57   aLast = theConflictingObjects.end();
58
59   QStringList anInfo;
60   for (; anIt != aLast; ++anIt) {
61     anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
62   }
63   QString anInfoStr = anInfo.join(";\n");*/
64   bool isRepaired = theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED);
65   qDebug(QString("PartSet_OverconstraintListener::processEvent:\n %1").arg(isRepaired ? "REPAIRED" : "FAILED").toStdString().c_str());
66 #endif
67
68   if (theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_FAILED) ||
69              theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED)) {
70     std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
71                    std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
72     bool anUpdated = false;
73     if (anErrorMsg.get()) {
74       const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
75       anUpdated = updateConflictingObjects(aConflictingObjects);
76     }
77     else
78       anUpdated = updateConflictingObjects(std::set<ObjectPtr>());
79   }
80 }
81
82 bool PartSet_OverconstraintListener::updateConflictingObjects(
83                                                   const std::set<ObjectPtr>& theConflictingObjects)
84 {
85   std::set<ObjectPtr>::const_iterator anIt, aLast;
86
87 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
88   anIt = theConflictingObjects.begin();
89   aLast = theConflictingObjects.end();
90
91   QStringList anInfo;
92   for (; anIt != aLast; ++anIt) {
93     anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
94   }
95   QString anInfoStr = anInfo.join(";\n");
96   qDebug(QString("PartSet_OverconstraintListener::updateConflictingObjects: %1: \n%2").arg(theConflictingObjects.size())
97                                                                                     .arg(anInfoStr).toStdString().c_str());
98 #endif
99
100   bool isUpdated = false;
101   std::set<ObjectPtr> aModifiedObjects;
102   // erase error state of absent current objects in the parameter list
103   for (anIt = myConflictingObjects.begin(), aLast = myConflictingObjects.end() ; anIt != aLast; anIt++) {
104     ObjectPtr anObject = *anIt;
105     if (theConflictingObjects.find(anObject) == theConflictingObjects.end()) { // it is not found
106       //setConflictingObject(anObject, false);
107       aModifiedObjects.insert(anObject);
108     }
109   }
110
111   // erase absent objects from the internal container
112   for (anIt = aModifiedObjects.begin(), aLast = aModifiedObjects.end(); anIt != aLast; anIt++) {
113     myConflictingObjects.erase(*anIt);
114   }
115
116   // set error state for new objects and append them in the internal map of objects
117   for (anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end() ; anIt != aLast; anIt++) {
118     ObjectPtr anObject = *anIt;
119     if (myConflictingObjects.find(anObject) == myConflictingObjects.end()) { // it is not found
120       //setConflictingObject(anObject, true);
121       aModifiedObjects.insert(anObject);
122       myConflictingObjects.insert(anObject);
123     }
124   }
125   isUpdated = !aModifiedObjects.empty();
126   if (isUpdated)
127     redisplayObjects(aModifiedObjects);
128
129   return isUpdated;
130 }
131
132 void PartSet_OverconstraintListener::redisplayObjects(
133                                               const std::set<ObjectPtr>& theObjects)
134 {
135 /*static Events_Loop* aLoop = Events_Loop::loop();
136   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_UPDATED);
137
138   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
139
140   std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
141   for (; anIt != aLast; anIt++) {
142     aECreator->sendUpdated(*anIt, EVENT_DISP);
143
144     //#ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
145     //  QString anInfoStr = ModuleBase_Tools::objectInfo(*anIt);
146     //  qDebug(QString("PartSet_OverconstraintListener::SEND UPDATED: %1").arg(anInfoStr).toStdString().c_str());
147     //#endif
148   }
149   aLoop->flush(EVENT_DISP);*/
150
151   XGUI_Displayer* aDisplayer = workshop()->displayer();
152   //QObjectPtrList aObjects = aDisplayer->displayedObjects();
153   bool aHidden;
154   std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
155   for (; anIt != aLast; anIt++) {
156     ObjectPtr anObject = *anIt;
157   //foreach(ObjectPtr aObj, aObjects) {
158     //TODO: replace by redisplay event.
159     aHidden = !anObject->data() || !anObject->data()->isValid() || 
160                anObject->isDisabled() || (!anObject->isDisplayed());
161     if (!aHidden)
162       aDisplayer->redisplay(anObject, false);
163   }
164   aDisplayer->updateViewer();
165 }
166
167 void PartSet_OverconstraintListener::setConflictingObject(const ObjectPtr& theObject,
168                                                           const bool theConflicting)
169 {
170   return;
171
172   AISObjectPtr anAISObject;
173   GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
174
175   if (aPrs.get() != NULL) {
176     XGUI_Workshop* aWorkshop = workshop();
177     XGUI_Displayer* aDisplayer = aWorkshop->displayer();
178
179     anAISObject = aPrs->getAISObject(aDisplayer->getAISObject(theObject));
180     if (anAISObject.get()) {
181       Handle(AIS_InteractiveObject) anAISIO = anAISObject->impl<Handle(AIS_InteractiveObject)>();
182       if (!anAISIO.IsNull()) {
183         if (!Handle(SketcherPrs_SymbolPrs)::DownCast(anAISIO).IsNull()) {
184           Handle(SketcherPrs_SymbolPrs) aPrs = Handle(SketcherPrs_SymbolPrs)::DownCast(anAISIO);
185           //if (!aPrs.IsNull())
186           //  aPrs->setConflictingConstraint(theConflicting);
187         }
188       }
189     }
190   }
191 }
192
193 XGUI_Workshop* PartSet_OverconstraintListener::workshop() const
194 {
195   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
196   return aConnector->workshop();
197 }
198