File: PresetManager.hxx

package info (click to toggle)
itksnap 3.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 10,196 kB
  • ctags: 9,196
  • sloc: cpp: 62,895; sh: 175; makefile: 13
file content (151 lines) | stat: -rw-r--r-- 4,398 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "PresetManager.h"
#include "SystemInterface.h"
#include "IRISException.h"
#include <algorithm>

template <class TManagedObjectTraits>
PresetManager<TManagedObjectTraits>
::PresetManager()
{
  m_System = NULL;
}

template <class TManagedObjectTraits>
void
PresetManager<TManagedObjectTraits>
::Initialize(SystemInterface *si)
{
  // Store the system interface pointer
  m_System = si;
  m_Category = TManagedObjectTraits::GetPresetCategoryName().c_str();

  // Create all the system presets
  m_PresetMap.clear();
  m_PresetSystem.clear();
  for(SystemPresetIterator i = TManagedObjectTraits::SystemPresetBegin();
      i != TManagedObjectTraits::SystemPresetEnd();
      i++)
    {
    std::string name = TManagedObjectTraits::GetSystemPresetName(i);
    ManagedTypePtr mtp = ManagedType::New();
    TManagedObjectTraits::SetToSystemPreset(mtp, i);
    m_PresetMap[name] = mtp;
    m_PresetSystem.push_back(name);
    }

  // Load all the user preset names
  m_PresetUser = m_System->GetSavedObjectNames(m_Category.c_str());

  // Load each of the presets from the registry
  for(int j = 0; j < m_PresetUser.size(); j++)
    {
    Registry reg = m_System->ReadSavedObject(m_Category.c_str(), m_PresetUser[j].c_str());
    ManagedTypePtr mtp = ManagedType::New();
    TManagedObjectTraits::ReadFromRegistry(mtp, reg);
    m_PresetMap[m_PresetUser[j]] = mtp;
    }

  this->Modified();
}

template <class TManagedObjectTraits>
typename PresetManager<TManagedObjectTraits>::PresetMatch
PresetManager<TManagedObjectTraits>
::QueryPreset(ManagedType *instance)
{
  // TODO: we currently employ brute force linear search. This is probably
  // just fine, but could be done more cleanly using some sort of a hash key
  std::vector<std::string>::const_iterator it;

  for(it = m_PresetSystem.begin(); it != m_PresetSystem.end(); it++)
    {
    if(*m_PresetMap[*it] == *instance)
      return std::make_pair(PRESET_SYSTEM, *it);
    }

  for(it = m_PresetUser.begin(); it != m_PresetUser.end(); it++)
    {
    if(*m_PresetMap[*it] == *instance)
      return std::make_pair(PRESET_USER, *it);
    }

  return std::make_pair(PRESET_NONE, std::string());
}

template <class TManagedObjectTraits>
void
PresetManager<TManagedObjectTraits>
::SetToPreset(ManagedType *instance, const std::string &preset)
{
  typename PresetMap::iterator it = m_PresetMap.find(preset);
  if(it == m_PresetMap.end())
    throw IRISException("Preset %s not found in category %s", preset.c_str(),
                        m_Category.c_str());
  instance->CopyInformation(it->second);
}

template <class TManagedObjectTraits>
void
PresetManager<TManagedObjectTraits>
::SaveAsPreset(ManagedType *instance, const std::string &preset)
{
  // Check that the name is not used for a system preset
  if(std::find(m_PresetSystem.begin(), m_PresetSystem.end(), preset) != m_PresetSystem.end())
    throw IRISException(
        "%s is not a valid user preset name. It conflicts with a system preset",
        preset.c_str());

  // Assign as a user preset
  if(std::find(m_PresetUser.begin(), m_PresetUser.end(), preset)  == m_PresetUser.end())
    m_PresetUser.push_back(preset);

  // Store the preset in memory
  ManagedTypePtr mtp = ManagedType::New();
  mtp->CopyInformation(instance);
  m_PresetMap[preset] = mtp;

  // Write the preset to disk
  Registry reg;
  TManagedObjectTraits::WriteToRegistry(instance, reg);
  m_System->UpdateSavedObject(m_Category.c_str(), preset.c_str(), reg);

  this->Modified();
}

template <class TManagedObjectTraits>
void
PresetManager<TManagedObjectTraits>
::DeletePreset(const std::string &preset)
{
  // Assign as a user preset
  std::vector<std::string>::iterator it =
      std::find(m_PresetUser.begin(), m_PresetUser.end(), preset);

  if(it != m_PresetUser.end())
    {
    // Delete the preset from the file system
    m_System->DeleteSavedObject(m_Category.c_str(), preset.c_str());

    // Also delete it from the list of user presets
    m_PresetUser.erase(it);
    }

  this->Modified();
}


template <class TManagedObjectTraits>
typename PresetManager<TManagedObjectTraits>::ManagedType *
PresetManager<TManagedObjectTraits>
::GetPreset(const std::string &preset)
{
  return m_PresetMap[preset];
}

template <class TManagedObjectTraits>
bool
PresetManager<TManagedObjectTraits>
::IsValidPreset(const std::string &preset)
{
  return m_PresetMap.find(preset) != m_PresetMap.end();
}