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 <Locale_Convert.h>
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>
40 #include <GeomDataAPI_Point.h>
41 #include <GeomDataAPI_Point2D.h>
43 #include <QMessageBox>
45 #include <ModuleBase_Tools.h>
51 //------------------------------------------------------------------------------
54 static std::string toStdString(double theValue)
56 std::ostringstream sstream;
58 size_t aPos = sstream.str().find(".");
59 std::string aPnt = "";
60 if (aPos == std::string::npos)
62 return sstream.str() + aPnt;
65 static std::set<std::string> toSet(const std::list<std::string>& theContainer)
67 return std::set<std::string>(theContainer.begin(), theContainer.end());
70 //------------------------------------------------------------------------------
72 ParametersPlugin_EvalListener::ParametersPlugin_EvalListener()
74 Events_Loop* aLoop = Events_Loop::loop();
76 Events_ID anEvents_IDs[] = {
77 ModelAPI_ObjectRenamedMessage::eventId(),
78 ModelAPI_ReplaceParameterMessage::eventId()
81 for (int i = 0; i < sizeof(anEvents_IDs)/sizeof(anEvents_IDs[0]); ++i)
82 aLoop->registerListener(this, anEvents_IDs[i], NULL, true);
85 ParametersPlugin_EvalListener::~ParametersPlugin_EvalListener()
89 void ParametersPlugin_EvalListener::processEvent(
90 const std::shared_ptr<Events_Message>& theMessage)
92 if (!theMessage.get())
95 const Events_ID kObjectRenamedEvent = ModelAPI_ObjectRenamedMessage::eventId();
96 const Events_ID kReplaceParameterEvent = ModelAPI_ReplaceParameterMessage::eventId();
98 if (theMessage->eventID() == kObjectRenamedEvent) {
99 processObjectRenamedEvent(theMessage);
100 } else if (theMessage->eventID() == kReplaceParameterEvent) {
101 processReplaceParameterEvent(theMessage);
105 std::string ParametersPlugin_EvalListener::renameInPythonExpression(
106 const std::string& theExpression,
107 const std::string& theOldName,
108 const std::string& theNewName)
110 std::string anExpressionString = theExpression;
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();
117 if (aPositions.empty())
118 return anExpressionString;
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);
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;
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);
142 return anExpressionString;
145 void ParametersPlugin_EvalListener::renameInParameter(
146 std::shared_ptr<ParametersPlugin_Parameter> theParameter,
147 const std::string& theOldName,
148 const std::string& theNewName)
150 std::shared_ptr<ModelAPI_AttributeString> anExpressionAttribute =
151 theParameter->string(ParametersPlugin_Parameter::EXPRESSION_ID());
153 std::string anExpressionString = anExpressionAttribute->value();
154 anExpressionString = renameInPythonExpression(anExpressionString,
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);
164 void ParametersPlugin_EvalListener::renameInAttribute(
165 std::shared_ptr<ModelAPI_Attribute> theAttribute,
166 const std::string& theOldName,
167 const std::string& theNewName)
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);
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);
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(),
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]);
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(),
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]);
215 void ParametersPlugin_EvalListener::renameInDependents(
216 std::shared_ptr<ModelAPI_ResultParameter> theResultParameter,
217 const std::string& theOldName,
218 const std::string& theNewName)
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())
232 renameInParameter(aParameter, theOldName, theNewName);
235 renameInAttribute(anAttribute, theOldName, theNewName);
239 bool isValidAttribute(const AttributePtr& theAttribute)
241 std::string aValidator;
242 Events_InfoMessage anError;
243 return ModelAPI_Session::get()->validators()->validate(theAttribute, aValidator, anError);
246 void setParameterName(ResultParameterPtr theResultParameter, const std::string& theName)
248 bool aWasBlocked = theResultParameter->data()->blockSendAttributeUpdated(true);
249 theResultParameter->data()->setName(Locale::Convert::toWString(theName));
250 theResultParameter->data()->blockSendAttributeUpdated(aWasBlocked, false);
252 std::shared_ptr<ParametersPlugin_Parameter> aParameter =
253 std::dynamic_pointer_cast<ParametersPlugin_Parameter>(
254 ModelAPI_Feature::feature(theResultParameter));
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);
263 void ParametersPlugin_EvalListener::processObjectRenamedEvent(
264 const std::shared_ptr<Events_Message>& theMessage)
266 std::shared_ptr<ModelAPI_ObjectRenamedMessage> aMessage =
267 std::dynamic_pointer_cast<ModelAPI_ObjectRenamedMessage>(theMessage);
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())
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())
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())
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()));
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());
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
321 renameInDependents(aResultParameter, anOldName,
322 Locale::Convert::toString(aMessage->newName()));
325 void ParametersPlugin_EvalListener::processReplaceParameterEvent(
326 const std::shared_ptr<Events_Message>& theMessage)
328 std::shared_ptr<ModelAPI_ReplaceParameterMessage> aMessage =
329 std::dynamic_pointer_cast<ModelAPI_ReplaceParameterMessage>(theMessage);
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())
338 ResultParameterPtr aResultParameter =
339 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(
340 aParameter->firstResult());
341 if (!aResultParameter.get())
344 double aRealValue = aResultParameter->data()->real(ModelAPI_ResultParameter::VALUE())->value();
345 std::string aValue = toStdString(aRealValue);
347 renameInDependents(aResultParameter,
348 Locale::Convert::toString(aResultParameter->data()->name()),