Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkColorPropertySerializer.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 mitkColorPropertySerializer_h_included
18 #define mitkColorPropertySerializer_h_included
19 
21 #include "mitkColorProperty.h"
22 #include "mitkStringsToNumbers.h"
23 #include <mitkLocaleSwitch.h>
24 
25 namespace mitk
26 {
27  class ColorPropertySerializer : public BasePropertySerializer
28  {
29  public:
30  mitkClassMacro(ColorPropertySerializer, BasePropertySerializer);
31  itkFactorylessNewMacro(Self) itkCloneMacro(Self)
32 
33  virtual TiXmlElement *Serialize() override
34  {
35  if (const ColorProperty *prop = dynamic_cast<const ColorProperty *>(m_Property.GetPointer()))
36  {
37  LocaleSwitch localeSwitch("C");
38 
39  auto element = new TiXmlElement("color");
40  Color color = prop->GetValue();
41  element->SetAttribute("r", boost::lexical_cast<std::string>(color[0]));
42  element->SetAttribute("g", boost::lexical_cast<std::string>(color[1]));
43  element->SetAttribute("b", boost::lexical_cast<std::string>(color[2]));
44  return element;
45  }
46  else
47  return nullptr;
48  }
49 
50  virtual BaseProperty::Pointer Deserialize(TiXmlElement *element) override
51  {
52  if (!element)
53  return nullptr;
54 
55  LocaleSwitch localeSwitch("C");
56 
57  std::string c_string[3];
58  if (element->QueryStringAttribute("r", &c_string[0]) != TIXML_SUCCESS)
59  return nullptr;
60  if (element->QueryStringAttribute("g", &c_string[1]) != TIXML_SUCCESS)
61  return nullptr;
62  if (element->QueryStringAttribute("b", &c_string[2]) != TIXML_SUCCESS)
63  return nullptr;
64  Color c;
65  try
66  {
67  StringsToNumbers<double>(3, c_string, c);
68  }
69  catch (boost::bad_lexical_cast &e)
70  {
71  MITK_ERROR << "Could not parse string as number: " << e.what();
72  return nullptr;
73  }
74 
75  return ColorProperty::New(c).GetPointer();
76  }
77 
78  protected:
79  ColorPropertySerializer() {}
80  virtual ~ColorPropertySerializer() {}
81  };
82 
83 } // namespace
84 
85 // important to put this into the GLOBAL namespace (because it starts with 'namespace mitk')
86 MITK_REGISTER_SERIALIZER(ColorPropertySerializer);
87 
88 #endif
#define MITK_ERROR
Definition: mitkLogMacros.h:24
static Pointer New()
DataCollection - Class to facilitate loading/accessing structured data.
itk::SmartPointer< Self > Pointer
#define mitkClassMacro(className, SuperClassName)
Definition: mitkCommon.h:44
itk::RGBPixel< float > Color
Color Standard RGB color typedef (float)
MITK_REGISTER_SERIALIZER(ColorPropertySerializer)