Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_DimensionStyleListener.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "SketcherPrs_DimensionStyleListener.h"
22 #include <Prs3d_DimensionAspect.hxx>
23 #include "SketcherPrs_Tools.h"
24
25 #include <Events_Loop.h>
26
27 #include <AIS_Dimension.hxx>
28 #include <TCollection_ExtendedString.hxx>
29
30 //#ifndef WNT
31 //  #define COMPILATION_CORRECTION
32 //#endif
33
34 // it is not possible to use 0x2211 as summ symbol because it is not supported by
35 // debian Linux platform
36 static const Standard_ExtCharacter MyEmptySymbol(' ');
37 static const Standard_ExtCharacter MySigmaSymbol('=');//0x03A3); // using equal instead of sigma
38
39 SketcherPrs_DimensionStyleListener::DimensionValue::DimensionValue(double theDoubleValue,
40                                      bool theHasParameters, const std::string& theTextValue)
41 : myDoubleValue(theDoubleValue),
42   myHasParameters(theHasParameters),
43   myTextValue(theTextValue)
44 {
45 }
46
47 void SketcherPrs_DimensionStyleListener::DimensionValue::init(
48                                                 const AttributeDoublePtr& theAttributeValue)
49 {
50   myDoubleValue = theAttributeValue->value();
51   myHasParameters = theAttributeValue->usedParameters().size() > 0;
52   myTextValue = theAttributeValue->text();
53 }
54
55 SketcherPrs_DimensionStyleListener::SketcherPrs_DimensionStyleListener()
56 {
57   Events_Loop* aLoop = Events_Loop::loop();
58   const Events_ID kDocCreatedEvent =
59                 SketcherPrs_ParameterStyleMessage::eventId();
60   aLoop->registerListener(this, kDocCreatedEvent, NULL, false);
61 }
62
63 SketcherPrs_DimensionStyleListener::~SketcherPrs_DimensionStyleListener()
64 {
65   Events_Loop* aLoop = Events_Loop::loop();
66   aLoop->removeListener(this);
67 }
68
69 void SketcherPrs_DimensionStyleListener::processEvent(
70   const std::shared_ptr<Events_Message>& theMessage)
71 {
72   const Events_ID kParameterStyleEvent = SketcherPrs_ParameterStyleMessage::eventId();
73   if (theMessage->eventID() == kParameterStyleEvent) {
74     std::shared_ptr<SketcherPrs_ParameterStyleMessage> aMessage = std::dynamic_pointer_cast<
75                                             SketcherPrs_ParameterStyleMessage>(theMessage);
76     myStyle = aMessage->style();
77   }
78 }
79
80 void SketcherPrs_DimensionStyleListener::updateDimensions(AIS_Dimension* theDimension,
81           const SketcherPrs_DimensionStyleListener::DimensionValue& theDimensionValue)
82 {
83   if (!theDimension)
84     return;
85   updateDimensions(theDimension, theDimensionValue.myHasParameters,
86                    theDimensionValue.myTextValue, theDimensionValue.myDoubleValue);
87 }
88
89 void SketcherPrs_DimensionStyleListener::updateDimensions(AIS_Dimension* theDimension,
90                                                           const bool theHasParameters,
91                                                           const std::string& theTextValue,
92                                                           const double theDoubleValue)
93 {
94   if (!theDimension)
95     return;
96
97   /// do not show special symbols of dimension: previous implementation did not allow to unite them
98   theDimension->SetSpecialSymbol(MyEmptySymbol);
99   theDimension->SetDisplaySpecialSymbol(AIS_DSS_No);
100
101   TCollection_ExtendedString aCustomValue;
102   if (theHasParameters) {
103     bool isParameterTextStyle = myStyle == SketcherPrs_ParameterStyleMessage::ParameterText;
104
105     if (isParameterTextStyle)
106       aCustomValue = theTextValue.c_str();
107     else {
108       // format value string using "sprintf"
109       TCollection_AsciiString aFormatStr =
110         theDimension->Attributes()->DimensionAspect()->ValueStringFormat();
111       char aFmtBuffer[256];
112       sprintf (aFmtBuffer, aFormatStr.ToCString(), theDoubleValue);
113       aCustomValue = TCollection_ExtendedString (aFmtBuffer);
114
115       aCustomValue.Insert (1, MySigmaSymbol);
116     }
117   }
118   else {
119     // format value string using "sprintf"
120     TCollection_AsciiString aFormatStr =
121       theDimension->Attributes()->DimensionAspect()->ValueStringFormat();
122     char aFmtBuffer[256];
123     sprintf (aFmtBuffer, aFormatStr.ToCString(), theDoubleValue);
124     aCustomValue = TCollection_ExtendedString (aFmtBuffer);
125   }
126 #ifdef COMPILATION_CORRECTION
127   theDimension->SetCustomValue(theDoubleValue);
128 #else
129   theDimension->SetCustomValue(aCustomValue);
130 #endif
131 }
132