comparison base/CommandHistory.h @ 17:2fb933f88604

* Update some copyright notice dates * Add commands for basic editing operations in time-instants and time-value layers
author Chris Cannam
date Tue, 31 Jan 2006 15:57:25 +0000
parents base/MultiViewCommandHistory.h@cc98d496d52b
children 701404725897
comparison
equal deleted inserted replaced
16:cc98d496d52b 17:2fb933f88604
1 /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 A waveform viewer and audio annotation editor.
5 Chris Cannam, Queen Mary University of London, 2005-2006
6
7 This is experimental software. Not for distribution.
8 */
9
10 /*
11 This is a modified version of a source file from the Rosegarden
12 MIDI and audio sequencer and notation editor, copyright 2000-2006
13 Chris Cannam, distributed under the GNU General Public License.
14
15 This file contains traces of the KCommandHistory class from the KDE
16 project, copyright 2000 Werner Trobin and David Faure and
17 distributed under the GNU Lesser General Public License.
18 */
19
20 #ifndef _MULTI_VIEW_COMMAND_HISTORY_H_
21 #define _MULTI_VIEW_COMMAND_HISTORY_H_
22
23 #include <QObject>
24 #include <QString>
25
26 #include <stack>
27 #include <set>
28 #include <map>
29
30 class Command;
31 class QAction;
32 class QMenu;
33 class QToolBar;
34
35 /**
36 * The CommandHistory class stores a list of executed commands and
37 * maintains Undo and Redo actions synchronised with those commands.
38 *
39 * CommandHistory allows you to associate more than one Undo and Redo
40 * menu or toolbar with the same command history, and it keeps them
41 * all up-to-date at once. This makes it effective in systems where
42 * multiple views may be editing the same data.
43 */
44
45 class CommandHistory : public QObject
46 {
47 Q_OBJECT
48
49 public:
50 virtual ~CommandHistory();
51
52 static CommandHistory *getInstance();
53
54 void clear();
55
56 void registerMenu(QMenu *menu);
57 void registerToolbar(QToolBar *toolbar);
58
59 void addCommand(Command *command, bool execute = true);
60
61 /// Return the maximum number of items in the undo history.
62 int undoLimit() { return m_undoLimit; }
63
64 /// Set the maximum number of items in the undo history.
65 void setUndoLimit(int limit);
66
67 /// Return the maximum number of items in the redo history.
68 int redoLimit() { return m_redoLimit; }
69
70 /// Set the maximum number of items in the redo history.
71 void setRedoLimit(int limit);
72
73 public slots:
74 /**
75 * Checkpoint function that should be called when the document is
76 * saved. If the undo/redo stack later returns to the point at
77 * which the document was saved, the documentRestored signal will
78 * be emitted.
79 */
80 virtual void documentSaved();
81
82 /**
83 * Add a command to the history that has already been executed,
84 * without executing it again. Equivalent to addCommand(command, false).
85 */
86 void addExecutedCommand(Command *);
87
88 /**
89 * Add a command to the history and also execute it. Equivalent
90 * to addCommand(command, true).
91 */
92 void addCommandAndExecute(Command *);
93
94 protected slots:
95 void undo();
96 void redo();
97 void undoActivated(QAction *);
98 void redoActivated(QAction *);
99
100 signals:
101 /**
102 * Emitted whenever a command has just been executed or
103 * unexecuted, whether by addCommand, undo, or redo.
104 */
105 void commandExecuted();
106
107 /**
108 * Emitted whenever a command has just been executed, whether by
109 * addCommand or redo.
110 */
111 void commandExecuted(Command *);
112
113 /**
114 * Emitted whenever a command has just been unexecuted, whether by
115 * addCommand or undo.
116 */
117 void commandUnexecuted(Command *);
118
119 /**
120 * Emitted when the undo/redo stack has reached the same state at
121 * which the documentSaved slot was last called.
122 */
123 void documentRestored();
124
125 protected:
126 CommandHistory();
127 static CommandHistory *m_instance;
128
129 QAction *m_undoAction;
130 QAction *m_redoAction;
131 QAction *m_undoMenuAction;
132 QAction *m_redoMenuAction;
133 QMenu *m_undoMenu;
134 QMenu *m_redoMenu;
135
136 std::map<QAction *, int> m_actionCounts;
137
138 typedef std::stack<Command *> CommandStack;
139 CommandStack m_undoStack;
140 CommandStack m_redoStack;
141
142 int m_undoLimit;
143 int m_redoLimit;
144 int m_savedAt;
145
146 void updateActions();
147
148 void clipCommands();
149
150 void clipStack(CommandStack &stack, int limit);
151 void clearStack(CommandStack &stack);
152 };
153
154
155 #endif