Salome HOME
Issue #2029 Change the color of the Sketch when fully constrained
[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 <ModelAPI_Tools.h>
8
9 #include "PartSet_OverconstraintListener.h"
10 #include <PartSet_Module.h>
11 #include <PartSet_SketcherMgr.h>
12
13 #include "XGUI_ModuleConnector.h"
14 #include "XGUI_Workshop.h"
15 #include "XGUI_Displayer.h"
16 #include "XGUI_CustomPrs.h"
17
18 #include "SketcherPrs_SymbolPrs.h"
19 #include "SketchPlugin_SketchEntity.h"
20
21 #include "Events_Loop.h"
22
23 #include <GeomAPI_IPresentable.h>
24 #include <ModelAPI_Events.h>
25 #include <ModuleBase_Tools.h>
26
27 #include <QString>
28
29 //#define DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
30
31 PartSet_OverconstraintListener::PartSet_OverconstraintListener(ModuleBase_IWorkshop* theWorkshop)
32 : myWorkshop(theWorkshop), myIsFullyConstrained(false)
33 {
34   Events_Loop* aLoop = Events_Loop::loop();
35   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_FAILED));
36   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SOLVER_REPAIRED));
37
38   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SKETCH_UNDER_CONSTRAINED));
39   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED));
40 }
41
42 bool PartSet_OverconstraintListener::hasCustomColor(const ObjectPtr& theObject,
43                                                     std::vector<int>& theColor)
44 {
45   if (myConflictingObjects.find(theObject) != myConflictingObjects.end()) {
46     Quantity_Color aColor = ModuleBase_Tools::color("Visualization",
47                                                     "sketch_overconstraint_color");
48     theColor.push_back(aColor.Red()*255.);
49     theColor.push_back(aColor.Green()*255.);
50     theColor.push_back(aColor.Blue()*255.);
51     return true;
52   }
53   if (myIsFullyConstrained) {
54     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
55     if (aFeature.get()) {
56       PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
57       CompositeFeaturePtr aCompositeFeature = aModule->sketchMgr()->activeSketch();
58       // the given object is sub feature of the current sketch(created or edited)
59       if (ModelAPI_Tools::compositeOwner(aFeature) == aCompositeFeature) {
60         Quantity_Color aColor = ModuleBase_Tools::color("Visualization",
61                                                         "sketch_fully_constrained_color");
62         theColor.push_back(aColor.Red()*255.);
63         theColor.push_back(aColor.Green()*255.);
64         theColor.push_back(aColor.Blue()*255.);
65         return true;
66       }
67     }
68   }
69   return false;
70 }
71
72 void PartSet_OverconstraintListener::processEvent(
73                                                  const std::shared_ptr<Events_Message>& theMessage)
74 {
75 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
76   bool isRepaired = theMessage->eventID() == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED);
77   int aCount = 0;
78
79   std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
80                   std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
81   QString anInfoStr;
82   if (anErrorMsg.get()) {
83     const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
84     aCount = aConflictingObjects.size();
85     anInfoStr = getObjectsInfo(aConflictingObjects);
86   }
87
88   QString aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
89
90   QString aMsg("PartSet_OverconstraintListener::processEvent: %1,\nobjects "
91                "count = %2:%3\ncurrent objects count = %4:%5");
92   qDebug(aMsg.arg(isRepaired ? "REPAIRED" : "FAILED")
93              .arg(aCount).arg(anInfoStr).arg(myConflictingObjects.size())
94              .arg(aCurrentInfoStr).toStdString().c_str());
95 #endif
96
97   Events_ID anEventID = theMessage->eventID();
98   if (anEventID == Events_Loop::eventByName(EVENT_SOLVER_FAILED) ||
99       anEventID == Events_Loop::eventByName(EVENT_SOLVER_REPAIRED)) {
100     std::shared_ptr<ModelAPI_SolverFailedMessage> anErrorMsg =
101                    std::dynamic_pointer_cast<ModelAPI_SolverFailedMessage>(theMessage);
102     bool anUpdated = false;
103     if (anErrorMsg.get()) {
104       const std::set<ObjectPtr>& aConflictingObjects = anErrorMsg->objects();
105       if (anEventID == Events_Loop::eventByName(EVENT_SOLVER_FAILED))
106         anUpdated = appendConflictingObjects(aConflictingObjects);
107       else
108         anUpdated = repairConflictingObjects(aConflictingObjects);
109     }
110   }
111   else if (anEventID == Events_Loop::eventByName(EVENT_SKETCH_UNDER_CONSTRAINED) ||
112            anEventID == Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED)) {
113     bool aPrevFullyConstrained = myIsFullyConstrained;
114     myIsFullyConstrained = anEventID == Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED);
115
116     if (aPrevFullyConstrained != myIsFullyConstrained) {
117       std::set<ObjectPtr> aModifiedObjects;
118       PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
119       CompositeFeaturePtr aSketch = aModule->sketchMgr()->activeSketch();
120       for (int i = 0; i < aSketch->numberOfSubs(); i++) {
121         FeaturePtr aFeature = aSketch->subFeature(i);
122         aModifiedObjects.insert(aFeature); // is necessary to redisplay presentations
123         std::list<ResultPtr> aResults = aFeature->results();
124         for (std::list<ResultPtr>::const_iterator aIt = aResults.begin();
125              aIt != aResults.end(); ++aIt) {
126           aModifiedObjects.insert(*aIt);
127         }
128       }
129       redisplayObjects(aModifiedObjects);
130     }
131   }
132
133 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
134   aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
135   qDebug(QString("RESULT: current objects count = %1:%2\n")
136                 .arg(myConflictingObjects.size()).arg(aCurrentInfoStr).toStdString().c_str());
137 #endif
138 }
139
140 bool PartSet_OverconstraintListener::appendConflictingObjects(
141                                                const std::set<ObjectPtr>& theConflictingObjects)
142 {
143   std::set<ObjectPtr> aModifiedObjects;
144
145   // set error state for new objects and append them in the internal map of objects
146   std::set<ObjectPtr>::const_iterator
147     anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
148   for (; anIt != aLast; anIt++) {
149     ObjectPtr anObject = *anIt;
150     if (myConflictingObjects.find(anObject) == myConflictingObjects.end()) { // it is not found
151       aModifiedObjects.insert(anObject);
152       myConflictingObjects.insert(anObject);
153     }
154   }
155   bool isUpdated = !aModifiedObjects.empty();
156   if (isUpdated)
157     redisplayObjects(aModifiedObjects);
158
159   return isUpdated;
160 }
161
162 bool PartSet_OverconstraintListener::repairConflictingObjects(
163                                               const std::set<ObjectPtr>& theConflictingObjects)
164 {
165   std::set<ObjectPtr> aModifiedObjects;
166   // erase error state of absent current objects in the parameter list
167   std::set<ObjectPtr>::const_iterator anIt, aLast;
168   for (anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
169        anIt != aLast; anIt++) {
170     ObjectPtr anObject = *anIt;
171     if (theConflictingObjects.find(anObject) != theConflictingObjects.end()) { // it is found
172       myConflictingObjects.erase(anObject);
173
174       aModifiedObjects.insert(anObject);
175     }
176   }
177   bool isUpdated = !aModifiedObjects.empty();
178   if (isUpdated)
179     redisplayObjects(aModifiedObjects);
180
181   return isUpdated;
182 }
183
184 void PartSet_OverconstraintListener::redisplayObjects(
185                                               const std::set<ObjectPtr>& theObjects)
186 {
187   static Events_Loop* aLoop = Events_Loop::loop();
188
189   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
190   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
191
192   std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
193   for (; anIt != aLast; anIt++)
194     aECreator->sendUpdated(*anIt, EVENT_DISP);
195
196   aLoop->flush(EVENT_DISP);
197 }
198
199 XGUI_Workshop* PartSet_OverconstraintListener::workshop() const
200 {
201   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
202   return aConnector->workshop();
203 }
204
205 #ifdef _DEBUG
206 QString PartSet_OverconstraintListener::getObjectsInfo(const std::set<ObjectPtr>& theObjects)
207 {
208   std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(),
209                                       aLast = theObjects.end();
210   QStringList anInfo;
211   for (; anIt != aLast; ++anIt)
212     anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
213
214   return anInfo.join(";\n");
215 }
216 #endif