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