fw4spl
SText.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "visuVTKAdaptor/SText.hpp"
8 
9 #include <fwData/Color.hpp>
10 #include <fwData/GenericFieldBase.hpp>
11 
12 #include <fwDataCamp/getObject.hpp>
13 
14 #include <fwServices/macros.hpp>
15 
16 #include <vtkActor2D.h>
17 #include <vtkTextMapper.h>
18 #include <vtkTextProperty.h>
19 
20 fwServicesRegisterMacro(::fwRenderVTK::IAdaptor, ::visuVTKAdaptor::SText);
21 
22 namespace visuVTKAdaptor
23 {
24 
25 static const ::fwServices::IService::KeyType s_OBJECT_IN = "object";
26 
27 SText::SText() :
28  m_actor(vtkActor2D::New()),
29  m_mapper(vtkTextMapper::New()),
30  m_fontSize(20),
31  m_fontFamily("courier"),
32  m_italic(false),
33  m_bold(false),
34  m_shadow(false),
35  m_vAlign("bottom"),
36  m_hAlign("left"),
37  m_textColor("#ffffffff")
38 {
39  m_mapper->GetTextProperty()->SetFontFamilyToCourier(); // Fixed-width font
40  m_mapper->GetTextProperty()->ShadowOn(); // better contrast
41  m_mapper->GetTextProperty()->BoldOn();
42 
43  m_actor->SetMapper(m_mapper);
44  m_actor->GetPositionCoordinate()->SetCoordinateSystemToNormalizedViewport();
45  m_actor->GetPosition2Coordinate()->SetCoordinateSystemToNormalizedViewport();
46 }
47 
48 //-----------------------------------------------------------------------------
49 
50 SText::~SText() noexcept
51 {
52  m_actor->Delete();
53  m_actor = nullptr;
54 
55  m_mapper->Delete();
56  m_mapper = nullptr;
57 }
58 
59 //-----------------------------------------------------------------------------
60 
61 void SText::configuring()
62 {
63  this->configureParams();
64 
65  const ConfigType srvconfig = this->getConfigTree();
66  const ConfigType config = srvconfig.get_child("config.<xmlattr>");
67 
68  std::string text = config.get<std::string>("text", "");
69 
70  if(text.empty())
71  {
72  text = srvconfig.get<std::string>("text", "");
73  }
74  else if(text[0] == '@')
75  {
76  m_path = text;
77  }
78 
79  if(m_path.empty())
80  {
81  m_text = text;
82  }
83 
84  m_fontSize = config.get<unsigned int>("fontSize", 20);
85  m_fontFamily = config.get<std::string>("fontFamily", "courier");
86 
87  m_italic = config.get<bool>("italic", false);
88  m_bold = config.get<bool>("bold", false);
89  m_shadow = config.get<bool>("shadow", false);
90 
91  m_hAlign = config.get<std::string>("hAlign", "left");
92  SLM_ASSERT("'hAlign' value must be 'left', 'center' or 'right'",
93  m_hAlign == "left"
94  || m_hAlign == "center"
95  || m_hAlign == "right"
96  );
97 
98  m_vAlign = config.get<std::string>("vAlign", "bottom");
99  SLM_ASSERT("'vAlign' value must be 'top', 'center' or 'bottom'",
100  m_vAlign == "top"
101  || m_hAlign == "center"
102  || m_vAlign == "bottom");
103 
104  m_textColor = config.get<std::string>("color", "#ffffffff");
105 }
106 
107 //-----------------------------------------------------------------------------
108 
109 void SText::starting()
110 {
111  this->initialize();
112 
113  this->updateText();
114  this->setStyle();
115  this->setText(m_text);
116 
117  this->addToRenderer(m_actor);
118  this->requestRender();
119 }
120 
121 //-----------------------------------------------------------------------------
122 
123 void SText::updating()
124 {
125  this->updateText();
126  this->setText(m_text);
127 
128  this->requestRender();
129 }
130 
131 //-----------------------------------------------------------------------------
132 
133 void SText::stopping()
134 {
135  this->removeAllPropFromRenderer();
136 }
137 
138 //-----------------------------------------------------------------------------
139 
140 void SText::setAlignment()
141 {
142  vtkTextProperty* textprop = m_mapper->GetTextProperty();
143 
144  double x = 0.5;
145  double y = 0.5;
146 
147  textprop->SetJustificationToCentered();
148  textprop->SetVerticalJustificationToCentered();
149 
150  if(m_hAlign == "left")
151  {
152  textprop->SetJustificationToLeft();
153  x = 0.001;
154  }
155  else if(m_hAlign == "right")
156  {
157  textprop->SetJustificationToRight();
158  x = 0.99;
159  }
160 
161  if(m_vAlign == "top")
162  {
163  textprop->SetVerticalJustificationToTop();
164  y = 0.98;
165  }
166  else if(m_vAlign == "bottom")
167  {
168  textprop->SetVerticalJustificationToBottom();
169  y = 0.02;
170  }
171 
172  m_actor->SetPosition(x, y);
173 }
174 
175 //-----------------------------------------------------------------------------
176 
177 void SText::setStyle()
178 {
179  vtkTextProperty* textprop = m_mapper->GetTextProperty();
180 
181  textprop->SetFontSize( int(m_fontSize) );
182  textprop->SetItalic(m_italic);
183  textprop->SetBold(m_bold);
184  textprop->SetShadow(m_shadow);
185 
186  if( m_textColor[0] == '#')
187  {
188  ::fwData::Color::sptr color = ::fwData::Color::New();
189  color->setRGBA(m_textColor);
190  textprop->SetColor(color->getRGBA()[0], color->getRGBA()[1], color->getRGBA()[2]);
191  }
192  else
193  {
194  // compatibility with "old" color
195  double color = std::stod(m_textColor);
196  textprop->SetColor(color, color, color);
197  }
198 
199  if(m_fontFamily == "arial")
200  {
201  textprop->SetFontFamilyToArial();
202  }
203  else if(m_fontFamily == "courier")
204  {
205  textprop->SetFontFamilyToCourier();
206  }
207  else if(m_fontFamily == "times")
208  {
209  textprop->SetFontFamilyToTimes();
210  }
211  else
212  {
213  OSLM_FATAL("Unknown font family : '" << m_fontFamily << "'");
214  }
215 }
216 
217 //-----------------------------------------------------------------------------
218 
219 void SText::updateText()
220 {
221  ::fwData::Object::csptr obj = this->getInput< ::fwData::Object >(s_OBJECT_IN);
222 
223  if(obj)
224  {
225  ::fwData::GenericFieldBase::csptr field;
226 
227  if(m_path.empty())
228  {
229  field = ::fwData::GenericFieldBase::dynamicCast(obj);
230  }
231  else
232  {
233  field = ::fwDataCamp::getObject< ::fwData::GenericFieldBase >(obj, m_path);
234  }
235 
236  if(field)
237  {
238  m_text = field->toString();
239  }
240  }
241 }
242 
243 //-----------------------------------------------------------------------------
244 
245 void SText::setText(const std::string& str)
246 {
247  m_text = str;
248  m_mapper->SetInput(m_text.c_str());
249  this->setAlignment();
250  this->setVtkPipelineModified();
251 }
252 
253 //-----------------------------------------------------------------------------
254 
255 fwServices::IService::KeyConnectionsMap SText::getAutoConnections() const
256 {
257  KeyConnectionsMap connections;
258 
259  connections.push(s_OBJECT_IN, ::fwData::Object::s_MODIFIED_SIG, s_UPDATE_SLOT);
260 
261  return connections;
262 }
263 
264 //-----------------------------------------------------------------------------
265 
266 } //namespace visuVTKAdaptor
This class is a helper to define the connections of a service and its data.
Definition: IService.hpp:454
The namespace visuVTKAdaptor contains the list of adaptors available for the generic scene...
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
#define OSLM_FATAL(message)
Definition: spyLog.hpp:285
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_MODIFIED_SIG
Key in m_signals map of signal m_sigModified.
Displays a text.
Definition: SText.hpp:68