Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkTextAnnotation2D.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "mitkTextAnnotation2D.h"
14 #include "vtkUnicodeString.h"
15 #include <vtkPropAssembly.h>
16 #include <vtkTextActor.h>
17 #include <vtkTextProperty.h>
18 
20 {
21  mitk::Point2D position;
22  position[0] = position[1] = 0;
23  this->SetPosition2D(position);
24  this->SetOffsetVector(position);
25  this->SetText("");
26  this->SetFontSize(20);
27  this->SetColor(1.0, 1.0, 1.0);
28  this->SetStringProperty("font.family", "Arial");
29  this->SetBoolProperty("font.bold", false);
30  this->SetBoolProperty("font.italic", false);
31  this->SetBoolProperty("drawShadow", false);
32 }
33 
35 {
36  for (BaseRenderer *renderer : m_LSH.GetRegisteredBaseRenderer())
37  {
38  if (renderer)
39  {
40  this->RemoveFromBaseRenderer(renderer);
41  }
42  }
43 }
44 
46 {
47  LocalStorage *ls = this->m_LSH.GetLocalStorage(renderer);
49  bounds.Position = ls->m_TextActor->GetPosition();
50 
51  double size[2];
52  ls->m_TextActor->GetSize(renderer->GetVtkRenderer(), size);
53  bounds.Size[0] = size[0];
54  bounds.Size[1] = size[1];
55  return bounds;
56 }
57 
59 {
60  LocalStorage *ls = this->m_LSH.GetLocalStorage(renderer);
61 
62  mitk::Point2D posT, posS;
63  posT[0] = bounds.Position[0];
64  posT[1] = bounds.Position[1];
65  posS[0] = posT[0] + 1;
66  posS[1] = posT[1] - 1;
67 
68  ls->m_TextActor->SetDisplayPosition(posT[0], posT[1]);
69  ls->m_STextActor->SetDisplayPosition(posS[0], posS[1]);
70 }
71 
73 {
74 }
75 
77 {
78  m_TextActor = vtkSmartPointer<vtkTextActor>::New();
79  m_TextProp = vtkSmartPointer<vtkTextProperty>::New();
80  m_STextActor = vtkSmartPointer<vtkTextActor>::New();
81  m_STextProp = vtkSmartPointer<vtkTextProperty>::New();
82  m_TextActor->SetTextProperty(m_TextProp);
83  m_STextActor->SetTextProperty(m_STextProp);
84  m_Assembly = vtkSmartPointer<vtkPropAssembly>::New();
85  m_Assembly->AddPart(m_STextActor);
86  m_Assembly->AddPart(m_TextActor);
87 }
88 
90 {
91  LocalStorage *ls = this->m_LSH.GetLocalStorage(renderer);
92 
93  if (ls->IsGenerateDataRequired(renderer, this))
94  {
95  float color[3] = {0.0, 1.0, 0.0};
96  float opacity = 1.0;
97  GetColor(color);
98  GetOpacity(opacity);
99  ls->m_TextProp->SetColor(color[0], color[1], color[2]);
100  ls->m_STextProp->SetColor(0, 0, 0);
101  ls->m_TextProp->SetFontSize(GetFontSize());
102  ls->m_TextProp->SetOpacity(opacity);
103  ls->m_STextProp->SetFontSize(GetFontSize());
104  ls->m_STextProp->SetOpacity(opacity);
105 
106  std::string fontFamilyAsString;
107  if (GetStringProperty("font.family", fontFamilyAsString) == false)
108  {
109  fontFamilyAsString = "Arial";
110  }
111  ls->m_TextProp->SetFontFamilyAsString(fontFamilyAsString.c_str());
112  ls->m_STextProp->SetFontFamilyAsString(fontFamilyAsString.c_str());
113 
114  bool boldFont(false);
115  GetBoolProperty("font.bold", boldFont);
116  ls->m_TextProp->SetBold(boldFont);
117  ls->m_STextProp->SetBold(boldFont);
118 
119  bool italicFont(false);
120  GetBoolProperty("font.italic", italicFont);
121  ls->m_TextProp->SetBold(italicFont);
122  ls->m_STextProp->SetBold(italicFont);
123 
124  bool drawShadow;
125  GetBoolProperty("drawShadow", drawShadow);
126  ls->m_TextProp->SetShadow(false);
127  ls->m_STextProp->SetShadow(false);
128  ls->m_STextActor->SetVisibility(drawShadow);
129 
130  ls->m_TextActor->SetInput(GetText().c_str());
131  ls->m_STextActor->SetInput(GetText().c_str());
132 
133  mitk::Point2D posT, posS;
134  posT[0] = GetPosition2D()[0] + GetOffsetVector()[0];
135  posT[1] = GetPosition2D()[1] + GetOffsetVector()[1];
136  posS[0] = posT[0] + 1;
137  posS[1] = posT[1] - 1;
138 
139  ls->m_TextActor->SetDisplayPosition(posT[0], posT[1]);
140  ls->m_STextActor->SetDisplayPosition(posS[0], posS[1]);
142  }
143 }
144 
146 {
147  LocalStorage *ls = this->m_LSH.GetLocalStorage(renderer);
148  return ls->m_Assembly;
149 }
150 
152 {
153  LocalStorage *ls = this->m_LSH.GetLocalStorage(renderer);
154  return ls->m_TextActor;
155 }
#define ls
Definition: MitkMCxyz.cpp:57
L * GetLocalStorage(mitk::BaseRenderer *forRenderer)
Retrieves a LocalStorage for a specific BaseRenderer.
void SetPosition2D(const Point2D &position2D)
void SetText(std::string text)
Point2D GetOffsetVector() const
std::vector< mitk::BaseRenderer * > GetRegisteredBaseRenderer()
Container for position and size on the display.
vtkRenderer * GetVtkRenderer() const
TextAnnotation2D()
explicit constructor which disallows implicit conversions
vtkSmartPointer< vtkTextActor > m_TextActor
Actor of a 2D render window.
itk::Point< double, 2 > Size
Organizes the rendering process.
bool IsGenerateDataRequired(mitk::BaseRenderer *renderer, mitk::Annotation *Annotation)
LocalStorage()
Default constructor of the local storage.
vtkProp * GetVtkProp(BaseRenderer *renderer) const override
This method is implemented by the specific VTKAnnotation in order to create the element as a vtkProp...
void SetBoundsOnDisplay(BaseRenderer *renderer, const Bounds &bounds) override
Sets position and size of the Annotation on the display.
void UpdateVtkAnnotation2D(mitk::BaseRenderer *renderer) override
void SetColor(const mitk::Color &color, const std::string &propertyKey="color")
Convenience method for setting color properties (instances of ColorProperty)
itk::Point< double, 2 > Position
Annotation::Bounds GetBoundsOnDisplay(BaseRenderer *renderer) const override
Returns position and size of the Annotation on the display.
~LocalStorage()
Default deconstructor of the local storage.
mitk::LocalStorageHandler< LocalStorage > m_LSH
The LocalStorageHandler holds all LocalStorages for the render windows.
bool GetColor(float rgb[], const std::string &propertyKey="color") const
Convenience access method for color properties (instances of ColorProperty)
void SetStringProperty(const std::string &propertyKey, const std::string &string)
Convenience method for setting int properties (instances of IntProperty)
vtkActor2D * GetVtkActor2D(BaseRenderer *renderer) const override
int GetFontSize() const
void RemoveFromBaseRenderer(BaseRenderer *renderer) override
Removes the Annotation from the specified renderer. It is not visible anymore then.
void SetBoolProperty(const std::string &propertyKey, bool boolValue)
Convenience method for setting int properties (instances of IntProperty)
std::string GetText() const
vtkSmartPointer< vtkTextProperty > m_STextProp
bool GetOpacity(float &opacity, const std::string &propertyKey="opacity") const
Convenience access method for opacity properties (instances of FloatProperty)
bool GetBoolProperty(const std::string &propertyKey, bool &boolValue) const
Convenience access method for bool properties (instances of BoolProperty)
bool GetStringProperty(const std::string &propertyKey, std::string &string) const
Convenience access method for string properties (instances of StringProperty)
vtkSmartPointer< vtkTextActor > m_STextActor
~TextAnnotation2D() override
virtual destructor in order to derive from this class
void SetFontSize(int fontSize)
vtkSmartPointer< vtkPropAssembly > m_Assembly
void SetOffsetVector(const Point2D &OffsetVector)
vtkSmartPointer< vtkTextProperty > m_TextProp