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