1// Undo Library
2// Copyright (C) 2015-2017 David Capello
3//
4// This file is released under the terms of the MIT license.
5// Read LICENSE.txt for more information.
6
7#ifndef UNDO_HISTORY_H_INCLUDED
8#define UNDO_HISTORY_H_INCLUDED
9#pragma once
10
11namespace undo {
12
13 class UndoCommand;
14 class UndoState;
15
16 class UndoHistoryDelegate {
17 public:
18 virtual ~UndoHistoryDelegate() { }
19 virtual void onDeleteUndoState(UndoState* state) { }
20 };
21
22 class UndoHistory {
23 public:
24 UndoHistory(UndoHistoryDelegate* delegate = nullptr);
25 virtual ~UndoHistory();
26
27 const UndoState* firstState() const { return m_first; }
28 const UndoState* lastState() const { return m_last; }
29 const UndoState* currentState() const { return m_cur; }
30
31 void add(UndoCommand* cmd);
32 bool canUndo() const;
33 bool canRedo() const;
34 void undo();
35 void redo();
36
37 // Deletes the whole redo history. Can be called before an add()
38 // to create a linear undo history.
39 void clearRedo();
40
41 // Deletes the first UndoState. It can be useful to limit the size
42 // of the undo history.
43 bool deleteFirstState();
44
45 // This can be used to jump to a specific UndoState in the whole
46 // history.
47 void moveTo(const UndoState* new_state);
48
49 private:
50 const UndoState* findCommonParent(const UndoState* a,
51 const UndoState* b);
52 void deleteState(UndoState* state);
53
54 UndoHistoryDelegate* m_delegate;
55 UndoState* m_first;
56 UndoState* m_last;
57 UndoState* m_cur; // Current action that can be undone
58 };
59
60} // namespace undo
61
62#endif // HISTORY_H_INCLUDED
63