Salome HOME
Call sendAttributeUpdated() after recalculation of Double, Point, Point2D expressions.
[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::setCalculatedValue(const double theValue)
19 {
20   if (!myIsInitialized || myReal->Get() != theValue) {
21     myReal->Set(theValue);
22     owner()->data()->sendAttributeUpdated(this);
23   }
24 }
25
26 void Model_AttributeDouble::setValue(const double theValue)
27 {
28   setCalculatedValue(text().empty() ? theValue : value());
29 }
30
31 double Model_AttributeDouble::value()
32 {
33   return myReal->Get();
34 }
35
36 Model_AttributeDouble::Model_AttributeDouble(TDF_Label& theLabel)
37 {
38   // check the attribute could be already presented in this doc (after load document)
39   myIsInitialized = theLabel.FindAttribute(TDataStd_Real::GetID(), myReal) == Standard_True;
40   if (!myIsInitialized) {
41     // create attribute: not initialized by value yet, just zero
42     myReal = TDataStd_Real::Set(theLabel, 0.);
43   }
44   if (!theLabel.FindAttribute(TDataStd_Name::GetID(), myText)) {
45     myText = TDataStd_Name::Set(theLabel, TCollection_ExtendedString());
46   }
47 }
48
49 void Model_AttributeDouble::setText(const std::string& theValue)
50 {
51   TCollection_ExtendedString aValue(theValue.c_str());
52   if (myText->Get() != aValue) {
53     myText->Set(aValue);
54     // Send it to evaluator to convert into the double and store in the attribute
55     ModelAPI_AttributeEvalMessage::send(owner()->data()->attribute(id()), this);
56     owner()->data()->sendAttributeUpdated(this);
57   }
58 }
59
60 string Model_AttributeDouble::text()
61 {
62   return TCollection_AsciiString(myText->Get()).ToCString();
63 }
64
65 Standard_GUID kInvalidGUID("caee5ce4-34b1-4b29-abcb-685287d18096");
66
67 void Model_AttributeDouble::setExpressionInvalid(const bool theFlag)
68 {
69   if (theFlag) {
70     TDataStd_UAttribute::Set(myReal->Label(), kInvalidGUID);
71   } else {
72     myReal->Label().ForgetAttribute(kInvalidGUID);
73   }
74 }
75
76 bool Model_AttributeDouble::expressionInvalid()
77 {
78   return myReal->Label().IsAttribute(kInvalidGUID) == Standard_True;
79 }