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