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)
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     myIsFullyConstrained = anEventID == Events_Loop::eventByName(EVENT_SKETCH_FULLY_CONSTRAINED);
114   }
115
116 #ifdef DEBUG_FEATURE_OVERCONSTRAINT_LISTENER
117   aCurrentInfoStr = getObjectsInfo(myConflictingObjects);
118   qDebug(QString("RESULT: current objects count = %1:%2\n")
119                 .arg(myConflictingObjects.size()).arg(aCurrentInfoStr).toStdString().c_str());
120 #endif
121 }
122
123 bool PartSet_OverconstraintListener::appendConflictingObjects(
124                                                const std::set<ObjectPtr>& theConflictingObjects)
125 {
126   std::set<ObjectPtr> aModifiedObjects;
127   //std::vector<int> aColor;
128   //getConflictingColor(aColor);
129
130   // set error state for new objects and append them in the internal map of objects
131   std::set<ObjectPtr>::const_iterator
132     anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
133   for (; anIt != aLast; anIt++) {
134     ObjectPtr anObject = *anIt;
135     if (myConflictingObjects.find(anObject) == myConflictingObjects.end()) { // it is not found
136       aModifiedObjects.insert(anObject);
137       myConflictingObjects.insert(anObject);
138     }
139   }
140   bool isUpdated = !aModifiedObjects.empty();
141   if (isUpdated)
142     redisplayObjects(aModifiedObjects);
143
144   return isUpdated;
145 }
146
147 bool PartSet_OverconstraintListener::repairConflictingObjects(
148                                               const std::set<ObjectPtr>& theConflictingObjects)
149 {
150   std::set<ObjectPtr> aModifiedObjects;
151   // erase error state of absent current objects in the parameter list
152   std::set<ObjectPtr>::const_iterator anIt, aLast;
153   for (anIt = theConflictingObjects.begin(), aLast = theConflictingObjects.end();
154        anIt != aLast; anIt++) {
155     ObjectPtr anObject = *anIt;
156     if (theConflictingObjects.find(anObject) != theConflictingObjects.end()) { // it is found
157       myConflictingObjects.erase(anObject);
158
159       aModifiedObjects.insert(anObject);
160     }
161   }
162   bool isUpdated = !aModifiedObjects.empty();
163   if (isUpdated)
164     redisplayObjects(aModifiedObjects);
165
166   return isUpdated;
167 }
168
169 void PartSet_OverconstraintListener::redisplayObjects(
170                                               const std::set<ObjectPtr>& theObjects)
171 {
172   static Events_Loop* aLoop = Events_Loop::loop();
173
174   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
175   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
176
177   std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
178   for (; anIt != aLast; anIt++)
179     aECreator->sendUpdated(*anIt, EVENT_DISP);
180
181   aLoop->flush(EVENT_DISP);
182 }
183
184 XGUI_Workshop* PartSet_OverconstraintListener::workshop() const
185 {
186   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
187   return aConnector->workshop();
188 }
189
190 #ifdef _DEBUG
191 QString PartSet_OverconstraintListener::getObjectsInfo(const std::set<ObjectPtr>& theObjects)
192 {
193   std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(),
194                                       aLast = theObjects.end();
195   QStringList anInfo;
196   for (; anIt != aLast; ++anIt)
197     anInfo.append(ModuleBase_Tools::objectInfo((*anIt)));
198
199   return anInfo.join(";\n");
200 }
201 #endif