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