1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 * ParametersPlugin_EvalListener.cpp
5 * Created on: Apr 28, 2015
11 #include <ParametersPlugin_EvalListener.h>
12 #include <ParametersPlugin_Parameter.h>
14 #include <Events_InfoMessage.h>
16 #include <ModelAPI_AttributeDouble.h>
17 #include <ModelAPI_AttributeInteger.h>
18 #include <ModelAPI_AttributeRefList.h>
19 #include <ModelAPI_AttributeString.h>
20 #include <ModelAPI_AttributeValidator.h>
21 #include <ModelAPI_Document.h>
22 #include <ModelAPI_Events.h>
23 #include <ModelAPI_ResultParameter.h>
24 #include <ModelAPI_Session.h>
25 #include <ModelAPI_Tools.h>
27 #include <GeomDataAPI_Point.h>
28 #include <GeomDataAPI_Point2D.h>
30 #include <QMessageBox>
36 //------------------------------------------------------------------------------
39 std::string toStdString(double theValue)
41 std::ostringstream sstream;
43 size_t aPos = sstream.str().find(".");
44 std::string aPnt = "";
45 if (aPos == std::string::npos)
47 return sstream.str() + aPnt;
50 std::set<std::string> toSet(const std::list<std::string>& theContainer)
52 return std::set<std::string>(theContainer.begin(), theContainer.end());
55 //------------------------------------------------------------------------------
57 ParametersPlugin_EvalListener::ParametersPlugin_EvalListener()
59 Events_Loop* aLoop = Events_Loop::loop();
61 Events_ID anEvents_IDs[] = {
62 ModelAPI_ObjectRenamedMessage::eventId(),
63 ModelAPI_ReplaceParameterMessage::eventId()
66 for (int i = 0; i < sizeof(anEvents_IDs)/sizeof(anEvents_IDs[0]); ++i)
67 aLoop->registerListener(this, anEvents_IDs[i], NULL, true);
70 ParametersPlugin_EvalListener::~ParametersPlugin_EvalListener()
74 void ParametersPlugin_EvalListener::processEvent(
75 const std::shared_ptr<Events_Message>& theMessage)
77 if (!theMessage.get())
80 const Events_ID kObjectRenamedEvent = ModelAPI_ObjectRenamedMessage::eventId();
81 const Events_ID kReplaceParameterEvent = ModelAPI_ReplaceParameterMessage::eventId();
83 if (theMessage->eventID() == kObjectRenamedEvent) {
84 processObjectRenamedEvent(theMessage);
85 } else if (theMessage->eventID() == kReplaceParameterEvent) {
86 processReplaceParameterEvent(theMessage);
90 std::string ParametersPlugin_EvalListener::renameInPythonExpression(
91 const std::string& theExpression,
92 const std::string& theOldName,
93 const std::string& theNewName)
95 std::string anExpressionString = theExpression;
97 // ask interpreter to compute the positions in the expression
98 std::shared_ptr<ModelAPI_ComputePositionsMessage> aMsg =
99 ModelAPI_ComputePositionsMessage::send(theExpression, theOldName, this);
100 const std::list<std::pair<int, int> >& aPositions = aMsg->positions();
102 if (aPositions.empty())
103 return anExpressionString;
105 std::map<int, std::list<int> > aLines;
106 std::list<std::pair<int, int> >::const_iterator it = aPositions.begin();
107 for (; it != aPositions.end(); ++it)
108 aLines[it->first].push_back(it->second);
110 // Start renaming from the end to keep indexes if theNewName is longer then theOldName
111 std::map<int, std::list<int> >::const_reverse_iterator ritLine = aLines.rbegin();
112 for (; ritLine != aLines.rend(); ++ritLine) {
113 // Calculate the start of the line (find the aLineNo occurrence of "\n" )
114 int aLineNo = ritLine->first - 1;
115 size_t aLineStart = 0;
116 for (int i = 0; i < aLineNo; ++i)
117 aLineStart = anExpressionString.find("\n", aLineStart) + 1;
119 const std::list<int>& aColOffsets = ritLine->second;
120 std::list<int>::const_reverse_iterator ritOffset = aColOffsets.rbegin();
121 for (; ritOffset != aColOffsets.rend(); ++ritOffset) {
122 int anOffset = *ritOffset;
123 anExpressionString.replace(aLineStart + anOffset, theOldName.size(), theNewName);
127 return anExpressionString;
130 void ParametersPlugin_EvalListener::renameInParameter(
131 std::shared_ptr<ParametersPlugin_Parameter> theParameter,
132 const std::string& theOldName,
133 const std::string& theNewName)
135 std::shared_ptr<ModelAPI_AttributeString> anExpressionAttribute =
136 theParameter->string(ParametersPlugin_Parameter::EXPRESSION_ID());
138 std::string anExpressionString = anExpressionAttribute->value();
139 anExpressionString = renameInPythonExpression(anExpressionString,
142 // Issue #588. No need for reevaluating expression.
143 // Moreover, current history may not contain necessary parameters.
144 bool aWasBlocked = anExpressionAttribute->owner()->data()->blockSendAttributeUpdated(true);
145 anExpressionAttribute->setValue(anExpressionString);
146 anExpressionAttribute->owner()->data()->blockSendAttributeUpdated(aWasBlocked);
149 void ParametersPlugin_EvalListener::renameInAttribute(
150 std::shared_ptr<ModelAPI_Attribute> theAttribute,
151 const std::string& theOldName,
152 const std::string& theNewName)
154 if (theAttribute->attributeType() == ModelAPI_AttributeInteger::typeId()) {
155 AttributeIntegerPtr anAttribute =
156 std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(theAttribute);
157 std::string anExpressionString = anAttribute->text();
158 anExpressionString = renameInPythonExpression(anExpressionString,
159 theOldName, theNewName);
160 anAttribute->setText(anExpressionString);
162 if (theAttribute->attributeType() == ModelAPI_AttributeDouble::typeId()) {
163 AttributeDoublePtr anAttribute =
164 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
165 std::string anExpressionString = anAttribute->text();
166 anExpressionString = renameInPythonExpression(anExpressionString,
167 theOldName, theNewName);
168 anAttribute->setText(anExpressionString);
170 if (theAttribute->attributeType() == GeomDataAPI_Point::typeId()) {
171 AttributePointPtr anAttribute =
172 std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
173 std::string anExpressionString[3] = {
174 anAttribute->textX(),
175 anAttribute->textY(),
178 for (int i = 0; i < 3; ++i)
179 anExpressionString[i] = renameInPythonExpression(anExpressionString[i],
180 theOldName, theNewName);
181 anAttribute->setText(anExpressionString[0],
182 anExpressionString[1],
183 anExpressionString[2]);
185 if (theAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
186 AttributePoint2DPtr anAttribute =
187 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
188 std::string anExpressionString[2] = {
189 anAttribute->textX(),
192 for (int i = 0; i < 2; ++i)
193 anExpressionString[i] = renameInPythonExpression(anExpressionString[i],
194 theOldName, theNewName);
195 anAttribute->setText(anExpressionString[0],
196 anExpressionString[1]);
200 void ParametersPlugin_EvalListener::renameInDependents(
201 std::shared_ptr<ModelAPI_ResultParameter> theResultParameter,
202 const std::string& theOldName,
203 const std::string& theNewName)
205 std::set<std::shared_ptr<ModelAPI_Attribute> > anAttributes =
206 theResultParameter->data()->refsToMe();
207 std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anAttributeIt =
208 anAttributes.cbegin();
209 for (; anAttributeIt != anAttributes.cend(); ++anAttributeIt) {
210 const AttributePtr& anAttribute = *anAttributeIt;
211 if (anAttribute->attributeType() == ModelAPI_AttributeRefList::typeId()) {
212 std::shared_ptr<ParametersPlugin_Parameter> aParameter =
213 std::dynamic_pointer_cast<ParametersPlugin_Parameter>(
214 anAttribute->owner());
215 if (aParameter.get())
217 renameInParameter(aParameter, theOldName, theNewName);
220 renameInAttribute(anAttribute, theOldName, theNewName);
224 bool isValidAttribute(const AttributePtr& theAttribute)
226 std::string aValidator;
227 Events_InfoMessage anError;
228 return ModelAPI_Session::get()->validators()->validate(theAttribute, aValidator, anError);
231 void setParameterName(ResultParameterPtr theResultParameter, const std::string& theName)
233 bool aWasBlocked = theResultParameter->data()->blockSendAttributeUpdated(true);
234 theResultParameter->data()->setName(theName);
235 theResultParameter->data()->blockSendAttributeUpdated(aWasBlocked, false);
237 std::shared_ptr<ParametersPlugin_Parameter> aParameter =
238 std::dynamic_pointer_cast<ParametersPlugin_Parameter>(
239 ModelAPI_Feature::feature(theResultParameter));
241 aWasBlocked = aParameter->data()->blockSendAttributeUpdated(true);
242 aParameter->data()->setName(theName);
243 aParameter->string(ParametersPlugin_Parameter::VARIABLE_ID())->setValue(theName);
244 aParameter->data()->blockSendAttributeUpdated(aWasBlocked);
247 void ParametersPlugin_EvalListener::processObjectRenamedEvent(
248 const std::shared_ptr<Events_Message>& theMessage)
250 std::shared_ptr<ModelAPI_ObjectRenamedMessage> aMessage =
251 std::dynamic_pointer_cast<ModelAPI_ObjectRenamedMessage>(theMessage);
253 // Empty new name is not available too but it will be rejected by
254 // name validator in isValidAttribute.
255 if (!aMessage.get() || aMessage->oldName().empty())
258 // check if the renamed object is a result parameter
259 ResultParameterPtr aResultParameter =
260 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aMessage->object());
261 if (!aResultParameter.get())
264 // get parameter feature for the result
265 std::shared_ptr<ParametersPlugin_Parameter> aParameter =
266 std::dynamic_pointer_cast<ParametersPlugin_Parameter>(
267 ModelAPI_Feature::feature(aResultParameter));
268 if (!aParameter.get())
271 std::string aNotActivatedNames;
272 if (!ModelAPI_Tools::allDocumentsActivated(aNotActivatedNames)) {
273 QMessageBox::StandardButton aRes = QMessageBox::warning(0, QObject::tr("Warning"),
274 QObject::tr("Selected objects can be used in Part documents which are not loaded: "
275 "%1. Whould you like to continue?").arg(aNotActivatedNames.c_str()),
276 QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
277 if (aRes != QMessageBox::Yes) {
278 setParameterName(aResultParameter, aMessage->oldName());
283 // try to update the parameter feature according the new name
284 setParameterName(aResultParameter, aMessage->newName());
285 if (!isValidAttribute(aParameter->string(ParametersPlugin_Parameter::VARIABLE_ID()))) {
286 //setParameterName(aResultParameter, aMessage->oldName());
287 if (myOldNames.find(aParameter.get()) == myOldNames.end())
288 myOldNames[aParameter.get()] = aMessage->oldName();
292 std::string anOldName = aMessage->oldName();
293 if (myOldNames.find(aParameter.get()) != myOldNames.end()) {
294 anOldName = myOldNames[aParameter.get()];
295 myOldNames.erase(aParameter.get());
296 aParameter->execute(); // to enable result because of previously incorrect name
299 renameInDependents(aResultParameter, anOldName, aMessage->newName());
302 void ParametersPlugin_EvalListener::processReplaceParameterEvent(
303 const std::shared_ptr<Events_Message>& theMessage)
305 std::shared_ptr<ModelAPI_ReplaceParameterMessage> aMessage =
306 std::dynamic_pointer_cast<ModelAPI_ReplaceParameterMessage>(theMessage);
308 // get parameter feature for the object
309 std::shared_ptr<ParametersPlugin_Parameter> aParameter =
310 std::dynamic_pointer_cast<ParametersPlugin_Parameter>(
311 ModelAPI_Feature::feature(aMessage->object()));
312 if (!aParameter.get())
315 ResultParameterPtr aResultParameter =
316 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(
317 aParameter->firstResult());
318 if (!aResultParameter.get())
321 double aRealValue = aResultParameter->data()->real(ModelAPI_ResultParameter::VALUE())->value();
322 std::string aValue = toStdString(aRealValue);
324 renameInDependents(aResultParameter, aResultParameter->data()->name(), aValue);