1 // Copyright (C) 2014-2020 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 //------------------------------------------------------------------------------
65 ParametersPlugin_EvalListener::ParametersPlugin_EvalListener()
67 Events_Loop* aLoop = Events_Loop::loop();
69 Events_ID anEvents_IDs[] = {
70 ModelAPI_ObjectRenamedMessage::eventId(),
71 ModelAPI_ReplaceParameterMessage::eventId()
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);
78 ParametersPlugin_EvalListener::~ParametersPlugin_EvalListener()
82 void ParametersPlugin_EvalListener::processEvent(
83 const std::shared_ptr<Events_Message>& theMessage)
85 if (!theMessage.get())
88 const Events_ID kObjectRenamedEvent = ModelAPI_ObjectRenamedMessage::eventId();
89 const Events_ID kReplaceParameterEvent = ModelAPI_ReplaceParameterMessage::eventId();
91 if (theMessage->eventID() == kObjectRenamedEvent) {
92 processObjectRenamedEvent(theMessage);
93 } else if (theMessage->eventID() == kReplaceParameterEvent) {
94 processReplaceParameterEvent(theMessage);
98 std::string ParametersPlugin_EvalListener::renameInPythonExpression(
99 const std::string& theExpression,
100 const std::string& theOldName,
101 const std::string& theNewName)
103 std::string anExpressionString = theExpression;
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();
110 if (aPositions.empty())
111 return anExpressionString;
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);
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;
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);
135 return anExpressionString;
138 void ParametersPlugin_EvalListener::renameInParameter(
139 std::shared_ptr<ParametersPlugin_Parameter> theParameter,
140 const std::string& theOldName,
141 const std::string& theNewName)
143 std::shared_ptr<ModelAPI_AttributeString> anExpressionAttribute =
144 theParameter->string(ParametersPlugin_Parameter::EXPRESSION_ID());
146 std::string anExpressionString = anExpressionAttribute->value();
147 anExpressionString = renameInPythonExpression(anExpressionString,
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);
157 void ParametersPlugin_EvalListener::renameInAttribute(
158 std::shared_ptr<ModelAPI_Attribute> theAttribute,
159 const std::string& theOldName,
160 const std::string& theNewName)
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);
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);
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(),
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]);
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(),
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]);
208 void ParametersPlugin_EvalListener::renameInDependents(
209 std::shared_ptr<ModelAPI_ResultParameter> theResultParameter,
210 const std::string& theOldName,
211 const std::string& theNewName)
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())
225 renameInParameter(aParameter, theOldName, theNewName);
228 renameInAttribute(anAttribute, theOldName, theNewName);
232 bool isValidAttribute(const AttributePtr& theAttribute)
234 std::string aValidator;
235 Events_InfoMessage anError;
236 return ModelAPI_Session::get()->validators()->validate(theAttribute, aValidator, anError);
239 void setParameterName(ResultParameterPtr theResultParameter, const std::string& theName)
241 bool aWasBlocked = theResultParameter->data()->blockSendAttributeUpdated(true);
242 theResultParameter->data()->setName(theName);
243 theResultParameter->data()->blockSendAttributeUpdated(aWasBlocked, false);
245 std::shared_ptr<ParametersPlugin_Parameter> aParameter =
246 std::dynamic_pointer_cast<ParametersPlugin_Parameter>(
247 ModelAPI_Feature::feature(theResultParameter));
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);
256 void ParametersPlugin_EvalListener::processObjectRenamedEvent(
257 const std::shared_ptr<Events_Message>& theMessage)
259 std::shared_ptr<ModelAPI_ObjectRenamedMessage> aMessage =
260 std::dynamic_pointer_cast<ModelAPI_ObjectRenamedMessage>(theMessage);
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())
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())
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())
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());
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();
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
314 renameInDependents(aResultParameter, anOldName, aMessage->newName());
317 void ParametersPlugin_EvalListener::processReplaceParameterEvent(
318 const std::shared_ptr<Events_Message>& theMessage)
320 std::shared_ptr<ModelAPI_ReplaceParameterMessage> aMessage =
321 std::dynamic_pointer_cast<ModelAPI_ReplaceParameterMessage>(theMessage);
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())
330 ResultParameterPtr aResultParameter =
331 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(
332 aParameter->firstResult());
333 if (!aResultParameter.get())
336 double aRealValue = aResultParameter->data()->real(ModelAPI_ResultParameter::VALUE())->value();
337 std::string aValue = toStdString(aRealValue);
339 renameInDependents(aResultParameter, aResultParameter->data()->name(), aValue);