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