File: HistoryManager.cxx

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 (133 lines) | stat: -rw-r--r-- 4,141 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
#include "HistoryManager.h"
#include "SystemInterface.h"
#include "Registry.h"
#include "itksys/SystemTools.hxx"
#include <algorithm>

const unsigned int HistoryManager::HISTORY_SIZE_LOCAL = 5;
const unsigned int HistoryManager::HISTORY_SIZE_GLOBAL = 20;

HistoryManager::HistoryManager()
{
}

HistoryManager::ConcreteHistoryModel
*HistoryManager::GetHistory(const std::string &category, HistoryMap &hmap)
{
  // Does the history exist?
  HistoryMap::iterator it = hmap.find(category);
  if(it == hmap.end())
    {
    ConcreteHistoryModelPtr model = ConcreteHistoryModel::New();
    hmap.insert(std::make_pair(category, model));
    return model;
    }
  else return it->second;
}

void HistoryManager
::SaveHistory(Registry &folder, HistoryManager::HistoryMap &hmap)
{
  // Write all the histories to the registry
  for(HistoryMap::iterator it = hmap.begin(); it != hmap.end(); it++)
    {
    ConcreteHistoryModel *model = it->second;
    folder.Folder(it->first).PutArray(model->GetValue());
    }
}

void HistoryManager
::LoadHistory(Registry &folder, HistoryManager::HistoryMap &hmap)
{
  hmap.clear();

  // Read all the relevant histories from the file. We do this dynamically
  // although it would also have made sense to hard code here the list of
  // all histories. I guess it does not really matter.
  Registry::StringListType historyNames;
  folder.GetFolderKeys(historyNames);
  for(Registry::StringListType::iterator it = historyNames.begin();
      it != historyNames.end(); it++)
    {
    // Histories are created on demand - so we must call the get model method,
    // which will create and store the history model
    ConcreteHistoryModel *model = GetHistory(*it, hmap);
    model->SetValue(folder.Folder(*it).GetArray(std::string("")));
    }
}

void HistoryManager
::UpdateHistoryList(ConcreteHistoryModel *model, const std::string &file, unsigned int maxsize)
{
  // Get the list (passing by value here, but these lists are not huge)
  HistoryListType array = model->GetValue();

  // Remove all occurences of the file from the array
  array.erase(std::remove(array.begin(), array.end(), file), array.end());

  // Append the file to the end of the array
  array.push_back(file);

  // Trim the array to appropriate size
  if(array.size() > maxsize)
    array.erase(array.begin(), array.begin() + array.size() - maxsize);

  // Put the new array back in the model
  model->SetValue(array);
}

void HistoryManager::UpdateHistory(
    const std::string &category,
    const std::string &filename,
    bool make_local)
{
  // Create a string for the new file
  std::string fullpath = itksys::SystemTools::CollapseFullPath(filename.c_str());

  // Get the current history registry
  UpdateHistoryList(GetHistory(category, m_GlobalHistory), fullpath, HISTORY_SIZE_GLOBAL);
  if(make_local)
    UpdateHistoryList(GetHistory(category, m_LocalHistory), fullpath, HISTORY_SIZE_LOCAL);

  // TODO: right now, no events are fired to notify changes to the HistoryManager as
  // a whole. Also, there is no mechanism for sharing histories across sessions.
}

void HistoryManager::DeleteHistoryItem(
    const std::string &category, const std::string &file)
{
  // Delete all occurences of file from the history
  ConcreteHistoryModel *model = GetHistory(category, m_GlobalHistory);
  HistoryListType hist = model->GetValue();
  hist.erase(std::remove(hist.begin(), hist.end(), file), hist.end());
  model->SetValue(hist);
}

void HistoryManager::ClearLocalHistory()
{
  m_LocalHistory.clear();
}

HistoryManager::AbstractHistoryModel *
HistoryManager::GetLocalHistoryModel(const std::string &category)
{
  return GetHistory(category, m_LocalHistory);
}

HistoryManager::AbstractHistoryModel *
HistoryManager::GetGlobalHistoryModel(const std::string &category)
{
  return GetHistory(category, m_GlobalHistory);
}

HistoryManager::HistoryListType
HistoryManager::GetLocalHistory(const std::string &category)
{
  return GetHistory(category, m_LocalHistory)->GetValue();
}

HistoryManager::HistoryListType
HistoryManager::GetGlobalHistory(const std::string &category)
{
  return GetHistory(category, m_GlobalHistory)->GetValue();
}