1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #include <ParametersPlugin_EvalListener.h>
23 #include <ParametersPlugin_Parameter.h>
25 #include <Events_InfoMessage.h>
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>
38 #include <GeomDataAPI_Point.h>
39 #include <GeomDataAPI_Point2D.h>
41 #include <QMessageBox>
43 #include <ModuleBase_Tools.h>
49 //------------------------------------------------------------------------------
52 static std::string toStdString(double theValue)
54 std::ostringstream sstream;
56 size_t aPos = sstream.str().find(".");
57 std::string aPnt = "";
58 if (aPos == std::string::npos)
60 return sstream.str() + aPnt;
63 static std::set<std::string> toSet(const std::list<std::string>& theContainer)
65 return std::set<std::string>(theContainer.begin(), theContainer.end());
68 //------------------------------------------------------------------------------
70 ParametersPlugin_EvalListener::ParametersPlugin_EvalListener()
72 Events_Loop* aLoop = Events_Loop::loop();
74 Events_ID anEvents_IDs[] = {
75 ModelAPI_ObjectRenamedMessage::eventId(),
76 ModelAPI_ReplaceParameterMessage::eventId()
79 for (int i = 0; i < sizeof(anEvents_IDs)/sizeof(anEvents_IDs[0]); ++i)
80 aLoop->registerListener(this, anEvents_IDs[i], NULL, true);
83 ParametersPlugin_EvalListener::~ParametersPlugin_EvalListener()
87 void ParametersPlugin_EvalListener::processEvent(
88 const std::shared_ptr<Events_Message>& theMessage)
90 if (!theMessage.get())
93 const Events_ID kObjectRenamedEvent = ModelAPI_ObjectRenamedMessage::eventId();
94 const Events_ID kReplaceParameterEvent = ModelAPI_ReplaceParameterMessage::eventId();
96 if (theMessage->eventID() == kObjectRenamedEvent) {
97 processObjectRenamedEvent(theMessage);
98 } else if (theMessage->eventID() == kReplaceParameterEvent) {
99 processReplaceParameterEvent(theMessage);
103 std::string ParametersPlugin_EvalListener::renameInPythonExpression(
104 const std::string& theExpression,
105 const std::string& theOldName,
106 const std::string& theNewName)
108 std::string anExpressionString = theExpression;
110 // ask interpreter to compute the positions in the expression
111 std::shared_ptr<ModelAPI_ComputePositionsMessage> aMsg =
112 ModelAPI_ComputePositionsMessage::send(theExpression, theOldName, this);
113 const std::list<std::pair<int, int> >& aPositions = aMsg->positions();
115 if (aPositions.empty())
116 return anExpressionString;
118 std::map<int, std::list<int> > aLines;
119 std::list<std::pair<int, int> >::const_iterator it = aPositions.begin();
120 for (; it != aPositions.end(); ++it)
121 aLines[it->first].push_back(it->second);
123 // Start renaming from the end to keep indexes if theNewName is longer then theOldName
124 std::map<int, std::list<int> >::const_reverse_iterator ritLine = aLines.rbegin();
125 for (; ritLine != aLines.rend(); ++ritLine) {
126 // Calculate the start of the line (find the aLineNo occurrence of "\n" )
127 int aLineNo = ritLine->first - 1;
128 size_t aLineStart = 0;
129 for (int i = 0; i < aLineNo; ++i)
130 aLineStart = anExpressionString.find("\n", aLineStart) + 1;
132 const std::list<int>& aColOffsets = ritLine->second;
133 std::list<int>::const_reverse_iterator ritOffset = aColOffsets.rbegin();
134 for (; ritOffset != aColOffsets.rend(); ++ritOffset) {
135 int anOffset = *ritOffset;
136 anExpressionString.replace(aLineStart + anOffset, theOldName.size(), theNewName);
140 return anExpressionString;
143 void ParametersPlugin_EvalListener::renameInParameter(
144 std::shared_ptr<ParametersPlugin_Parameter> theParameter,
145 const std::string& theOldName,
146 const std::string& theNewName)
148 std::shared_ptr<ModelAPI_AttributeString> anExpressionAttribute =
149 theParameter->string(ParametersPlugin_Parameter::EXPRESSION_ID());
151 std::string anExpressionString = anExpressionAttribute->value();
152 anExpressionString = renameInPythonExpression(anExpressionString,
155 // Issue #588. No need for reevaluating expression.
156 // Moreover, current history may not contain necessary parameters.
157 bool aWasBlocked = anExpressionAttribute->owner()->data()->blockSendAttributeUpdated(true);
158 anExpressionAttribute->setValue(anExpressionString);
159 anExpressionAttribute->owner()->data()->blockSendAttributeUpdated(aWasBlocked);
162 void ParametersPlugin_EvalListener::renameInAttribute(
163 std::shared_ptr<ModelAPI_Attribute> theAttribute,
164 const std::string& theOldName,
165 const std::string& theNewName)
167 if (theAttribute->attributeType() == ModelAPI_AttributeInteger::typeId()) {
168 AttributeIntegerPtr anAttribute =
169 std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(theAttribute);
170 std::string anExpressionString = anAttribute->text();
171 anExpressionString = renameInPythonExpression(anExpressionString,
172 theOldName, theNewName);
173 anAttribute->setText(anExpressionString);
175 if (theAttribute->attributeType() == ModelAPI_AttributeDouble::typeId()) {
176 AttributeDoublePtr anAttribute =
177 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
178 std::string anExpressionString = anAttribute->text();
179 anExpressionString = renameInPythonExpression(anExpressionString,
180 theOldName, theNewName);
181 anAttribute->setText(anExpressionString);
183 if (theAttribute->attributeType() == GeomDataAPI_Point::typeId()) {
184 AttributePointPtr anAttribute =
185 std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
186 std::string anExpressionString[3] = {
187 anAttribute->textX(),
188 anAttribute->textY(),
191 for (int i = 0; i < 3; ++i)
192 anExpressionString[i] = renameInPythonExpression(anExpressionString[i],
193 theOldName, theNewName);
194 anAttribute->setText(anExpressionString[0],
195 anExpressionString[1],
196 anExpressionString[2]);
198 if (theAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
199 AttributePoint2DPtr anAttribute =
200 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
201 std::string anExpressionString[2] = {
202 anAttribute->textX(),
205 for (int i = 0; i < 2; ++i)
206 anExpressionString[i] = renameInPythonExpression(anExpressionString[i],
207 theOldName, theNewName);
208 anAttribute->setText(anExpressionString[0],
209 anExpressionString[1]);
213 void ParametersPlugin_EvalListener::renameInDependents(
214 std::shared_ptr<ModelAPI_ResultParameter> theResultParameter,
215 const std::string& theOldName,
216 const std::string& theNewName)
218 std::set<std::shared_ptr<ModelAPI_Attribute> > anAttributes =
219 theResultParameter->data()->refsToMe();
220 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anAttributeIt =
221 anAttributes.cbegin();
222 for (; anAttributeIt != anAttributes.cend(); ++anAttributeIt) {
223 const AttributePtr& anAttribute = *anAttributeIt;
224 if (anAttribute->attributeType() == ModelAPI_AttributeRefList::typeId()) {
225 std::shared_ptr<ParametersPlugin_Parameter> aParameter =
226 std::dynamic_pointer_cast<ParametersPlugin_Parameter>(
227 anAttribute->owner());
228 if (aParameter.get())
230 renameInParameter(aParameter, theOldName, theNewName);
233 renameInAttribute(anAttribute, theOldName, theNewName);
237 bool isValidAttribute(const AttributePtr& theAttribute)
239 std::string aValidator;
240 Events_InfoMessage anError;
241 return ModelAPI_Session::get()->validators()->validate(theAttribute, aValidator, anError);
244 void setParameterName(ResultParameterPtr theResultParameter, const std::string& theName)
246 bool aWasBlocked = theResultParameter->data()->blockSendAttributeUpdated(true);
247 theResultParameter->data()->setName(theName);
248 theResultParameter->data()->blockSendAttributeUpdated(aWasBlocked, false);
250 std::shared_ptr<ParametersPlugin_Parameter> aParameter =
251 std::dynamic_pointer_cast<ParametersPlugin_Parameter>(
252 ModelAPI_Feature::feature(theResultParameter));
254 std::string anOldName = aParameter->name();
255 aWasBlocked = aParameter->data()->blockSendAttributeUpdated(true);
256 aParameter->data()->setName(theName);
257 aParameter->string(ParametersPlugin_Parameter::VARIABLE_ID())->setValue(theName);
258 aParameter->data()->blockSendAttributeUpdated(aWasBlocked);
261 void ParametersPlugin_EvalListener::processObjectRenamedEvent(
262 const std::shared_ptr<Events_Message>& theMessage)
264 std::shared_ptr<ModelAPI_ObjectRenamedMessage> aMessage =
265 std::dynamic_pointer_cast<ModelAPI_ObjectRenamedMessage>(theMessage);
267 // Empty new name is not available too but it will be rejected by
268 // name validator in isValidAttribute.
269 if (!aMessage.get() || aMessage->oldName().empty())
272 // check if the renamed object is a result parameter
273 ResultParameterPtr aResultParameter =
274 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aMessage->object());
275 if (!aResultParameter.get())
278 // get parameter feature for the result
279 std::shared_ptr<ParametersPlugin_Parameter> aParameter =
280 std::dynamic_pointer_cast<ParametersPlugin_Parameter>(
281 ModelAPI_Feature::feature(aResultParameter));
282 if (!aParameter.get())
285 std::string aNotActivatedNames;
286 if (!ModelAPI_Tools::allDocumentsActivated(aNotActivatedNames)) {
287 static const std::string aMsgContext("ParametersPlugin");
288 static const std::string aMsgText =
289 "Selected objects can be used in Part documents which are not loaded: " +
290 std::string("%1. Would you like to continue?");
291 Events_InfoMessage aMsg(aMsgContext, aMsgText);
292 aMsg.arg(aNotActivatedNames.c_str());
293 QMessageBox::StandardButton aRes =
294 QMessageBox::warning(0, ModuleBase_Tools::translate(aMsgContext, "Warning"),
295 ModuleBase_Tools::translate(aMsg),
296 QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
297 if (aRes != QMessageBox::Yes) {
298 setParameterName(aResultParameter, aMessage->oldName());
303 // try to update the parameter feature according the new name
304 setParameterName(aResultParameter, aMessage->newName());
305 if (!isValidAttribute(aParameter->string(ParametersPlugin_Parameter::VARIABLE_ID()))) {
306 //setParameterName(aResultParameter, aMessage->oldName());
307 if (myOldNames.find(aParameter.get()) == myOldNames.end())
308 myOldNames[aParameter.get()] = aMessage->oldName();
312 std::string anOldName = aMessage->oldName();
313 if (myOldNames.find(aParameter.get()) != myOldNames.end()) {
314 anOldName = myOldNames[aParameter.get()];
315 myOldNames.erase(aParameter.get());
316 aParameter->execute(); // to enable result because of previously incorrect name
319 renameInDependents(aResultParameter, anOldName, aMessage->newName());
322 void ParametersPlugin_EvalListener::processReplaceParameterEvent(
323 const std::shared_ptr<Events_Message>& theMessage)
325 std::shared_ptr<ModelAPI_ReplaceParameterMessage> aMessage =
326 std::dynamic_pointer_cast<ModelAPI_ReplaceParameterMessage>(theMessage);
328 // get parameter feature for the object
329 std::shared_ptr<ParametersPlugin_Parameter> aParameter =
330 std::dynamic_pointer_cast<ParametersPlugin_Parameter>(
331 ModelAPI_Feature::feature(aMessage->object()));
332 if (!aParameter.get())
335 ResultParameterPtr aResultParameter =
336 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(
337 aParameter->firstResult());
338 if (!aResultParameter.get())
341 double aRealValue = aResultParameter->data()->real(ModelAPI_ResultParameter::VALUE())->value();
342 std::string aValue = toStdString(aRealValue);
344 renameInDependents(aResultParameter, aResultParameter->data()->name(), aValue);