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