Salome HOME
Fix for issue #562 : correct update by dependencies in the parameters
[modules/shaper.git] / src / Model / Model_AttributeDouble.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModelAPI_AttributeDouble.cxx
4 // Created:     2 Apr 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include "Model_AttributeDouble.h"
8 #include <ModelAPI_Feature.h>
9 #include <ModelAPI_Data.h>
10 #include <ModelAPI_Events.h>
11
12 #include <TCollection_AsciiString.hxx>
13 #include <TCollection_ExtendedString.hxx>
14 #include <TDataStd_UAttribute.hxx>
15
16 using namespace std;
17
18 void Model_AttributeDouble::setValue(const double theValue)
19 {
20   if (!myIsInitialized || myReal->Get() != theValue) {
21     myReal->Set(theValue);
22     owner()->data()->sendAttributeUpdated(this);
23   }
24 }
25
26 double Model_AttributeDouble::value()
27 {
28   return myReal->Get();
29 }
30
31 Model_AttributeDouble::Model_AttributeDouble(TDF_Label& theLabel)
32 {
33   // check the attribute could be already presented in this doc (after load document)
34   myIsInitialized = theLabel.FindAttribute(TDataStd_Real::GetID(), myReal) == Standard_True;
35   if (!myIsInitialized) {
36     // create attribute: not initialized by value yet, just zero
37     myReal = TDataStd_Real::Set(theLabel, 0.);
38   }
39   if (!theLabel.FindAttribute(TDataStd_Name::GetID(), myText)) {
40     myText = TDataStd_Name::Set(theLabel, TCollection_ExtendedString());
41   }
42 }
43
44 void Model_AttributeDouble::setText(const std::string& theValue)
45 {
46   TCollection_ExtendedString aValue(theValue.c_str());
47   if (myText->Get() != aValue) {
48     myText->Set(aValue);
49     owner()->data()->sendAttributeUpdated(this);
50     // Send it to evaluator to convert into the double and store in the attribute
51     static Events_ID anId = ModelAPI_AttributeEvalMessage::eventId();
52     std::shared_ptr<ModelAPI_AttributeEvalMessage> aMessage =
53       std::shared_ptr<ModelAPI_AttributeEvalMessage>(new ModelAPI_AttributeEvalMessage(anId, this));
54     aMessage->setAttribute(owner()->data()->attribute(id())); // to get shared pointer to this
55     Events_Loop::loop()->send(aMessage);
56   }
57 }
58
59 string Model_AttributeDouble::text()
60 {
61   return TCollection_AsciiString(myText->Get()).ToCString();
62 }
63
64 Standard_GUID kInvalidGUID("caee5ce4-34b1-4b29-abcb-685287d18096");
65
66 void Model_AttributeDouble::setExpressionInvalid(const bool theFlag)
67 {
68   if (theFlag) {
69     TDataStd_UAttribute::Set(myReal->Label(), kInvalidGUID);
70   } else {
71     myReal->Label().ForgetAttribute(kInvalidGUID);
72   }
73 }
74
75 bool Model_AttributeDouble::expressionInvalid()
76 {
77   return myReal->Label().IsAttribute(kInvalidGUID) == Standard_True;
78 }