VPTissue Reference Manual
UndoStack.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2016 Universiteit Antwerpen
3  *
4  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
5  * the European Commission - subsequent versions of the EUPL (the "Licence");
6  * You may not use this work except in compliance with the Licence.
7  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the Licence is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the Licence for the specific language governing
13  * permissions and limitations under the Licence.
14  */
20 #include "UndoStack.h"
21 
22 namespace SimPT_Editor {
23 
25 
27 
28 void UndoStack::Initialize(const boost::property_tree::ptree& tissue)
29 {
30  m_stack.clear();
31  m_stack.push_back(tissue);
32  m_current = m_stack.begin();
33 }
34 
35 void UndoStack::Push(const boost::property_tree::ptree& tissue)
36 {
37  assert(m_stack.size() > 0 && "The undo stack isn't initialized.");
38  m_stack.erase(std::next(m_current), m_stack.end());
39  m_stack.push_back(tissue);
40 
41  while (m_stack.size() > m_max_tissues) {
42  m_stack.pop_front();
43  }
44 
45  m_current = std::prev(m_stack.end());
46 }
47 
48 bool UndoStack::CanUndo() const {
49  assert(m_stack.size() > 0 && "The undo stack isn't initialized.");
50  return (m_current != m_stack.begin());
51 }
52 
53 bool UndoStack::CanRedo() const {
54  assert(m_stack.size() > 0 && "The undo stack isn't initialized.");
55  return (std::next(m_current) != m_stack.end());
56 }
57 
58 const boost::property_tree::ptree& UndoStack::Undo()
59 {
60  assert(m_stack.size() > 0 && "The undo stack isn't initialized.");
61  if (CanUndo()) {
62  m_current--;
63  }
64  return *m_current;
65 }
66 
67 const boost::property_tree::ptree& UndoStack::Redo()
68 {
69  assert(m_stack.size() > 0 && "The undo stack isn't initialized.");
70  if (CanRedo()) {
71  m_current++;
72  }
73  return *m_current;
74 }
75 
76 } // namespace
const boost::property_tree::ptree & Redo()
Redo an action.
Definition: UndoStack.cpp:67
Namespace for SimPT tissue editor package.
Definition: Cell.h:32
void Push(const boost::property_tree::ptree &tissue)
Push a tissue to the stack.
Definition: UndoStack.cpp:35
void Initialize(const boost::property_tree::ptree &tissue)
Initialize the undo stack with a given tissue.
Definition: UndoStack.cpp:28
const boost::property_tree::ptree & Undo()
Undo an action.
Definition: UndoStack.cpp:58
Interface for UndoStack.
virtual ~UndoStack()
Destructor.
Definition: UndoStack.cpp:26
bool CanUndo() const
True if the current action can be undone.
Definition: UndoStack.cpp:48
CircularIterator< T > next(CircularIterator< T > i)
Helper yields the position the iterator would have if moved forward (in circular fashion) by 1 positi...
UndoStack()
Constructor.
Definition: UndoStack.cpp:24
bool CanRedo() const
True if the current action can be redone.
Definition: UndoStack.cpp:53
CircularIterator< T > prev(CircularIterator< T > i)
Helper yields the position the iterator would have if moved backward (in circular fashion) by 1 posit...