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