]> SALOME platform Git repositories - modules/shaper.git/blob - src/ParametersPlugin/ParametersPlugin_EvalListener.cpp
Salome HOME
#2027 Sketcher Trim Feature: 1. preview/selected attributes in trim; 2. avoid includi...
[modules/shaper.git] / src / ParametersPlugin / ParametersPlugin_EvalListener.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2 /*
3  * ParametersPlugin_EvalListener.cpp
4  *
5  *  Created on: Apr 28, 2015
6  *      Author: sbh
7  */
8
9 #include <pyconfig.h>
10
11 #include <ParametersPlugin_EvalListener.h>
12 #include <ParametersPlugin_Parameter.h>
13
14 #include <Events_InfoMessage.h>
15
16 #include <ModelAPI_AttributeDouble.h>
17 #include <ModelAPI_AttributeInteger.h>
18 #include <ModelAPI_AttributeRefList.h>
19 #include <ModelAPI_AttributeString.h>
20 #include <ModelAPI_AttributeValidator.h>
21 #include <ModelAPI_Document.h>
22 #include <ModelAPI_Events.h>
23 #include <ModelAPI_ResultParameter.h>
24 #include <ModelAPI_Session.h>
25 #include <ModelAPI_Tools.h>
26
27 #include <GeomDataAPI_Point.h>
28 #include <GeomDataAPI_Point2D.h>
29
30 #include <QMessageBox>
31
32 #include <string>
33 #include <set>
34 #include <sstream>
35
36 //------------------------------------------------------------------------------
37 // Tools
38
39 std::string toStdString(double theValue)
40 {
41   std::ostringstream sstream;
42   sstream << theValue;
43   size_t aPos = sstream.str().find(".");
44   std::string aPnt = "";
45   if (aPos == std::string::npos)
46     aPnt = ".";
47   return sstream.str() + aPnt;
48 }
49
50 std::set<std::string> toSet(const std::list<std::string>& theContainer)
51 {
52   return std::set<std::string>(theContainer.begin(), theContainer.end());
53 }
54
55 //------------------------------------------------------------------------------
56
57 ParametersPlugin_EvalListener::ParametersPlugin_EvalListener()
58 {
59   Events_Loop* aLoop = Events_Loop::loop();
60
61   Events_ID anEvents_IDs[] = {
62       ModelAPI_ObjectRenamedMessage::eventId(),
63       ModelAPI_ReplaceParameterMessage::eventId()
64   };
65
66   for (int i = 0; i < sizeof(anEvents_IDs)/sizeof(anEvents_IDs[0]); ++i)
67     aLoop->registerListener(this, anEvents_IDs[i], NULL, true);
68 }
69
70 ParametersPlugin_EvalListener::~ParametersPlugin_EvalListener()
71 {
72 }
73
74 void ParametersPlugin_EvalListener::processEvent(
75     const std::shared_ptr<Events_Message>& theMessage)
76 {
77   if (!theMessage.get())
78     return;
79
80   const Events_ID kObjectRenamedEvent = ModelAPI_ObjectRenamedMessage::eventId();
81   const Events_ID kReplaceParameterEvent = ModelAPI_ReplaceParameterMessage::eventId();
82
83   if (theMessage->eventID() == kObjectRenamedEvent) {
84     processObjectRenamedEvent(theMessage);
85   } else if (theMessage->eventID() == kReplaceParameterEvent) {
86     processReplaceParameterEvent(theMessage);
87   }
88 }
89
90 std::string ParametersPlugin_EvalListener::renameInPythonExpression(
91     const std::string& theExpression,
92     const std::string& theOldName,
93     const std::string& theNewName)
94 {
95   std::string anExpressionString = theExpression;
96
97   // ask interpreter to compute the positions in the expression
98   std::shared_ptr<ModelAPI_ComputePositionsMessage> aMsg =
99     ModelAPI_ComputePositionsMessage::send(theExpression, theOldName, this);
100   const std::list<std::pair<int, int> >& aPositions = aMsg->positions();
101
102   if (aPositions.empty())
103     return anExpressionString;
104
105   std::map<int, std::list<int> > aLines;
106   std::list<std::pair<int, int> >::const_iterator it = aPositions.begin();
107   for (; it != aPositions.end(); ++it)
108     aLines[it->first].push_back(it->second);
109
110   // Start renaming from the end to keep indexes if theNewName is longer then theOldName
111   std::map<int, std::list<int> >::const_reverse_iterator ritLine = aLines.rbegin();
112   for (; ritLine != aLines.rend(); ++ritLine) {
113     // Calculate the start of the line (find the aLineNo occurrence of "\n" )
114     int aLineNo = ritLine->first - 1;
115     size_t aLineStart = 0;
116     for (int i = 0; i < aLineNo; ++i)
117       aLineStart = anExpressionString.find("\n", aLineStart) + 1;
118
119     const std::list<int>& aColOffsets = ritLine->second;
120     std::list<int>::const_reverse_iterator ritOffset = aColOffsets.rbegin();
121     for (; ritOffset != aColOffsets.rend(); ++ritOffset) {
122       int anOffset = *ritOffset;
123       anExpressionString.replace(aLineStart + anOffset, theOldName.size(), theNewName);
124     }
125   }
126
127   return anExpressionString;
128 }
129
130 void ParametersPlugin_EvalListener::renameInParameter(
131     std::shared_ptr<ParametersPlugin_Parameter> theParameter,
132     const std::string& theOldName,
133     const std::string& theNewName)
134 {
135   std::shared_ptr<ModelAPI_AttributeString> anExpressionAttribute =
136       theParameter->string(ParametersPlugin_Parameter::EXPRESSION_ID());
137
138   std::string anExpressionString = anExpressionAttribute->value();
139   anExpressionString = renameInPythonExpression(anExpressionString,
140                                                 theOldName,
141                                                 theNewName);
142   // Issue #588. No need for reevaluating expression.
143   // Moreover, current history may not contain necessary parameters.
144   bool aWasBlocked = anExpressionAttribute->owner()->data()->blockSendAttributeUpdated(true);
145   anExpressionAttribute->setValue(anExpressionString);
146   anExpressionAttribute->owner()->data()->blockSendAttributeUpdated(aWasBlocked);
147 }
148
149 void ParametersPlugin_EvalListener::renameInAttribute(
150     std::shared_ptr<ModelAPI_Attribute> theAttribute,
151     const std::string& theOldName,
152     const std::string& theNewName)
153 {
154   if (theAttribute->attributeType() == ModelAPI_AttributeInteger::typeId()) {
155     AttributeIntegerPtr anAttribute =
156         std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(theAttribute);
157     std::string anExpressionString = anAttribute->text();
158     anExpressionString = renameInPythonExpression(anExpressionString,
159                                                   theOldName, theNewName);
160     anAttribute->setText(anExpressionString);
161   } else
162   if (theAttribute->attributeType() == ModelAPI_AttributeDouble::typeId()) {
163     AttributeDoublePtr anAttribute =
164         std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
165     std::string anExpressionString = anAttribute->text();
166     anExpressionString = renameInPythonExpression(anExpressionString,
167                                                   theOldName, theNewName);
168     anAttribute->setText(anExpressionString);
169   } else
170   if (theAttribute->attributeType() == GeomDataAPI_Point::typeId()) {
171     AttributePointPtr anAttribute =
172         std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
173     std::string anExpressionString[3] = {
174       anAttribute->textX(),
175       anAttribute->textY(),
176       anAttribute->textZ()
177     };
178     for (int i = 0; i < 3; ++i)
179       anExpressionString[i] = renameInPythonExpression(anExpressionString[i],
180                                                        theOldName, theNewName);
181     anAttribute->setText(anExpressionString[0],
182                          anExpressionString[1],
183                          anExpressionString[2]);
184   } else
185   if (theAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
186     AttributePoint2DPtr anAttribute =
187         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
188     std::string anExpressionString[2] = {
189       anAttribute->textX(),
190       anAttribute->textY()
191     };
192     for (int i = 0; i < 2; ++i)
193       anExpressionString[i] = renameInPythonExpression(anExpressionString[i],
194                                                        theOldName, theNewName);
195     anAttribute->setText(anExpressionString[0],
196                          anExpressionString[1]);
197   }
198 }
199
200 void ParametersPlugin_EvalListener::renameInDependents(
201               std::shared_ptr<ModelAPI_ResultParameter> theResultParameter,
202               const std::string& theOldName,
203               const std::string& theNewName)
204 {
205   std::set<std::shared_ptr<ModelAPI_Attribute> > anAttributes =
206     theResultParameter->data()->refsToMe();
207   std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anAttributeIt =
208     anAttributes.cbegin();
209   for (; anAttributeIt != anAttributes.cend(); ++anAttributeIt) {
210     const AttributePtr& anAttribute = *anAttributeIt;
211     if (anAttribute->attributeType() == ModelAPI_AttributeRefList::typeId()) {
212       std::shared_ptr<ParametersPlugin_Parameter> aParameter =
213         std::dynamic_pointer_cast<ParametersPlugin_Parameter>(
214         anAttribute->owner());
215       if (aParameter.get())
216         // Rename
217         renameInParameter(aParameter, theOldName, theNewName);
218     } else
219       // Rename
220       renameInAttribute(anAttribute, theOldName, theNewName);
221   }
222 }
223
224 bool isValidAttribute(const AttributePtr& theAttribute)
225 {
226   std::string aValidator;
227   Events_InfoMessage anError;
228   return ModelAPI_Session::get()->validators()->validate(theAttribute, aValidator, anError);
229 }
230
231 void setParameterName(ResultParameterPtr theResultParameter, const std::string& theName)
232 {
233   bool aWasBlocked = theResultParameter->data()->blockSendAttributeUpdated(true);
234   theResultParameter->data()->setName(theName);
235   theResultParameter->data()->blockSendAttributeUpdated(aWasBlocked, false);
236
237   std::shared_ptr<ParametersPlugin_Parameter> aParameter =
238       std::dynamic_pointer_cast<ParametersPlugin_Parameter>(
239           ModelAPI_Feature::feature(theResultParameter));
240
241   aWasBlocked = aParameter->data()->blockSendAttributeUpdated(true);
242   aParameter->data()->setName(theName);
243   aParameter->string(ParametersPlugin_Parameter::VARIABLE_ID())->setValue(theName);
244   aParameter->data()->blockSendAttributeUpdated(aWasBlocked);
245 }
246
247 void ParametersPlugin_EvalListener::processObjectRenamedEvent(
248     const std::shared_ptr<Events_Message>& theMessage)
249 {
250   std::shared_ptr<ModelAPI_ObjectRenamedMessage> aMessage =
251       std::dynamic_pointer_cast<ModelAPI_ObjectRenamedMessage>(theMessage);
252
253   // Empty new name is not available too but it will be rejected by
254   // name validator in isValidAttribute.
255   if (!aMessage.get() || aMessage->oldName().empty())
256     return;
257
258   // check if the renamed object is a result parameter
259   ResultParameterPtr aResultParameter =
260       std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aMessage->object());
261   if (!aResultParameter.get())
262     return;
263
264   // get parameter feature for the result
265   std::shared_ptr<ParametersPlugin_Parameter> aParameter =
266       std::dynamic_pointer_cast<ParametersPlugin_Parameter>(
267           ModelAPI_Feature::feature(aResultParameter));
268   if (!aParameter.get())
269     return;
270
271   std::string aNotActivatedNames;
272   if (!ModelAPI_Tools::allDocumentsActivated(aNotActivatedNames)) {
273     QMessageBox::StandardButton aRes = QMessageBox::warning(0, QObject::tr("Warning"),
274                QObject::tr("Selected objects can be used in Part documents which are not loaded: "
275                            "%1. Whould you like to continue?").arg(aNotActivatedNames.c_str()),
276                QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
277     if (aRes != QMessageBox::Yes) {
278       setParameterName(aResultParameter, aMessage->oldName());
279       return;
280     }
281   }
282
283   // try to update the parameter feature according the new name
284   setParameterName(aResultParameter, aMessage->newName());
285   if (!isValidAttribute(aParameter->string(ParametersPlugin_Parameter::VARIABLE_ID()))) {
286     //setParameterName(aResultParameter, aMessage->oldName());
287     if (myOldNames.find(aParameter.get()) == myOldNames.end())
288       myOldNames[aParameter.get()] = aMessage->oldName();
289     return;
290   }
291
292   std::string anOldName = aMessage->oldName();
293   if (myOldNames.find(aParameter.get()) != myOldNames.end()) {
294     anOldName = myOldNames[aParameter.get()];
295     myOldNames.erase(aParameter.get());
296     aParameter->execute(); // to enable result because of previously incorrect name
297   }
298
299   renameInDependents(aResultParameter, anOldName, aMessage->newName());
300 }
301
302 void ParametersPlugin_EvalListener::processReplaceParameterEvent(
303     const std::shared_ptr<Events_Message>& theMessage)
304 {
305   std::shared_ptr<ModelAPI_ReplaceParameterMessage> aMessage =
306       std::dynamic_pointer_cast<ModelAPI_ReplaceParameterMessage>(theMessage);
307
308   // get parameter feature for the object
309   std::shared_ptr<ParametersPlugin_Parameter> aParameter =
310       std::dynamic_pointer_cast<ParametersPlugin_Parameter>(
311           ModelAPI_Feature::feature(aMessage->object()));
312   if (!aParameter.get())
313     return;
314
315   ResultParameterPtr aResultParameter =
316       std::dynamic_pointer_cast<ModelAPI_ResultParameter>(
317           aParameter->firstResult());
318   if (!aResultParameter.get())
319     return;
320
321   double aRealValue = aResultParameter->data()->real(ModelAPI_ResultParameter::VALUE())->value();
322   std::string aValue = toStdString(aRealValue);
323
324   renameInDependents(aResultParameter, aResultParameter->data()->name(), aValue);
325 }