Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkVerboseLimitedLinearUndo.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 
14 #include "mitkOperationEvent.h"
15 
17 {
18 }
19 
21 {
22 }
23 
25 {
26  if (!undoStackItem)
27  return false;
28 
29  // clear the redolist, if a new operation is saved
30  if (!m_RedoList.empty())
31  {
32  this->ClearList(&m_RedoList);
33  InvokeEvent(RedoEmptyEvent());
34  }
35 
36  std::size_t undoLimit = this->GetUndoLimit();
37  if (0 != undoLimit && m_UndoList.size() == undoLimit)
38  {
39  auto item = m_UndoList.front();
40  m_UndoList.pop_front();
41  delete item;
42  }
43  m_UndoList.push_back(undoStackItem);
44 
45  InvokeEvent(UndoNotEmptyEvent());
46 
47  return true;
48 }
49 
51 {
53 
54  if (m_UndoList.empty())
55  return descriptions;
56 
57  int oeid = m_UndoList.back()->GetObjectEventId(); // ObjectEventID of current group
58  std::string currentDescription; // description of current group
59  int currentDescriptionCount(0); // counter, how many items of the current group gave descriptions
60  bool niceDescriptionFound(false); // have we yet seen a plain descriptive entry (not OperationEvent)?
61  std::string lastDescription; // stores the last description to inhibit entries like "name AND name AND name..." if
62  // name is always the same
63 
64  for (auto iter = m_UndoList.rbegin(); iter != m_UndoList.rend(); ++iter)
65  {
66  if (oeid != (*iter)->GetObjectEventId())
67  {
68  // current description complete, append to list
69  if (currentDescription.empty())
70  currentDescription = "Some unnamed action"; // set a default description
71 
72  descriptions.push_back(StackDescriptionItem(oeid, currentDescription));
73 
74  currentDescription = ""; // prepare for next group
75  currentDescriptionCount = 0;
76  niceDescriptionFound = false;
77  oeid = (*iter)->GetObjectEventId();
78  }
79 
80  if (!(*iter)->GetDescription().empty()) // if there is a description
81  {
82  if (!dynamic_cast<OperationEvent *>(*iter))
83  {
84  // anything but an OperationEvent overrides the collected descriptions
85  currentDescription = (*iter)->GetDescription();
86  niceDescriptionFound = true;
87  }
88  else if (!niceDescriptionFound) // mere descriptive items override OperationEvents' descriptions
89  {
90  if (currentDescriptionCount) // if we have already seen another description
91  {
92  if (lastDescription != (*iter)->GetDescription())
93  {
94  // currentDescription += '\n'; // concatenate descriptions with newline
95  currentDescription += " AND "; // this has to wait until the popup can process multiline items
96  currentDescription += (*iter)->GetDescription();
97  }
98  }
99  else
100  {
101  currentDescription += (*iter)->GetDescription();
102  }
103  }
104  lastDescription = (*iter)->GetDescription();
105  ++currentDescriptionCount;
106  }
107 
108  } // for
109 
110  // add last description to list
111  if (currentDescription.empty())
112  currentDescription = "Some unnamed action";
113  descriptions.push_back(StackDescriptionItem(oeid, currentDescription));
114 
115  return descriptions; // list ready
116 }
117 
119 {
121 
122  if (m_RedoList.empty())
123  return descriptions;
124 
125  int oeid = m_RedoList.back()->GetObjectEventId(); // ObjectEventID of current group
126  std::string currentDescription; // description of current group
127  int currentDescriptionCount(0); // counter, how many items of the current group gave descriptions
128  bool niceDescriptionFound(false); // have we yet seen a plain descriptive entry (not OperationEvent)?
129  std::string lastDescription; // stores the last description to inhibit entries like "name AND name AND name..." if
130  // name is always the same
131 
132  for (auto iter = m_RedoList.rbegin(); iter != m_RedoList.rend(); ++iter)
133  {
134  if (oeid != (*iter)->GetObjectEventId())
135  {
136  // current description complete, append to list
137  if (currentDescription.empty())
138  currentDescription = "Some unnamed action"; // set a default description
139 
140  descriptions.push_back(StackDescriptionItem(oeid, currentDescription));
141 
142  currentDescription = ""; // prepare for next group
143  currentDescriptionCount = 0;
144  niceDescriptionFound = false;
145  oeid = (*iter)->GetObjectEventId();
146  }
147 
148  if (!(*iter)->GetDescription().empty()) // if there is a description
149  {
150  if (!dynamic_cast<OperationEvent *>(*iter))
151  {
152  // anything but an OperationEvent overrides the collected descriptions
153  currentDescription = (*iter)->GetDescription();
154  niceDescriptionFound = true;
155  }
156  else if (!niceDescriptionFound) // mere descriptive items override OperationEvents' descriptions
157  {
158  if (currentDescriptionCount) // if we have already seen another description
159  {
160  if (lastDescription != (*iter)->GetDescription())
161  {
162  // currentDescription += '\n'; // concatenate descriptions with newline
163  currentDescription += " AND "; // this has to wait until the popup can process multiline items
164  currentDescription += (*iter)->GetDescription();
165  }
166  }
167  else
168  {
169  currentDescription += (*iter)->GetDescription();
170  }
171  }
172  lastDescription = (*iter)->GetDescription();
173  ++currentDescriptionCount;
174  }
175 
176  } // for
177 
178  // add last description to list
179  if (currentDescription.empty())
180  currentDescription = "Some unnamed action";
181  descriptions.push_back(StackDescriptionItem(oeid, currentDescription));
182 
183  return descriptions; // list ready
184 }
bool SetOperationEvent(UndoStackItem *undoStackItem) override
a list of pairs (int,string), representing a stack with ObjectEventIDs and descriptions ...
std::vcl_size_t GetUndoLimit() const override
Gets the limit on the size of the undo history. The undo limit determines how many items can be store...
void ClearList(UndoContainer *list)
std::pair< int, std::string > StackDescriptionItem
Represents an entry of the undo or redo stack.
virtual StackDescription GetRedoDescriptions()
std::vector< StackDescriptionItem > StackDescription
virtual StackDescription GetUndoDescriptions()