Salome HOME
5c9e3fbe6c81cc9102c1ad36d5a402aa25ee6935
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_DimensionStyleListener.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        SketcherPrs_DimensionStyleListener.cpp
4 // Created:     31 March 2016
5 // Author:      Natalia ERMOLAEVA
6
7 #include "SketcherPrs_DimensionStyleListener.h"
8 #include "SketcherPrs_Tools.h"
9
10 #include <Events_Loop.h>
11
12 #include <AIS_Dimension.hxx>
13 #include <TCollection_ExtendedString.hxx>
14
15 //#ifndef WNT
16 //  #define COMPILATION_CORRECTION
17 //#endif
18
19 // it is not possible to use 0x2211 as summ symbol because it is not supported by
20 // debian Linux platform
21 static const Standard_ExtCharacter MyEmptySymbol(' ');
22 static const Standard_ExtCharacter MySigmaSymbol('=');//0x03A3); // using equal instead of sigma
23
24 SketcherPrs_DimensionStyleListener::DimensionValue::DimensionValue(double theDoubleValue,
25                                      bool theHasParameters, const std::string& theTextValue)
26 : myDoubleValue(theDoubleValue),
27   myHasParameters(theHasParameters),
28   myTextValue(theTextValue)
29 {
30 }
31
32 void SketcherPrs_DimensionStyleListener::DimensionValue::init(
33                                                 const AttributeDoublePtr& theAttributeValue)
34 {
35   myDoubleValue = theAttributeValue->value();
36   myHasParameters = theAttributeValue->usedParameters().size() > 0;
37   myTextValue = theAttributeValue->text();
38 }
39
40 SketcherPrs_DimensionStyleListener::SketcherPrs_DimensionStyleListener()
41 {
42   Events_Loop* aLoop = Events_Loop::loop();
43   const Events_ID kDocCreatedEvent =
44                 SketcherPrs_ParameterStyleMessage::eventId();
45   aLoop->registerListener(this, kDocCreatedEvent, NULL, false);
46 }
47
48 SketcherPrs_DimensionStyleListener::~SketcherPrs_DimensionStyleListener()
49 {
50   Events_Loop* aLoop = Events_Loop::loop();
51   aLoop->removeListener(this);
52 }
53
54 void SketcherPrs_DimensionStyleListener::processEvent(const std::shared_ptr<Events_Message>& theMessage)
55 {
56   const Events_ID kParameterStyleEvent = SketcherPrs_ParameterStyleMessage::eventId();
57   if (theMessage->eventID() == kParameterStyleEvent) {
58     std::shared_ptr<SketcherPrs_ParameterStyleMessage> aMessage = std::dynamic_pointer_cast<
59                                             SketcherPrs_ParameterStyleMessage>(theMessage);
60     myStyle = aMessage->style();
61   }
62 }
63
64 void SketcherPrs_DimensionStyleListener::updateDimensions(AIS_Dimension* theDimension,
65           const SketcherPrs_DimensionStyleListener::DimensionValue& theDimensionValue)
66 {
67   if (!theDimension)
68     return;
69   updateDimensions(theDimension, theDimensionValue.myHasParameters,
70                    theDimensionValue.myTextValue, theDimensionValue.myDoubleValue);
71 }
72
73 void SketcherPrs_DimensionStyleListener::updateDimensions(AIS_Dimension* theDimension,
74                                                           const bool theHasParameters,
75                                                           const std::string& theTextValue,
76                                                           const double theDoubleValue)
77 {
78   if (!theDimension)
79     return;
80
81   /// do not show special symbols of dimension: previous implementation did not allow to unite them
82   theDimension->SetSpecialSymbol(MyEmptySymbol);
83   theDimension->SetDisplaySpecialSymbol(AIS_DSS_No);
84
85   TCollection_ExtendedString aCustomValue;
86   if (theHasParameters) {
87     bool isParameterTextStyle = myStyle == SketcherPrs_ParameterStyleMessage::ParameterText;
88
89     if (isParameterTextStyle)
90       aCustomValue = theTextValue.c_str();
91     else {
92       // format value string using "sprintf"
93       TCollection_AsciiString aFormatStr = theDimension->Attributes()->DimensionAspect()->ValueStringFormat();
94       char aFmtBuffer[256];
95       sprintf (aFmtBuffer, aFormatStr.ToCString(), theDoubleValue);
96       aCustomValue = TCollection_ExtendedString (aFmtBuffer);
97
98       aCustomValue.Insert (1, MySigmaSymbol);
99     }
100   }
101   else {
102     // format value string using "sprintf"
103     TCollection_AsciiString aFormatStr = theDimension->Attributes()->DimensionAspect()->ValueStringFormat();
104     char aFmtBuffer[256];
105     sprintf (aFmtBuffer, aFormatStr.ToCString(), theDoubleValue);
106     aCustomValue = TCollection_ExtendedString (aFmtBuffer);
107   }
108 #ifdef COMPILATION_CORRECTION
109   theDimension->SetCustomValue(theDoubleValue);
110 #else
111   theDimension->SetCustomValue(aCustomValue);
112 #endif
113 }
114