Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3
Medical Imaging Interaction Toolkit
mitkLevelWindowPreset.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 "mitkLevelWindowPreset.h"
14 #include <vtkObjectFactory.h>
15 
16 #include "usGetModuleContext.h"
17 #include "usModule.h"
18 #include "usModuleContext.h"
19 #include "usModuleResource.h"
20 #include "usModuleResourceStream.h"
21 
22 namespace mitk
23 {
24  const std::string LevelWindowPreset::PRESET = "preset";
25 
26  vtkStandardNewMacro(LevelWindowPreset);
27 
31  {
32  us::ModuleResource presetResource = us::GetModuleContext()->GetModule()->GetResource("mitkLevelWindowPresets.xml");
33  if (!presetResource)
34  return false;
35 
36  us::ModuleResourceStream presetStream(presetResource);
37  vtkXMLParser::SetStream(&presetStream);
38  if (!vtkXMLParser::Parse())
39  {
40 #ifdef INTERDEBUG
41  MITK_INFO << "LevelWindowPreset::LoadPreset xml file cannot parse!" << std::endl;
42 #endif
43  return false;
44  }
45  return true;
46  }
47 
48  bool LevelWindowPreset::LoadPreset(std::string fileName)
49  {
50  if (fileName.empty())
51  return false;
52 
53  vtkXMLParser::SetFileName(fileName.c_str());
54 
55  if (!vtkXMLParser::Parse())
56  {
57 #ifdef INTERDEBUG
58  MITK_INFO << "LevelWindowPreset::LoadPreset xml file cannot parse!" << std::endl;
59 #endif
60  }
61 
62  return true;
63  }
64 
65  void LevelWindowPreset::StartElement(const char *elementName, const char **atts)
66  {
67  std::string elementNameString = elementName;
68  if (elementNameString == PRESET)
69  {
70  std::string name = ReadXMLStringAttribut("NAME", atts);
71  std::string level = ReadXMLStringAttribut("LEVEL", atts);
72  double lev = atof(level.c_str());
73  std::string window = ReadXMLStringAttribut("WINDOW", atts);
74  double win = atof(window.c_str());
75  m_Level[name] = lev;
76  m_Window[name] = win;
77  }
78  }
79 
80  std::string LevelWindowPreset::ReadXMLStringAttribut(std::string name, const char **atts)
81  {
82  if (atts)
83  {
84  const char **attsIter = atts;
85 
86  while (*attsIter)
87  {
88  if (name == *attsIter)
89  {
90  attsIter++;
91  return *attsIter;
92  }
93  attsIter++;
94  attsIter++;
95  }
96  }
97 
98  return std::string();
99  }
100 
101  double LevelWindowPreset::getLevel(std::string name) { return m_Level[name]; }
102  double LevelWindowPreset::getWindow(std::string name) { return m_Window[name]; }
103  std::map<std::string, double> &LevelWindowPreset::getLevelPresets() { return m_Level; }
104  std::map<std::string, double> &LevelWindowPreset::getWindowPresets() { return m_Window; }
105  void LevelWindowPreset::save()
106  {
107  // XMLWriter writer(m_XmlFileName.c_str());
108  // saveXML(writer);
109  }
110 
111  void LevelWindowPreset::newPresets(std::map<std::string, double> newLevel, std::map<std::string, double> newWindow)
112  {
113  m_Level = newLevel;
114  m_Window = newWindow;
115  save();
116  }
117 }
#define MITK_INFO
Definition: mitkLogMacros.h:18
DataCollection - Class to facilitate loading/accessing structured data.
std::map< std::string, double > & getLevelPresets()
Module * GetModule() const
void newPresets(std::map< std::string, double > newLevel, std::map< std::string, double > newWindow)
vtkStandardNewMacro(AnatomicalStructureColorPresets)
double getWindow(std::string window)
std::map< std::string, double > & getWindowPresets()
double getLevel(std::string name)
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.