Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3
Medical Imaging Interaction Toolkit
mitkAnnotation.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 "mitkAnnotation.h"
14 #include "usGetModuleContext.h"
15 
16 const std::string mitk::Annotation::US_INTERFACE_NAME = "org.mitk.services.Annotation";
17 const std::string mitk::Annotation::US_PROPKEY_AnnotationNAME = US_INTERFACE_NAME + ".name";
18 const std::string mitk::Annotation::US_PROPKEY_ID = US_INTERFACE_NAME + ".id";
19 const std::string mitk::Annotation::US_PROPKEY_MODIFIED = US_INTERFACE_NAME + ".modified";
20 const std::string mitk::Annotation::US_PROPKEY_RENDERER_ID = US_INTERFACE_NAME + ".rendererId";
21 const std::string mitk::Annotation::US_PROPKEY_AR_ID = US_INTERFACE_NAME + ".arId";
22 
23 mitk::Annotation::Annotation() : m_PropertyListModifiedObserverTag(0)
24 {
26  itk::MemberCommand<mitk::Annotation>::Pointer _PropertyListModifiedCommand =
27  itk::MemberCommand<mitk::Annotation>::New();
28  _PropertyListModifiedCommand->SetCallbackFunction(this, &mitk::Annotation::PropertyListModified);
29  m_PropertyListModifiedObserverTag = m_PropertyList->AddObserver(itk::ModifiedEvent(), _PropertyListModifiedCommand);
30  this->SetName(this->GetNameOfClass());
31  this->SetVisibility(true);
32  this->SetOpacity(1.0);
33 }
34 
35 void mitk::Annotation::PropertyListModified(const itk::Object * /*caller*/, const itk::EventObject &)
36 {
38 }
39 
41 {
42  this->UnRegisterMicroservice();
43 }
44 
45 void mitk::Annotation::SetUSProperty(const std::string &propertyKey, us::Any value)
46 {
47  if (this->m_ServiceRegistration)
48  {
50  std::vector<std::string> propertyKeys;
51  m_ServiceRegistration.GetReference().GetPropertyKeys(propertyKeys);
52  for (std::string key : propertyKeys)
53  {
54  props[key] = m_ServiceRegistration.GetReference().GetProperty(key);
55  }
56  props[propertyKey] = value;
57  m_ServiceRegistration.SetProperties(props);
58  }
59 }
60 
61 void mitk::Annotation::SetProperty(const std::string &propertyKey, const BaseProperty::Pointer &propertyValue)
62 {
63  this->m_PropertyList->SetProperty(propertyKey, propertyValue);
64 }
65 
66 void mitk::Annotation::ReplaceProperty(const std::string &propertyKey, const BaseProperty::Pointer &propertyValue)
67 {
68  this->m_PropertyList->ReplaceProperty(propertyKey, propertyValue);
69 }
70 
71 void mitk::Annotation::AddProperty(const std::string &propertyKey,
72  const BaseProperty::Pointer &propertyValue,
73  bool overwrite)
74 {
75  if ((overwrite) || (GetProperty(propertyKey) == nullptr))
76  {
77  SetProperty(propertyKey, propertyValue);
78  }
79 }
80 
82 {
83  m_PropertyList->ConcatenatePropertyList(pList, replace);
84 }
85 
86 mitk::BaseProperty *mitk::Annotation::GetProperty(const std::string &propertyKey) const
87 {
88  mitk::BaseProperty::Pointer property = m_PropertyList->GetProperty(propertyKey);
89  if (property.IsNotNull())
90  return property;
91 
92  // only to satisfy compiler!
93  return nullptr;
94 }
95 
96 bool mitk::Annotation::GetBoolProperty(const std::string &propertyKey, bool &boolValue) const
97 {
98  mitk::BoolProperty::Pointer boolprop = dynamic_cast<mitk::BoolProperty *>(GetProperty(propertyKey));
99  if (boolprop.IsNull())
100  return false;
101 
102  boolValue = boolprop->GetValue();
103  return true;
104 }
105 
106 bool mitk::Annotation::GetIntProperty(const std::string &propertyKey, int &intValue) const
107 {
108  mitk::IntProperty::Pointer intprop = dynamic_cast<mitk::IntProperty *>(GetProperty(propertyKey));
109  if (intprop.IsNull())
110  return false;
111 
112  intValue = intprop->GetValue();
113  return true;
114 }
115 
116 bool mitk::Annotation::GetFloatProperty(const std::string &propertyKey, float &floatValue) const
117 {
118  mitk::FloatProperty::Pointer floatprop = dynamic_cast<mitk::FloatProperty *>(GetProperty(propertyKey));
119  if (floatprop.IsNull())
120  return false;
121 
122  floatValue = floatprop->GetValue();
123  return true;
124 }
125 
126 bool mitk::Annotation::GetStringProperty(const std::string &propertyKey, std::string &string) const
127 {
128  mitk::StringProperty::Pointer stringProp = dynamic_cast<mitk::StringProperty *>(GetProperty(propertyKey));
129  if (stringProp.IsNull())
130  {
131  return false;
132  }
133  else
134  {
135  // memcpy((void*)string, stringProp->GetValue(), strlen(stringProp->GetValue()) + 1 ); // looks dangerous
136  string = stringProp->GetValue();
137  return true;
138  }
139 }
140 
141 void mitk::Annotation::SetIntProperty(const std::string &propertyKey, int intValue)
142 {
143  this->m_PropertyList->SetProperty(propertyKey, mitk::IntProperty::New(intValue));
144  Modified();
145 }
146 void mitk::Annotation::SetBoolProperty(const std::string &propertyKey, bool boolValue)
147 {
148  this->m_PropertyList->SetProperty(propertyKey, mitk::BoolProperty::New(boolValue));
149  Modified();
150 }
151 
152 void mitk::Annotation::SetFloatProperty(const std::string &propertyKey, float floatValue)
153 {
154  this->m_PropertyList->SetProperty(propertyKey, mitk::FloatProperty::New(floatValue));
155  Modified();
156 }
157 
158 void mitk::Annotation::SetDoubleProperty(const std::string &propertyKey, double doubleValue)
159 {
160  this->m_PropertyList->SetProperty(propertyKey, mitk::DoubleProperty::New(doubleValue));
161  Modified();
162 }
163 
164 void mitk::Annotation::SetStringProperty(const std::string &propertyKey, const std::string &stringValue)
165 {
166  this->m_PropertyList->SetProperty(propertyKey, mitk::StringProperty::New(stringValue));
167  Modified();
168 }
169 
170 std::string mitk::Annotation::GetName() const
171 {
172  mitk::StringProperty *sp = dynamic_cast<mitk::StringProperty *>(this->GetProperty("name"));
173  if (sp == nullptr)
174  return "";
175  return sp->GetValue();
176 }
177 
178 void mitk::Annotation::SetName(const std::string &name)
179 {
180  this->SetStringProperty("name", name);
181 }
182 
183 bool mitk::Annotation::GetName(std::string &nodeName, const std::string &propertyKey) const
184 {
185  return GetStringProperty(propertyKey, nodeName);
186 }
187 
188 void mitk::Annotation::SetText(std::string text)
189 {
190  SetStringProperty("Text", text.c_str());
191 }
192 
193 std::string mitk::Annotation::GetText() const
194 {
195  std::string text;
196  GetStringProperty("Text", text);
197  return text;
198 }
199 
201 {
202  SetIntProperty("FontSize", fontSize);
203 }
204 
206 {
207  int fontSize = 1;
208  GetIntProperty("FontSize", fontSize);
209  return fontSize;
210 }
211 
212 bool mitk::Annotation::GetVisibility(bool &visible, const std::string &propertyKey) const
213 {
214  return GetBoolProperty(propertyKey, visible);
215 }
216 
217 bool mitk::Annotation::IsVisible(const std::string &propertyKey, bool defaultIsOn) const
218 {
219  return IsOn(propertyKey, defaultIsOn);
220 }
221 
222 bool mitk::Annotation::GetColor(float rgb[], const std::string &propertyKey) const
223 {
224  mitk::ColorProperty::Pointer colorprop = dynamic_cast<mitk::ColorProperty *>(GetProperty(propertyKey));
225  if (colorprop.IsNull())
226  return false;
227 
228  memcpy(rgb, colorprop->GetColor().GetDataPointer(), 3 * sizeof(float));
229  return true;
230 }
231 
232 void mitk::Annotation::SetColor(const mitk::Color &color, const std::string &propertyKey)
233 {
235  prop = mitk::ColorProperty::New(color);
236  this->m_PropertyList->SetProperty(propertyKey, prop);
237 }
238 
239 void mitk::Annotation::SetColor(float red, float green, float blue, const std::string &propertyKey)
240 {
241  float color[3];
242  color[0] = red;
243  color[1] = green;
244  color[2] = blue;
245  SetColor(color, propertyKey);
246 }
247 
248 void mitk::Annotation::SetColor(const float rgb[], const std::string &propertyKey)
249 {
251  prop = mitk::ColorProperty::New(rgb);
252  this->m_PropertyList->SetProperty(propertyKey, prop);
253 }
254 
255 bool mitk::Annotation::GetOpacity(float &opacity, const std::string &propertyKey) const
256 {
257  mitk::FloatProperty::Pointer opacityprop = dynamic_cast<mitk::FloatProperty *>(GetProperty(propertyKey));
258  if (opacityprop.IsNull())
259  return false;
260 
261  opacity = opacityprop->GetValue();
262  return true;
263 }
264 
265 void mitk::Annotation::SetOpacity(float opacity, const std::string &propertyKey)
266 {
268  prop = mitk::FloatProperty::New(opacity);
269  this->m_PropertyList->SetProperty(propertyKey, prop);
270 }
271 
272 void mitk::Annotation::SetVisibility(bool visible, const std::string &propertyKey)
273 {
275  prop = mitk::BoolProperty::New(visible);
276  this->m_PropertyList->SetProperty(propertyKey, prop);
277  Modified();
278 }
279 
282 {
283  if (m_LastGenerateDataTime < Annotation->GetMTime())
284  return true;
285 
286  if (m_LastGenerateDataTime < Annotation->GetPropertyList()->GetMTime())
287  return true;
288 
289  if (renderer && m_LastGenerateDataTime < renderer->GetTimeStepUpdateTime())
290  return true;
291 
292  return false;
293 }
294 
296 {
298  bounds.Position[0] = bounds.Position[1] = bounds.Size[0] = bounds.Size[1] = 0;
299  return bounds;
300 }
301 
303 {
304 }
305 
306 void mitk::Annotation::SetForceInForeground(bool forceForeground)
307 {
308  m_ForceInForeground = forceForeground;
309 }
310 
312 {
313  return m_ForceInForeground;
314 }
315 
317 {
318  return m_PropertyList;
319 }
320 
322 {
323  return this->m_ServiceRegistration.GetReference().GetProperty(US_PROPKEY_ID).ToString();
324 }
325 
327 {
328  if (m_ServiceRegistration != nullptr)
329  m_ServiceRegistration.Unregister();
330  us::ModuleContext *context = us::GetModuleContext();
331  // Define ServiceProps
332  mitk::UIDGenerator uidGen = mitk::UIDGenerator("org.mitk.services.Annotation.id_", 16);
333  props[US_PROPKEY_ID] = uidGen.GetUID();
334  m_ServiceRegistration = context->RegisterService(this, props);
335 }
336 
338 {
339  if (m_ServiceRegistration != nullptr)
340  m_ServiceRegistration.Unregister();
341  m_ServiceRegistration = 0;
342 }
343 
345 {
346  Modified();
347  this->SetUSProperty(US_PROPKEY_MODIFIED, this->GetMTime());
348 }
ServiceReference< I1 > GetReference(InterfaceType< I1 >) const
virtual std::string GetName() const
Extra convenience access method for accessing the name of an object (instance of StringProperty with ...
void SetProperty(const std::string &propertyKey, const BaseProperty::Pointer &property)
Set the property (instance of BaseProperty) with key propertyKey in the PropertyList of the renderer ...
static Pointer New()
void SetVisibility(bool visible, const std::string &propertyKey="visible")
Convenience method for setting visibility properties (instances of BoolProperty)
void SetText(std::string text)
static Pointer New()
mitk::BaseProperty * GetProperty(const std::string &propertyKey) const
Get the property (instance of BaseProperty) with key propertyKey from the PropertyList of the rendere...
Generated unique IDs.
void AddProperty(const std::string &propertyKey, const BaseProperty::Pointer &property, bool overwrite=false)
Add the property (instance of BaseProperty) if it does not exist (or always if overwrite is true) wit...
virtual const char * GetValue() const
signed integer value
Definition: jsoncpp.h:348
Container for position and size on the display.
void SetUSProperty(const std::string &propertyKey, us::Any value)
bool GetFloatProperty(const std::string &propertyKey, float &floatValue) const
Convenience access method for float properties (instances of FloatProperty)
virtual void RegisterAsMicroservice(us::ServiceProperties props)
Registers this object as a Microservice, making it available to every module and/or plugin...
PropertyList::Pointer m_PropertyList
BaseRenderer-independent PropertyList.
bool IsOn(const std::string &propertyKey, bool defaultIsOn=true) const
Convenience access method for boolean properties (instances of BoolProperty). Return value is the val...
itk::Point< double, 2 > Size
static Pointer New()
static const std::string US_PROPKEY_ID
bool IsVisible(const std::string &propertyKey="visible", bool defaultIsOn=true) const
Convenience access method for visibility properties (instances of BoolProperty). Return value is the ...
Organizes the rendering process.
bool IsGenerateDataRequired(mitk::BaseRenderer *renderer, mitk::Annotation *Annotation)
Key-value list holding instances of BaseProperty.
static const std::string US_INTERFACE_NAME
These Constants are used in conjunction with Microservices.
bool GetVisibility(bool &visible, const std::string &propertyKey="visible") const
Convenience access method for visibility properties (instances of BoolProperty with property-key "vis...
virtual void UnRegisterMicroservice()
Registers this object as a Microservice, making it available to every module and/or plugin...
void SetColor(const mitk::Color &color, const std::string &propertyKey="color")
Convenience method for setting color properties (instances of ColorProperty)
static Pointer New()
void SetDoubleProperty(const std::string &propertyKey, double doubleValue)
Convenience method for setting int properties (instances of IntProperty)
The ColorProperty class RGB color property.
static const std::string US_PROPKEY_AnnotationNAME
virtual Bounds GetBoundsOnDisplay(BaseRenderer *renderer) const
Returns position and size of the Annotation on the display.
Abstract base class for properties.
itk::Point< double, 2 > Position
void ConcatenatePropertyList(PropertyList *pList, bool replace=false)
Add values from another PropertyList.
std::string GetMicroserviceID()
Returns the id that this device is registered with. The id will only be valid, if the Annotation has ...
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)
Definition: usAny.h:163
void SetOpacity(float opacity, const std::string &propertyKey="opacity")
Convenience method for setting opacity properties (instances of FloatProperty)
int GetFontSize() const
void SetFloatProperty(const std::string &propertyKey, float floatValue)
Convenience method for setting int properties (instances of IntProperty)
void ReplaceProperty(const std::string &propertyKey, const BaseProperty::Pointer &property)
Replace the property (instance of BaseProperty) with key propertyKey in the PropertyList of the rende...
Annotation()
explicit constructor which disallows implicit conversions
itk::RGBPixel< float > Color
Color Standard RGB color typedef (float)
US_UNORDERED_MAP_TYPE< std::string, Any > ServiceProperties
static Pointer New()
void SetBoolProperty(const std::string &propertyKey, bool boolValue)
Convenience method for setting int properties (instances of IntProperty)
Base class for all Annotation This class is to be implemented in order to create Annotation which are...
static const std::string US_PROPKEY_RENDERER_ID
static Pointer New()
static const std::string US_PROPKEY_AR_ID
UTF-8 string value.
Definition: jsoncpp.h:351
std::string GetText() const
virtual void SetName(const std::string &name)
Extra convenience access method to set the name of an object.
bool GetOpacity(float &opacity, const std::string &propertyKey="opacity") const
Convenience access method for opacity properties (instances of FloatProperty)
static const char * replace[]
This is a dictionary to replace long names of classes, modules, etc. to shorter versions in the conso...
~Annotation() override
virtual destructor in order to derive from this class
void SetProperties(const ServiceProperties &properties)
bool GetBoolProperty(const std::string &propertyKey, bool &boolValue) const
Convenience access method for bool properties (instances of BoolProperty)
Property for strings.
bool GetStringProperty(const std::string &propertyKey, std::string &string) const
Convenience access method for string properties (instances of StringProperty)
PropertyList * GetPropertyList() const
void SetFontSize(int fontSize)
void SetIntProperty(const std::string &propertyKey, int intValue)
Convenience method for setting int properties (instances of IntProperty)
virtual void SetBoundsOnDisplay(BaseRenderer *renderer, const Bounds &)
Sets position and size of the Annotation on the display.
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
void SetForceInForeground(bool forceForeground)
bool IsForceInForeground() const
static Pointer New()
bool GetIntProperty(const std::string &propertyKey, int &intValue) const
Convenience access method for int properties (instances of IntProperty)
static const std::string US_PROPKEY_MODIFIED