Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkLevelWindowPropertySerializer.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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #ifndef mitkLevelWindowPropertySerializer_h_included
18 #define mitkLevelWindowPropertySerializer_h_included
19 
22 #include <boost/lexical_cast.hpp>
23 #include <mitkLocaleSwitch.h>
24 
25 namespace mitk
26 {
27  class LevelWindowPropertySerializer : public BasePropertySerializer
28  {
29  public:
30  mitkClassMacro(LevelWindowPropertySerializer, BasePropertySerializer);
31  itkFactorylessNewMacro(Self) itkCloneMacro(Self)
32 
33  virtual TiXmlElement *Serialize() override
34  {
35  if (const LevelWindowProperty *prop = dynamic_cast<const LevelWindowProperty *>(m_Property.GetPointer()))
36  {
37  LocaleSwitch localeSwitch("C");
38 
39  auto element = new TiXmlElement("LevelWindow");
40 
41  LevelWindow lw = prop->GetLevelWindow();
42  std::string boolString("false");
43  if (lw.IsFixed() == true)
44  boolString = "true";
45  element->SetAttribute("fixed", boolString.c_str());
46 
47  std::string boolStringFltImage("false");
48  if (lw.IsFloatingValues() == true)
49  boolStringFltImage = "true";
50  element->SetAttribute("isFloatingImage", boolStringFltImage.c_str());
51 
52  auto child = new TiXmlElement("CurrentSettings");
53  element->LinkEndChild(child);
54  child->SetAttribute("level", boost::lexical_cast<std::string>(lw.GetLevel()));
55  child->SetAttribute("window", boost::lexical_cast<std::string>(lw.GetWindow()));
56 
57  child = new TiXmlElement("DefaultSettings");
58  element->LinkEndChild(child);
59  child->SetAttribute("level", boost::lexical_cast<std::string>(lw.GetDefaultLevel()));
60  child->SetAttribute("window", boost::lexical_cast<std::string>(lw.GetDefaultWindow()));
61 
62  child = new TiXmlElement("CurrentRange");
63  element->LinkEndChild(child);
64  child->SetAttribute("min", boost::lexical_cast<std::string>(lw.GetRangeMin()));
65  child->SetAttribute("max", boost::lexical_cast<std::string>(lw.GetRangeMax()));
66 
67  return element;
68  }
69  else
70  return nullptr;
71  }
72 
73  virtual BaseProperty::Pointer Deserialize(TiXmlElement *element) override
74  {
75  if (!element)
76  return nullptr;
77 
78  LocaleSwitch localeSwitch("C");
79 
80  bool isFixed(false);
81  if (element->Attribute("fixed"))
82  isFixed = std::string(element->Attribute("fixed")) == "true";
83  bool isFloatingImage(false);
84  if (element->Attribute("isFloatingImage"))
85  isFloatingImage = std::string(element->Attribute("isFloatingImage")) == "true";
86 
87  std::string level_string;
88  std::string window_string;
89  TiXmlElement *child = element->FirstChildElement("CurrentSettings");
90  if (child->QueryStringAttribute("level", &level_string) != TIXML_SUCCESS)
91  return nullptr;
92  if (child->QueryStringAttribute("window", &window_string) != TIXML_SUCCESS)
93  return nullptr;
94 
95  std::string defaultLevel_string;
96  std::string defaultWindow_string;
97  child = element->FirstChildElement("DefaultSettings");
98  if (child->QueryStringAttribute("level", &defaultLevel_string) != TIXML_SUCCESS)
99  return nullptr;
100  if (child->QueryStringAttribute("window", &defaultWindow_string) != TIXML_SUCCESS)
101  return nullptr;
102 
103  std::string minRange_string;
104  std::string maxRange_string;
105  child = element->FirstChildElement("CurrentRange");
106  if (child->QueryStringAttribute("min", &minRange_string) != TIXML_SUCCESS)
107  return nullptr;
108  if (child->QueryStringAttribute("max", &maxRange_string) != TIXML_SUCCESS)
109  return nullptr;
110 
111  LevelWindow lw;
112  try
113  {
114  lw.SetRangeMinMax(boost::lexical_cast<double>(minRange_string), boost::lexical_cast<double>(maxRange_string));
115  lw.SetDefaultLevelWindow(boost::lexical_cast<double>(defaultLevel_string),
116  boost::lexical_cast<double>(defaultWindow_string));
117  lw.SetLevelWindow(boost::lexical_cast<double>(level_string), boost::lexical_cast<double>(window_string));
118  lw.SetFixed(isFixed);
119  lw.SetFloatingValues(isFloatingImage);
120  }
121  catch (boost::bad_lexical_cast &e)
122  {
123  MITK_ERROR << "Could not parse string as number: " << e.what();
124  return nullptr;
125  }
126  return LevelWindowProperty::New(lw).GetPointer();
127  }
128 
129  protected:
130  LevelWindowPropertySerializer() {}
131  virtual ~LevelWindowPropertySerializer() {}
132  };
133 
134 } // namespace
135 
136 // important to put this into the GLOBAL namespace (because it starts with 'namespace mitk')
137 MITK_REGISTER_SERIALIZER(LevelWindowPropertySerializer);
138 
139 #endif
static Pointer New()
#define MITK_ERROR
Definition: mitkLogMacros.h:24
DataCollection - Class to facilitate loading/accessing structured data.
itk::SmartPointer< Self > Pointer
MITK_REGISTER_SERIALIZER(LevelWindowPropertySerializer)
#define mitkClassMacro(className, SuperClassName)
Definition: mitkCommon.h:44